def art_gallery_problem(interface, points=None, show_decomposition=True): if points is None: points = ioclass.read_from_file(filename) poly = Polygon() poly.set_points(points) linked_list = poly.get_linked_list() headnode = linked_list[0] size = len(points) # Check if the file reading was successful or if there were inconsistencies: if not headnode or size < 3: print("No triangulations to output") return # Create a Triangulation object from the linked list: t1 = EarTriangulation(headnode, size) # Do the triangulation. The return value is a list of 3-tuples, which # represent the vertices of each triangle. triangles1 = t1.triangulate() # Now for the GUI. Both the polygon and its triangulation have been scaled, # as specified above. Now we need to draw them on a Tkinter Canvas. # Setup and init a canvas: if show_decomposition: interface.draw_triangles(triangles1) # The last step is to output the triangulation of the original, non-scaled # polygon to the console: ioclass.print_triangles_to_console(triangles1) art_gallery_coloring = Coloring() art_gallery_coloring.set_triangulation(points, triangles1) points_str, res = art_gallery_coloring.colorize() list_res = [] for p in points_str: p = p.name list_res.append([points[int(p)].x, points[int(p)].y]) interface.draw_result(list_res) interface.set_result(res)
def getStartingPopulation(populationSize, graph): solutions = [] for i in range(populationSize): solutions.append(Coloring(graph)) startingPoint = random.randint(0, graph.numVertices - 1) #Greedly color the graph starting at a random point solutions[i].colorGreedy(startingPoint) solutions[i].restrictedColors return solutions
def __init__(self, point): self._point = Point(point.id, point.x, point.y) self._points = [point] self._polygon = [(point.x, point.y)] self._triangulation = Triangulation(self._points) self._color = Coloring(self._points, self._triangulation) self.lock = threading.RLock() self.version = 0
def construct_from_coloring(self, coloring: Coloring, g: Graph): """ Construct permutation from coloring. Color classes form cycles. :param coloring: Coloring to create permutation from """ for _, vertices in coloring.items(): vertex1, vertex2 = vertices if vertex1.in_graph(g): self.P[vertex1.id] = vertex2.id else: self.P[vertex2.id] = vertex1.id
def art_gallery_problem(interface, points=None, show_decomposition=True): if points is None: points = ioclass.read_from_file(filename) poly = Polygon() poly.set_points(points) size = len(points) list_points = [] for p in points: list_points.append([p.x, p.y]) seidel_triangalator = seidel.Triangulator(list_points) triangles1 = seidel_triangalator.triangles() # Now for the GUI. Both the polygon and its triangulation have been scaled, # as specified above. Now we need to draw them on a Tkinter Canvas. # Setup and init a canvas: if show_decomposition: interface.draw_triangles(triangles1) # The last step is to output the triangulation of the original, non-scaled # polygon to the console: # ioclass.print_triangles_to_console(triangles1) art_gallery_coloring = Coloring() art_gallery_coloring.set_triangulation(points, triangles1) points_str, res = art_gallery_coloring.colorize() list_res = [] for p in points_str: p = p.name list_res.append([points[int(p)].x, points[int(p)].y]) interface.draw_result(list_res) interface.set_result(res)
def test_permutation_coloring(self): g = Graph(directed=False, n=5) h = Graph(directed=False, n=5) vg_0, vg_1, vg_2, vg_3, vg_4 = g.vertices vh_0, vh_1, vh_2, vh_3, vh_4 = h.vertices coloring_p = Coloring() p = Permutation(0, coloring=coloring_p, g=g) self.assertEqual(0, len(p)) coloring_p.add([vg_0, vh_0]) coloring_p.add([vg_1, vh_1]) p = Permutation(2, coloring=coloring_p, g=g) self.assertEqual(0, p.P[0]) self.assertEqual(1, p.P[1]) coloring_p = Coloring() coloring_p.add([vg_0, vh_1]) coloring_p.add([vg_1, vh_0]) p = Permutation(2, coloring=coloring_p, g=g) self.assertEqual(1, p.P[0]) self.assertEqual(0, p.P[1]) # TODO: deze test gaat nog mis... coloring_p = Coloring() coloring_p.add([vg_0, vh_1]) coloring_p.add([vg_1, vh_2]) coloring_p.add([vg_2, vh_3]) coloring_p.add([vg_3, vh_4]) coloring_p.add([vg_4, vh_0]) p = Permutation(5, coloring=coloring_p, g=g) self.assertEqual(1, p.P[0]) self.assertEqual(2, p.P[1]) self.assertEqual(3, p.P[2]) self.assertEqual(4, p.P[3]) self.assertEqual(0, p.P[4])
def test_order_computation( self): # TODO: to a testclass of basicpermutations g = Graph(directed=False, n=6, name='g') h = Graph(directed=False, n=6, name='h') vg_0, vg_1, vg_2, vg_3, vg_4, vg_5 = g.vertices vh_0, vh_1, vh_2, vh_3, vh_4, vh_5 = h.vertices # mapping only to itself coloring_p = Coloring() coloring_p.add([vg_0, vh_0]) coloring_p.add([vg_1, vh_1]) p = Permutation(len(g.vertices), coloring=coloring_p) H = [p] self.assertEqual(1, order_computation(H)) # mapping to itself and 1 other node coloring_p = Coloring() coloring_p.add([vg_0, vh_1]) coloring_p.add([vg_1, vh_0]) p = Permutation(len(g.vertices), coloring=coloring_p) H = [p] self.assertEqual(2, order_computation(H)) # this is the permutation example of the lecture coloring_p = Coloring() coloring_p.add([vg_0, vh_1]) coloring_p.add([vg_1, vh_2]) coloring_p.add([vg_2, vh_0]) coloring_p.add([vg_4, vh_5]) coloring_p.add([vg_5, vh_4]) p = Permutation(6, coloring=coloring_p) coloring_q = Coloring() coloring_q.add([vg_2, vh_3]) coloring_q.add([vg_3, vh_2]) q = Permutation(6, coloring=coloring_q) H = [p, q] self.assertEqual(48, order_computation(H))
class ArtGallery(object): """ This class resolves The art gallery problem. It is a visibility problem in computational geometry. Guarding an art gallery with the minimum number of guards who together can observe the whole gallery """ def __init__(self, point): self._point = Point(point.id, point.x, point.y) self._points = [point] self._polygon = [(point.x, point.y)] self._triangulation = Triangulation(self._points) self._color = Coloring(self._points, self._triangulation) self.lock = threading.RLock() self.version = 0 @staticmethod def load(file_name): points = [] with open(file_name, "r") as hfile: hfile.readline() i = 0 for line in hfile: x, y = line.split() point = Point(i, Decimal(x), Decimal(y)) i += 1 points.append(point) return points def _update(self): self._points.sort() self._polygon = [] for p in self._points: self._polygon.append((p.x, p.y)) def get_polygon(self): with self.lock: return self._polygon def get_process_point(self): with self.lock: return self._point def get_points(self): with self.lock: return self._points def get_diagonals(self): with self.lock: return self._triangulation.get_diagonals() def get_min_color(self): with self.lock: return self._color.get_min_color() def is_guard(self, point): with self.lock: return point.color == self._color.get_min_color() def include(self, point): """ Add a new point to the art gallery """ with self.lock: self._points.append(Point(point.id, point.x, point.y)) self._update() triangulated = self._triangulation.process() if (triangulated): self._color.process() self.version += 1
i0 = i1 i1 += 1 return (area / 2) # Testing the class if __name__ == "__main__": from art_gallery import ArtGallery points = ArtGallery.load("inputs/test_0.poly") points.sort() triangulation = Triangulation(points) triangulation.process() from coloring import Coloring color = Coloring(points, triangulation) color.process() i = 0 for p in triangulation._polygon: i += 1 print "Point %d => %s" % (i, p) i = 0 for t in triangulation.get_triangles(): i += 1 print "Triangle %d => (%s,%s)[%s] (%s,%s)[%s] (%s,%s)[%s]" \ % (i, t.u.x, t.u.y, t.u.color, t.v.x, t.v.y, t.v.color, t.w.x, t.w.y, t.w.color) if points.count(t.v) != 1: print "Where is v " + t.v if points.count(t.u) != 1: