-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathReadBestsellers.py
More file actions
106 lines (75 loc) · 3.03 KB
/
ReadBestsellers.py
File metadata and controls
106 lines (75 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#Reading Books
"""
Example of the optimization problem. If you want to read maximum amount of bestsellers, which books should you order?
For example you can read only 5 hours/week. How many books can you read per year
**Assuming that average speed of reading The average reader snails through prose at a rate of about
250-300 words per minute, which roughly equates to about one page per minute, we assume 60 pages/ hour is the regular speed.
**Another definition of the problem is to achieve max rating.
"""
from pulp import *
import numpy as np
import pandas as pd
import re
#write a scaper before hand
data = pd.read_csv('goodreads_bestsellers.csv')
problem_name = 'BuyingBestsellers'
hours_week_read = 5
pages_per_hour = 60
def optimize_bestseller_reading():
# create the LP object, set up as a maximization problem --> since we want to maximize the number of books we read in a year
prob = pulp.LpProblem(problem_name, pulp.LpMaximize)
#create decision - yes or no to buy the book?
decision_variables = []
for rownum, row in data.iterrows():
variable = str('x' + str(rownum))
variable = pulp.LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Integer') #make variables binary
decision_variables.append(variable)
print ("Total number of decision_variables: " + str(len(decision_variables)))
#create optimization function
total_books = ""
for i, book in enumerate(decision_variables):
total_books += book
prob += total_books
print ("Optimization function: " + str(total_books))
#create constrains - there are only 365 days
total_pages_needs_to_read = ""
for rownum, row in data.iterrows():
for i, schedule in enumerate(decision_variables):
if rownum == i:
formula = row['pages']*schedule
total_pages_needs_to_read += formula
total_pages_can_read = 52*hours_week_read*pages_per_hour
prob += (total_pages_needs_to_read == total_pages_can_read)
#now run optimization
optimization_result = prob.solve()
assert optimization_result == pulp.LpStatusOptimal
prob.writeLP(problem_name + ".lp" )
print("Status:", LpStatus[prob.status])
# print("Optimal Solution to the problem: ", value(prob.objective))
print ("Individual decision_variables: ")
for v in prob.variables():
print(v.name, "=", v.varValue)
#transform the data back
#########################
#Format the Results
#reorder results
variable_name = []
variable_value = []
for v in prob.variables():
variable_name.append(v.name)
variable_value.append(v.varValue)
df = pd.DataFrame({'variable': variable_name, 'value': variable_value})
for rownum, row in df.iterrows():
value = re.findall(r'(\d+)', row['variable'])
df.loc[rownum, 'variable'] = int(value[0])
df = df.sort_index(by='variable')
#append results
for rownum, row in data.iterrows():
for results_rownum, results_row in df.iterrows():
if rownum == results_row['variable']:
data.loc[rownum, 'decision'] = results_row['value']
#export data-table
data.to_csv('reading_list.csv')
print("Your Reading List is ready")
if __name__ == "__main__":
optimize_bestseller_reading()