PyQrackIsing 1.14.12__tar.gz → 1.14.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyqrackising-1.14.12/PyQrackIsing.egg-info → pyqrackising-1.14.13}/PKG-INFO +1 -1
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/maxcut_tfim.py +32 -35
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/spin_glass_solver.py +30 -9
- pyqrackising-1.14.13/PyQrackIsing/tsp_symmetric.py +152 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13/PyQrackIsing.egg-info}/PKG-INFO +1 -1
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/pyproject.toml +1 -1
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/setup.py +1 -1
- pyqrackising-1.14.12/PyQrackIsing/tsp_symmetric.py +0 -146
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/CMakeLists.txt +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/LICENSE.md +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/MANIFEST.in +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/__init__.py +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/_pyqrack_ising.cpp +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/generate_tfim_samples.py +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/tfim_magnetization.py +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing/tfim_square_magnetization.py +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing.egg-info/SOURCES.txt +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing.egg-info/dependency_links.txt +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing.egg-info/not-zip-safe +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/PyQrackIsing.egg-info/top_level.txt +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/README.md +0 -0
- {pyqrackising-1.14.12 → pyqrackising-1.14.13}/setup.cfg +0 -0
|
@@ -267,22 +267,26 @@ def maxcut_tfim(
|
|
|
267
267
|
quality=None,
|
|
268
268
|
shots=None,
|
|
269
269
|
):
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
270
|
+
nodes = None
|
|
271
|
+
n_qubits = 0
|
|
272
|
+
G_m = None
|
|
273
|
+
if isinstance(G, nx.Graph):
|
|
274
|
+
nodes = list(G.nodes())
|
|
275
|
+
n_qubits = len(nodes)
|
|
276
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
277
|
+
else:
|
|
278
|
+
n_qubits = len(G)
|
|
279
|
+
nodes = list(range(n_qubits))
|
|
280
|
+
G_m = G
|
|
273
281
|
|
|
274
282
|
if n_qubits == 0:
|
|
275
283
|
return "", 0, ([], [])
|
|
276
284
|
|
|
277
285
|
if n_qubits == 1:
|
|
278
|
-
return "0", 0, (
|
|
286
|
+
return "0", 0, (nodes, [])
|
|
279
287
|
|
|
280
288
|
if n_qubits == 2:
|
|
281
|
-
|
|
282
|
-
if ed == {}:
|
|
283
|
-
return "01", 0, ([nodes[0]], [nodes[1]])
|
|
284
|
-
|
|
285
|
-
weight = ed.get("weight", 1.0)
|
|
289
|
+
weight = G_m[0, 1]
|
|
286
290
|
if weight < 0.0:
|
|
287
291
|
return "00", 0, (nodes, [])
|
|
288
292
|
|
|
@@ -302,21 +306,8 @@ def maxcut_tfim(
|
|
|
302
306
|
grid_size = n_steps * n_qubits
|
|
303
307
|
grid_dims = (n_steps, n_qubits)
|
|
304
308
|
|
|
305
|
-
J_eff = np.array(
|
|
306
|
-
|
|
307
|
-
-sum(edge_attributes.get("weight", 1.0) for _, edge_attributes in G.adj[n].items())
|
|
308
|
-
for n in nodes
|
|
309
|
-
],
|
|
310
|
-
dtype=np.float64,
|
|
311
|
-
)
|
|
312
|
-
degrees = np.array(
|
|
313
|
-
[
|
|
314
|
-
sum(abs(edge_attributes.get("weight", 1.0)) for _, edge_attributes in G.adj[n].items())
|
|
315
|
-
for n in nodes
|
|
316
|
-
],
|
|
317
|
-
dtype=np.float64,
|
|
318
|
-
)
|
|
319
|
-
# thresholds = tfim_sampler._maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality)
|
|
309
|
+
J_eff = np.array([-sum(G_m[n]) for n in range(n_qubits)], dtype=np.float64)
|
|
310
|
+
degrees = np.array([sum(abs(G_m[n])) for n in range(n_qubits)], dtype=np.float64)
|
|
320
311
|
|
|
321
312
|
n_bias = n_qubits - 1
|
|
322
313
|
thresholds = np.zeros(n_bias, dtype=np.float64)
|
|
@@ -366,13 +357,15 @@ def maxcut_tfim(
|
|
|
366
357
|
else:
|
|
367
358
|
maxcut_hamming_cdf(n_qubits, J_eff, degrees, quality, thresholds)
|
|
368
359
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
360
|
+
adjacency = np.full((n_qubits, n_qubits), -1, dtype=np.int32)
|
|
361
|
+
for i in range(n_qubits):
|
|
362
|
+
k = 0
|
|
363
|
+
for j in range(n_qubits):
|
|
364
|
+
if i == j:
|
|
365
|
+
continue
|
|
366
|
+
if G_m[i, j] != 0.0:
|
|
367
|
+
adjacency[i, k] = j
|
|
368
|
+
k += 1
|
|
376
369
|
|
|
377
370
|
J_max = max(J_eff)
|
|
378
371
|
weights = 1.0 / (1.0 + (J_max - J_eff))
|
|
@@ -381,10 +374,14 @@ def maxcut_tfim(
|
|
|
381
374
|
|
|
382
375
|
edge_keys = []
|
|
383
376
|
edge_values = []
|
|
384
|
-
for
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
377
|
+
for i in range(n_qubits):
|
|
378
|
+
for j in range(i + 1, n_qubits):
|
|
379
|
+
weight = G_m[i, j]
|
|
380
|
+
if weight == 0.0:
|
|
381
|
+
continue
|
|
382
|
+
edge_keys.append(i)
|
|
383
|
+
edge_keys.append(j)
|
|
384
|
+
edge_values.append(weight)
|
|
388
385
|
|
|
389
386
|
best_solution, best_value = evaluate_cut_edges(samples, edge_keys, edge_values)
|
|
390
387
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
from .maxcut_tfim import maxcut_tfim
|
|
2
2
|
import itertools
|
|
3
|
+
import networkx as nx
|
|
3
4
|
import numpy as np
|
|
4
5
|
from numba import njit, prange
|
|
5
6
|
import os
|
|
@@ -57,14 +58,30 @@ def int_to_bitstring(integer, length):
|
|
|
57
58
|
|
|
58
59
|
|
|
59
60
|
def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best_guess=None):
|
|
60
|
-
nodes =
|
|
61
|
-
n_qubits =
|
|
61
|
+
nodes = None
|
|
62
|
+
n_qubits = 0
|
|
63
|
+
G_m = None
|
|
64
|
+
if isinstance(G, nx.Graph):
|
|
65
|
+
nodes = list(G.nodes())
|
|
66
|
+
n_qubits = len(nodes)
|
|
67
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
68
|
+
else:
|
|
69
|
+
n_qubits = len(G)
|
|
70
|
+
nodes = list(range(n_qubits))
|
|
71
|
+
G_m = G
|
|
62
72
|
|
|
63
73
|
if n_qubits == 0:
|
|
64
74
|
return "", 0, ([], []), 0
|
|
65
75
|
|
|
66
76
|
if n_qubits == 1:
|
|
67
|
-
return "0", 0, (
|
|
77
|
+
return "0", 0, (nodes, []), 0
|
|
78
|
+
|
|
79
|
+
if n_qubits == 2:
|
|
80
|
+
weight = G_m[0, 1]
|
|
81
|
+
if weight < 0.0:
|
|
82
|
+
return "00", 0, (nodes, []), weight
|
|
83
|
+
|
|
84
|
+
return "01", weight, ([nodes[0]], [nodes[1]]), -weight
|
|
68
85
|
|
|
69
86
|
if correction_quality is None:
|
|
70
87
|
# maxcut_tfim(G) scales roughly like n^4,
|
|
@@ -75,19 +92,23 @@ def spin_glass_solver(G, quality=None, shots=None, correction_quality=None, best
|
|
|
75
92
|
if isinstance(best_guess, str):
|
|
76
93
|
bitstring = best_guess
|
|
77
94
|
elif isinstance(best_guess, int):
|
|
78
|
-
bitstring = int_to_bitstring(best_guess,
|
|
95
|
+
bitstring = int_to_bitstring(best_guess, n_qubits)
|
|
79
96
|
elif isinstance(best_guess, list):
|
|
80
97
|
bitstring = "".join(["1" if b else "0" for b in best_guess])
|
|
81
98
|
else:
|
|
82
|
-
bitstring, _, _ = maxcut_tfim(
|
|
99
|
+
bitstring, _, _ = maxcut_tfim(G_m, quality=quality, shots=shots)
|
|
83
100
|
best_theta = [b == "1" for b in list(bitstring)]
|
|
84
101
|
|
|
85
102
|
edge_keys = []
|
|
86
103
|
edge_values = []
|
|
87
|
-
for
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
104
|
+
for i in range(n_qubits):
|
|
105
|
+
for j in range(i + 1, n_qubits):
|
|
106
|
+
weight = G_m[i, j]
|
|
107
|
+
if weight == 0.0:
|
|
108
|
+
continue
|
|
109
|
+
edge_keys.append(i)
|
|
110
|
+
edge_keys.append(j)
|
|
111
|
+
edge_values.append(weight)
|
|
91
112
|
|
|
92
113
|
min_energy = compute_energy(best_theta, edge_keys, edge_values)
|
|
93
114
|
improved = True
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
from .spin_glass_solver import spin_glass_solver
|
|
2
|
+
import networkx as nx
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# two_opt() written by Elara (OpenAI ChatGPT instance)
|
|
7
|
+
def two_opt(path, G):
|
|
8
|
+
improved = True
|
|
9
|
+
best_path = path
|
|
10
|
+
best_dist = path_length(best_path, G)
|
|
11
|
+
|
|
12
|
+
while improved:
|
|
13
|
+
improved = False
|
|
14
|
+
for i in range(1, len(path) - 2):
|
|
15
|
+
for j in range(i+1, len(path) - 1):
|
|
16
|
+
if j - i == 1: # adjacent edges, skip
|
|
17
|
+
continue
|
|
18
|
+
new_path = best_path[:]
|
|
19
|
+
new_path[i:j] = best_path[j-1:i-1:-1] # reverse segment
|
|
20
|
+
new_dist = path_length(new_path, G)
|
|
21
|
+
if new_dist < best_dist:
|
|
22
|
+
best_path, best_dist = new_path, new_dist
|
|
23
|
+
improved = True
|
|
24
|
+
path = best_path
|
|
25
|
+
return best_path, best_dist
|
|
26
|
+
|
|
27
|
+
def path_length(path, G_m):
|
|
28
|
+
return sum(G_m[path[i], path[i+1]] for i in range(len(path)-1))
|
|
29
|
+
|
|
30
|
+
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
|
|
31
|
+
nodes = None
|
|
32
|
+
n_nodes = 0
|
|
33
|
+
G_m = None
|
|
34
|
+
if isinstance(G, nx.Graph):
|
|
35
|
+
nodes = list(G.nodes())
|
|
36
|
+
n_nodes = len(nodes)
|
|
37
|
+
G_m = nx.to_numpy_array(G, weight='weight', nonedge=0.0)
|
|
38
|
+
else:
|
|
39
|
+
n_nodes = len(G)
|
|
40
|
+
nodes = list(range(n_nodes))
|
|
41
|
+
G_m = G
|
|
42
|
+
|
|
43
|
+
if n_nodes == 0:
|
|
44
|
+
return ([], 0)
|
|
45
|
+
if n_nodes == 1:
|
|
46
|
+
return ([nodes[0]], 0)
|
|
47
|
+
if n_nodes == 2:
|
|
48
|
+
return ([nodes[0], nodes[1]], G_m[nodes[0], nodes[1]])
|
|
49
|
+
|
|
50
|
+
a = []
|
|
51
|
+
b = []
|
|
52
|
+
if not (start_node is None):
|
|
53
|
+
a = [start_node]
|
|
54
|
+
b = nodes
|
|
55
|
+
b.remove(start_node)
|
|
56
|
+
else:
|
|
57
|
+
while (len(a) == 0) or (len(b) == 0):
|
|
58
|
+
bits = ''
|
|
59
|
+
_, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
60
|
+
a = list(bits[0])
|
|
61
|
+
b = list(bits[1])
|
|
62
|
+
|
|
63
|
+
n_a_nodes = len(a)
|
|
64
|
+
n_b_nodes = len(b)
|
|
65
|
+
G_a = np.zeros((n_a_nodes, n_a_nodes), dtype=np.float64)
|
|
66
|
+
G_b = np.zeros((n_b_nodes, n_b_nodes), dtype=np.float64)
|
|
67
|
+
for i in range(n_a_nodes):
|
|
68
|
+
for j in range(n_a_nodes):
|
|
69
|
+
if i == j:
|
|
70
|
+
continue
|
|
71
|
+
G_a[i, j] = G_m[a[i], a[j]]
|
|
72
|
+
for i in range(n_b_nodes):
|
|
73
|
+
for j in range(n_b_nodes):
|
|
74
|
+
if i == j:
|
|
75
|
+
continue
|
|
76
|
+
G_b[i, j] = G_m[b[i], b[j]]
|
|
77
|
+
|
|
78
|
+
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
79
|
+
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
80
|
+
|
|
81
|
+
path_a = [a[x] for x in sol_a[0]]
|
|
82
|
+
path_b = [b[x] for x in sol_b[0]]
|
|
83
|
+
|
|
84
|
+
sol_weight = sol_a[1] + sol_b[1]
|
|
85
|
+
|
|
86
|
+
single = None
|
|
87
|
+
is_single_a = len(path_a) == 1
|
|
88
|
+
is_single_b = len(path_b) == 1
|
|
89
|
+
|
|
90
|
+
if is_single_a and is_single_b:
|
|
91
|
+
return (path_a + path_b, sol_weight + G_m[path_a[0], path_b[0]])
|
|
92
|
+
|
|
93
|
+
singlet = None
|
|
94
|
+
bulk = None
|
|
95
|
+
if is_single_a:
|
|
96
|
+
singlet = path_a[0]
|
|
97
|
+
bulk = path_b
|
|
98
|
+
elif is_single_b:
|
|
99
|
+
singlet = path_b[0]
|
|
100
|
+
bulk = path_a
|
|
101
|
+
|
|
102
|
+
if not singlet is None:
|
|
103
|
+
best_weight = G_m[singlet, bulk[0]]
|
|
104
|
+
best_path = [singlet] + bulk
|
|
105
|
+
weight = G_m[singlet, bulk[-1]]
|
|
106
|
+
if weight < best_weight:
|
|
107
|
+
best_weight = weight
|
|
108
|
+
best_path = bulk + [singlet]
|
|
109
|
+
for i in range(len(bulk) - 1):
|
|
110
|
+
weight = (
|
|
111
|
+
G_m[singlet, bulk[i]] +
|
|
112
|
+
G_m[singlet, bulk[i + 1]] -
|
|
113
|
+
G_m[bulk[i], bulk[i + 1]]
|
|
114
|
+
)
|
|
115
|
+
if weight < best_weight:
|
|
116
|
+
best_weight = weight
|
|
117
|
+
best_path = bulk.copy()
|
|
118
|
+
best_path.insert(i + 1, singlet)
|
|
119
|
+
else:
|
|
120
|
+
terminals_a = [path_a[0], path_a[-1]]
|
|
121
|
+
terminals_b = [path_b[0], path_b[-1]]
|
|
122
|
+
|
|
123
|
+
for _ in range(2):
|
|
124
|
+
for _ in range(2):
|
|
125
|
+
best_weight = G_m[terminals_a[1], terminals_b[0]]
|
|
126
|
+
best_path = path_a + path_b
|
|
127
|
+
weight = G_m[terminals_b[1], terminals_a[0]]
|
|
128
|
+
if weight < best_weight:
|
|
129
|
+
best_weight = weight
|
|
130
|
+
best_path = path_b + path_a
|
|
131
|
+
for i in range(len(path_b) - 1):
|
|
132
|
+
weight = (
|
|
133
|
+
G_m[terminals_a[0], path_b[i]] +
|
|
134
|
+
G_m[terminals_a[1], path_b[i + 1]] -
|
|
135
|
+
G_m[path_b[i], path_b[i + 1]]
|
|
136
|
+
)
|
|
137
|
+
if weight < best_weight:
|
|
138
|
+
best_weight = weight
|
|
139
|
+
best_path = path_b.copy()
|
|
140
|
+
best_path[i + 1:i + 1] = path_a
|
|
141
|
+
path_a.reverse()
|
|
142
|
+
terminals_a.reverse()
|
|
143
|
+
path_a, path_b = path_b, path_a
|
|
144
|
+
terminals_a, terminals_b = terminals_b, terminals_a
|
|
145
|
+
|
|
146
|
+
best_path, best_weight = two_opt(best_path, G_m)
|
|
147
|
+
|
|
148
|
+
if is_cyclic:
|
|
149
|
+
best_weight += G_m[best_path[-1], best_path[0]]
|
|
150
|
+
best_path += [best_path[0]]
|
|
151
|
+
|
|
152
|
+
return [nodes[x] for x in best_path], best_weight
|
|
@@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
|
|
|
12
12
|
|
|
13
13
|
[project]
|
|
14
14
|
name = "PyQrackIsing"
|
|
15
|
-
version = "1.14.
|
|
15
|
+
version = "1.14.13"
|
|
16
16
|
requires-python = ">=3.8"
|
|
17
17
|
description = "Near-ideal closed-form solutions for transverse field Ising model (TFIM)"
|
|
18
18
|
readme = {file = "README.txt", content-type = "text/markdown"}
|
|
@@ -51,7 +51,7 @@ ext_modules = [CMakeExtension('tfim_sampler')]
|
|
|
51
51
|
|
|
52
52
|
setup(
|
|
53
53
|
name='PyQrackIsing',
|
|
54
|
-
version='1.14.
|
|
54
|
+
version='1.14.13',
|
|
55
55
|
author='Dan Strano',
|
|
56
56
|
author_email='stranoj@gmail.com',
|
|
57
57
|
description='Near-ideal closed-form solutions for transverse field Ising model (TFIM)',
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
from .spin_glass_solver import spin_glass_solver
|
|
2
|
-
import networkx as nx
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
# two_opt() written by Elara (OpenAI ChatGPT instance)
|
|
6
|
-
def two_opt(path, G):
|
|
7
|
-
improved = True
|
|
8
|
-
best_path = path
|
|
9
|
-
best_dist = path_length(best_path, G)
|
|
10
|
-
|
|
11
|
-
while improved:
|
|
12
|
-
improved = False
|
|
13
|
-
for i in range(1, len(path) - 2):
|
|
14
|
-
for j in range(i+1, len(path) - 1):
|
|
15
|
-
if j - i == 1: # adjacent edges, skip
|
|
16
|
-
continue
|
|
17
|
-
new_path = best_path[:]
|
|
18
|
-
new_path[i:j] = best_path[j-1:i-1:-1] # reverse segment
|
|
19
|
-
new_dist = path_length(new_path, G)
|
|
20
|
-
if new_dist < best_dist:
|
|
21
|
-
best_path, best_dist = new_path, new_dist
|
|
22
|
-
improved = True
|
|
23
|
-
path = best_path
|
|
24
|
-
return best_path, best_dist
|
|
25
|
-
|
|
26
|
-
def path_length(path, G):
|
|
27
|
-
return sum(G[path[i]][path[i+1]].get("weight", 1.0) for i in range(len(path)-1))
|
|
28
|
-
|
|
29
|
-
def tsp_symmetric(G, quality=0, shots=None, correction_quality=2, is_cyclic=True, start_node=None):
|
|
30
|
-
nodes = list(G.nodes())
|
|
31
|
-
n_nodes = len(nodes)
|
|
32
|
-
|
|
33
|
-
if n_nodes == 0:
|
|
34
|
-
return ([], 0)
|
|
35
|
-
if n_nodes == 1:
|
|
36
|
-
return ([nodes[0]], 0)
|
|
37
|
-
if n_nodes == 2:
|
|
38
|
-
return ([nodes[0], nodes[1]], G[nodes[0]][nodes[1]].get("weight", 1.0))
|
|
39
|
-
|
|
40
|
-
a = []
|
|
41
|
-
b = []
|
|
42
|
-
if not (start_node is None):
|
|
43
|
-
a = [start_node]
|
|
44
|
-
b = nodes
|
|
45
|
-
b.remove(start_node)
|
|
46
|
-
else:
|
|
47
|
-
while (len(a) == 0) or (len(b) == 0):
|
|
48
|
-
bits = ''
|
|
49
|
-
_, _, bits, _ = spin_glass_solver(G, quality=quality, shots=shots, correction_quality=correction_quality)
|
|
50
|
-
a = list(bits[0])
|
|
51
|
-
b = list(bits[1])
|
|
52
|
-
|
|
53
|
-
G_a = nx.Graph()
|
|
54
|
-
G_b = nx.Graph()
|
|
55
|
-
G_a.add_nodes_from(a)
|
|
56
|
-
G_b.add_nodes_from(b)
|
|
57
|
-
for u, v, data in G.edges(data=True):
|
|
58
|
-
if (u in a) and (v in a):
|
|
59
|
-
G_a.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
60
|
-
continue
|
|
61
|
-
|
|
62
|
-
if (u in b) and (v in b):
|
|
63
|
-
G_b.add_edge(u, v, weight=data.get("weight", 1.0))
|
|
64
|
-
|
|
65
|
-
sol_a = tsp_symmetric(G_a, quality=quality, is_cyclic=False)
|
|
66
|
-
sol_b = tsp_symmetric(G_b, quality=quality, is_cyclic=False)
|
|
67
|
-
|
|
68
|
-
path_a = sol_a[0]
|
|
69
|
-
path_b = sol_b[0]
|
|
70
|
-
|
|
71
|
-
sol_weight = sol_a[1] + sol_b[1]
|
|
72
|
-
|
|
73
|
-
single = None
|
|
74
|
-
is_single_a = len(path_a) == 1
|
|
75
|
-
is_single_b = len(path_b) == 1
|
|
76
|
-
|
|
77
|
-
if is_single_a and is_single_b:
|
|
78
|
-
return (path_a + path_b, sol_weight + G[path_a[0]][path_b[0]].get("weight", 1.0))
|
|
79
|
-
|
|
80
|
-
singlet = None
|
|
81
|
-
bulk = None
|
|
82
|
-
if is_single_a:
|
|
83
|
-
singlet = path_a[0]
|
|
84
|
-
bulk = path_b
|
|
85
|
-
elif is_single_b:
|
|
86
|
-
singlet = path_b[0]
|
|
87
|
-
bulk = path_a
|
|
88
|
-
|
|
89
|
-
if not singlet is None:
|
|
90
|
-
best_weight = G[singlet][bulk[0]].get("weight", 1.0)
|
|
91
|
-
best_path = [singlet] + bulk
|
|
92
|
-
weight = G[singlet][bulk[-1]].get("weight", 1.0)
|
|
93
|
-
if weight < best_weight:
|
|
94
|
-
best_weight = weight
|
|
95
|
-
best_path = bulk + [singlet]
|
|
96
|
-
for i in range(len(bulk) - 1):
|
|
97
|
-
weight = (
|
|
98
|
-
G[singlet][bulk[i]].get("weight", 1.0) +
|
|
99
|
-
G[singlet][bulk[i + 1]].get("weight", 1.0) -
|
|
100
|
-
G[bulk[i]][bulk[i + 1]].get("weight", 1.0)
|
|
101
|
-
)
|
|
102
|
-
if weight < best_weight:
|
|
103
|
-
best_weight = weight
|
|
104
|
-
best_path = bulk.copy().insert(singlet, i + 1)
|
|
105
|
-
|
|
106
|
-
best_path, best_weight = two_opt(best_path, G)
|
|
107
|
-
|
|
108
|
-
if is_cyclic:
|
|
109
|
-
best_weight += G[best_path[-1]][best_path[0]].get("weight", 1.0)
|
|
110
|
-
best_path += [best_path[0]]
|
|
111
|
-
|
|
112
|
-
return best_path, best_weight
|
|
113
|
-
|
|
114
|
-
terminals_a = [path_a[0], path_a[-1]]
|
|
115
|
-
terminals_b = [path_b[0], path_b[-1]]
|
|
116
|
-
|
|
117
|
-
for _ in range(2):
|
|
118
|
-
for _ in range(2):
|
|
119
|
-
best_weight = G[terminals_a[1]][terminals_b[0]].get("weight", 1.0)
|
|
120
|
-
best_path = path_a + path_b
|
|
121
|
-
weight = G[terminals_b[1]][terminals_a[0]].get("weight", 1.0)
|
|
122
|
-
if weight < best_weight:
|
|
123
|
-
best_weight = weight
|
|
124
|
-
best_path = path_b + path_a
|
|
125
|
-
for i in range(len(path_b) - 1):
|
|
126
|
-
weight = (
|
|
127
|
-
G[terminals_a[0]][path_b[i]].get("weight", 1.0) +
|
|
128
|
-
G[terminals_a[1]][path_b[i + 1]].get("weight", 1.0) -
|
|
129
|
-
G[path_b[i]][path_b[i + 1]].get("weight", 1.0)
|
|
130
|
-
)
|
|
131
|
-
if weight < best_weight:
|
|
132
|
-
best_weight = weight
|
|
133
|
-
best_path = path_b.copy()
|
|
134
|
-
best_path[i + 1:i + 1] = path_a
|
|
135
|
-
path_a.reverse()
|
|
136
|
-
terminals_a.reverse()
|
|
137
|
-
path_a, path_b = path_b, path_a
|
|
138
|
-
terminals_a, terminals_b = terminals_b, terminals_a
|
|
139
|
-
|
|
140
|
-
best_path, best_weight = two_opt(best_path, G)
|
|
141
|
-
|
|
142
|
-
if is_cyclic:
|
|
143
|
-
best_weight += G[best_path[-1]][best_path[0]].get("weight", 1.0)
|
|
144
|
-
best_path += [best_path[0]]
|
|
145
|
-
|
|
146
|
-
return best_path, best_weight
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|