def _add_callbacks(self,
                    callbacks: Optional[Union[Multidict, dict]] = None):
     # Add callbacks
     own_callbacks = Multidict()
     if callbacks:
         own_callbacks.update(callbacks)
     callback_rerouter.inner_callbacks = own_callbacks
示例#2
0
 def _add_callbacks(self,
                    callbacks: Optional[Union[Multidict, dict]] = None):
     # Add callbacks
     own_callbacks = Multidict(
         {grb.GRB.Callback.MIPSOL: self._general_circle_elimination})
     if callbacks:
         own_callbacks.update(callbacks)
     callback_rerouter.inner_callbacks = own_callbacks
 def get_ordered_times(self) -> Multidict:
     multidict = Multidict()
     for time_key in self.times:
         # Check edge exists in graph
         if self.graph.ad_matrix[time_key[0], time_key[1]] == 0:
             raise KeyError("Graph does not containt an edge with indices {0}".format(time_key))
         multidict[self.times[time_key]] = time_key
         self.makespan = max(0, self.times[time_key])
     return multidict
示例#4
0
 def _decode_multidict(self, m_dict):
     return Multidict(
         {float(key): m_dict["dict"]
          for key in m_dict["dict"]})
示例#5
0
def visualize_min_sum_sol_2d(solution: 'AngularGraphSolution'):
    graph = solution.graph
    fig = plt.figure()
    axis = plt.subplot()
    axis.axis('off')

    _visualize_edges_2d(solution.graph)
    _visualize_vertices_2d(solution.graph)
    _visualize_celest_body_2d(axis, solution.graph)

    # Make an edge order for vertices
    vertex_order = Multidict()
    ordered_times = solution.get_ordered_times()
    for time_key in ordered_times.get_ordered_keys():
        for edges in ordered_times[time_key]:
            if edges[0] < edges[1]:
                vertex_order[edges[0]] = edges
                vertex_order[edges[1]] = edges
    # Get minimum edge length
    min_length = max(
        np.array([
            np.linalg.norm(solution.graph.vertices[i] -
                           solution.graph.vertices[j])
            for i, j in solution.graph.edges
        ]).min(), 0.4)
    # Draws the angle paths in a circular fashion
    path_list = []
    last_points = []
    for vertex_key in vertex_order:
        last_edge = None
        last_direction = None
        current_min_length = min_length * 0.3
        last_point = None
        for edge in vertex_order[vertex_key]:
            if last_edge:
                other_vertices = np.hstack([
                    np.setdiff1d(np.array(last_edge), np.array([vertex_key])),
                    np.setdiff1d(np.array(edge), np.array([vertex_key]))
                ])

                angles = [
                    get_angle(graph.vertices[vertex_key],
                              graph.vertices[vertex_key] + [1, 0],
                              graph.vertices[other_vertex])
                    for other_vertex in other_vertices
                ]

                # If y-coord is below the current vertex we need to calculate the angle different
                for i in range(len(angles)):
                    if graph.vertices[other_vertices[i]][1] < graph.vertices[
                            vertex_key][1]:
                        angles[i] = 360 - angles[i]

                # Calculate if we need to go from angle[0] to angle[1] or other way around
                # to not create an arc over 180 degrees
                diff = abs(angles[0] - angles[1])
                if diff > 180:
                    diff = 360 - diff
                normal_angle_direction = math.isclose((angles[0] + diff) % 360,
                                                      angles[1],
                                                      rel_tol=1e-5)
                if not normal_angle_direction:
                    angles = reversed(angles)

                # 1 shall be clockwise and -1 counter-clockwise direction
                current_direction = 1 if normal_angle_direction else -1

                if last_direction:
                    if current_direction != last_direction:  # direction change happened
                        current_min_length *= 1.25

                # Transform the arc to the right position
                transform = mtransforms.Affine2D().scale(
                    current_min_length, current_min_length)
                transform = transform.translate(*graph.vertices[vertex_key])
                arc = Path.arc(*angles)
                arc_t = arc.transformed(transform)

                if last_direction:
                    if current_direction != last_direction:  # direction change happened
                        last_vertex = path_list[-1].vertices[
                            -1] if last_direction == 1 else path_list[
                                -1].vertices[0]
                        new_vertex = arc_t.vertices[
                            0] if current_direction == 1 else arc_t.vertices[-1]
                        bridge_path = Path([last_vertex, new_vertex])
                        path_list.append(bridge_path)

                last_direction = current_direction
                path_list.append(arc_t)
                last_point = path_list[-1].vertices[
                    -1] if last_direction == 1 else path_list[-1].vertices[0]
                last_points.append(last_point)
            last_edge = edge
        # Add these points to detect direction
        last_points.append(last_point)

    path_collection = PathCollection(path_list,
                                     edgecolor='r',
                                     facecolor='#00000000')
    axis.add_collection(path_collection)
    a_last_points = np.array([l for l in last_points if l is not None])
    plt.plot(a_last_points[:, 0], a_last_points[:, 1], 'r.')
    axis.autoscale()
    plt.show()
 def _add_callbacks(self, callbacks):
     own_callbacks = Multidict(
         {gurobipy.GRB.Callback.MIPSOL: self._subtour_elimination})
     if callbacks:
         own_callbacks.update(callbacks)
     callback_rerouter.inner_callbacks = own_callbacks