def test_flow_a(self): graph = Graph() graph.add_source(Node(1, Point(5, 1), NodeType.SOURCE)) graph.add_source(Node(1, Point(1, 3), NodeType.SOURCE)) graph.add_sink(Node(2, Point(0, 0), NodeType.SINK)) graph.add_bifurcation(Point(0, 0)) network = Network(0.000001, 0.5, graph) flow = Flow(network) flow.get_flow() steps = flow.steps theta = flow.theta cost = flow.cost self.assertLessEqual(len(steps), flow.max_iterations) self.assertGreaterEqual(theta[-1], 90 - 0.2) self.assertLessEqual(cost[-1], cost[-2] + flow.difference_cutoff)
def get_network(): graph = Graph() source1 = Node(1, Point(0, 1), NodeType.SOURCE) source2 = Node(1, Point(0, 5), NodeType.SOURCE) sink = Node(2, Point(4, 3), NodeType.SINK) bifurcation = Node(0, Point(4, 3), NodeType.BIFURCATION) graph.add_node(source1) graph.add_node(source2) graph.add_node(sink) graph.add_node(bifurcation) graph.add_edge(source1, bifurcation) graph.add_edge(source2, bifurcation) graph.add_edge(bifurcation, sink) network = Network(.2, 0.5, graph) network.calculate_g(np.array([3.840210415833968, 3.000012521887091])) return network
def test_network(self): graph = Graph() graph.add_source(Node(1, Point(5, 1), NodeType.SOURCE)) graph.add_source(Node(1, Point(1, 3), NodeType.SOURCE)) graph.add_sink(Node(2, Point(0, 0), NodeType.SINK)) graph.add_bifurcation(Point(0, 0)) network = Network(0.1, 0.5, graph) self.assertEqual(network.alpha, 0.5) self.assertEqual(network.h, 0.1) self.assertEqual(len(network.graph.sinks), 1) self.assertEqual(len(network.graph.sources), 2) self.assertEqual(len(network.graph.bifurcations), 1) self.assertEqual(network.calculate_optimal_angle(), 90) network_2 = Network(0.000001, 0.45, graph) self.assertEqual(network_2.calculate_optimal_angle(), 93.83980058897298)
def get_flow(self, verbose=False): i: int = 0 graph: Graph = self.network.graph bifurcation = graph.get_bifurcations()[0] self.update_lists(bifurcation) #print(graph) # checks for L shape criteria - based on cost? while self.should_repeat(i): graph.remove_node(bifurcation) #minimized = minimize(self.network.calculate_g, bifurcation.point.point_as_array(), method = 'Nelder-Mead', options={'disp': True}) minimized = minimize(self.network.calculate_g, \ bifurcation.point.point_as_array(), method = 'Nelder-Mead') if verbose: logging.warning(minimized) bifurcation = Node(0, Point(minimized.x[0], minimized.x[1]),\ NodeType.BIFURCATION) graph.add_node(bifurcation) for source in graph.get_sources(): graph.add_edge(source, bifurcation) graph.add_edge(bifurcation, graph.get_sink()) self.network.graph = graph #print(graph) self.update_lists(bifurcation) i += 1 return self.network
def test_node(self): weight = 5 point1 = Point(2, -3) node_type = NodeType.SOURCE node1 = Node(weight, point1, node_type) self.assertEqual(node1.weight, weight) self.assertEqual(node1.point, point1) self.assertEqual(node1.node_type, NodeType.SOURCE)
def get_network(self): self.graph = Graph() source1 = Node(self.source_1_weight.value, Point(self.source_1_x.value, self.source_1_y.value), NodeType.SOURCE) source2 = Node(self.source_2_weight.value, Point(self.source_2_x.value, self.source_2_y.value), NodeType.SOURCE) sink = Node((self.source_1_weight.value + self.source_2_weight.value), Point(self.sink_x.value, self.sink_y.value), NodeType.SINK) bifurcation = Node(0, Point(self.sink_x.value, self.sink_y.value), NodeType.BIFURCATION) self.graph.add_node(source1) self.graph.add_node(source2) self.graph.add_node(sink) self.graph.add_node(bifurcation) self.graph.add_edge(source1, bifurcation) self.graph.add_edge(source2, bifurcation) self.graph.add_edge(bifurcation, sink) return Network(self.h.value, self.alpha.value, self.graph)
def test_node_distance(self): point1 = Point(2, -3) point2 = Point(5, 1) node1 = Node(1, point1, NodeType.SOURCE) node2 = Node(1, point2, NodeType.SOURCE) self.assertEqual(node1.get_distance_to(node2), 5)
def test_point_distance(self): point1 = Point(2, -3) point2 = Point(5, 1) self.assertEqual(point1.get_distance_to(point2), 5)
def test_point(self): x = 4.4 y = 5.5 point = Point(x, y) self.assertEqual(point.x, x) self.assertEqual(point.y, y)
def calculate_g(self, new_bifurcation_arr: np.array) -> float: new_bifurcation = Point(new_bifurcation_arr[0], new_bifurcation_arr[1]) cost: float = self.calculate_transportation_cost(new_bifurcation) fill: float = self.calculate_fill(new_bifurcation) #print(f"new_bifurcation: {new_bifurcation}, cost: {cost}, fill: {fill}") return (cost**2) + ((fill**2) / self.h)