-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.py
49 lines (38 loc) · 1.3 KB
/
solver.py
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
from pulp import LpMaximize, LpProblem, LpVariable, LpBinary, lpSum
from pulp.apis import PULP_CBC_CMD
import random
random.seed(21)
def solver_result(P, B, ldm, d, log=False, leap_year=False):
for m in range(1, 13):
ldm.append([])
if m in (1, 3, 5, 7, 8, 10, 12):
count = 31
elif m in (4, 6, 9, 11):
count = 30
else:
if leap_year:
count = 29
else:
count = 28
for i in range(count):
ldm[m-1].append(d)
d += 1
prob = LpProblem('meterobalones', LpMaximize)
x = {}
for i in range(1, 367):
lowerBound = 0
upperBound = 1
x[i] = LpVariable('x' + '_' + str(i), lowerBound, upperBound, LpBinary)
# Add objectives
prob += lpSum(x[i] * P[i-1] for i in range(1, 367))
# constraints
for month in ldm:
prob += lpSum(x[i] for i in month) >= 1
prob += lpSum(x[i] for i in range(1, 367)) <= B
prob.solve(PULP_CBC_CMD(msg=log))
objective_score = prob.objective.value()
solution_days = []
for v in prob.variables():
if v.varValue is not None and v.varValue != 0:
solution_days.append(int((v.name)[2:]))
return objective_score, solution_days