def cost_delta(self): if self.cost is not None: return self.cost # find second trip to exchange gifts with and valid gifts to swap first_gifts_to_try = np.random.permutation( len(self.trips[self.first_trip])) for fg in first_gifts_to_try: self.second_trip = np.random.randint(len(self.trips)) while len(self.trips[self.second_trip] ) < 3 or self.first_trip == self.second_trip: self.second_trip = np.random.randint(len(self.trips)) self.first_gift = fg self.second_gift = self._get_valid_swapee() if self.second_gift is not None: break # find insertion indexes with minimum cost self.first_trip_insertion_index, cost_to_insert_first = Neighbor.find_best_insertion_index( self.trips[self.first_trip], self.trips[self.second_trip][self.second_gift], index_to_be_removed=self.first_gift) self.second_trip_insertion_index, cost_to_insert_second = Neighbor.find_best_insertion_index( self.trips[self.second_trip], self.trips[self.first_trip][self.first_gift], index_to_be_removed=self.second_gift) # update temporary trips with new insertion to accurately calculate the cost of deletion temporary_first_trip = self.trips[self.first_trip] temporary_first_trip = np.insert( temporary_first_trip, self.first_trip_insertion_index, self.trips[self.second_trip][self.second_gift], axis=0) temporary_second_trip = self.trips[self.second_trip] temporary_second_trip = np.insert( temporary_second_trip, self.second_trip_insertion_index, self.trips[self.first_trip][self.first_gift], axis=0) # calculate (negative) cost of deletion cost_to_remove_first = self._cost_to_remove_gift( temporary_first_trip, self.first_gift if self.first_gift < self.first_trip_insertion_index else self.first_gift + 1) cost_to_remove_second = self._cost_to_remove_gift( temporary_second_trip, self.second_gift if self.second_gift < self.second_trip_insertion_index else self.second_gift + 1) self.cost = cost_to_insert_first + cost_to_insert_second + cost_to_remove_first + cost_to_remove_second return self.cost
def cost_delta(self): if self.cost is not None: return self.cost if self.trip is None: self.trip = np.random.randint(len(self.trips)) while len(self.trips[self.trip]) < 2: self.trip = np.random.randint(len(self.trips)) source = self.trips[self.trip] if self.gift_to_move is not None or self.destination_trip is not None or self.destination_insertion_index is not None or self.cost_to_insert_in_destination is not None: # we should have *all* of these set return self.cost_to_insert_in_destination + self._cost_to_remove_gift( source, self.gift_to_move) self.gift_to_move = np.random.randint(len(source)) self.destination_trip = self._get_valid_target_trip() gift = source[self.gift_to_move] self.destination_insertion_index, cost_to_insert = Neighbor.find_best_insertion_index( self.trips[self.destination_trip], gift) cost_to_remove = self._cost_to_remove_gift(source, self.gift_to_move) self.cost = cost_to_insert + cost_to_remove return self.cost
def get_optimally_sorted_trips(self, trips): self.log.warning("THIS DOESN'T WORK") self.log.info("Putting trips into optimal order") total_trip_count = len(trips) all_trips = [] for i, trip in enumerate(trips.values()): self.log.debug("Processing trip {}/{} with length {}".format( i + 1, total_trip_count, len(trip[1]))) sorted_trip = sorted(trip[1], key=lambda tup: tup.Weight, reverse=True) this_trip = [] for gift in sorted_trip: if len(this_trip) == 0: this_trip.append(gift) continue gift[utils.TRIP] = len(all_trips) best_index, _ = Neighbor.find_best_insertion_index( np.asarray(this_trip), gift.values, lat_index=1, lon_index=2, weight_index=3) this_trip.insert(best_index, gift) if this_trip: all_trips.append(this_trip) # extract gift/trip mapping combined_trips = np.concatenate(all_trips)[:, [utils.GIFT, utils.TRIP]] return pd.DataFrame(combined_trips, columns=["GiftId", "TripId"])
def cost_delta(self): if self.cost is not None: return self.cost trip = self.trips[self.trip] gift = trip[self.gift_index] cost_to_remove = self._cost_to_remove_gift(trip, self.gift_index) trip_without_gift = np.delete(trip, self.gift_index, axis=0) self.new_index, cost_to_insert = Neighbor.find_best_insertion_index( trip_without_gift, gift, index_to_be_removed=self.gift_index + 1) self.cost = cost_to_insert + cost_to_remove return self.cost
def cost_delta(self): if self.cost is not None: return self.cost self.trip_to_merge, self.trip_assignments_for_gifts = self._find_trip_to_merge() if self.trip_to_merge is None: return 0 trip = self.trips[self.trip_to_merge] self.gift_insertions = [] cost_of_insertions = 0 for trip_index, gift in self.trip_assignments_for_gifts.items(): index_in_trip, cost = Neighbor.find_best_insertion_index(self.trips[trip_index], gift) self.gift_insertions.append((gift, trip_index, index_in_trip)) cost_of_insertions += cost self.cost = cost_of_insertions - utils.weighted_trip_length(trip[:, utils.LOCATION], trip[:, utils.WEIGHT]) return self.cost