Пример #1
0
    def run(self):
        """ Fills schedule with trajects. """

        # Run algorithm until all connections are visited
        while sum([
                len(city.get_connections(False))
                for city in self.schedule.get_cities().values()
        ]):
            self.city_function(self)
            start_city = self.current_city
            traject = Traject()

            # Run until no cities can be connected to traject
            while traject.get_next_city() or traject.get_reversible():
                traject.set_next_city(False)
                connections = self.connections_function(self)

                if len(connections):
                    connection = random.choice(connections)

                    # Add connection to traject if constraints are satisfied
                    if (traject.get_time() + connection.get_time() < self.schedule.get_max_time()) \
                    and connection not in traject.get_connections():
                        traject.add(connection)
                        self.current_city = self.current_city.new_current(
                            connection)
                        traject.set_next_city(True)

                # Reverse direction of traject if no city added
                if not traject.get_next_city():
                    self.current_city = start_city
                    traject.reverse()

            if len(traject.get_connections()):
                self.schedule.add_traject(traject)
Пример #2
0
    def run(self):
        """ Optimizes schedule by merging trajects. """

        # Run until schedule not improved
        while self.shortest["time"] != 100:
            self.shortest = {"time": 100, "state": None, "old_trajects": []}

            # Find best merge of all trajects
            for traject in self.schedule.get_trajects():
                route = traject.get_route()
                self.traject = traject
                self.DepthFirst(route[0])
                self.DepthFirst(route[-1])

            state = self.shortest["state"]

            if state:
                traject_0 = self.shortest["old_trajects"][0]
                traject_1 = self.shortest["old_trajects"][1]

                # Decide to merge two trajects or delete one
                if (state.length() + traject_0.get_time()) \
                < ((len(traject_0.get_connections()) / self.schedule.number_connections) * 10000):
                    traject = Traject()
                    traject.set_time(state.length() + traject_0.get_time() +
                                     traject_1.get_time())

                    if state.get_city() != traject_1.get_route()[0]:
                        traject_1.reverse()

                    if state.start_node().get_city() != traject_0.get_route(
                    )[-1]:
                        traject_0.reverse()

                    traject.set_connections(traject_0.get_connections() + state.connections() \
                                          + traject_1.get_connections())

                    self.schedule.trajects.remove(traject_0)
                    self.schedule.trajects.remove(traject_1)
                    self.schedule.add_traject(traject)
                else:
                    traject_0.remove_traject(self.schedule)

        # Delete traject if score improves
        for traject in self.schedule.get_trajects():
            if 100 + traject.get_time() \
            > ((len(traject.get_connections()) / self.schedule.number_connections) * 10000):
                traject.remove_traject(self.schedule)