-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearProgram.py
41 lines (33 loc) · 1.43 KB
/
LinearProgram.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
from Utils import Utils2D, my_linprog
class LinearProgram:
# Functions are stored as
# min cTx
# with respect to mx<=b where constraints = (m|b)
def __init__(self, constraints, minimize_function, was_maximize):
self.constraints = constraints
self.minimize_function = minimize_function
self.was_maximize = was_maximize # max cTx is stored as -min -cTx. This stores the - at the front
def __str__(self):
ret = f"min: {self.minimize_function}\n\n"
for x in self.constraints:
ret += f"{x}\n"
return ret
def solve(self):
if len(self.minimize_function) == 2:
return Utils2D.solve_linprog_2d(self.minimize_function, self.constraints)
else:
# res = scipy.optimize.linprog(c=self.minimize_function, A_ub=self.get_constraints_matrix(),
# b_ub=self.get_constraint_vector())
res = my_linprog.linprog(c=self.minimize_function, A_ub=self.get_constraints_matrix(),
b_ub=self.get_constraint_vector())
return list(res.x), res.fun
def get_constraints_matrix(self):
ret = []
for constraint in self.constraints:
ret.append(constraint[:-1])
return ret
def get_constraint_vector(self):
ret = []
for constraint in self.constraints:
ret.append(constraint[-1])
return ret