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)
def new_traject(self, name, station, color): traject = Traject(name, station) for row in self.stations: if station == row.name: start_station = row break self.travel(traject, start_station, color)
def new_traject(self, name, station, color): # Set travelled connections to false for connection in self.connections: connection.travelled == False # Create new traject traject = Traject(name, station) # Transform string to object for row in self.stations: if station == row.name: start_station = row break self.travel(traject, start_station, color)
def _create_schedules(self): """ Parameters: None Returns: None Creates the schedules in the simulation, adjusted for passing whole hours """ for schedule in timeslots: current = [] last = -1 hour = False for slot in schedule: slot1 = slot[1] slot2 = slot[2] if len(slot) <= 3: slot3 = False else: slot3 = slot[3] if slot1 < 0 and slot2 < 0: slot3 = True if ((slot1 > slot2 and slot2 >= 0) or (slot1 < last and slot1 >= 0)): hour = True if hour: if slot1 >= 0 and (slot1 <= slot2 or slot2 < 0): slot1 += 60 if slot1 >= 0: while slot1 < last: slot1 += 60 if slot[2] >= 0: slot2 += 60 if slot2 >= 0: while slot2 < slot1: slot2 += 60 current.append( TimeSlot(self._get_station(slot[0]), slot1, slot2, slot3)) else: current.append( TimeSlot(self._get_station(slot[0]), slot1, slot2, slot3)) if slot2 >= 0: last = slot2 self.schedules.append(Traject(current))
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)