-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinprog.py
More file actions
196 lines (167 loc) · 6.29 KB
/
linprog.py
File metadata and controls
196 lines (167 loc) · 6.29 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
class Constraint():
"""Represents an inequality in form
ax + by + c >= 0
"""
def __init__(self, a, b, c, strict=False, variables=("x", "y")):
self.coeff = [a, b, c]
self.strict = strict
self.variables = variables
def check_constraint(self, point):
c = self.coeff
value = c[0] * point[0] + c[1] * point[1] + c[2]
if self.strict:
return value > 0
else:
return value >= 0
class Problem():
""" Expression has form [a,b] to represent ax + by"""
def __init__(
self,
constraints,
expression,
maximise=True,
variables=("x", "y")):
self.constraints = constraints
self.expression = expression
self.maximise = maximise
self.variables = variables
self.polygon = []
self.polygon_closed = False
def add_constraint(self, constraint):
self.constraints.append(constraint)
def evaluate_at_point(self, point):
"""Essentially a dot product"""
return sum([self.expression[i] * point[i] for i in range(len(point))])
def solve(self):
self.build_polygon()
vertex_dict = {point: self.evaluate_at_point(point)
for point in self.polygon}
if self.maximise:
return max(vertex_dict, key=vertex_dict.get)
else:
return min(vertex_dict, key=vertex_dict.get)
def build_polygon(self):
if self.polygon == []:
self.rebuild_polygon()
def rebuild_polygon(self):
points = {(0, 1): solve_sim(self.constraints[0], self.constraints[1])}
for cons_index, constraint in enumerate(self.constraints[2:]):
for c2_index, constraint2 in enumerate(
self.constraints[0:cons_index + 2]):
point = solve_sim(constraint, constraint2)
points[(c2_index, cons_index + 2)] = point
self.clear_bad_points(points)
self.polygon_closed = self.test_closed(points)
self.polygon = points.values()
def clear_bad_points(self, points):
delete_list = []
for cons_indices, point in points.items():
other_constraints = (
self.constraints[:cons_indices[0]] +
self.constraints[cons_indices[0] + 1:cons_indices[1]] +
self.constraints[cons_indices[1] + 1:])
if not check_constraints(other_constraints, point):
delete_list.append(cons_indices)
for cons_indices in delete_list:
del points[cons_indices]
@staticmethod
def test_closed(points):
con_indices = list(points.keys())
ordered_points = [con_indices[0]]
del con_indices[0]
while con_indices:
line = ordered_points[-1][1] # second element of last point
for i,p in enumerate(con_indices):
if p[0] == line:
ordered_points.append(p)
del con_indices[i]
break
elif p[1] == line:
ordered_points.append(tuple(reversed(p)))
del con_indices[i]
break
else: # of the for loop if no suitable point is found
return False
if len(con_indices) > 0:
return False
if ordered_points[-1][1] != ordered_points[0][0]:
return False
return True
def bounding_box(self):
self.build_polygon()
if not self.polygon_closed:
return None
x_coords = [vertex[0] for vertex in self.polygon]
y_coords = [vertex[1] for vertex in self.polygon]
bottom_left = (min(x_coords),min(y_coords))
top_right = (max(x_coords),max(y_coords))
return (bottom_left,top_right)
def solve_integer(self):
box = self.bounding_box()
if not box:
return None
optimum = op_value = None
for x in range(int(box[0][0]), int(box[1][0])+1):
for y in range(int(box[0][1]), int(box[1][1])+1):
if check_constraints(self.constraints,(x,y)):
point_value = self.evaluate_at_point((x,y))
if optimum == None:
optimum = (x,y)
op_value = point_value
elif self.maximise and point_value > op_value:
optimum = (x,y)
op_value = point_value
elif not self.maximise and point_value < op_value:
optimum = (x,y)
op_value = point_value
return optimum
def check_constraints(constraints, point):
return all([constraint.check_constraint(point)
for constraint in constraints])
def solve_sim(constraint1, constraint2):
"""solve a pair of simulataneous equations. use an inverse matrix method"""
c1, c2 = constraint1.coeff, constraint2.coeff
determinant = c1[0] * c2[1] - c1[1] * c2[0]
if determinant == 0:
raise ValueError("Singular matrix")
x = (-c2[1] * c1[2] + c1[1] * c2[2]) / determinant
y = (c2[0] * c1[2] - c1[0] * c2[2]) / determinant
return (x, y)
def test1():
constraints = [
Constraint(-1, 0, 5),
Constraint(0, 1, -2),
Constraint(1, -1, 3),
Constraint(-1, -1, 8)
]
problem = Problem(constraints, (1, 2)) # maximise x+2y (2.5,5,5)
print(problem.solve())
def test2():
constraints = [
Constraint(3,1,-12),
Constraint(2,-1,0),
Constraint(-1,3,0),
Constraint(-6,-5,120)]
problem_a = Problem(constraints,(1,0))
problem_g = Problem(constraints,(2,5))
problem_h = Problem(constraints,(2,5),maximise=False)
problem_a.build_polygon()
print(problem_a.polygon)
print(problem_a.polygon_closed)
print(problem_a.bounding_box())
print(problem_a.solve())
print(problem_a.solve_integer())
def test3():
constraints= [
Constraint(-3,-4,36),
Constraint(-13,-9,117),
Constraint(4,-5,10),
Constraint(6,5,-30),
Constraint(0,1,-2)
]
problem = Problem(constraints,(1,2),maximise=True)
problem.build_polygon()
print(problem.polygon_closed)
print(problem.bounding_box())
print(problem.solve_integer())
test3()