def _create_abs_graph_and_edge_translation(self, graph): tripel_edges, abs_graph = convert_graph_to_angular_abstract_graph( graph, simple_graph=False, return_tripel_edges=True) edge_vert = {i: {} for i in range(graph.vert_amount)} for edge in tripel_edges: edge_vert[edge[0]][edge[1:]] = tripel_edges[edge] return edge_vert, abs_graph
def test_get_tripel_edges(): graph = create_circle_n_k(15, 5) tripel_control, abs_graph = convert_graph_to_angular_abstract_graph( graph, simple_graph=False, return_tripel_edges=True) tripel_edges = get_tripeledges_from_abs_graph(abs_graph) assert len(tripel_edges) == len(tripel_control) for edge in tripel_edges: assert tripel_control[edge] == tripel_edges[edge]
def _build_model(self, graph): self.graph = graph self.abstract_graph = convert_graph_to_angular_abstract_graph(graph, simple_graph=False) self.model = cp_model.CpModel() self._add_variables() costs = self._to_int_cost(self.abstract_graph.costs) self._add_objective(costs) self._add_constraints()
def solve(self, graph: Graph, **kwargs): # count how many vertices have largest angle north or south # Maybe choose which set get which starting position accordingly order = None start_time = time.time() error_message = None try: colors = kwargs.pop("colors", None) if colors is None: colors = self._calc_subsets(graph) # Make sure V_1 and V_2 are bipartite partitions assert len(colors) == graph.vert_amount assert np.alltrue([colors[i] != colors[j] for i, j in graph.edges]) sectors_V = { i: get_vertex_sectors(graph.vertices[i], graph.vertices[np.nonzero( graph.ad_matrix[i])], start_north=bool(colors[i])) for i in range(graph.vert_amount) } tripel_edges, abs_graph = convert_graph_to_angular_abstract_graph( graph, simple_graph=False, return_tripel_edges=True) edge_vert = {i: {} for i in range(graph.vert_amount)} for edge in tripel_edges: edge_vert[edge[0]][edge[1:]] = tripel_edges[edge] used_edges = [] for vertex_key in sectors_V: sectors_info = sectors_V[vertex_key] if len(sectors_info) == 1: continue non_zeros = np.nonzero(graph.ad_matrix[vertex_key])[0] for from_vert, to_vert, angle in sectors_info[:-1]: used_edges.append( edge_vert[vertex_key][non_zeros[from_vert], non_zeros[to_vert]]) dep_graph = get_dep_graph(used_edges, abs_graph) order = [ tuple(abs_graph.vertices[i]) for i in calculate_order(dep_graph, calculate_circle_dep=True) ] except NotImplementedError as e: error_message = str(e) raise e sol = AngularGraphSolution(graph, time.time() - start_time, self.__class__.__name__, self.solution_type, is_optimal=False, error_message=error_message, order=order) return sol
def build_model(self, graph: Graph): self.found_circles = [] self.graph = graph self.abstract_graph = convert_graph_to_angular_abstract_graph(graph, simple_graph=False) self.model = grb.Model() self.model.setParam("LazyConstraints", 1) for param in self.params: self.model.setParam(param, self.params[param]) #self.vertex_edges = grb.tupledict() costs = self._add_variables(self.abstract_graph) self._add_objective(costs) self._add_constraints() self.model.update()
def solve(self, graph: Graph, **kwargs): # Sort'em hull = ConvexHull(graph.vertices) # Indices are in counter clockwise order, therefore reverse them indices = [vertex for vertex in reversed(hull.vertices)] length = len(indices) assert length == graph.vert_amount k = graph.ad_matrix[0].sum() / 2 assert k.is_integer() k = int(k) neighbors = { indices[i]: tuple(indices[int(j % length)] for j in range(i - k, i + k + 1) if i != j) for i in range(length) } # Just check if every node has 2k neighbors for index in indices: #neighbors = tuple(indices[int(j % length)] for j in range(i-k, i+k+1) if i != j) assert graph.ad_matrix[index, neighbors[index]].sum() == 2 * k tripel_edges, abs_graph = convert_graph_to_angular_abstract_graph( graph, simple_graph=False, return_tripel_edges=True) edge_vert = {i: {} for i in range(length)} for edge in tripel_edges: edge_vert[edge[0]][edge[1:]] = tripel_edges[edge] #edge_vert[edge[0]][tuple(reversed(edge[1:]))] = tripel_edges[edge] taken_edges = [] # Phase one self._phase_one(indices, k, neighbors, taken_edges, edge_vert) # Phase two split_vertex_index = round(length / 2) self._phase_two(split_vertex_index, k, indices, neighbors, taken_edges, edge_vert, length) # Clockwise part self._phase_three(k, indices, split_vertex_index, neighbors, taken_edges, edge_vert) dep_graph = get_dep_graph(taken_edges, abs_graph) order = calculate_order(dep_graph, calculate_circle_dep=True) returned_order = [tuple(abs_graph.vertices[i]) for i in order] return AngularGraphSolution(graph, 0, self.__class__.__name__, 'MinSum', False, order=reversed(returned_order))
def solve(self, graph: Graph, **kwargs): self.abstract_graph = convert_graph_to_angular_abstract_graph(graph) self.graph = graph self.model = grb.Model() for param in self.params: self.model.setParam(param, self.params[param]) self._add_callbacks(kwargs.pop("callbacks", None)) self.vertex_edges = grb.tupledict() costs = self._add_variables(self.abstract_graph) for vertex_index in len(max(self.abstract_graph.vertices)): edges = self._build_hamilton_path(vertex_index, self.abstract_graph) edges_dict = grb.tupledict({(vertex_index, u, v): self.edges[u, v] for u, v in edges}) self.vertex_edges.update(edges_dict) self._add_objective(costs) self.model.optimize()