def generatePoints_rays(self, s, vs, all_vars, others=[], tags=None): from lpi import C_Polyhedron cons = And(s.get_constraints(tags=tags)) from ppl import Variable poly = C_Polyhedron(cons, variables=vs) gene = poly.get_generators() g_points = [g for g in gene if g.is_point()] g_lines = [g for g in gene if g.is_line()] g_rays = [g for g in gene if g.is_ray()] rs = [] ps = [] for l in g_lines: item = {} item2 = {} for i in range(len(vs)): item[vs[i]] = int(l.coefficient(Variable(i))) item2[vs[i]] = -int(l.coefficient(Variable(i))) rs.append(item) rs.append(item2) for r in g_rays: item = {} for i in range(len(vs)): item[vs[i]] = int(r.coefficient(Variable(i))) rs.append(item) for p in g_points: item = {} for i in range(len(vs)): item[vs[i]] = int(p.coefficient(Variable(i))) ps.append((item, int(p.divisor()))) def combine(p, rays, coeffs, vs, incr): point = {v: p[v] for v in vs} for i in range(len(coeffs)): for v in vs: point[v] += coeffs[i] * rays[i][v] if coeffs[incr] < 10: yield from combine( p, rays, coeffs[:i] + [coeffs[i] + 1] + coeffs[i + 1:], vs, incr) elif incr < len(coeffs): yield from combine(p, rays, coeffs, vs, incr + 1) yield point points = [] for p, d in ps: points.append({v: (p[v] / d) for v in vs}) for r in rs: for k in range(1, 100): points.append({v: ((p[v] + k * r[v]) / d) for v in vs}) return points points = [] for p, d in ps: points += list(combine(p, rs, [0 for __ in rs], vs, 0)) print(points, "\n", gene) return points
def generateRay_cons(self, generators, vs, lvs, all_vars): from ppl import Variable, Linear_Expression, ray from termination.algorithm.utils import get_free_name g_points = [g for g in generators if g.is_point()] g_lines = [g for g in generators if g.is_line()] g_rays = [g for g in generators if g.is_ray()] for l in g_lines: exp = Linear_Expression(0) for i in range(len(vs)): ci = int(l.coefficient(Variable(i))) exp += ci * Variable(i) g_rays.append(ray(exp)) g_rays.append(ray(-exp)) p_vars = get_free_name(all_vars, name="_a", num=len(g_points)) r_vars = get_free_name(all_vars, name="_br", num=len(g_rays)) ray_cons = [] relation = {} for i in range(len(vs)): exp = Term(0) for pi in range(len(g_points)): if i == 0: relation[p_vars[pi]] = g_points[pi] exp += Term(p_vars[pi], g_points[pi].divisor()) * int(g_points[pi].coefficient(Variable(i))) for ri in range(len(g_rays)): if i == 0: relation[r_vars[ri]] = g_rays[ri] ci = int(g_rays[ri].coefficient(Variable(i))) if ci == 0: continue exp += Term(r_vars[ri]) * ci ray_cons.append(exp == Term(vs[i])) exp = Term(0) for a in p_vars: ai = Term(a) exp += ai ray_cons.append(ai >= 0) ray_cons.append(exp == 1) exp = Term(0) for b in r_vars: bi = Term(b) exp += bi ray_cons.append(bi >= 0) ray_cons.append(exp >= 1) ors = [] for b in r_vars: bi = Term(b) ors.append(exp == bi) ray_cons.append(Or(ors)) return ray_cons, relation
def has_IP_property(self): """ Whether the lattice polytope has the IP property. That is, the polytope is full-dimensional and the origin is a interior point not on the boundary. OUTPUT: Boolean. EXAMPLES:: sage: from sage.geometry.polyhedron.ppl_lattice_polytope import LatticePolytope_PPL sage: LatticePolytope_PPL((-1,-1),(0,1),(1,0)).has_IP_property() True sage: LatticePolytope_PPL((-1,-1),(1,1)).has_IP_property() False """ origin = C_Polyhedron(point(0*Variable(self.space_dimension()))) is_included = Poly_Con_Relation.is_included() saturates = Poly_Con_Relation.saturates() for c in self.constraints(): rel = origin.relation_with(c) if (not rel.implies(is_included)) or rel.implies(saturates): return False return True
def vertices(self): r""" Return the vertices as a tuple of `\ZZ`-vectors. OUTPUT: A tuple of `\ZZ`-vectors. Each entry is the coordinate vector of an integral points of the lattice polytope. EXAMPLES:: sage: from sage.geometry.polyhedron.ppl_lattice_polytope import LatticePolytope_PPL sage: p = LatticePolytope_PPL((-9,-6,-1,-1),(0,0,0,1),(0,0,1,0),(0,1,0,0),(1,0,0,0)) sage: p.vertices() ((-9, -6, -1, -1), (0, 0, 0, 1), (0, 0, 1, 0), (0, 1, 0, 0), (1, 0, 0, 0)) sage: p.minimized_generators() Generator_System {point(-9/1, -6/1, -1/1, -1/1), point(0/1, 0/1, 0/1, 1/1), point(0/1, 0/1, 1/1, 0/1), point(0/1, 1/1, 0/1, 0/1), point(1/1, 0/1, 0/1, 0/1)} """ d = self.space_dimension() v = vector(ZZ, d) points = [] for g in self.minimized_generators(): for i in range(0,d): v[i] = g.coefficient(Variable(i)) v_copy = copy.copy(v) v_copy.set_immutable() points.append(v_copy) return tuple(points)
def bounding_box(self): r""" Return the coordinates of a rectangular box containing the non-empty polytope. OUTPUT: A pair of tuples ``(box_min, box_max)`` where ``box_min`` are the coordinates of a point bounding the coordinates of the polytope from below and ``box_max`` bounds the coordinates from above. EXAMPLES:: sage: from sage.geometry.polyhedron.ppl_lattice_polytope import LatticePolytope_PPL sage: LatticePolytope_PPL((0,0),(1,0),(0,1)).bounding_box() ((0, 0), (1, 1)) """ box_min = [] box_max = [] if self.is_empty(): raise ValueError('empty polytope is not allowed') for i in range(0, self.space_dimension()): x = Variable(i) coords = [Integer(v.coefficient(x)) for v in self.generators()] max_coord = max(coords) min_coord = min(coords) box_max.append(max_coord) box_min.append(min_coord) return (tuple(box_min), tuple(box_max))
def rays_from_constraints(self, halfspaces): # Create PPL variables. x = [Variable(i) for i in range(self.dim)] # Init constraint system. constraints = Constraint_System() # Add polytope facets to constraint systems. # Format of ConvexHull.equations: [a1 a2 .. b] => a1*x1 + a2*x2 + .. + b <= 0. for hyp, orient in halfspaces: # PPL works on integral values, so all # coordinates are scaled and truncated. if orient == -1: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b <= 0) elif orient == 1: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b >= 0) elif orient == 0: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b == 0) # Build PPL polyhedra. poly = C_Polyhedron(constraints) # Get vertices of polytope resulting from intersection. only rays rays = [] for gen in poly.minimized_generators(): if gen.is_ray(): ray = np.array(gen.coefficients()).astype(float) ray = ray / np.linalg.norm(ray) rays.append(ray) return np.array(rays)
def poly_from_vertices(self, vertices): # Create PPL variables. x = [Variable(i) for i in range(self.dim)] # Init constraint system. generators = Generator_System() # ppl needs coordinates in form of point(sum(a_i *x_i), denom) with a_i integers for vertex in vertices: generators.insert(vertex.ppl) # Build PPL polyhedra. return C_Polyhedron(generators)
def add_constraint(self, halfspace): # Create PPL variables. x = [Variable(i) for i in range(self.dim)] if halfspace[1] == -1: self.poly.add_constraint( sum(halfspace[0].a[i] * x[i] for i in range(self.dim)) + halfspace[0].b <= 0) elif halfspace[1] == 1: self.poly.add_constraint( sum(halfspace[0].a[i] * x[i] for i in range(self.dim)) + halfspace[0].b >= 0) elif halfspace[1] == 0: self.poly.add_constraint( sum(halfspace[0].a[i] * x[i] for i in range(self.dim)) + halfspace[0].b == 0) self.hyperplanes = self.hyperplanes_from_poly() self.halfspaces.append(halfspace) self.vertices = self.vertices_from_poly()
def position(self, hyperplane): x = [Variable(i) for i in range(self.dim)] p = C_Polyhedron(Generator_System(self.ppl)) s_rel = p.relation_with( sum(hyperplane.a[i] * x[i] for i in range(self.dim)) + hyperplane.b < 0) if s_rel.implies(Poly_Con_Relation.is_included()): return -1 else: b_rel = p.relation_with( sum(hyperplane.a[i] * x[i] for i in range(self.dim)) + hyperplane.b > 0) if b_rel.implies(Poly_Con_Relation.is_included()): return 1 else: return 0
def poly_from_constraints(self, halfspaces): # Create PPL variables. x = [Variable(i) for i in range(self.dim)] # Init constraint system. constraints = Constraint_System() # Add polytope facets to constraint systems. # Format of ConvexHull.equations: [a1 a2 .. b] => a1*x1 + a2*x2 + .. + b <= 0. for hyp, orient in halfspaces: if orient == -1: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b <= 0) elif orient == 1: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b >= 0) elif orient == 0: constraints.insert(sum(hyp.a[i] * x[i] for i in range(self.dim)) + hyp.b == 0) # Build PPL polyhedra. return C_Polyhedron(constraints)
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 vertex_from_coordinates(coordinates): x = [Variable(i) for i in range(len(coordinates))] denom = 1e+10 p = point(sum(x[i] * int(coordinates[i] * denom) for i in range(len(coordinates))), denom) return Vertex(p)
import numpy as np from bellpolytope import BellPolytope from bellscenario import BellScenario from bellpolytopewithonewaycomm import BellPolytopeWithOneWayCommunication from ppl import Variable, Generator_System, C_Polyhedron, point from sequentialbellpolytope import SequentialBellPolytope from sequentialbellscenario import SequentialBellScenario if __name__ == '__main__': outputsAliceSeq = [[2, 2], [2, 2]] outputsBob = [2, 2] scenario = SequentialBellScenario(outputsAliceSeq, outputsBob) variables = [Variable(i) for i in range(len(scenario.getTuplesOfEvents()))] gs = Generator_System() for v in BellPolytopeWithOneWayCommunication( SequentialBellPolytope(scenario)).getGeneratorForVertices(): prob = v.getProbabilityList() gs.insert(point(sum(prob[i] * variables[i] for i in range(len(prob))))) poly = C_Polyhedron(gs) constraints = poly.constraints() for constraint in constraints: inequality = str(constraint.inhomogeneous_term().__float__()) + ' ' for coef in constraint.coefficients(): inequality = inequality + str(-coef.__float__()) + ' ' print(inequality)