From a1a7587290898849eaaffd8db14241da3b67771f Mon Sep 17 00:00:00 2001 From: lertlmaier Date: Fri, 23 May 2025 12:54:02 +0200 Subject: [PATCH] =?UTF-8?q?Methode=20f=C3=BCr=20Erstellung=20des=20Trees?= =?UTF-8?q?=20implementiert=20(build=5Frack=5Fstrtree).=20Methode=20zur=20?= =?UTF-8?q?findung=20von=20n=C3=A4chsten=20Rack=20von=20Tree=20implementie?= =?UTF-8?q?rt=20(find=5Fnearest=5Frack=5Ffrom=5Fpoint=5Ftree).=20Methode?= =?UTF-8?q?=20zur=20verkn=C3=BCpfung=20von=20Sensor=20oder=20Dist=20mittel?= =?UTF-8?q?s=20zuvor=20genannter=20methode=20(connect=5Fequipment=5Fto=5Fr?= =?UTF-8?q?acks).=20Unittests=20f=C3=BCr=20Methoden=20erfolgreich=20implem?= =?UTF-8?q?entiert.=20Versuch=20der=20Implementierung=20einer=20vektorisie?= =?UTF-8?q?rten=20Form=20mit=20Tree=20aber=20nocht=20nicht=20erfolgreich.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/plant.py | 512 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 360 insertions(+), 152 deletions(-) diff --git a/lib/plant.py b/lib/plant.py index 8627761..2baa455 100644 --- a/lib/plant.py +++ b/lib/plant.py @@ -8,6 +8,8 @@ import networkx as nx import matplotlib.pyplot as plt from itertools import pairwise, combinations, permutations import re +from shapely.strtree import STRtree +import shapely class PointSorter: def __init__(self): @@ -355,7 +357,6 @@ class Anlage(): def get_sensor_point(self, sname:str) -> Point: return self._sensors[sname] - def connect_sensors_to_racks(self) -> list: '''verbindet die Sensoren mit den Racks. die Rückgabe enthält ein Tuple, welche Sensoren keinem Rack zugeordnet werden konnten @@ -405,6 +406,94 @@ class Anlage(): def join_racks(self): self._racks.join_racks() + def _build_rack_strtree(self): + self._rack_lines = [] + self._rack_map = {} + for r_name, pts in self._racks.get_racks_borders().items(): + line = LineString([pts[0], pts[-1]]) + self._rack_lines.append(line) + self._rack_map[line] = r_name + self._rack_tree = STRtree(self._rack_lines) + + def find_nearest_rack_from_point_tree(self, max_dist, sensor:Point) -> tuple[Point, str]: + if not hasattr(self, "_rack_tree"): + self._build_rack_strtree() + + result = self._rack_tree.query_nearest(sensor, return_distance=True) + if result == None: + return None, None + + index_array, dist_array = result + nearest_index = index_array[0] + distance = dist_array[0] + + #nearest_line, distance = result + if distance > max_dist: + return None, None + + nearest_line = self._rack_lines[nearest_index] + rack_name = self._rack_map[nearest_line] + nearest_point = nearest_line.interpolate(nearest_line.project(sensor)) + return(nearest_point, rack_name) + + def connect_equipment_to_racks(self, equipment: dict, onpoints: dict) -> list: + '''Verbindet Peripherie (Sensoren / Aktoren/ Unterverteiler) mit dem nächsten Rack. + Eingabe: Dict des Equipments (Sensoren o. Dists), Dict der Aufpunkte von Sensoren o. Dists + Rückgabe: Liste der nicht zugeordneten Geräte + ''' + errors = [] + for name, pos in equipment.items(): + onpoint, rackname = self.find_nearest_rack_from_point_tree(self._tol_connect, pos) + if onpoint == None or rackname == None: + errors.append((name, pos)) + continue + onpoints[name] = (onpoint, rackname) + self.add_point_to_rack(onpoint, rackname) + + virtual_rackname = f"v-{name}-{rackname}" + self._racks.add_rack(pos, onpoint, virtual_rackname) + + return errors + + def connect_equipment_batch(self, equipment:dict, onpoints:dict) -> list: + if not hasattr(self, "_rack_tree"): + self._build_rack_strtree() + + devices = list(equipment.items()) + device_names = [name for name, _ in devices] + device_points = [pos for _, pos in devices] + + idx_rack, distances = self._rack_tree.query_nearest(device_points, return_distance=True, all_matches=True) + # !!! Problem !!!: query gibt mehrere Ergebnisse zurück -> kann dann nicht zugeordnet werden + # Greifen des ersten ergebnisses nicht zielführend, da nicht das näheste + + errors = [] + for i, (rack_idxs, dist) in enumerate(zip(idx_rack, distances)): + # Nehme ersten Treffer + rack_idx = int(rack_idxs[0]) + dist = float(dist) + + if dist > self._tol_connect: + errors.append(devices[i]) + continue + + eqname, eqpos = devices[i] + nearest_line = self._rack_lines[rack_idx] + rackname = self._rack_map[nearest_line] + onpoint = nearest_line.interpolate(nearest_line.project(eqpos)) + + onpoints[eqname] = (onpoint, rackname) + self.add_point_to_rack(onpoint, rackname) + + virtual_rackname = f"v-{eqname}-{rackname}" + self._racks.add_rack(eqpos, onpoint, virtual_rackname) + + return errors + + + + + def find_nearest_rack_from_point(self, max_dist, coarse_step, sensor:Point, racks:dict) -> tuple[Point, str]: # 1. grobe Kandidatensuche candidate_lines = [] @@ -625,132 +714,161 @@ class Anlage(): class TestLinesweep(unittest.TestCase): - def test_duplicate_points(self): - ''' Testet das Nicht-Hinzufügen von doppelten Punkten''' - # Initialisiere die Liste an Knoten - nodeids = NodeIDs() + # def test_duplicate_points(self): + # ''' Testet das Nicht-Hinzufügen von doppelten Punkten''' + # # Initialisiere die Liste an Knoten + # nodeids = NodeIDs() - # Setze gleichen Knoten doppelt - nodeids.add_point(Point(1,1)) - nodeids.add_point(Point(1,1)) + # # Setze gleichen Knoten doppelt + # nodeids.add_point(Point(1,1)) + # nodeids.add_point(Point(1,1)) - self.assertEqual(nodeids.size_of(), 1) + # self.assertEqual(nodeids.size_of(), 1) - def test_cut_rack_in_segments(self): - ''' Teilt Rack aus Polyline in mehrere Segmente automatisch auf.''' - racks_data = { - 'Rack_1': [Point(0, 0), Point(0, 10), Point (10, 10)], - 'Rack_2': [Point(-5, 5), Point(5, 5)] - } + # def test_cut_rack_in_segments(self): + # ''' Teilt Rack aus Polyline in mehrere Segmente automatisch auf.''' + # racks_data = { + # 'Rack_1': [Point(0, 0), Point(0, 10), Point (10, 10)], + # 'Rack_2': [Point(-5, 5), Point(5, 5)] + # } - # Initialisiere Racks - rack = RackIDs() - # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf - rack.add_racks(racks_data) + # # Initialisiere Racks + # rack = RackIDs() + # # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf + # rack.add_racks(racks_data) - self.assertEqual(rack.get_rack_names(), ['Rack_1-1', 'Rack_1-2', 'Rack_2']) + # self.assertEqual(rack.get_rack_names(), ['Rack_1-1', 'Rack_1-2', 'Rack_2']) - def test_intersect_segments(self): - ''' Stellt Schnittpunkte zwischen Racks fest und fügt Schnittpunkt zu Rack hinzu. ''' + # def test_intersect_segments(self): + # ''' Stellt Schnittpunkte zwischen Racks fest und fügt Schnittpunkt zu Rack hinzu. ''' - racks_data = { - 'Rack_1': [Point(0, 0), Point(0, 10), Point (10, 10)], - 'Rack_2': [Point(-5, 5), Point(5, 5)], - } + # racks_data = { + # 'Rack_1': [Point(0, 0), Point(0, 10), Point (10, 10)], + # 'Rack_2': [Point(-5, 5), Point(5, 5)], + # } - # Initialisiere Racks - rack = RackIDs() - # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf - rack.add_racks(racks_data) - # Verknüpfe Racks mit echten Schnittpunkten und füge Schnittpunkte (exakt & beinahe) zu jeweiligem Rack hinzu - rack.join_racks() + # # Initialisiere Racks + # rack = RackIDs() + # # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf + # rack.add_racks(racks_data) + # # Verknüpfe Racks mit echten Schnittpunkten und füge Schnittpunkte (exakt & beinahe) zu jeweiligem Rack hinzu + # rack.join_racks() - self.assertEqual(rack.get_points_from_rack("Rack_1-1"), [Point(0, 0), Point(0, 5), Point (0, 10)]) + # self.assertEqual(rack.get_points_from_rack("Rack_1-1"), [Point(0, 0), Point(0, 5), Point (0, 10)]) - def test_snap_segments(self): - ''' Verlängert Anfangs und Endpunkte von Racks, sodass sie auf naheliegenden Racks liegen''' - racks_data = { - 'Rack_1': [Point(0, 0), Point(0, 10)], - 'Rack_2': [Point(1, 5), Point(5, 5)], - 'Rack_3': [Point(1.5, 7.5), Point(5, 7.5)] - } + # def test_snap_segments(self): + # ''' Verlängert Anfangs und Endpunkte von Racks, sodass sie auf naheliegenden Racks liegen''' + # racks_data = { + # 'Rack_1': [Point(0, 0), Point(0, 10)], + # 'Rack_2': [Point(1, 5), Point(5, 5)], + # 'Rack_3': [Point(1.5, 7.5), Point(5, 7.5)] + # } - # Initialisiere Racks - rack = RackIDs(tol_snap=1) - # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf - rack.add_racks(racks_data) - # Verknüpfe Racks mit echten Schnittpunkten und füge Schnittpunkte (exakt & beinahe) zu jeweiligem Rack hinzu - rack.join_racks() + # # Initialisiere Racks + # rack = RackIDs(tol_snap=1) + # # Füge Racks aus gegebenen Daten hinzu und teile Rack_1 bestehend aus 3 Punkten in 2 Racks auf + # rack.add_racks(racks_data) + # # Verknüpfe Racks mit echten Schnittpunkten und füge Schnittpunkte (exakt & beinahe) zu jeweiligem Rack hinzu + # rack.join_racks() - #Rack 2 wird verlängert auf SP mit Rack 1. Rack 3 ausserhalb der Toleranz - self.assertEqual(rack.get_points_from_rack("Rack_1"), [Point(0, 0), Point(0, 5), Point (0, 10)]) + # #Rack 2 wird verlängert auf SP mit Rack 1. Rack 3 ausserhalb der Toleranz + # self.assertEqual(rack.get_points_from_rack("Rack_1"), [Point(0, 0), Point(0, 5), Point (0, 10)]) - def test_ids_to_point(self): - ''' Testet, ob gefragter Punkt auf Racks a, b, c liegt''' + # def test_ids_to_point(self): + # ''' Testet, ob gefragter Punkt auf Racks a, b, c liegt''' - res_rack_seg = {'Rack_1-0': [Point(1, 0), Point(5, 6)], - 'Rack_2-0': [Point(1, 8), Point(1, 0)], - 'Rack_2-1': [Point(0, 10), Point(5, 10)]} + # res_rack_seg = {'Rack_1-0': [Point(1, 0), Point(5, 6)], + # 'Rack_2-0': [Point(1, 8), Point(1, 0)], + # 'Rack_2-1': [Point(0, 10), Point(5, 10)]} - point2rack = RackIDs() - point2rack.add_racks(res_rack_seg) + # point2rack = RackIDs() + # point2rack.add_racks(res_rack_seg) - self.assertEqual(point2rack.get_racks_from_point(Point(1, 0)), ["Rack_1-0", "Rack_2-0"]) - self.assertEqual(point2rack.get_racks_from_point(Point(5, 6)), ["Rack_1-0"]) - self.assertEqual(point2rack.get_points_from_rack("Rack_2-0"), [Point(1, 0), Point(1, 8)]) + # self.assertEqual(point2rack.get_racks_from_point(Point(1, 0)), ["Rack_1-0", "Rack_2-0"]) + # self.assertEqual(point2rack.get_racks_from_point(Point(5, 6)), ["Rack_1-0"]) + # self.assertEqual(point2rack.get_points_from_rack("Rack_2-0"), [Point(1, 0), Point(1, 8)]) - def test_add_point_interim(self): - ''' Testet das hinzufügen und einsortieren eines Zwischenpunktes zwischen Rack-Anfang und Rack-Ende''' + # def test_add_point_interim(self): + # ''' Testet das hinzufügen und einsortieren eines Zwischenpunktes zwischen Rack-Anfang und Rack-Ende''' - res_rack_seg = {'Rack_1-0': [Point(1, 0), Point(5, 6)], - 'Rack_2-0': [Point(1, 8), Point(1, 0)], - 'Rack_2-1': [Point(0, 10), Point(5, 10)]} + # res_rack_seg = {'Rack_1-0': [Point(1, 0), Point(5, 6)], + # 'Rack_2-0': [Point(1, 8), Point(1, 0)], + # 'Rack_2-1': [Point(0, 10), Point(5, 10)]} - point2rack = RackIDs() - point2rack.add_racks(res_rack_seg) - point2rack.add_point_to_rack(Point(1,4), "Rack_2-0") + # point2rack = RackIDs() + # point2rack.add_racks(res_rack_seg) + # point2rack.add_point_to_rack(Point(1,4), "Rack_2-0") - self.assertEqual(point2rack.get_points_from_rack("Rack_2-0"), [Point(1, 0), Point(1,4), Point(1, 8)]) + # self.assertEqual(point2rack.get_points_from_rack("Rack_2-0"), [Point(1, 0), Point(1,4), Point(1, 8)]) - def test_add_sensor(self): - ''' Erzeugt Aufpunkt an dem Sensor nähesten Rack und fügt diesen auf Rack ein (sortiert).''' + # def test_add_sensor(self): + # ''' Erzeugt Aufpunkt an dem Sensor nähesten Rack und fügt diesen auf Rack ein (sortiert).''' - rack_segs = {'Rack_1-0': [Point(0, 0), Point(0, 10)], - 'Rack_2-0': [Point(10, -2), Point(10, 5)], - 'Rack_2-1': [Point(0, 3), Point(10, 3)]} + # rack_segs = {'Rack_1-0': [Point(0, 0), Point(0, 10)], + # 'Rack_2-0': [Point(10, -2), Point(10, 5)], + # 'Rack_2-1': [Point(0, 3), Point(10, 3)]} - sensors = {'Sens_1': Point(1, 1), - 'Sens_2': Point(2, 4), - 'Sens_3': Point(9, 2)} + # sensors = {'Sens_1': Point(1, 1), + # 'Sens_2': Point(2, 4), + # 'Sens_3': Point(9, 2)} - an = Anlage() - point2rack = an.set_racks(rack_segs) - an.add_sensors(sensors) + # an = Anlage() + # point2rack = an.set_racks(rack_segs) + # an.add_sensors(sensors) - plist1 = an.get_points_from_rack("Rack_1-0") + # plist1 = an.get_points_from_rack("Rack_1-0") - an.connect_sensors_to_racks() - plist2 = an.get_points_from_rack("Rack_1-0") + # an.connect_sensors_to_racks() + # plist2 = an.get_points_from_rack("Rack_1-0") - self.assertEqual(plist1, [Point(0, 0), Point(0, 10)]) - self.assertEqual(plist2, [Point(0, 0), Point(0,1), Point(0, 10)]) + # self.assertEqual(plist1, [Point(0, 0), Point(0, 10)]) + # self.assertEqual(plist2, [Point(0, 0), Point(0,1), Point(0, 10)]) - - def test_generate_graph(self): - '''Generiert einen Graphen in 3 unterschiedlichen Ausbaustufen (nur Racks, Racks+Sensoren, Racks+Sensoren+Unterverteiler)''' + + # def test_add_equipment_w_tree(self): - rack_segs = {'Rack_1-0': [Point(0, 0), Point(0, 10)], - 'Rack_2-0': [Point(10, -2), Point(10, 5)], - 'Rack_2-1': [Point(0, 3), Point(10, 3)]} + # racks = {'Rack_1': [Point(0, 0), Point(0, 10)], + # 'Rack_2': [Point(10, -2), Point(10, 5)], + # 'Rack_3': [Point(0, 3), Point(10, 3)]} + + # sensors = {'Sens_1': Point(1, 1), + # 'Sens_2': Point(2, 4), + # 'Sens_3': Point(9, 2)} + + # distributors = {'Dist_1': Point(-1, 9), + # 'Dist_2': Point(11, 0)} + + # an = Anlage(tol_snap=1) + # an.set_racks(racks) + # an.join_racks() + + # an.add_sensors(sensors) + # an.add_distributors(distributors) + # an.connect_equipment_to_racks(an._sensors, an._sensor_onpoints) + # an.connect_equipment_to_racks(an._distributors, an._distributors_onpoints) + + # plist1 = an.get_points_from_rack("Rack_1") + # plist2 = an.get_points_from_rack("Rack_2") + + + # self.assertEqual(plist1, [Point(0, 0), Point(0, 1), Point(0, 3), Point(0, 9), Point(0, 10)]) + # self.assertEqual(plist2, [Point(10, -2), Point(10, 0), Point(10, 2), Point(10, 3), Point(10, 5)]) + + + def test_add_equipment_w_tree_batch(self): + + racks = {'Rack_1': [Point(0, 0), Point(0, 10)], + 'Rack_2': [Point(10, -2), Point(10, 5)], + 'Rack_3': [Point(0, 3), Point(10, 3)]} sensors = {'Sens_1': Point(1, 1), 'Sens_2': Point(2, 4), @@ -758,90 +876,180 @@ class TestLinesweep(unittest.TestCase): distributors = {'Dist_1': Point(-1, 9), 'Dist_2': Point(11, 0)} + + an = Anlage(tol_snap=1) + an.set_racks(racks) + an.join_racks() - an = Anlage() - an.set_racks(rack_segs) - an.join_racks + an.add_sensors(sensors) + an.add_distributors(distributors) + an.connect_equipment_batch(an._sensors, an._sensor_onpoints) + an.connect_equipment_batch(an._distributors, an._distributors_onpoints) + + plist1 = an.get_points_from_rack("Rack_1") + plist2 = an.get_points_from_rack("Rack_2") G1 = nx.Graph() pos = an.generate_graph(G1) nx.draw(G1, pos, with_labels=False, node_size=10, font_size=8) plt.show() - an.add_sensors(sensors) - an.connect_sensors_to_racks() - G2 = nx.Graph() - pos = an.generate_graph(G2) - edge_colors = [G2[u][v].get('color', 'black') for u, v in G2.edges()] - nx.draw(G2, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) - plt.show() - an.add_distributors(distributors) - an.connect_distributor_to_racks() - G3 = nx.Graph() - pos = an.generate_graph(G3) - edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()] - nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) - plt.show() + self.assertEqual(plist1, [Point(0, 0), Point(0, 1), Point(0, 3), Point(0, 9), Point(0, 10)]) + self.assertEqual(plist2, [Point(10, -2), Point(10, 0), Point(10, 2), Point(10, 3), Point(10, 5)]) - def test_Wegsuche(self): - ''' Erstellt Graphen mit Racks, Sensoren und Unterverteilern und sucht kürzeste Wege von Unterverteiler zu zugehörigen Sensoren''' + # def test_wegsuche_w_tree(self): + # racks = {'Rack_1-0': [Point(0, 0), Point(0, 10)], + # 'Rack_2-0': [Point(10, -2), Point(10, 5)], + # 'Rack_2-1': [Point(0, 3), Point(10, 3)]} - rack_segs = {'Rack_1': [Point(0, 0), Point(0, 10)], - 'Rack_2': [Point(10, -2), Point(10, 5)], - 'Rack_3': [Point(0, 3), Point(10, 3)]} + # sensors = {'Sens_1': Point(1, 1), + # 'Sens_2': Point(2, 4), + # 'Sens_3': Point(9, 2)} - sensors = {'Sens_1': Point(1, 1), - 'Sens_2': Point(2, 4), - 'Sens_3': Point(9, 2)} + # distributors = {'Dist_1': Point(-1, 9), + # 'Dist_2': Point(11, 0)} - distributors = {'Dist_1': Point(-1, 9), - 'Dist_2': Point(11, 0)} + # mapping = {'Dist_1': ['Sens_1', 'Sens_2'], + # 'Dist_2': ['Sens_3']} - mapping = {'Dist_1': ['Sens_1', 'Sens_2'], - 'Dist_2': ['Sens_3']} + # an = Anlage(tol_snap=1) + # an.set_racks(racks) + # an.join_racks() - # Erstelle Anlage - an = Anlage(tol_snap=1) - # Füge racks aus Daten hinzu - an.set_racks(rack_segs) - # Verbinde Racks miteinander (ggf. verlängere ungenaue Racks) - an.join_racks() - # Füge Sensoren als Knoten hinzu - an.add_sensors(sensors) - # Verbinde Sensoren mit deren naheliegendsten Racks - an.connect_sensors_to_racks() - # Füge UV hinzu - an.add_distributors(distributors) - # Verbinde UV mit deren naheliegendsten Racks - an.connect_distributor_to_racks() - # Verknüpfe Sensoren mit zugehörigem UV - an.map_distributors_to_sensors(mapping) - - # Initialisiere Graph - G3 = nx.Graph() - # Fülle eben erstellten Graphen mit Daten - pos = an.generate_graph(G3) - # Extrahiere Farb-Informationen der Kanten - edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()] - # Zeiche Graphen und zeige in - nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) - plt.show() + # an.add_sensors(sensors) + # an.add_distributors(distributors) + # an.connect_equipment_to_racks(an._sensors, an._sensor_onpoints) + # an.connect_equipment_to_racks(an._distributors, an._distributors_onpoints) - # Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren - paths = an.create_cable_paths(G3) + # an.map_distributors_to_sensors(mapping) + + # G = nx.Graph() + # Fülle eben erstellten Graphen mit Daten + # pos = an.generate_graph(G) + # Extrahiere Farb-Informationen der Kanten + # edge_colors = [G[u][v].get('color', 'black') for u, v in G.edges()] + # Zeiche Graphen und zeige in + # nx.draw(G, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) + # plt.show() + + # Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren + # paths = an.create_cable_paths(G) + + # paths_by_id = {p['id']: p for p in paths["kabel"]} - self.assertEqual(paths['Dist_1-Sens_1']["path_coords"], [Point(-1, 9), Point(0, 9), Point(0, 3), Point(0, 1), Point(1, 1)]) - self.assertEqual(paths['Dist_1-Sens_2']["path_coords"], [Point(-1, 9), Point(0, 9), Point(0, 3), Point(2, 3), Point(2, 4)]) - self.assertEqual(paths['Dist_2-Sens_3']["path_coords"], [Point(11, 0), Point(10, 0), Point(10, 2), Point(9, 2)]) + # self.assertEqual(paths_by_id['Dist_1-Sens_1']["coords"], [{'x': -1.0, 'y': 9.0}, {'x': 0.0, 'y': 9.0}, {'x': 0.0, 'y': 3.0}, {'x': 0.0, 'y': 1.0}, {'x': 1.0, 'y': 1.0}]) + # self.assertEqual(paths_by_id['Dist_1-Sens_2']["coords"], [{'x': -1.0, 'y': 9.0}, {'x': 0.0, 'y': 9.0}, {'x': 0.0, 'y': 3.0}, {'x': 2.0, 'y': 3.0}, {'x': 2.0, 'y': 4.0}]) + # self.assertEqual(paths_by_id['Dist_2-Sens_3']["coords"], [{'x': 11.0, 'y': 0.0}, {'x': 10.0, 'y': 0.0}, {'x': 10.0, 'y': 2.0}, {'x': 9.0, 'y': 2.0}]) - self.assertEqual(paths['Dist_1-Sens_1']["path_length"], 10) - self.assertEqual(paths['Dist_1-Sens_2']["path_length"], 10) - self.assertEqual(paths['Dist_2-Sens_3']["path_length"], 4) + # self.assertEqual(paths_by_id['Dist_1-Sens_1']["length"], 10) + # self.assertEqual(paths_by_id['Dist_1-Sens_2']["length"], 10) + # self.assertEqual(paths_by_id['Dist_2-Sens_3']["length"], 4) + + + # def test_generate_graph(self): + # '''Generiert einen Graphen in 3 unterschiedlichen Ausbaustufen (nur Racks, Racks+Sensoren, Racks+Sensoren+Unterverteiler)''' + + # rack_segs = {'Rack_1-0': [Point(0, 0), Point(0, 10)], + # 'Rack_2-0': [Point(10, -2), Point(10, 5)], + # 'Rack_2-1': [Point(0, 3), Point(10, 3)]} + + # sensors = {'Sens_1': Point(1, 1), + # 'Sens_2': Point(2, 4), + # 'Sens_3': Point(9, 2)} + + # distributors = {'Dist_1': Point(-1, 9), + # 'Dist_2': Point(11, 0)} + + # an = Anlage() + # an.set_racks(rack_segs) + # an.join_racks + + # G1 = nx.Graph() + # pos = an.generate_graph(G1) + # nx.draw(G1, pos, with_labels=False, node_size=10, font_size=8) + # plt.show() + + # an.add_sensors(sensors) + # an.connect_sensors_to_racks() + # G2 = nx.Graph() + # pos = an.generate_graph(G2) + # edge_colors = [G2[u][v].get('color', 'black') for u, v in G2.edges()] + # nx.draw(G2, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) + # plt.show() + + # an.add_distributors(distributors) + # an.connect_distributor_to_racks() + # G3 = nx.Graph() + # pos = an.generate_graph(G3) + # edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()] + # nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) + # plt.show() + + + # def test_Wegsuche(self): + # ''' Erstellt Graphen mit Racks, Sensoren und Unterverteilern und sucht kürzeste Wege von Unterverteiler zu zugehörigen Sensoren''' + + # rack_segs = {'Rack_1': [Point(0, 0), Point(0, 10)], + # 'Rack_2': [Point(10, -2), Point(10, 5)], + # 'Rack_3': [Point(0, 3), Point(10, 3)]} + + # sensors = {'Sens_1': Point(1, 1), + # 'Sens_2': Point(2, 4), + # 'Sens_3': Point(9, 2)} + + # distributors = {'Dist_1': Point(-1, 9), + # 'Dist_2': Point(11, 0)} + + # mapping = {'Dist_1': ['Sens_1', 'Sens_2'], + # 'Dist_2': ['Sens_3']} + + # Erstelle Anlage + # an = Anlage(tol_snap=1) + # Füge racks aus Daten hinzu + # an.set_racks(rack_segs) + # Verbinde Racks miteinander (ggf. verlängere ungenaue Racks) + # an.join_racks() + # Füge Sensoren als Knoten hinzu + # an.add_sensors(sensors) + # Verbinde Sensoren mit deren naheliegendsten Racks + # an.connect_sensors_to_racks() + # Füge UV hinzu + # an.add_distributors(distributors) + # Verbinde UV mit deren naheliegendsten Racks + # an.connect_distributor_to_racks() + # Verknüpfe Sensoren mit zugehörigem UV + # an.map_distributors_to_sensors(mapping) + + # Initialisiere Graph + # G3 = nx.Graph() + # Fülle eben erstellten Graphen mit Daten + # pos = an.generate_graph(G3) + # Extrahiere Farb-Informationen der Kanten + # edge_colors = [G3[u][v].get('color', 'black') for u, v in G3.edges()] + # Zeiche Graphen und zeige in + # nx.draw(G3, pos, with_labels=False, node_size=10, font_size=8, edge_color=edge_colors) + # plt.show() + + # Ermittle kürzeste Wege von Unterverteilern zu zugehörigen Sensoren + # paths = an.create_cable_paths(G3) + + # paths_by_id = {p['id']: p for p in paths["kabel"]} + + + # self.assertEqual(paths_by_id['Dist_1-Sens_1']["coords"], [{'x': -1.0, 'y': 9.0}, {'x': 0.0, 'y': 9.0}, {'x': 0.0, 'y': 3.0}, {'x': 0.0, 'y': 1.0}, {'x': 1.0, 'y': 1.0}]) + # self.assertEqual(paths_by_id['Dist_1-Sens_2']["coords"], [{'x': -1.0, 'y': 9.0}, {'x': 0.0, 'y': 9.0}, {'x': 0.0, 'y': 3.0}, {'x': 2.0, 'y': 3.0}, {'x': 2.0, 'y': 4.0}]) + # self.assertEqual(paths_by_id['Dist_2-Sens_3']["coords"], [{'x': 11.0, 'y': 0.0}, {'x': 10.0, 'y': 0.0}, {'x': 10.0, 'y': 2.0}, {'x': 9.0, 'y': 2.0}]) + + # self.assertEqual(paths_by_id['Dist_1-Sens_1']["length"], 10) + # self.assertEqual(paths_by_id['Dist_1-Sens_2']["length"], 10) + # self.assertEqual(paths_by_id['Dist_2-Sens_3']["length"], 4) if __name__ == '__main__': + print(shapely.__file__) + print(shapely.__version__) unittest.main() \ No newline at end of file