def filter_close_places(from_place, limit_extent, place_list): allowed_list = [] for p in place_list: d = utils.compute_distance(p, from_place) if d <= limit_extent: allowed_list.append(p) return allowed_list
def get_next_place( limit_extent, place_list, from_place, prob_come_back ): if limit_extent < 0: return None r_ = random.randint(0, len(place_list) - 1) # select the first place if from_place is None: return place_list[r_] # otherwise select the next one from a given place _from_place else: allowed_list = filter_close_places(from_place, limit_extent, place_list) l = np.max([len(allowed_list) - 1, 1]) # len is at least 1, which include from_place itself # if more than one PoI is found if len(allowed_list) > 1: r = random.randint(0, l) return allowed_list[r] # otherwise else: nn = get_nearest_place(from_place, place_list) distance = utils.compute_distance(from_place, nn) # returns the nearest place if the distance between it and from_place is shorter than limit_extent # otherwise do not retrieve any place return nn if distance < limit_extent else None
def generate_trajectory(extent, place_list, today, prob_come_back, extent_factor): total_extent = 0.0 from_place = None traj = [] while True: tomorrow = today + TIME_DELTA place = get_next_place(extent - total_extent, place_list, from_place, prob_come_back) if place is None: break traj.append(([place], today, tomorrow)) total_extent += utils.compute_distance(place, from_place) if from_place is not None else 0 # total_extent = extent - traj_extent # print extent, total_extent # print from_place, place from_place = place # go forward in time today = tomorrow + TIME_DELTA # print extent, total_extent, utils.compute_trajectory_extent(traj) return traj
def get_nearest_place(given_place, place_list): min_dist = float("inf") p = None for place in place_list: if place[0] != given_place[0]: d = utils.compute_distance(place, given_place) if d < min_dist: min_dist = d p = place return p
def compute_distance_matrix(self): """ This method produces the distance matrix by computing the Euclidean distance between each pair of nodes. :return: nothing, the distance matrix is an attribute of the instance itself """ # initialization of the matrix dimension self.distance_matrix = np.zeros( (self.n_customers + 1, self.n_customers + 1)) # linking customers customers = np.concatenate( ([self.depot_node], self.linehaul_list, self.backhaul_list)) # Euclidean Distance between each pair of nodes for i in customers: for j in customers: self.distance_matrix[i.id, j.id] = compute_distance(i, j)
def generate_trajectory(n_points, extent, place_list, today): total_extent = extent / (n_points - 1) while True: points_count = 0 from_place = None traj = [] while points_count < n_points: tomorrow = today + TIME_DELTA place = get_next_place(total_extent, place_list, from_place) if place is None: break traj.append(([place], today, tomorrow)) total_extent += utils.compute_distance(place, from_place) if from_place is not None else 0 # total_extent = extent - traj_extent # print extent, total_extent # print from_place, place from_place = place # go forward in time today = tomorrow + TIME_DELTA points_count += 1 print points_count, n_points if points_count == n_points: break # print extent, total_extent, utils.compute_trajectory_extent(traj) return traj