# import numpy as np import matplotlib pyplot as plt from scipy spatial

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181``` ```import numpy as np import matplotlib.pyplot as plt from scipy.spatial import Delaunay import networkx as nx from scipy.spatial import distance from scipy.optimize import linprog from functools import cmp_to_key class Path: def __init__(self, C, f): self.C = C self.flow = f def getK(self, p): d = 0 for i in range(len(self.C) - 1): d += distance.euclidean(p[self.C[i]], p[self.C[i + 1]]) return d / distance.euclidean(p[self.C[0]], p[self.C[-1]]) def __repr__(self): return " %s<-%s " % (self.C, self.flow) def __str__(self): return " %s<-%s " % (self.C, self.flow) def __eq__(self, other): if len(self.C) != len(other.C): return False for i in range(len(self.C)): if self.C[i] != other.C[i]: return False return True def koefficient(path, points): d = 0 for i in range(len(path) - 1): d += distance.euclidean(points[path[i]], points[path[i + 1]]) return d / distance.euclidean(points[path[0]], points[path[-1]]) def compare(p1, p2): return koefficient(p1, p) - koefficient(p2, p) def check(lst, sub): for i in range(0, len(lst)): if lst[i:i + len(sub)] == sub: return True return False def shortest_path_ignore(Graph, source, target, ignore_edges): tmp_edges = [(i[0], i[1], Graph[i[0]][i[1]]) for i in ignore_edges] for tmp_edge in tmp_edges: Graph.remove_edge(tmp_edge[0], tmp_edge[1]) if nx.has_path(Graph, source, target): result_path = nx.shortest_path(Graph, source, target) else: return None for tmp_edge in tmp_edges: Graph.add_edge(tmp_edge[0], tmp_edge[1]) Graph[tmp_edge[0]][tmp_edge[1]].update(tmp_edge[2]) return result_path def simplexNewPaths(G, loupe_paths, V, p): print("\n Work simplex method") N = len(p) k = [i.getK(p) for i in loupe_paths] size = len(loupe_paths) A_V = [[0 for j in range(size)] for i in range(N*N)] for lp in range(len(loupe_paths)): i = loupe_paths[lp].C[0] j = loupe_paths[lp].C[-1] A_V[N*i+j][lp] = 1 b_V = [] for i in range(len(V)): for j in range(len(V)): b_V.append(V[i][j]) A_e = [] b_e = [] for u, v, d in G.edges(data=True): A_e_list = list(d['flow']) A_e.append([0 for i in range(size)]) for i in A_e_list: A_e[-1][i] = 1 b_e.append(d['capacity']) res = linprog(k, A_ub=A_e, b_ub=b_e, A_eq=A_V, b_eq=b_V, bounds=(0, None)) print(res.x) return res.x N = 4 p = [] while len(p) 0: print("Задача не разрешима") exit(1) else: all_paths[i].append(sorted(list(nx.all_simple_paths(G, i, j)), key=cmp_to_key(compare))) if i != j: paths.append(Path(all_paths[i][j][0], V[i][j])) del all_paths[i][j][0] for i in range(len(paths)): for k in range(len(paths[i].C) - 1): G[paths[i].C[k]][paths[i].C[k + 1]]['flow'].add(i) used_edges = [[[] for i in range(N)] for j in range(N)] flag_change = True flag_problem = True while flag_change and flag_problem: flag_change = False flag_problem = False for u, v, d in G.edges(data=True): sum_flow = sum([paths[paths_edge].flow for paths_edge in d['flow']]) if sum_flow > d['capacity']: flag_problem = True current_paths = list(d['flow']) for route in current_paths: if len(all_paths[paths[route].C[0]][paths[route].C[-1]]) > 0: flag_change = True new_path = all_paths[paths[route].C[0]][paths[route].C[-1]][0] del all_paths[paths[route].C[0]][paths[route].C[-1]][0] paths.append(Path(new_path, 0)) for i in range(len(new_path)-1): G[new_path[i]][new_path[i+1]]['flow'].add(len(paths)-1) resx = simplexNewPaths(G, paths, V, p) for i in range(len(resx)): paths[i].flow = resx[i] if not flag_problem: print("Задача решена") # собрать с ребер данные exit(0) else: if not flag_change: print("Задача не разрешима") ```