-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrute_force.py
47 lines (40 loc) · 1.61 KB
/
brute_force.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
from ppl import Constraint_System, C_Polyhedron, Variable
from vertex import Vertex
import itertools
def solve_les(hyperplanes):
dim = len(hyperplanes[0]) - 1
x = [Variable(i) for i in range(dim)]
constraints = Constraint_System()
for hyp in hyperplanes:
constraints.insert(sum(hyp[i + 1] * x[i] for i in range(dim)) + hyp[0] == 0)
poly = C_Polyhedron(constraints)
ppl_points = [pt for pt in poly.minimized_generators() if pt.is_point()]
if len(ppl_points) != len(poly.minimized_generators()):
# Not uniquely determined.
return None
if len(ppl_points) == 1:
vertex = Vertex(tuple(c / ppl_points[0].divisor() for c in ppl_points[0].coefficients()), None)
return vertex
elif len(ppl_points) == 0:
return None
else:
raise ValueError
def inside_box(coordinates, box):
for box_constraint in box:
if sum(coordinates[i] * box_constraint[i+1] for i in range(len(coordinates))) + box_constraint[0] < 0:
return False
return True
def brute_force_vertices(hyperplanes, box_constraints):
vertices = set()
d = len(hyperplanes[0]) - 1
for combination in itertools.combinations(range(len(hyperplanes)), d):
hyperplane_combination = [hyperplanes[i] for i in combination]
vertex = solve_les(hyperplane_combination)
if vertex is not None:
vertex.cobasis = combination
vertices.add(vertex)
vertices_inside_box = set()
for v in vertices:
if inside_box(v.coordinates, box_constraints):
vertices_inside_box.add(v)
return vertices, vertices_inside_box