-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (146 loc) · 4.37 KB
/
main.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from scipy.optimize import linprog
# days
DAYS = ["mon", "tues", "weds", "thurs", "fri", "sat"]
# museums
museum_1 = {
"name": "museum_1",
"reqs": {
"mon": 1,
"tues": 1,
"weds": 1,
"thurs": 1,
"fri": 1,
"sat": 1,
}
}
museum_2 = {
"name": "museum_2",
"reqs": {
"mon": 1,
"tues": 1,
"weds": 1,
"thurs": 1,
"fri": 1,
"sat": 1,
}
}
# mvsas
bob = {
"name": "bob",
"days_per_week_pref": 5,
"weighing": {
"mon": 1,
"tues": 1,
"weds": 1,
"thurs": 1,
"fri": 1,
"sat": 1,
}
}
sue = {
"name": "sue",
"days_per_week_pref": 4,
"weighing": {
"mon": 1,
"tues": 1,
"weds": 1,
"thurs": 1,
"fri": 1,
"sat": 1,
}
}
marie = {
"name": "marie",
"days_per_week_pref": 4,
"weighing": {
"mon": 1,
"tues": 1,
"weds": 1,
"thurs": 1,
"fri": 9,
"sat": 9,
}
}
def get_variables_list(museums, mvsas, days):
"""Return variables in order that they appear in optimisation matrices"""
ret = []
for m in museums:
for p in mvsas:
for d in days:
ret.append(m["name"] + ": " + p["name"] + ": " + d)
return ret
def get_objective_function(museums, mvsas, days, direction="min"):
"""Return the objective function vector coefficients (minimization problem)"""
coeffs = []
sign = 1
if direction == "max":
sign = -1
for _ in museums:
for p in mvsas:
for d in days:
coeffs.append(sign*p["weighing"][d])
return coeffs
def ix(museum_ix, mvsas_ix, day_ix, p_len, d_len):
"""Return the index of the variable list represented by the composite indexes"""
return day_ix + (d_len)*(mvsas_ix + (p_len)*(museum_ix))
def get_museum_req_constraints(museums, mvsas, days):
"""Museum must have at least N MVSAs on given day"""
constraints = []
constraint_b = []
for museum_ix, m in enumerate(museums):
for day_ix, d in enumerate(days):
m_reqs = m["reqs"][d]
constraints.append([0]*len(mvsas)*len(museums)*len(days))
constraint_b.append(-1*m_reqs) # times by -1 to convert to <=
for mvsa_ix, _ in enumerate(mvsas):
constraints[len(constraints)-1][ix(museum_ix, mvsa_ix, day_ix, len(mvsas), len(days))] = -1
return (constraints, constraint_b)
def get_mvsa_hour_constraints(museums, mvsas, days):
"""MVSA must work at least this many days"""
constraints = []
constraint_b = []
for mvsa_ix, p in enumerate(mvsas):
constraints.append([0]*len(mvsas)*len(museums)*len(days))
constraint_b.append(-1*p["days_per_week_pref"])
for museum_ix, _ in enumerate(museums):
for day_ix, _ in enumerate(days):
constraints[len(constraints)-1][ix(museum_ix, mvsa_ix, day_ix, len(mvsas), len(days))] = -1
return (constraints, constraint_b)
def get_mvsa_one_shift_at_a_time_constraints(museums, mvsas, days):
"""MVSA can only be in one place at a time"""
constraints = []
constraint_b = []
for mvsa_ix, _ in enumerate(mvsas):
for day_ix, _ in enumerate(days):
constraints.append([0]*len(mvsas)*len(museums)*len(days))
constraint_b.append(1)
for museum_ix, _ in enumerate(museums):
constraints[len(constraints)-1][ix(museum_ix, mvsa_ix, day_ix, len(mvsas), len(days))] = 1
return (constraints, constraint_b)
def get_boundary_conditions(museums, mvsas, days):
"""Return that each var must between 0 and 1"""
return [(0,1)]*len(museums)*len(mvsas)*len(days)
MUSEUMS = [museum_1, museum_2]
MVSAS = [bob, sue, marie]
c = get_objective_function(MUSEUMS, MVSAS, DAYS)
A = []
b = []
cons, cons_b = get_museum_req_constraints(MUSEUMS, MVSAS, DAYS)
A = A + cons
b = b + cons_b
cons2, cons_b2 = get_mvsa_hour_constraints(MUSEUMS, MVSAS, DAYS)
A = A + cons2
b = b + cons_b2
cons3, cons_b3 = get_mvsa_one_shift_at_a_time_constraints(MUSEUMS, MVSAS, DAYS)
A = A + cons3
b = b + cons_b3
boundaries = get_boundary_conditions(MUSEUMS, MVSAS, DAYS)
res = linprog(c, A_ub=A, b_ub=b, bounds=boundaries, method="simplex")
if not res.success or res.status == 2 or res.status == 3:
print("Optimisation not feasible")
else:
x = res.x
vars = get_variables_list(MUSEUMS, MVSAS, DAYS)
for shifts_ix, s in enumerate(x):
if s > 0:
print(vars[shifts_ix])