def main(): nodes = [1, 2, 3, 4, 5, 6, 7] world = pants.World(nodes, length_function) solver = pants.Solver() solution = solver.solve(world) print(solution.distance) print(solution.tour) # Nodes visited in order
def optimize(self, route): # exclude one HQ (otherwise there will be 2 HQs in the path) nodes = route.path[:len(route.path)-1] world = pants.World(nodes, self.maps.get_distance) solver = pants.Solver() return solver.solve(world)
def click_res(event, x, y, flags, params): if event == cv2.EVENT_LBUTTONDOWN: #print(x, y) points.append((x,y)) nodes.append((x, y)) cv2.circle(img, (x, y), 2, [0, 0, 255], 2) draw_lines((x, y)) cv2.imshow('ACO', img) if event == cv2.EVENT_RBUTTONDOWN: print("Right click") world = pants.World(nodes, distance) solver = pants.Solver(rho=0.5, q=1, t0=0.01, limit=50, ant_count=10) solutions = solver.solutions(world) best = float("inf") for solution in solutions: print(solution.distance) if solution.distance < best: best = solution.distance c = solution print(best) print(c.tour) c=c.tour for i in range(len(c)-1): cv2.line(img, c[i], c[i+1], [150, 0, 0], 3) cv2.line(img, c[0], c[len(c)-1], [150, 0, 0], 3) cv2.imshow('ACO', img)
def solve_vrp(nodes, vehicles): def real_distance_time(a, b: NODE) -> (float, float): if not a.db_uid or not b.db_uid: return float('inf'), float('inf') else: return local_db['matrix'][a.db_uid][b.db_uid]['distance'], local_db['matrix'][a.db_uid][b.db_uid]['time'] world = pants.World(nodes, real_distance_time) solver = pants.Solver(ant_capacity=sum([ant.capacity for ant in vehicles]) / len(vehicles), iterations=100, ant_count=15) vrp_solution = [] unreachable_clients = [] nodes_left = world.real_nodes() total_nodes = len(nodes_left) while len(nodes_left) != 1: logging.info('%s done' % (100 - (len(nodes_left) / total_nodes))) # print(len(world.real_nodes()) - 1) solution = solver.solve(world) # nodes_left = solution.remaining_moves() if len(solution.visited) == 1: # Nothing was visited # TODO: use saved unreachable clients somehow unreachable_clients.append(solution.unvisited) break vrp_solution.append(solution.clone()) nodes_left = [v for i, v in enumerate(nodes_left) if i not in frozenset(solution.visited[1:])] del world world = pants.World(nodes_left, real_distance_time) # print(solution.distance) return vrp_solution, unreachable_clients
def main(): try: readCSV() world = pants.World() solver = pants.Solver() displaySolution(solver, world) except Exception as ex: print("main : " + format(ex))
def plan_aco(self): import pants world = pants.World(self.points, self.two_points_cost) solver = pants.Solver() solution = solver.solve(world) return solution, solution.distance
def solve(self): world = pants.World(self.nodes, self.distMetric) ants = [Ant().initialize(world, world.nodes[0]) for i in range(10)] solver = pants.Solver() # solution = solver.solve(world) # solutions = solver.solutions(world) solver.find_solutions(ants) ants = sorted(ants) for i in range(1, len(ants)): assert ants[i - 1].distance <= ants[i].distance return (ants[0].distance, ants[0].tour)
def solve_ACO(nodes): world = pants.world.World(nodes=nodes, lfunc=euclidean) solver = pants.Solver() solution = solver.solve(world) solutions = solver.solutions(world) # print(solution.distance) # print(solution.tour) # Nodes visited in order # print(solution.path) # Edges taken in order # best = float("inf") # for solution in solutions: # assert solution.distance < best # best = solution.distance return solutions, solution
def main(): nodes = [1, 2, 3, 4, 5, 6] world = pants.World(nodes, length_function) tiempo_inicial = time() solver = pants.Solver() solution = solver.solve(world) tiempo_final = time() tiempo_ejecucion = tiempo_final - tiempo_inicial print ('El tiempo de ejecucion fue:',tiempo_ejecucion) #En segundos print(solution.distance) print(solution.tour) # Nodes visited in order
def findOptimalSolutions(self): nodes = list(self.timeDf.columns) world = pants.World(nodes, self.calculateLength) solver = pants.Solver() sol = solver.solve(world) sols = solver.solutions(world) print("****************") print(sol.distance) print("****************") print(self.beautifyLatLon(sol.tour)) print("****************") print(sol.path) print("***********\n**********\n########") for s in sols: print(s.distance) processTrail(self.beautifyLatLon(sol.tour))
def aco(self): '''ACO is a simulated approach wherein it is assumed that ants are exploring possible routes on the graph. Using a probabilistic approach which considers both the cost and the amount of pheromone deposited each ant chooses the next city to visit. Until each opf the ants complets a tour, they explore, depositing pheromone on each edge that they cross. The Ant which completes with tour with the lowest cost wins. The amount of pheromone deposited is inversely proportional to the tour length: the shorter the tour, the more it deposits. Time Comlexity: http://ls2-www.cs.tu-dortmund.de/~sudholt/chapterACO09.pdf''' nodes = list(range(len(self.dist))) world = pants.World(nodes, lambda a, b: self.dist[a][b]) solver = pants.Solver() solution = solver.solve(world) path = self.minimal_subset_path(solution.tour) return path
def MiniAco(nodes): world = pants.World(nodes, Cost) solver = pants.Solver() solution = solver.solve(world) # or solutions = solver.solutions(world) # print('distance',solution.distance) # print('tour',solution.tour) # Nodes visited in order # print('path',solution.path) # Edges taken in order # or best = float("inf") bestT = 0 for solution in solutions: assert solution.distance < best best = solution.distance bestT = solution.tour # bestT.insert(0,centru) # bestT.append(centru) return bestT
position = '' #ITERRATING csv rows with open('./nantes-street.csv', 'rt') as csvfile: file = csv.reader(csvfile, delimiter=',', quotechar='|') for row in file: biMin = clearString(row[8]) biMax = clearString(row[10]) bpMin = clearString(row[9]) bpMax = clearString(row[11]) tenant = clearString(row[6]) aboutissant = clearString(row[7]) label = clearString(row[1]) if biMin != '' and biMax != '' and bpMin != '' and bpMax != '' and tenant != '' and aboutissant != '' and label != '': biWeight = getWeight(biMax, biMin) bpWeight = getWeight(biMax, biMin) weight = getGlobalWeight(biWeight, bpWeight) graph.add_edge(tenant, aboutissant, weight=weight, label=label) position = networkx.spring_layout(graph) points = getPoints(position) #CREATING pants env (world, solver) to finally get the shortest path pantsWorld = pants.World(points, evalFunction) pantsSol = pants.Solver() res = pantsSol.solutions(pantsWorld) shortest = getShortest(res) print('Resultat :') print(shortest)
def main(): # Arguments # parser = argparse.ArgumentParser( description='Pengtai Instagram RNN LSTM Model') parser.add_argument( '-t', '--type', type=str, help="run type Options: 'n' for new | 'o' for overwrite", default='o', nargs='+') # parser.add_argument('-d', '--dest_dir', type=str, help='CSV data file') parser.add_argument('-i', '--input_dir', type=str, help='Input Raw CSV directory') parser.add_argument('-u', '--user_id', type=str, help='Instagram User ID') parser.add_argument('-v', '--version', help='current version', action='store_true') args = parser.parse_args() # End Argparse # # VERSION CONTROL # if args.version: with open(settings.VERSION_JSON, "r") as jsonFile: data = json.load(jsonFile) return print(data['version']) if args.type: if args.type[0] == 'n' and args.type[1]: with open(settings.VERSION_JSON, "r") as jsonFile: data = json.load(jsonFile) data["version"] = args.type[1] with open(settings.VERSION_JSON, "w") as jsonFile: json.dump(data, jsonFile) VERSION = args.type[1] elif args.type[0] == 'o': with open(settings.VERSION_JSON, "r") as jsonFile: data = json.load(jsonFile) VERSION = data["version"] # End VERSION CONTROL # with open('./dic/polarity.csv', 'r', encoding='UTF-8') as file: csvreader = csv.DictReader(file) kosac = [row for row in csvreader] total_arr = [] rowI = 0 rowDict = {} # File List in the directory from the arguments for filename in glob.glob(os.path.join(args.input_dir, '*.csv')): # i = ['id', 'img', 'text', 'has_tag', 'write_date', 'reg_date'] with open(filename, 'r', encoding='UTF-8') as f: csvreader = csv.DictReader(f) # csvreader = csv.reader(f) for row in csvreader: if rowI == 0: rowDict = {"user_id": row['user_id'], "posts": []} else: # print(user_id, row['user_id'], rowDict) if rowDict['user_id'] != row['user_id']: total_arr.append(rowDict) rowDict = {"user_id": row['user_id'], "posts": []} # text preprocess text = re.sub(r'@\w+', '', row['text']) text = re.sub( 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', '', text) text = re.sub(r'[\[]|[\]]', '', text) text = re.sub(r'[\r]|[\n]', ' ', text) text = re.sub(r'[.]|[ㆍ]', '', text) text = re.sub(r'#', ' ', text) rowDict['posts'].append({ "datetime": row['write_date'], "text": text }) rowI = rowI + 1 # print(total_arr) trg_res = [item for item in total_arr if item["user_id"] == args.user_id] temp = [] kkma = Kkma() t = Twitter() for post in trg_res[0]['posts']: date = datetime.datetime(int(post['datetime'][0:4]), int(post['datetime'][5:7]), int(post['datetime'][8:10]), int(post['datetime'][11:13]), int(post['datetime'][14:16]), int(post['datetime'][17:19])) text = post['text'] temp.append((date, text)) temp = sorted(temp, key=lambda t: t[0], reverse=False) sentArr = [] newArr = [] tokens_ko = [] index = 0 nounsArr = [] for data in temp: sentPosArr = kkma.pos(data[1]) # sentNouns = kkma.nouns(data[1]) inArr = [] for outA in sentPosArr: # for inA in outA: inArr.append("/".join(outA)) morph_arr = t.morphs(data[1]) morphWords = [word for word in morph_arr if not word in tokens_ko] for word in morphWords: if not word in nounsArr: nounsArr.append(word) tokens_ko.extend(morphWords) newArr.append({"sentence": "", "words": morph_arr, "score": 0}) index = index + 1 sentArr.append(";".join(inArr)) index = 0 for eaSent in sentArr: sentiScore = 0 for corp in kosac: if eaSent.find(corp['ngram']) > -1: if corp['max.value'] == 'NEG': sentiScore = sentiScore - float(corp['max.prop']) elif corp['max.value'] == 'POS': sentiScore = sentiScore + float(corp['max.prop']) newArr[index]["sentence"] = eaSent newArr[index]["score"] = sentiScore index = index + 1 # ACO 알고리즘 # doc_ko = " ".join([row[1] for row in temp]) # text_arr = [row[1] for row in temp] # for text in text_arr: # morph_arr = t.morphs(text) # temp = [word for word in morph_arr if not word in tokens_ko] # tokens_ko.extend(temp) print(tokens_ko) ko = nltk.Text(tokens_ko) # For Python 2, input `name` as u'유니코드' # # print(len(set(ko.tokens))) # returns number of unique tokens vocab = dict([(item[0], index + 1) for index, item in enumerate(ko.vocab().items())]) # pprint(vocab) # returns number of tokens (document length) minTimeVal = int(temp[0][0].timestamp()) maxTimeVal = int(temp[len(temp) - 1][0].timestamp() - minTimeVal) tenPow = len(str(int(temp[len(temp) - 1][0].timestamp() - minTimeVal))) tenPow = pow(10, tenPow) index = 0 nodes = [] for data in temp: # print(data[0].utctimetuple) # print(data[0].time()) diffTimeVal = int(data[0].timestamp() - minTimeVal) opt1 = float(diffTimeVal / tenPow) opt2 = float(diffTimeVal / maxTimeVal) print(diffTimeVal, opt1, opt2) nodes.append((opt2, newArr[index]["words"])) index = index + 1 # print(nounsArr) nodes2 = [] for noun in nounsArr: for corp in kosac: hts = "%s/NNG" % (noun) if hts.find(corp['ngram']) > -1: if corp['max.value'] == 'NEG': nodes2.append({ "noun": noun, "score": -float(corp['max.prop']) }) elif corp['max.value'] == 'POS': nodes2.append({ "noun": noun, "score": float(corp['max.prop']) }) print() antCount = len(newArr) rhoVal = 0.3 # ACO 알고리즘 예시 # nodes = [] # for _ in range(20): # x = random.uniform(-10, 10) # y = random.uniform(-10, 10) # nodes.append((x, y)) # def euclidean(a, b): return math.sqrt(pow(a[1] - b[1], 2) + pow(a[0] - b[0], 2)) # world = pants.World(nodes, euclidean) # solver = pants.Solver(rho=rhoVal, )
def createSolver(): return pants.Solver()
# Run Ant Colony Algorithm ''' :param float alpha: relative importance of pheromone (default=1) solver = pants.Solver(ant_count=20) :param float beta: relative importance of distance (default=3) :param float rho: percent evaporation of pheromone (0..1, default=0.8) :param float q: total pheromone deposited by each :class:`Ant` after each iteration is complete (>0, default=1) :param float t0: initial pheromone level along each :class:`Edge` of the :class:`World` (>0, default=0.01) :param int limit: number of iterations to perform (default=100) :param float ant_count: how many :class:`Ant`\s will be used (default=10) :param float elite: multiplier of the pheromone deposited by the elite :class:`Ant` (default=0.5) ''' world = pants.World(nodes, distance) # solver = pants.Solver() solver = pants.Solver(ant_count=20, limit=150, q=3,beta=5) solution = solver.solve(world) # print('Distance: ', solution.distance) # print(solution.tour) # Nodes visited in order # print(solution.path) # Edges taken in order final_sequence = solution.tour answer = [] print ("Final ", final_sequence) if (final_sequence[1] == 'NODE'): a = final_sequence[0] final_sequence = final_sequence[2:] # print (final_sequence) for r in range(NUM_PARTS): sequence_to_check = final_sequence[0:r] + [ a ] + final_sequence[r:]
def trevize(G, s, t, v1, verbose, first_pairs=[]): """Find path. input: graph, s, t, v1 process: BFS & check path when completed. - using deque for a first implementation - sort edges before adding to deque as a stack output: valid path list `valid_paths` """ def init_globals(s, t, v1): """Init preds, succs of first layer. store as vertex: {depth: {end_vertex: [list of paths]}} vertices in preds: v in v1 and t. vertices in succs: v in v1 and s.""" # init s, t succs[s] = {1: {}} preds[t] = {1: {}} for v_pred in G.predecessors_iter(t): preds[t][1][v_pred] = [[v_pred, t]] for v_succ in G.successors_iter(s): succs[s][1][v_succ] = [[s, v_succ]] # init v in v1 for v in v1: preds[v] = {1: {}} succs[v] = {1: {}} for v_pred in G.predecessors_iter(v): preds[v][1][v_pred] = [[v_pred, v]] for v_succ in G.successors_iter(v): succs[v][1][v_succ] = [[v, v_succ]] def add_link_to_links(p_out, p_in): """Add link to links_in, links_out.""" v_out = p_out[0] v_in = p_in[-1] new_link_path = p_out + p_in if (v_out, v_in) not in links_in[v_in]: links_in[v_in][(v_out, v_in)] = [new_link_path] links_out[v_out][(v_out, v_in)] = [new_link_path] elif new_link_path not in links_in[v_in][(v_out, v_in)]: links_in[v_in][(v_out, v_in)] += [new_link_path] links_out[v_out][(v_out, v_in)] += [new_link_path] def iter_layer(v, depth, direction='in'): """Iterate one more layer of predecessors/successors BFS. add one more vertex to begin/end of paths.""" if direction is 'in': if v not in preds: # init first layer preds[v] = {} preds[v][1] = {} for v_pred in G.predecessors_iter(v): if v_pred in set_sv1: add_link_to_links([v_pred], [v]) else: preds[v][1][v_pred] = [[v_pred, v]] return elif depth in preds[v]: # if calculated, return return else: preds[v][depth] = {} # else, init new layer for end_vertex in preds[v][depth - 1]: for end_path in preds[v][depth - 1][end_vertex]: for v_pred in G.predecessors_iter(end_vertex): if v_pred not in end_path: # no cycle if v_pred in set_sv1: # link: add to links add_link_to_links([v_pred], end_path) continue # not in v1, add to preds/succs for iteration if v_pred not in preds[v][depth]: preds[v][depth][v_pred] =\ [[v_pred] + end_path] else: preds[v][depth][v_pred] +=\ [[v_pred] + end_path] elif direction is 'out': # out, successors if v not in succs: # init first layer succs[v] = {} succs[v][1] = {} for v_succ in G.successors_iter(v): if v_succ in set_v1t: add_link_to_links([v], [v_succ]) else: succs[v][1][v_succ] = [[v, v_succ]] return elif depth in succs[v]: # if calculated, return return else: succs[v][depth] = {} # else, init new layer for end_vertex in succs[v][depth - 1]: # from every end_vertex, add one layer for end_path in succs[v][depth - 1][end_vertex]: for v_succ in G.successors_iter(end_vertex): if v_succ not in end_path: if v_succ in set_v1t: # link: add to links add_link_to_links(end_path, [v_succ]) continue # not in v1, add to preds/succs for iteration elif v_succ not in succs[v][depth]: succs[v][depth][v_succ] =\ [end_path + [v_succ]] else: succs[v][depth][v_succ] +=\ [end_path + [v_succ]] else: return 1 def get_v_layer(v, depth, direction): """Wrapper to get demanded layer of in/out BFS. - direction: 'in', 'out' """ if direction is 'in': global_dict = preds elif direction is 'out': global_dict = succs else: # wrong direction return False for i_depth in range(depth, 0, -1): if v not in global_dict: iter_layer(v, 1, direction) if i_depth in global_dict[v]: layers_to_cal = list(range(i_depth + 1, depth + 1)) for layer in layers_to_cal: iter_layer(v, layer, direction) if global_dict[v][depth]: return True else: return False def merge_dicts(x, y): """Given two dicts, merge them into a new dict as a shallow copy.""" z = x.copy() z.update(y) return z # for py < 3.5 # return {**x, **y} def check_path(path): """Check if path con tains all vertices in v1.""" if set_v1 <= set(path[1:-1]): if path not in valid_paths: return True else: return False def sort_path(vertex, next_list): """Sort next vertex list by V' first, then by weight.""" weight_list = {} if len(next_list) is 1: return list(next_list) for next_v in next_list: # generate value list: add large num to vertices not in v1 LARGE_NUM = 20 if next_v is t: weight_list[next_v] = 0 if next_v in dict_v1: weight_list[next_v] = next_list[next_v]['weight'] if iter_depth is 0: nn_v_list = G[next_v] if nn_v_list: nn_weight = sort_path(next_v, nn_v_list, 1)[0] # least weight from nn_v_list weight_list[next_v] = (next_list[next_v]['weight'] + nn_weight) else: weight_list[next_v] = (next_list[next_v]['weight']) else: weight_list[next_v] = (next_list[next_v]['weight'] + LARGE_NUM) return sorted(weight_list, key=weight_list.get) def find_least_pair(links, pair_to_find=False): """Find least merge pair.""" # links: {link:(outdegree+indegree), outdegree, indegree, length, path} if pair_to_find: print(pair_to_find) path_pair = links_out[pair_to_find[0]][tuple(pair_to_find)][0] return path_pair least = (100, 100, 100, 600, 0) for v in path_set_sv1: if v not in links_out: # a vertex is not in links_out print('no way for vertex:', v) print(path_set_sv1) pprint(links_out) return 0 for v in path_set_sv1: for pair in links_out[v]: # iterate all link in links_out v_in = pair[1] outdegree = sv1_outdegree[v] # print(links_out[v]) # print(pair) indegree = v1t_indegree[v_in] length = len(links_out[v][pair][0]) # MVP: shortest path links[pair] = (outdegree+indegree, outdegree, indegree, length, links_out[v][pair]) # print(links[pair]) # print(least) if outdegree < least[1] and indegree < least[2]: least = links[pair] elif outdegree + indegree < least[0]: least = links[pair] elif outdegree + indegree == least[0]: if length < least[3]: # least[length] least = links[pair] elif ((least[1] > 1 and least[2] > 1) and (outdegree is 1 or indegree is 1)): # in and out > 1, but new one is (1,n) least = links[pair] # pprint(links) # pprint(least) return least[4] # all paths in pair def del_v1_pair(v, direction): """Delete v1 pair from out or in.""" if direction is 'out': path_set_sv1.remove(v) del sv1_outdegree[v] if v in links_out: del links_out[v] for v_in in links_in: for key in links_in[v_in]: if key[0] == v and key in links_in[v_in]: del links_in[v_in][key] break # update v_outdegree else: path_set_v1t.remove(v) del v1t_indegree[v] if v in links_in: del links_in[v] for v_out in links_out: for key in links_out[v_out]: if key[1] == v and key in links_out[v_out]: del links_out[v_out][key] break def del_v1v2_pair(v_out, v_in): """Del v1v2 pair. if (2,3) is the new merged pair, then 3->2 is not valid.""" if v_out in links_out: if (v_out, v_in) in links_out[v_out]: del links_out[v_out][(v_out, v_in)] if v_in in links_in: if (v_out, v_in) in links_in[v_in]: del links_in[v_in][(v_out, v_in)] def merge_path(final_path, new_link): """Merge paths.""" global used start, end = new_link[0], new_link[-1] if len(new_link) > 2: # add v not in v1 to used used += new_link[1:-1] # print(final_path, new_link) for i in range(len(final_path)): if final_path[i][-1] == start: del_start = final_path[i] if final_path[i][0] == end: del_end = final_path[i] new_ele = del_start + new_link[1:-1] + del_end final_path.remove(del_start) final_path.remove(del_end) final_path.append(new_ele) new_out = new_ele[-1] new_in = new_ele[0] # print(final_path) return final_path, new_out, new_in def one_move(final_path, first_pairs): if first_pairs: best_path = find_least_pair(links, first_pairs[0]) del first_pairs[0] else: least_paths = find_least_pair(links) # print('least_paths', least_paths) if not least_paths: # no paths return False for best_path in least_paths: if len(best_path) > 2: # if new v not in V' is added to path new_used_v = set(best_path[1:-1]) if verbose: print('new added to v:', new_used_v) for v in new_used_v: del_v_between(v) if not (new_used_v & set(used)): break else: break else: print(least_paths) first_pairs0.append([best_path[0], best_path[-1]]) print(first_pairs0) print('No right path.\n') return False v_out, v_in = best_path[0], best_path[-1] if verbose: print("new merge:", best_path) final_path, new_out, new_in = merge_path(final_path, best_path) del_v1_pair(v_out, 'out') del_v1_pair(v_in, 'in') del_v1v2_pair(new_out, new_in) for v in path_set_sv1: if v in links_out: sv1_outdegree[v] = len(links_out[v]) for v in path_set_v1t: if v in links_in: v1t_indegree[v] = len(links_in[v]) if verbose: print(final_path) input('pause') return True def del_v_between(v): """Delete all paths contains certain vertex. Delete from links_out and links_in. links_out[v_out][(v_out, v_in)] = [new_link_path] """ # pprint(links_out) # print('del v:', v) for v_out in links_out: for link in links_out[v_out]: for path in links_out[v_out][link]: # iterate all available paths if v in path: # print(path, v) links_out[v_out][link].remove(path) for v_in in links_in: for link in links_in[v_in]: for path in links_in[v_in][link]: # iterate all available paths if v in path: # print(path, v) links_in[v_in][link].remove(path) # pprint(links_out) clean_in_out() # pprint(links_out) def clean_in_out(): """Clean links_in and links_out after delete some v.""" for v in links_out: for link in list(links_out[v].keys()): # list(dict.keys()) to safely iterate dict and del keys if not links_out[v][link]: del links_out[v][link] for v in list(links_out.keys()): if not links_out[v]: del links_out[v] for v in links_in: for link in list(links_in[v].keys()): if not links_in[v][link]: del links_in[v][link] for v in list(links_in.keys()): if not links_in[v]: del links_in[v] def generate_seq(v1): """Generate sequence of V'.""" import random v1_copy = v1[:] random.shuffle(v1_copy) return v1_copy # print(generate_seq(v1)) # conten_path(v1) # print(generate_seq(v1)) # print(generate_seq(v1)) # print(generate_seq(v1)) def conten_path(seq_v1): """Generate path(possibly with cycle).""" seq_path = [s] + seq_v1 + [t] seq_path2 = [s] + seq_v1 + [t] seq_v1 = [] path = [s] weight = 0 while len(seq_path) > 1: pair = (seq_path[0], seq_path[1]) try: new_conten = links_out[seq_path[0]][pair][0][1:] except KeyError: new_conten = [-1, seq_path[1]] seq_v1.append([seq_path[0], -1, seq_path[1]]) weight += 10000 # punish no edge else: seq_v1.append([seq_path[0]] + new_conten) weight += cal_path_weight([seq_path[0]] + new_conten) finally: path += new_conten del seq_path[0] # print(new_conten) # print(path) return path, seq_path2, weight def cal_v1_weight(seq_v1): """Generate path(possibly with cycle).""" weight = 0 for i in range(len(seq_v1)-1): weight += distance_v1(seq_v1[i], seq_v1[i+1]) # print(weight) # print(seq_v1, weight) return weight def cal_path_weight(path): """Calculate path weight. - input: path node list. - output: path weight. """ weight = 0 for i in range(len(path) - 1): # as used i+1 weight += G[path[i]][path[i+1]]['weight'] return weight def distance_v1(a, b): """Calculate distance of vi and vj.""" if a == t: if b == s: return 0 else: return 100000 # test for big elif a == s and b == t: return 100000 else: try: new_conten = links_out[a][(a,b)][0][:] except KeyError: return 10000 # punish no path else: return cal_path_weight(new_conten) from pprint import pprint set_v1 = set(v1) set_sv1 = set([s] + v1) # out set_v1t = set(v1 + [t]) # in valid_paths = [] preds = {} # two dict to store values succs = {} links = {} # (vi, vj): [path1, path2, ...] links_out = {} links_in = {} for v in set_sv1: links_out[v] = {} for v in set_v1t: links_in[v] = {} global num_paths, max_weight num_paths = 0 BIG_WEIGHT = 4800 max_weight = BIG_WEIGHT global i_searched # num of paths searched i_searched = 0 print("s:", s, "t:", t, "V':", v1) # printout for debugging INIT_DEPTH = 9 # init depth: 5 works for std case 1, 2, 4 for v in set_sv1: depth = 1 while (not links_out[v]) or (depth < INIT_DEPTH): if get_v_layer(v, depth, 'out'): depth += 1 else: break for v in set_v1t: depth = 1 while (not links_in[v]) or (depth < INIT_DEPTH): if get_v_layer(v, depth, 'in'): depth += 1 else: break sv1_outdegree = {} v1t_indegree = {} for v in set_sv1: sv1_outdegree[v] = len(links_out[v]) # print(v, 'out', len(links_out[v])) for v in set_v1t: v1t_indegree[v] = len(links_in[v]) # print(v, 'in', len(links_in[v])) path_set_sv1 = set_sv1.copy() path_set_v1t = set_v1t.copy() global final_path final_path = [[s], [t]] + [[v] for v in v1] global used used = [] global first_pairs0 first_pairs0 = [] for i in first_pairs: first_pairs0.append(i[:]) pij = {} for v in set_sv1: for v2 in set_v1t: if v2 != v: pair = (v, v2) pij[(v,v2)] = distance_v1(v, v2) # pprint(pij) nodes = [] node_dict = {} for v in [s,t] + v1: nodes.append(v) node_dict[len(nodes)] = v # print(node_dict) world = pants.World(nodes, distance_v1) solver = pants.Solver(start=s, alpha=1, beta=3, limit=100, links_out=links_out) solution = solver.solve(world) # solutions = solver.solutions(world) # print('distance: {}'.format(solution.distance)) # print('tour: {}'.format(solution.tour)) i = 0 def local_DFS(seq, depth=6): """local DFS to reduce weight. 1. generate sequence for local DFS. 2. DFS to find least local weight.""" for i in range(len(seq) - 1): if distance_v1(seq[i], seq[i+1]) >= 10000: # no path between # print(seq[i]) begin = max(0, i-depth) end = min(i+depth, len(seq)) # print('begin {}, end{}'.format(begin,end)) # print(seq[begin:end]) weight0 = cal_v1_weight(seq[begin:end]) # init weight for DFS DFS(seq[begin:end], weight0) def DFS(seq, begin_weight=1000000): print(seq, begin_weight) weight0 = begin_weight best_path = [] stack = deque() stack.append([[seq[0]], 0]) while stack: # is not blank # print(stack) # input() t = stack.popleft() # print(t) path, weight = t[0], t[1] if weight >= weight0: continue if len(path) == len(seq): # come to end print(weight0, weight) weight0 = weight best_path = path for i in seq: # seq of random sequence if i not in path: stack.appendleft((path+[i], weight + distance_v1(path[-1], i))) print(begin_weight, weight0) return best_path def MC_DFS(seq, begin_weight=1000000): print(seq, begin_weight) weight0 = begin_weight best_path = [] stack = deque() stack.append([[seq[0]], 0]) # MC for first layer sequence t = stack.popleft() path, weight = t[0], t[1] if weight >= weight0: continue if len(path) == len(seq): # come to end print(weight0, weight) weight0 = weight best_path = path num_coins = 500 seq = MC_path(seq, num_coins) for i in seq: if i not in path: stack.appendleft((path+[i], weight + distance_v1(path[-1], i))) while stack: t = stack.pop() path, weight = t[0], t[1] if weight >= weight0: continue if len(path) == len(seq): # come to end print(weight0, weight) weight0 = weight best_path = path for i in seq: if i not in path: stack.append((path+[i], weight + distance_v1(path[-1], i))) print(begin_weight, weight0) return best_path def MC_path(seq, num_coins): """MC方法排序DFS, 启发式决定DFS栈中元素的先后顺序.""" paths_for_MC = [] # path 列表, 注意控制总量 weight_paths = [] # weight 列表 for node1 in seq: if distance_v1(s, node1) <= 100000: paths_for_MC.append([s,node1]) weight_paths.append(0) # 初始化, 加入s附近的路径 # print('len of MC ', len(paths_for_MC)) coin_path = num_coins / len(paths_for_MC) # 每个路径的MC次数 for path in paths_for_MC: for i in range(int(coin_path)): try_path = generate_seq(list(set(v1)-set([node1]))) weight = cal_v1_weight(path + try_path + [t]) # 加入pseudo-weight计算 # should add cycle weight in later version # print(path+try_path +[t]) # print(weight) weight_paths[paths_for_MC.index(path)] += weight / int(coin_path) for i in range(len(paths_for_MC)): print(paths_for_MC[i], weight_paths[i]) sorted_paths = [] sorted_index = sorted(weight_paths) for i in range(len(paths_for_MC)): sorted_paths.append(paths_for_MC[weight_paths.index(sorted_index[i])]) # print(sorted_paths) return sorted_paths MC_path(v1, 5000) input() while True: i += 1 seq_v1 = solution.tour j = seq_v1.index(s) seq_v1 = seq_v1[j:] + seq_v1[:j] print('seq of v1:', seq_v1) path, seq, weight = conten_path(seq_v1[1:-1]) print(path, 'weight', weight) if path: if -1 not in path and len(path) == len(set(path)): print('valid path.') return path else: print('not valid') print(path) print(seq) print('gaps:', len(path)-len(set(path))) local_DFS(seq) input() if verbose: # verbose printout print("added route:", i_searched) print("num of paths: {}".format(num_paths)) pprint(valid_paths) if valid_paths: # output return valid_paths, G else: if verbose: print('no path') return "NA"
tab_nom = network.get_edge_attributes( Graph, 'label') #On récupère les labels des arêtes for x in tab_nom: print(tab_nom[x], tab_poids[x] ) #On affiche le nom et le poids de chaque rue network.draw_networkx_edges(Graph, pos) # On récupère les traits du schéma network.draw_networkx_labels(Graph, pos) #On récupère les noms network.draw_networkx_edge_labels(Graph, pos) #On récupère les noms sur les arêtes network.draw_networkx_nodes(Graph, pos) #On récupère les points rouge pyp.show() noeuds = [] choix = r.randint(1, 10) for i in range(choix): x = r.uniform(0, 10) y = r.uniform(0, 10) noeuds.append([x, y]) ensemble = p.World(noeuds, function) solver = p.Solver() solutions = solver.solutions(ensemble) for solution in solutions: print(solution.distance) print(noeuds)
nodes.append(vals) nodes = [ node for i, node in enumerate(nodes) if NUMBER_NODES <= 0 or i < NUMBER_NODES ] # We apply the filter ( no need to check for doubles, as this file is generated by the script print("Temp file load finished") #Calculation print("Creating world") world = pants.World( nodes, calcul_distance) # We create the environement for the ant system print("Finished creating world") print("Creating solver") solver = pants.Solver() # We create the solver print("Finished creating solver") print("Starting solving world") solutions = solver.solutions(world) # We generate our solutions best = float("inf") bestSolution = None for currSolution in solutions: # We try to find wich solution has the lower distance in total if currSolution.distance < best: best = currSolution.distance solution = currSolution # We keep the solution print("Finished solving world") print("the best distance is {} {}".format(round( best, 3), "miles" if USE_MILES_UNIT else "km")) # We display the distance
import pants import math import random nodes = [] for i in range(0, 100): nodes.append((random.uniform(0, 100), random.uniform(0, 100))) print(nodes) def fn(a, b): return math.sqrt(pow(a[1] - b[1], 2) + pow(a[0] - b[0], 2)) monde = pants.World(nodes, fn) solver = pants.Solver() sol = solver.solve(monde)
def _solve(self): world = pants.World(list(self.nodes), self.get_weight) solver = pants.Solver() return solver, world