def __init__(self, policy_path, true_path, virtual_path, edge_desty, subpath, axes_file, speed_file, query_name): self.sigma = 30 self.vopt = VOpt() self.B = 3 self.policy_path = policy_path self.true_path = true_path self.virtual_path = virtual_path self.edge_desty = edge_desty self.subpath = subpath self.axes_file = axes_file self.speed_file = speed_file self.speed = 50 self.query_name = query_name
def main(self, ): data = self.load() tpath = TPath(self.filename, self.subpath, self.dinx, self.threads_num) edge_time_freq = tpath.get_edge_time_freq(data) self.mkgraph = MkGraph(self.filename) anodes, aedges = self.get_graph() self.mkgraph.add_node_list(anodes) self.mkgraph.add_edge_list(aedges) self.anodes = anodes points = self.get_axes() path_desty, path_count, path_num, overlap, TP = self.get_dict() vopt = VOpt() print('v opt edge ...') #self.edge_time_freq = multiprocessing.Manager().dict() edge_time_freq = self.get_vopt(vopt, edge_time_freq, False, True) path_desty = self.get_vopt(vopt, path_desty, True, True) print('gen ...') #self.vopt = vopt path_desty, out_degrees, out_degrees2, All_Path = self.gen(vopt, path_desty, path_count, path_num, overlap, edge_time_freq, TP, points) print('path desty length %d'%len(path_desty)) self.write_degree(out_degrees, self.subpath+self.store_degree_fname) self.write_degree(out_degrees2, self.subpath+self.store_degree_fname2) self.store_graph(self.subpath+self.graph_store_fname) All_Path_ = {} for ak in All_Path: All_Path_[ak] = All_Path[ak] self.write_json(All_Path_, self.subpath+self.store_desty_fname)
class Rout(): def __init__(self, policy_path, true_path, virtual_path, edge_desty, subpath, axes_file, speed_file, query_name, sigma, eta): self.sigma = 30 self.vopt = VOpt() self.B = 3 self.policy_path = policy_path self.true_path = true_path self.virtual_path = virtual_path self.edge_desty = edge_desty self.subpath = subpath self.axes_file = axes_file self.speed_file = speed_file self.speed = 50 self.query_name = query_name self.sigma = sigma self.eta = eta self.hU = {} def get_axes(self, ): fo = open(self.axes_file) points = {} for line in fo: line = line.split('\t') points[line[0]] = (float(line[2]), float(line[1])) return points def get_distance(self, points, point): (la1, lo1) = points[point[0]] (la2, lo2) = points[point[1]] return geodesic((lo1, la1), (lo2, la2)).kilometers def get_U2(self, u_name): if u_name in self.hU: return self.hU[u_name] if not os.path.isfile(self.policy_path + u_name): print('no this U : %s' % (self.fpath + u_name)) return {} fn = open(self.policy_path + u_name) U = {} for line in fn: line = line.strip().split(';') U[line[0]] = np.zeros(self.eta) if line[1] == '-1' and line[2] == '-1': pass elif line[1] == '-1' and line[2] == '0': U[line[0]] = np.ones(self.eta) else: for i in range(int(line[1]) + 1, int(line[2])): t = 3 + i - int(line[1]) - 1 U[line[0]][i] = float(line[t]) for i in range(int(line[2]), self.eta): U[line[0]][i] = 1.0 fn.close() self.hU[u_name] = U return U def get_dict(self, ): path_desty = {} for l in range(1, 5): with open(self.subpath + self.virtual_path + '_%d.json' % l) as js_file: path_desty_ = json.load(js_file) path_desty.update(path_desty_) with open(self.subpath + self.edge_desty) as js_file: edge_desty = json.load(js_file) edge_desty = dict( sorted(edge_desty.items(), key=operator.itemgetter(0))) vedge_desty = {} for p_k in path_desty: vedge_desty[p_k] = path_desty[p_k][0][1] return vedge_desty, edge_desty, path_desty def get_speed(self, ): speed_dict = {} with open(self.speed_file) as fn: for line in fn: line = line.strip().split('\t') #speed_dict[line[0]] = {int(float(line[1])/float(line[2])*3600): 1.0} speed_dict[line[0]] = { 3600 * float(line[1]) / float(line[2]): 1.0 } return speed_dict def get_graph(self, edge_desty, vedge_desty): speed_dict = self.get_speed() all_nodes, all_edges = set(), set() for key in speed_dict: line = key.split('-') all_nodes.add(line[0]) all_nodes.add(line[1]) all_edges.add(key) all_nodes, all_edges = list(all_nodes), list(all_edges) All_edges = [] for edge in all_edges: edge_ = edge.split('-') if edge in edge_desty: cost1 = edge_desty[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) elif edge in speed_dict: cost1 = speed_dict[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) G2 = nx.DiGraph() G2.add_nodes_from(all_nodes) G2.add_weighted_edges_from(All_edges) return all_edges, all_nodes, G2, speed_dict def get_P(self, ): pass def conv(self, A, B): D = {} for a_k in A: for b_k in B: n_k = float(a_k) + float(b_k) n_v = float(A[a_k]) * float(B[b_k]) D[n_k] = n_v if len(D) <= 3: return D D = dict(sorted(D.items(), key=operator.itemgetter(0))) new_w = self.vopt.v_opt(list(D.keys()), list(D.values()), self.B) new_w = dict(sorted(new_w.items(), key=operator.itemgetter(0))) return new_w def get_dijkstra3(self, G, target): Gk = G.reverse() inf = float('inf') D = {target: 0} Que = PQDict(D) P = {} nodes = Gk.nodes U = set(nodes) while U: #print('len U %d'%len(U)) #print('len Q %d'%len(Que)) if len(Que) == 0: break (v, d) = Que.popitem() D[v] = d U.remove(v) #if v == target: break neigh = list(Gk.successors(v)) for u in neigh: if u in U: d = D[v] + Gk[v][u]['weight'] if d < Que.get(u, inf): Que[u] = d P[u] = v return P def get_min(self, U, node): return np.argwhere(U[node] > 0)[0][0] def cosin_distance(self, vector1, vector2): dot_product = 0.0 normA = 0.0 normB = 0.0 for a, b in zip(vector1, vector2): dot_product += a * b normA += a**2 normB += b**2 if normA == 0.0 or normB == 0.0: return None else: return dot_product / ((normA * normB)**0.5) def rout(self, start, desti, edge_desty, vedge_desty, nodes_order, U, G2, points, speed_dict): path = [] Q, P_hat = [], [] neigh = list(G2.successors(start)) print('neigh %d' % len(neigh)) start_ax = points[start] desti_ax = points[desti] s_d_arr = [desti_ax[0] - start_ax[0], desti_ax[1] - start_ax[1]] all_expire = 0.0 def get_weight(p_hat): w_p_hat = {} if p_hat in edge_desty: w_p_hat = edge_desty[p_hat] elif p_hat in speed_dict: w_p_hat = speed_dict[p_hat] elif p_hat in vedge_desty: w_p_hat = vedge_desty[p_hat] return w_p_hat def get_maxP(w_p_, vv): p_max = 0.0 for pk in w_p_: w_pk = w_p_[pk] pk_ = int(int(pk) / self.sigma) if int(pk) % self.sigma == 0: pk_ += 1 p_max += float(w_pk) * U[vv][pk_] return p_max has_visit = set() has_visit.add(start) Que = PQDict.maxpq() Q = {} for vi in neigh: if vi in has_visit: continue else: has_visit.add(vi) p_hat = start + '-' + vi w_p_hat = get_weight(p_hat) w_min = min([float(l) for l in w_p_hat.keys()]) p_order = nodes_order[vi] #p_hat] tcost1 = time.time() inx_min = np.argwhere(U[vi] > 0) if len(inx_min) == 0: #print('u 0 vd: %s %s'%(vi, desti)) continue inx_min = inx_min[0][0] all_expire += time.time() - tcost1 cost_time = w_min + inx_min * self.sigma if cost_time <= self.T: tcost1 = time.time() p_max = get_maxP(w_p_hat, vi) all_expire += time.time() - tcost1 Que[p_hat] = p_max Q[p_hat] = (p_max, w_p_hat, cost_time) #print('len Q %d'%len(Q)) QQ = {} p_best_p, flag = 'none', False p_max_m, p_best_cost, p_w_p = -1, -1, -1 if len(Q) == 0: return 'none1', -1, -1, -1, all_expire, -1 all_rounds = 0 while len(Q) != 0: (p_hat, pqv) = Que.popitem() all_rounds += 1 (p_max, w_p_hat, cost_time) = Q[p_hat] del Q[p_hat] a = p_hat.rfind('-') v_l = p_hat[a + 1:] if v_l == desti: p_best_p = p_hat p_max_m = p_max p_best_cost = cost_time p_w_p = w_p_hat flag = True break neigh = list(G2.successors(v_l)) cost_sv = min([float(l) for l in w_p_hat.keys()]) vd_d_arr = [ points[desti][0] - points[v_l][0], points[desti][1] - points[v_l][1] ] for u in neigh: if u == desti: vu = v_l + '-' + u w_vu = get_weight(vu) if len(w_vu) == 0: cost_vu = 0 else: cost_vu = min([float(l) for l in w_vu.keys()]) tcost1 = time.time() inx_min = np.argwhere(U[u] > 0) inx_min = inx_min[0][0] p_best_p = p_hat + ';' + vu p_w_p = self.conv(w_p_hat, w_vu) p_max_m = get_maxP(w_p_hat, u) all_expire += time.time() - tcost1 p_best_cost = cost_sv + cost_vu + inx_min * self.sigma flag = True break if u in has_visit: #print('u1 %s, vd %s'%(u, desti)) continue else: has_visit.add(u) if u in p_hat: #print('u2 %s, vd %s'%(u, desti)) continue vu = v_l + '-' + u w_vu = get_weight(vu) if len(w_vu) == 0: #print('vu %s, vd %s'%(vu, desti)) continue cost_vu = min([float(l) for l in w_vu.keys()]) p_order = nodes_order[u] #p_hat] tcost1 = time.time() inx_min = np.argwhere(U[u] > 0) if len(inx_min) == 0: #print('inx vu %s, vd %s'%(vu, desti)) continue inx_min = inx_min[0][0] all_expire += time.time() - tcost1 cost_time = cost_sv + cost_vu + inx_min * self.sigma if cost_time <= self.T: p_hat_p = p_hat + ';' + vu w_p_hat_p = self.conv(w_p_hat, w_vu) tcost1 = time.time() p_hat_max = get_maxP(w_p_hat, u) all_expire += time.time() - tcost1 QQ[p_hat_p] = (p_hat_max, w_p_hat_p, cost_time) if flag: break if len(Q) == 0: Q = copy.deepcopy(QQ) for qqk in QQ: Que[qqk] = QQ[qqk][0] QQ = {} return p_best_p, p_max_m, p_best_cost, p_w_p, all_expire, all_rounds def main(self, ): vedge_desty, edge_desty, path_desty = self.get_dict() edges, nodes, G2, speed_dict = self.get_graph(edge_desty, vedge_desty) points = self.get_axes() df2 = open(self.query_name, 'rb') r_pairs = pickle.load(df2) df2.close() nodes_order, i = {}, 0 for node in nodes: nodes_order[node] = i i += 1 PT = 5 all_iters, flag = 0, False One_Plot = np.zeros(20).reshape(4, 5) One_Plot2 = np.zeros(20).reshape(4, 5) One_Sums = np.zeros(20).reshape(4, 5) one_dis = -1 cate = ['0-5km', '5-10km', '10-25km', '25-35km'] for pairs in r_pairs: one_dis += 1 for pair_ in pairs[:]: _ = self.get_U2(pair_[-1]) dij_time = 0.0 print('distance category %s' % cate[one_dis]) for pair_ in pairs[:]: all_iters += 1 tstart = time.time() print('o-d pair: %s' % pair_[0] + '-' + pair_[1]) start, desti = pair_[-2], pair_[-1] pred = self.get_dijkstra3(G2, desti) path_, st1 = [start], start distan2 = 0 while st1 != desti: st2 = st1 st1 = pred[st1] path_.append(st1) distan2 += self.get_distance(points, (st2, st1)) distan = self.get_distance(points, (start, desti)) st_key = start + '+' + desti + ':' + str(distan) + ':' + str( distan2) st1, time_budget = start, 0.0 for st2 in path_[1:]: sedge = st1 + '-' + st2 if sedge in edge_desty: speed_key = list( [float(l) for l in edge_desty[sedge].keys()]) time_budget += max(speed_key) elif sedge in speed_dict: speed_key = list( [float(l) for l in speed_dict[sedge].keys()]) time_budget += max(speed_key) else: print(' edge: %s not in speed_dict, exit' % sedge) sys.exit() st1 = st2 #print('time budget: %f'%time_budget) U = self.get_U2(desti) if len(U) == 0: continue tend = time.time() for t_b_, t_b in enumerate([0.5, 0.75, 1.0, 1.25, 1.5]): tstart = time.time() self.T = time_budget * t_b best_p, max_m, best_c, best_pw, all_expires, all_rounds = self.rout( start, desti, edge_desty, vedge_desty, nodes_order, U, G2, points, speed_dict) if best_p == 'none1' or best_p == 'none2' or best_p == 'none': continue tend = time.time() One_Plot2[one_dis][t_b_] += tend - tstart - all_expires One_Sums[one_dis][t_b_] += 1 One_Plot2 = One_Plot2 / One_Sums One_Plot2 = np.nan_to_num(One_Plot2) print('The time cost for routing') print(One_Plot2) print('Time cost for budget: 50%, 75%, 100%, 125%, 150%') print(One_Plot2.mean(0)) print('Time cost for distance: 0-5km, 5-10km, 10-25km, 25-35km') print(One_Plot2.mean(1))
class Modified(): def __init__(self, policy_path, true_path, virtual_path, edge_desty, subpath, axes_file, speed_file, query_name): self.sigma = 30 self.vopt = VOpt() self.B = 3 self.policy_path = policy_path self.true_path = true_path self.virtual_path = virtual_path self.edge_desty = edge_desty self.subpath = subpath self.axes_file = axes_file self.speed_file = speed_file self.speed = 50 self.query_name = query_name def add_tpath(self, ): with open(self.subpath + self.true_path) as fn: gt_path_ = json.load(fn) gt_path = {} for gt in gt_path_: a = gt.find('-') a1 = gt[:a] b = gt.rfind('-') b1 = gt[b + 1:] gt_key = a1 + '-' + b1 nw_key = list(gt_path_[gt].keys()) nw_value = list(gt_path_[gt].values()) lens1 = len(gt.split(';')) if gt_key not in gt_path: gt_path[gt_key] = [ str(gt), gt_path_[gt], lens1, nw_key[0], a1, b1 ] else: ex_path = gt_path[gt_key] #lens2 = len(ex_path[0].split(';')) mins = min(len(ex_path[1]), len(gt_path_[gt])) ex_key = list(ex_path.keys()) ex_value = list(ex_path.values()) a, b = 0, 0 for i in range(mins): if float(ex_key[i]) < float(nw_key[i]): a += 1 elif float(ex_key[i]) > float(nw_key[i]): b += 1 else: if float(ex_value[i]) > float(nw_value[i]): a += 1 else: b += 1 if lens1 < gt_path[gt_key][2]: b += 1 elif lens1 > gt_path[gt_key][2]: a += 1 if a < b: gt_path[gt_key] = [ str(gt), gt_path_[gt], lens1, nw_key[0], a1, b1 ] return gt_path, gt_path_ def get_axes(self, ): fo = open(self.axes_file) points = {} for line in fo: line = line.split('\t') points[line[0]] = (float(line[2]), float(line[1])) return points def get_distance(self, points, point): (la1, lo1) = points[point[0]] (la2, lo2) = points[point[1]] return geodesic((lo1, la1), (lo2, la2)).kilometers def get_dict(self, ): path_desty = {} for l in range(1, 5): with open(self.subpath + self.virtual_path + '_%d.json' % l) as js_file: path_desty_ = json.load(js_file) path_desty.update(path_desty_) with open(self.subpath + self.edge_desty) as js_file: edge_desty = json.load(js_file) edge_desty = dict( sorted(edge_desty.items(), key=operator.itemgetter(0))) vedge_desty = {} for p_k in path_desty: vedge_desty[p_k] = path_desty[p_k][0][1] return vedge_desty, edge_desty, path_desty def get_speed(self, ): speed_dict = {} with open(self.speed_file) as fn: for line in fn: line = line.strip().split('\t') speed_dict[line[0]] = { 3600 * float(line[1]) / float(line[2]): 1.0 } return speed_dict def get_graph(self, edge_desty, gt_path, vedge_desty): speed_dict = self.get_speed() all_nodes, all_edges = set(), set() for key in speed_dict: line = key.split('-') all_nodes.add(line[0]) all_nodes.add(line[1]) all_edges.add(key) all_nodes, all_edges = list(all_nodes), list(all_edges) All_edges = [] for edge in all_edges: edge_ = edge.split('-') if edge in edge_desty: cost1 = edge_desty[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) elif edge in speed_dict: cost1 = speed_dict[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) G2 = nx.DiGraph() G2.add_nodes_from(all_nodes) G2.add_weighted_edges_from(All_edges) for gt_ in gt_path: #edges.append((gt_path[gt_][-2], gt_path[gt_][-1], abs(float(gt_path[gt_][3])))) All_edges.append((gt_path[gt_][-2], gt_path[gt_][-1], abs(float(gt_path[gt_][3])))) All_edges.append((gt_path[gt_][-1], gt_path[gt_][-2], abs(float(gt_path[gt_][3])))) temp_edge = gt_path[gt_][-2] + '-' + gt_path[gt_][-1] temp_edge2 = gt_path[gt_][-1] + '-' + gt_path[gt_][-2] if temp_edge not in edge_desty: edge_desty[temp_edge] = {abs(float(gt_path[gt_][3])): 1.0} if temp_edge2 not in edge_desty: edge_desty[temp_edge2] = {abs(float(gt_path[gt_][3])): 1.0} G = nx.DiGraph() G.add_nodes_from(all_nodes) G.add_weighted_edges_from(All_edges) for edge in speed_dict: if edge not in edge_desty: edge_desty[edge] = speed_dict[edge] return all_edges, all_nodes, G, G2, speed_dict def conv(self, A, B): D = {} for a_k in A: for b_k in B: n_k = float(a_k) + float(b_k) n_v = float(A[a_k]) * float(B[b_k]) D[n_k] = n_v if len(D) <= 3: return D D = dict(sorted(D.items(), key=operator.itemgetter(0))) new_w = self.vopt.v_opt(list(D.keys()), list(D.values()), self.B) new_w = dict(sorted(new_w.items(), key=operator.itemgetter(0))) return new_w def get_min(self, U, node): return np.argwhere(U[node] > 0)[0][0] def cosin_distance(self, vector1, vector2): dot_product = 0.0 normA = 0.0 normB = 0.0 for a, b in zip(vector1, vector2): dot_product += a * b normA += a**2 normB += b**2 if normA == 0.0 or normB == 0.0: return None else: return dot_product / ((normA * normB)**0.5) def get_dijkstra3(self, G, target): Gk = G.reverse() inf = float('inf') D = {target: 0} Que = PQDict(D) P = {} nodes = Gk.nodes U = set(nodes) while U: #print('len U %d'%len(U)) #print('len Q %d'%len(Que)) if len(Que) == 0: break (v, d) = Que.popitem() D[v] = d U.remove(v) #if v == target: break neigh = list(Gk.successors(v)) for u in neigh: if u in U: d = D[v] + Gk[v][u]['weight'] if d < Que.get(u, inf): Que[u] = d P[u] = v return P def get_modified_one_to_all3(self, G, target): Gk = G.reverse() inf = float('inf') D = {target: 0} Que = PQDict(D) P = {} nodes = Gk.nodes U = set(nodes) while U: #print('len U %d'%len(U)) #print('len Q %d'%len(Que)) if len(Que) == 0: break (v, d) = Que.popitem() D[v] = d U.remove(v) #if v == target: break neigh = list(Gk.successors(v)) for u in neigh: if u in U: d = D[v] + Gk[v][u]['weight'] if d < Que.get(u, inf): Que[u] = d P[u] = v return P def rout(self, start, desti, edge_desty, vedge_desty, speed_dict, nodes_order, U, G, G2, points, gt_path, gt_path_, pred): Q, P_hat = [], [] neigh = list(G2.successors(start)) print('neigh %d' % len(neigh)) start_ax = points[start] desti_ax = points[desti] s_d_arr = [desti_ax[0] - start_ax[0], desti_ax[1] - start_ax[1]] all_expire = 0.0 def get_weight(p_hat): w_p_hat = {} if p_hat in edge_desty: w_p_hat = edge_desty[p_hat] elif p_hat in gt_path: w_p_hat = gt_path[p_hat][1] elif p_hat in vedge_desty: w_p_hat = vedge_desty[p_hat] #print('vedge %s' %p_hat) elif p_hat in speed_dict: w_p_hat = speed_dict[p_hat] if len(w_p_hat) == 0: print('zero %s' % p_hat) return w_p_hat def get_maxP3(w_p_, vv, vi_getmin): p_max = 0.0 for pk in w_p_: w_pk = w_p_[pk] pk_ = int(int(pk) / self.sigma) if int(pk) % self.sigma == 0: pk_ += 1 if (1.0 * self.T - float(pk)) > vi_getmin: p_max += float(w_pk) return p_max def get_vigetmin2(vv): start1 = time.time() v = vv path_ = [v] while v != desti: v = pred[v] #[2] path_.append(v) paths_ = [] spath = path_[0] vi_getmin = 0.0 expire_time = time.time() - start1 for epath in path_[1:]: key = spath + '-' + epath if key in edge_desty: vi_getmin += min( abs(float(l)) for l in edge_desty[key].keys()) elif key in speed_dict: vi_getmin += min( abs(float(l)) for l in speed_dict[key].keys()) elif key in gt_path: vi_getmin += min( abs(float(l)) for l in gt_path[p_hat][1].keys()) else: print('edge not in here') spath = epath return vi_getmin, expire_time has_visit = set() has_visit.add(start) Que = PQDict.maxpq() Q = {} for vi in neigh: if vi in has_visit: continue else: has_visit.add(vi) p_hat = start + '-' + vi w_p_hat = get_weight(p_hat) w_min = min([float(l) for l in w_p_hat.keys()]) p_order = nodes_order[vi] #p_hat] vi_getmin, ex_p = get_vigetmin2(vi) all_expire += ex_p cost_time = w_min + vi_getmin if cost_time <= self.T: p_max = max(list(w_p_hat.values())) Que[p_hat] = p_max Q[p_hat] = (p_max, w_p_hat, cost_time) print('len Q %d' % len(Q)) QQ = {} p_best_p, flag = 'none', False p_max_m, p_best_cost, p_w_p = -1, -1, -1 if len(Q) == 0: return 'none1', -1, -1, -1, all_expire, -1 all_rounds = 0 while len(Q) != 0: (p_hat, pqv) = Que.popitem() all_rounds += 1 (p_max, w_p_hat, cost_time) = Q[p_hat] del Q[p_hat] a = p_hat.rfind('-') v_l = p_hat[a + 1:] if v_l == desti: p_best_p = p_hat p_max_m = p_max p_best_cost = cost_time p_w_p = w_p_hat flag = True break neigh = list(G2.successors(v_l)) if len(w_p_hat.keys()) == 0: print(w_p_hat) print(p_hat) cost_sv = min([float(l) for l in w_p_hat.keys()]) vd_d_arr = [ points[desti][0] - points[v_l][0], points[desti][1] - points[v_l][1] ] for u in neigh: if u == desti: vu = v_l + '-' + u w_vu = get_weight(vu) if len(w_vu) == 0: cost_vu = 0 else: cost_vu = min([float(l) for l in w_vu.keys()]) vi_getmin, ex_p = get_vigetmin2(u) all_expire += ex_p p_best_p = p_hat + ';' + vu p_w_p = self.conv(w_p_hat, w_vu) p_max_m = max(list(p_w_p.values())) p_best_cost = cost_sv + cost_vu + vi_getmin #inx_min*self.sigma flag = True break if u in has_visit: #print('u1 %s'%u) continue else: has_visit.add(u) if u in p_hat: #print('u2 %s'%u) continue vu = v_l + '-' + u w_vu = get_weight(vu) if len(w_vu) == 0: #print('vu %s'%vu) continue cost_vu = min([float(l) for l in w_vu.keys()]) p_order = nodes_order[u] #p_hat] vi_getmin, ex_p = get_vigetmin2(u) all_expire += ex_p cost_time = cost_sv + cost_vu + vi_getmin #inx_min*self.sigma if cost_time <= self.T: p_hat_p = p_hat + ';' + vu w_p_hat_p = self.conv(w_p_hat, w_vu) p_hat_max = max(list(w_p_hat_p.values())) QQ[p_hat_p] = (p_hat_max, w_p_hat_p, cost_time) if flag: break if len(Q) == 0: Q = copy.deepcopy(QQ) for qqk in QQ: Que[qqk] = QQ[qqk][0] QQ = {} return p_best_p, p_max_m, p_best_cost, p_w_p, all_expire, all_rounds def main(self, ): vedge_desty, edge_desty, path_desty = self.get_dict() gt_path, gt_path_ = self.add_tpath() #print('len of edge_desty: %d'%len(edge_desty)) edges, nodes, G, G2, speed_dict = self.get_graph( edge_desty, gt_path, vedge_desty) #print('len of edge_desty2: %d'%len(edge_desty)) points = self.get_axes() df2 = open(self.query_name, 'rb') r_pairs = pickle.load(df2) df2.close() nodes_order, i = {}, 0 for node in nodes: nodes_order[node] = i i += 1 One_Plot2 = np.zeros(20).reshape(4, 5) One_Sums = np.zeros(20).reshape(4, 5) one_dis = -1 cate = ['0-5km', '5-10km', '10-25km', '25-35km'] for pairs in r_pairs: one_dis += 1 #print('one_dis : %d'%one_dis) print('distance category %s' % cate[one_dis]) tstart = time.time() #print('len pairs %d'%len(pairs)) all_expires = 0.0 for pair_ in pairs: print('o-d pair: %s' % pair_[0] + '-' + pair_[1]) start, desti = pair_[-2], pair_[-1] pred2 = self.get_modified_one_to_all3(G, desti) path_2, st1 = [start], start while st1 != desti: st2 = st1 st1 = pred2[st1] #[2] path_2.append(st1) at = 0 for st2 in path_2[1:]: sedge = st1 + '-' + st2 if sedge in gt_path: at += 1 pred = self.get_dijkstra3(G, desti) path_, st1 = [start], start distan2 = 0 while st1 != desti: st2 = st1 st1 = pred[st1] path_.append(st1) distan2 += self.get_distance(points, (st2, st1)) distan = self.get_distance(points, (start, desti)) st_key = start + '+' + desti + ':' + str(distan) + ':' + str( distan2) + ':' + str(len(pred2)) + ':' + str(at) st1, time_budget = start, 0.0 print(path_) for st2 in path_[1:]: sedge = st1 + '-' + st2 if sedge in edge_desty: speed_key = list( [float(l) for l in edge_desty[sedge].keys()]) time_budget += max(speed_key) elif sedge in speed_dict: speed_key = list( [float(l) for l in speed_dict[sedge].keys()]) time_budget += max(speed_key) else: print(' edge: %s not in speed_dict, exit' % sedge) sys.exit() st1 = st2 print('time budget: %f' % time_budget) for t_b_, t_b in enumerate([0.5, 0.75, 1.0, 1.25, 1.5]): self.T = time_budget * t_b tstart = time.time() best_p, max_m, best_c, best_pw, all_expire, all_rounds = self.rout( start, desti, edge_desty, vedge_desty, speed_dict, nodes_order, '', G, G2, points, gt_path, gt_path_, pred2) all_expires += all_expire if best_p == 'none1': continue if not isinstance(best_pw, dict): continue tend = time.time() One_Plot2[one_dis][t_b_] += tend - tstart - all_expire One_Sums[one_dis][t_b_] += 1 One_Plot2 = One_Plot2 / One_Sums One_Plot2 = np.nan_to_num(One_Plot2) print('The success account') print(One_Sums) print('The time cost for routing') print(One_Plot2) print('Time cost for budget: 50%, 75%, 100%, 125%, 150%') print(One_Plot2.mean(0)) print('Time cost for distance: 0-5km, 5-10km, 10-25km, 25-35km') print(One_Plot2.mean(1))
class PaceRout(): def __init__(self, policy_path, true_path, virtual_path, edge_desty, subpath, axes_file, speed_file, query_name): self.sigma = 30 self.vopt = VOpt() self.B = 3 self.policy_path = policy_path self.true_path = true_path self.virtual_path = virtual_path self.edge_desty = edge_desty self.subpath = subpath self.axes_file = axes_file self.speed_file = speed_file self.speed = 50 self.query_name = query_name def get_axes(self, ): fo = open(self.axes_file) points = {} for line in fo: line = line.split('\t') points[line[0]] = (float(line[2]), float(line[1])) return points def get_distance(self, points, point): (la1, lo1) = points[point[0]] (la2, lo2) = points[point[1]] return geodesic((lo1, la1), (lo2, la2)).kilometers def get_dict(self, ): path_desty = {} for l in range(1, 5): with open(self.subpath + self.virtual_path + '_%d.json' % l) as js_file: path_desty_ = json.load(js_file) path_desty.update(path_desty_) with open(self.subpath + self.edge_desty) as js_file: edge_desty = json.load(js_file) edge_desty = dict( sorted(edge_desty.items(), key=operator.itemgetter(0))) vedge_desty = {} for p_k in path_desty: vedge_desty[p_k] = path_desty[p_k][0][1] return vedge_desty, edge_desty, path_desty def get_speed(self, ): speed_dict = {} with open(self.speed_file) as fn: for line in fn: line = line.strip().split('\t') speed_dict[line[0]] = { 3600 * float(line[1]) / float(line[2]): 1.0 } return speed_dict def get_graph2(self, edge_desty, vedge_desty, r_pairs, points): speed_dict = self.get_speed() all_nodes, all_edges = set(), set() for key in speed_dict: line = key.split('-') all_nodes.add(line[0]) all_nodes.add(line[1]) all_edges.add(key) All_edges = [] for edge in all_edges: edge_ = edge.split('-') if edge in edge_desty: cost1 = edge_desty[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) elif edge in speed_dict: cost1 = speed_dict[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) for edge in edge_desty: #edge_ = edge.split('-') if edge not in speed_dict: print(edge) G2 = nx.DiGraph() G2.add_nodes_from(all_nodes) G2.add_weighted_edges_from(All_edges) temp_edges = set() for pairs in r_pairs: for pair in pairs: stedge = pair[-2] + '-' + pair[-1] #print(stedge) temp_edges.add(stedge) for edge in vedge_desty: if edge not in edge_desty and edge not in speed_dict: edge_ = edge.split('-') cost1 = vedge_desty[edge].keys() cost = min(float(l) for l in cost1) All_edges.append((edge_[0], edge_[1], cost)) all_edges.add(edge) print('len of vedge_desty 2: %d' % len(vedge_desty)) all_nodes, all_edges = list(all_nodes), list(all_edges) G = nx.DiGraph() G.add_nodes_from(all_nodes) G.add_weighted_edges_from(All_edges) return all_edges, all_nodes, G, G2, speed_dict def conv(self, A, B): D = {} for a_k in A: for b_k in B: n_k = int(float(a_k) + float(b_k)) n_v = float(A[a_k]) * float(B[b_k]) D[n_k] = n_v if len(D) <= 3: return D D = dict(sorted(D.items(), key=operator.itemgetter(0))) new_w = self.vopt.v_opt(list(D.keys()), list(D.values()), self.B) new_w = dict(sorted(new_w.items(), key=operator.itemgetter(0))) return new_w def path_conv(self, prob1, prob2, edge, edge_desty): DD = {} edge_w = edge_desty[edge] wk1, wk2, wk3 = list(prob1.keys()), list(prob2.keys()), list( edge_w.keys()) N, J, M = len(prob1), len(prob2), len(edge_w) for n in range(N): for j in range(J): for m in range(M): n_k = int(wk1[n]) + int(wk2[j]) - int(wk3[m]) if n_k < 0: continue #else: n_k = str(n_k) if n_k in DD: DD[n_k] += float(prob1[wk1[n]]) * float( prob2[wk2[j]]) / float(edge_w[wk3[m]]) else: DD[n_k] = float(prob1[wk1[n]]) * float( prob2[wk2[j]]) / float(edge_w[wk3[m]]) if len(DD) <= 3: return DD #print(DD) DD = dict(sorted(DD.items(), key=operator.itemgetter(0))) DD = self.vopt.v_opt(list(DD.keys()), list(DD.values()), self.B) DD = dict(sorted(DD.items(), key=operator.itemgetter(0))) return DD def get_dijkstra(self, G, source, target): path = nx.dijkstra_path(G, source, target) return path def get_last_edge(self, apath): a = apath.rfind(';') if a == -1: return apath else: return apath[a + 1:] def rout(self, start, desti, edge_desty, vedge_desty, path_desty, nodes_order, U, G, points, speed_dict): all_expire = 0.0 neigh = list(G.successors(start)) has_visit = set() has_visit.add(start) Que = PQDict.maxpq() Q, QQ = {}, [] print('neigh : %d' % len(neigh)) for vi in neigh: if vi in has_visit: continue else: has_visit.add(vi) candidite = start + '-' + vi if candidite in edge_desty: w_can = edge_desty[candidite] elif candidite in speed_dict: w_can = speed_dict[candidite] elif candidite in vedge_desty: w_can = vedge_desty[candidite] w_min = min([float(l) for l in w_can.keys()]) w_exp = 0.0 for w_k, w_v in w_can.items(): w_exp += float(w_k) * float(w_v) if w_min <= self.T: Que[candidite] = w_exp Q[candidite] = w_can Qkey = list(Q.keys()) flag = False best_path, best_w = None, -1 bts = time.time() all_rounds = 0 while len(Q) != 0: (cand, pqv) = Que.popitem() all_rounds += 1 w_can = Q[cand] del Q[cand] a = cand.rfind('-') v_l = cand[a + 1:] if v_l == desti: best_path = cand best_w = w_can flag = True break if desti in cand: a = cand.find('-' + desti) best_path = cand[:a + len(desti) + 1] best_w = w_can flag = True break neigh = list(G.successors(v_l)) for u in neigh: if u == desti: next_cand = v_l + '-' + u if next_cand in edge_desty: next_w = edge_desty[next_cand] elif next_cand in speed_dict: next_w = speed_dict[next_cand] elif next_cand in vedge_desty: next_w = vedge_desty[next_cand] tcost1 = time.time() next_w_min = min([float(l) for l in next_w.keys()]) best_path = cand + ';' + next_cand best_w = self.conv(w_can, next_w) all_expire += time.time() - tcost1 flag = True break if u in has_visit: continue else: has_visit.add(u) if u in cand: continue next_cand = v_l + '-' + u if next_cand in vedge_desty: next_w = vedge_desty[next_cand] elif next_cand in edge_desty: next_w = edge_desty[next_cand] elif next_cand in speed_dict: next_w = speed_dict[next_cand] next_w_min = min([float(l) for l in next_w.keys()]) if next_w_min <= self.T: new_path = cand + ';' + next_cand temp_1 = cand.rfind(';') if temp_1 != -1: new_p = cand[temp_1 + 1:] + ';' + next_cand if new_p in path_desty: new_path_w = self.path_conv( w_can, path_desty[new_p], cand[tep_1 + 1:], edge_desty) else: new_path_w = self.conv(w_can, next_w) else: new_path_w = self.conv(w_can, next_w) w_exp = 0.0 for w_k, w_v in w_can.items(): w_exp += float(w_k) * float(w_v) QQ.append((new_path, new_path_w, w_exp)) last_edge = self.get_last_edge(cand) if flag: break if len(Q) == 0: for QQ_ in QQ: Q[QQ_[0]] = QQ_[1] Que[QQ_[0]] = QQ_[2] return best_path, best_w, all_rounds, all_expire def get_dijkstra3(self, G, target): Gk = G.reverse() inf = float('inf') D = {target: 0} Que = PQDict(D) P = {} nodes = Gk.nodes UU = set(nodes) while UU: #print('len U %d'%len(U)) #print('len Q %d'%len(Que)) if len(Que) == 0: break (v, d) = Que.popitem() D[v] = d UU.remove(v) #if v == target: break neigh = list(Gk.successors(v)) for u in neigh: if u in UU: d = D[v] + Gk[v][u]['weight'] if d < Que.get(u, inf): Que[u] = d P[u] = v return P def get_U2(self, u_name): if u_name in self.hU: return self.hU[u_name] if not os.path.isfile(self.fpath + u_name): #print('no this U : %s'%(self.fpath+u_name)) print(u_name) return {} fn = open(self.fpath + u_name) U = {} for line in fn: line = line.strip().split(';') U[line[0]] = np.zeros(self.eta) if line[1] == '-1' and line[2] == '-1': pass elif line[1] == '-1' and line[2] == '0': U[line[0]] = np.ones(self.eta) else: for i in range(int(line[1]) + 1, int(line[2])): t = 3 + i - int(line[1]) - 1 U[line[0]][i] = float(line[t]) for i in range(int(line[2]), self.eta): U[line[0]][i] = 1.0 fn.close() self.hU[u_name] = U return U def main(self, ): df2 = open(self.query_name, 'rb') r_pairs = pickle.load(df2) df2.close() vedge_desty, edge_desty, path_desty = self.get_dict() points = self.get_axes() edges, nodes, G, G2, speed_dict = self.get_graph2( edge_desty, vedge_desty, r_pairs, points) nodes_order, i = {}, 0 for node in nodes: nodes_order[node] = i i += 1 U = '' PT = 5 One_Plot2 = np.zeros(20).reshape(4, 5) One_Sums = np.zeros(20).reshape(4, 5) one_dis = -1 cate = ['0-5km', '5-10km', '10-25km', '25-35km'] for pairs in r_pairs: one_dis += 1 print('distance category %s' % cate[one_dis]) tstart2 = time.time() #print('len pairs %d'%len(pairs)) sums2 = 0 mps, mps2 = 0.0, 0.0 alls = 0 cost_t = 0 for pair_ in pairs: #print(pair_) print('o-d pair: %s' % pair_[0] + '-' + pair_[1]) start, desti = pair_[-2], pair_[-1] if start == desti: print('wrong: start and desti is same') print(pair_) continue start, desti = pair_[-2], pair_[-1] pred = self.get_dijkstra3(G2, desti) path_, st1 = [start], start while st1 != desti: st1 = pred[st1] path_.append(st1) st1, time_budget = start, 0.0 for st2 in path_[1:]: sedge = st1 + '-' + st2 if sedge in edge_desty: speed_key = list( [abs(float(l)) for l in edge_desty[sedge].keys()]) time_budget += max(speed_key) elif sedge in speed_dict: speed_key = list( [abs(float(l)) for l in speed_dict[sedge].keys()]) time_budget += max(speed_key) elif sedge in vedge_desty: speed_key = list( [abs(float(l)) for l in vedge_desty[sedge].keys()]) time_budget += max(speed_key) else: print(' edge: %s not in speed_dict, exit' % sedge) sys.exit() st1 = st2 tend = time.time() print('budget %f' % time_budget) for t_b_, t_b in enumerate([0.5, 0.75, 1.0, 1.25, 1.5]): tstart = time.time() self.T = time_budget * t_b best_p, best_w, all_rounds, all_expire = self.rout( start, desti, edge_desty, vedge_desty, path_desty, nodes_order, U, G2, points, speed_dict) if best_p == None: continue tend = time.time() One_Plot2[one_dis][t_b_] += tend - tstart One_Sums[one_dis][t_b_] += 1 One_Plot2 = One_Plot2 / One_Sums One_Plot2 = np.nan_to_num(One_Plot2) print('The success account') print(One_Sums) print('The time cost for routing') print(One_Plot2) print('Time cost for budget: 50%, 75%, 100%, 125%, 150%') print(One_Plot2.mean(0)) print('Time cost for distance: 0-5km, 5-10km, 10-25km, 25-35km') print(One_Plot2.mean(1))
pvalues = vopt.v_opt( time_cost, time_freq, B) # pvalues is a dict, contains new key and value dicts[e_key] = pvalues else: if not is_path: k1 = str(int(np.mean(time_cost))) pvalues[k1] = 1.0 return norms(dicts, is_norm) tpath = TPath(fname) data = tpath.load() edge_desty = tpath.get_edge_time_freq(data) vopt = VOpt() edge_desty_ = get_vopt(vopt, edge_desty, False, True) keys = list(edge_desty_.keys()) print(keys[0]) print(edge_desty_[keys[0]]) edge_desty_1 = {} for edge in edge_desty_: #ky = list(edge.keys()) edge_ = {} ky = edge_desty_[edge] for k in ky.keys(): edge_[str(np.abs(k))] = float(ky[k]) / 100 edge_desty_1[edge] = edge_