"""
=================================
Travelling Salesman Problem (TSP)
=================================
Implementation of approximate algorithms
for solving and approximating the TSP problem.
Categories of algorithms which are implemented:
- Christofides (provides a 3/2-approximation of TSP)
- Greedy
- Simulated Annealing (SA)
- Threshold Accepting (TA)
The Travelling Salesman Problem tries to find, given the weight
(distance) between all points where a salesman has to visit, the
route so that:
- The total distance (cost) which the salesman travels is minimized.
- The salesman returns to the starting point.
- Note that for a complete graph, the salesman visits each point once.
The function `travelling_salesman_problem` allows for incomplete
graphs by finding all-pairs shortest paths, effectively converting
the problem to a complete graph problem. It calls one of the
approximate methods on that problem and then converts the result
back to the original graph using the previously found shortest paths.
TSP is an NP-hard problem in combinatorial optimization,
important in operations research and theoretical computer science.
http://en.wikipedia.org/wiki/Travelling_salesman_problem
"""
import math
import networkx as nx
from networkx.utils import py_random_state, not_implemented_for, pairwise
__all__ = [
"traveling_salesman_problem",
"christofides",
"greedy_tsp",
"simulated_annealing_tsp",
"threshold_accepting_tsp",
]
def swap_two_nodes(soln, seed):
"""Swap two nodes in `soln` to give a neighbor solution.
Parameters
----------
soln : list of nodes
Current cycle of nodes
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
Returns
-------
list
The solution after move is applied. (A neighbor solution.)
Notes
-----
This function assumes that the incoming list `soln` is a cycle
(that the first and last element are the same) and also that
we don't want any move to change the first node in the list
(and thus not the last node either).
The input list is changed as well as returned. Make a copy if needed.
See Also
--------
move_one_node
"""
a, b = seed.sample(range(1, len(soln) - 1), k=2)
soln[a], soln[b] = soln[b], soln[a]
return soln
def move_one_node(soln, seed):
"""Move one node to another position to give a neighbor solution.
The node to move and the position to move to are chosen randomly.
The first and last nodes are left untouched as soln must be a cycle
starting at that node.
Parameters
----------
soln : list of nodes
Current cycle of nodes
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
Returns
-------
list
The solution after move is applied. (A neighbor solution.)
Notes
-----
This function assumes that the incoming list `soln` is a cycle
(that the first and last element are the same) and also that
we don't want any move to change the first node in the list
(and thus not the last node either).
The input list is changed as well as returned. Make a copy if needed.
See Also
--------
swap_two_nodes
"""
a, b = seed.sample(range(1, len(soln) - 1), k=2)
soln.insert(b, soln.pop(a))
return soln
[docs]@not_implemented_for("directed")
def christofides(G, weight="weight", tree=None):
"""Approximate a solution of the traveling salesman problem
Compute a 3/2-approximation of the traveling salesman problem
in a complete undirected graph using Christofides [1]_ algorithm.
Parameters
----------
G : Graph
`G` should be a complete weighted undirected graph.
The distance between all pairs of nodes should be included.
weight : string, optional (default="weight")
Edge data key corresponding to the edge weight.
If any edge does not have this attribute the weight is set to 1.
tree : NetworkX graph or None (default: None)
A minimum spanning tree of G. Or, if None, the minimum spanning
tree is computed using :func:`networkx.minimum_spanning_tree`
Returns
-------
list
List of nodes in `G` along a cycle with a 3/2-approximation of
the minimal Hamiltonian cycle.
References
----------
.. [1] Christofides, Nicos. "Worst-case analysis of a new heuristic for
the travelling salesman problem." No. RR-388. Carnegie-Mellon Univ
Pittsburgh Pa Management Sciences Research Group, 1976.
"""
# Remove selfloops if necessary
loop_nodes = nx.nodes_with_selfloops(G)
try:
node = next(loop_nodes)
except StopIteration:
pass
else:
G = G.copy()
G.remove_edge(node, node)
G.remove_edges_from((n, n) for n in loop_nodes)
# Check that G is a complete graph
N = len(G) - 1
# This check ignores selfloops which is what we want here.
if any(len(nbrdict) != N for n, nbrdict in G.adj.items()):
raise nx.NetworkXError("G must be a complete graph.")
if tree is None:
tree = nx.minimum_spanning_tree(G, weight=weight)
L = G.copy()
L.remove_nodes_from([v for v, degree in tree.degree if not (degree % 2)])
MG = nx.MultiGraph()
MG.add_edges_from(tree.edges)
edges = nx.min_weight_matching(L, maxcardinality=True, weight=weight)
MG.add_edges_from(edges)
return _shortcutting(nx.eulerian_circuit(MG))
def _shortcutting(circuit):
"""Remove duplicate nodes in the path"""
nodes = []
for u, v in circuit:
if v in nodes:
continue
if not nodes:
nodes.append(u)
nodes.append(v)
nodes.append(nodes[0])
return nodes
[docs]def traveling_salesman_problem(G, weight="weight", nodes=None, cycle=True, method=None):
"""Find the shortest path in `G` connecting specified nodes
This function allows approximate solution to the traveling salesman
problem on networks that are not complete graphs and/or where the
salesman does not need to visit all nodes.
This function proceeds in two steps. First, it creates a complete
graph using the all-pairs shortest_paths between nodes in `nodes`.
Edge weights in the new graph are the lengths of the paths
between each pair of nodes in the original graph.
Second, an algorithm (default: `christofides`) is used to approximate
the minimal Hamiltonian cycle on this new graph. The available
algorithms are:
- christofides
- greedy_tsp
- simulated_annealing_tsp
- threshold_accepting_tsp
Once the Hamiltonian Cycle is found, this function post-processes to
accommodate the structure of the original graph. If `cycle` is ``False``,
the biggest weight edge is removed to make a Hamiltonian path.
Then each edge on the new complete graph used for that analysis is
replaced by the shortest_path between those nodes on the original graph.
Parameters
----------
G : NetworkX graph
Undirected possibly weighted graph
nodes : collection of nodes (default=G.nodes)
collection (list, set, etc.) of nodes to visit
weight : string, optional (default="weight")
Edge data key corresponding to the edge weight.
If any edge does not have this attribute the weight is set to 1.
cycle : bool (default: True)
Indicates whether a cycle should be returned, or a path.
Note: the cycle is the approximate minimal cycle.
The path simply removes the biggest edge in that cycle.
method : function (default: None)
A function that returns a cycle on all nodes and approximates
the solution to the traveling salesman problem on a complete
graph. The returned cycle is then used to find a corresponding
solution on `G`. `method` should be callable; take inputs
`G`, and `weight`; and return a list of nodes along the cycle.
Provided options include :func:`christofides`, :func:`greedy_tsp`,
:func:`simulated_annealing_tsp` and :func:`threshold_accepting_tsp`.
If `method is None`: use :func:`christofides` for undirected `G` and
:func:`threshold_accepting_tsp` for directed `G`.
To specify parameters for these provided functions, construct lambda
functions that state the specific value. `method` must have 2 inputs.
(See examples).
Returns
-------
list
List of nodes in `G` along a path with a 3/2-approximation of the minimal
path through `nodes`.
Examples
--------
>>> tsp = nx.approximation.traveling_salesman_problem
>>> G = nx.cycle_graph(9)
>>> G[4][5]["weight"] = 5 # all other weights are 1
>>> tsp(G, nodes=[3, 6])
[3, 2, 1, 0, 8, 7, 6, 7, 8, 0, 1, 2, 3]
>>> path = tsp(G, cycle=False)
>>> path in ([4, 3, 2, 1, 0, 8, 7, 6, 5], [5, 6, 7, 8, 0, 1, 2, 3, 4])
True
Build (curry) your own function to provide parameter values to the methods.
>>> SA_tsp = nx.approximation.simulated_annealing_tsp
>>> method = lambda G, wt: SA_tsp(G, "greedy", weight=wt, temp=500)
>>> path = tsp(G, cycle=False, method=method)
>>> path in ([4, 3, 2, 1, 0, 8, 7, 6, 5], [5, 6, 7, 8, 0, 1, 2, 3, 4])
True
"""
if method is None:
if G.is_directed():
def threshold_tsp(G, weight):
return threshold_accepting_tsp(G, "greedy", weight)
method = threshold_tsp
else:
method = christofides
if nodes is None:
nodes = list(G.nodes)
dist = {}
path = {}
for n, (d, p) in nx.all_pairs_dijkstra(G, weight=weight):
dist[n] = d
path[n] = p
GG = nx.Graph()
for u in nodes:
for v in nodes:
if u == v:
continue
GG.add_edge(u, v, weight=dist[u][v])
best_GG = method(GG, weight)
if not cycle:
# find and remove the biggest edge
biggest_edge = None
length_biggest = float("-inf")
(u, v) = max(pairwise(best_GG), key=lambda x: dist[x[0]][x[1]])
pos = best_GG.index(u) + 1
while best_GG[pos] != v:
pos = best_GG[pos:].index(u) + 1
best_GG = best_GG[pos:-1] + best_GG[:pos]
best_path = []
for u, v in pairwise(best_GG):
best_path.extend(path[u][v][:-1])
best_path.append(v)
return best_path
[docs]def greedy_tsp(G, weight="weight", source=None):
"""Return a low cost cycle starting at `source` and its cost.
This approximates a solution to the traveling salesman problem.
It finds a cycle of all the nodes that a salesman can visit in order
to visit many nodes while minimizing total distance.
It uses a simple greedy algorithm.
In essence, this function returns a large cycle given a source point
for which the total cost of the cycle is minimized.
Parameters
----------
G : Graph
The Graph should be a complete weighted undirected graph.
The distance between all pairs of nodes should be included.
weight : string, optional (default="weight")
Edge data key corresponding to the edge weight.
If any edge does not have this attribute the weight is set to 1.
source : node, optional (default: first node in list(G))
Starting node. If None, defaults to ``next(iter(G))``
Returns
-------
cycle : list of nodes
Returns the cycle (list of nodes) that a salesman
can follow to minimize total weight of the trip.
Raises
------
NetworkXError
If `G` is not complete, the algorithm raises an exception.
Examples
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> cycle = approx.greedy_tsp(G, source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
['D', 'C', 'B', 'A', 'D']
>>> cost
31
Notes
-----
This implementation of a greedy algorithm is based on the following:
- The algorithm adds a node to the solution at every iteration.
- The algorithm selects a node not already in the cycle whose connection
to the previous node adds the least cost to the cycle.
A greedy algorithm does not always give the best solution.
However, it can construct a first feasible solution which can
be passed as a parameter to an iterative improvement algorithm such
as Simulated Annealing, or Threshold Accepting.
Time complexity: It has a running time $O(|V|^2)$
"""
# Check that G is a complete graph
N = len(G) - 1
# This check ignores selfloops which is what we want here.
if any(len(nbrdict) - (n in nbrdict) != N for n, nbrdict in G.adj.items()):
raise nx.NetworkXError("G must be a complete graph.")
if source is None:
source = nx.utils.arbitrary_element(G)
if G.number_of_nodes() == 2:
neighbor = next(G.neighbors(source))
return [source, neighbor, source]
nodeset = set(G)
nodeset.remove(source)
cycle = [source]
next_node = source
while nodeset:
nbrdict = G[next_node]
next_node = min(nodeset, key=lambda n: nbrdict[n].get(weight, 1))
cycle.append(next_node)
nodeset.remove(next_node)
cycle.append(cycle[0])
return cycle
[docs]@py_random_state(9)
def simulated_annealing_tsp(
G,
init_cycle,
weight="weight",
source=None,
temp=100,
move="1-1",
max_iterations=10,
N_inner=100,
alpha=0.01,
seed=None,
):
"""Returns an approximate solution to the traveling salesman problem.
This function uses simulated annealing to approximate the minimal cost
cycle through the nodes. Starting from a suboptimal solution, simulated
annealing perturbs that solution, occasionally accepting changes that make
the solution worse to escape from a locally optimal solution. The chance
of accepting such changes decreases over the iterations to encourage
an optimal result. In summary, the function returns a cycle starting
at `source` for which the total cost is minimized. It also returns the cost.
The chance of accepting a proposed change is related to a parameter called
the temperature (annealing has a physical analogue of steel hardening
as it cools). As the temperature is reduced, the chance of moves that
increase cost goes down.
Parameters
----------
G : Graph
`G` should be a complete weighted undirected graph.
The distance between all pairs of nodes should be included.
init_cycle : list of all nodes or "greedy"
The initial solution (a cycle through all nodes returning to the start).
This argument has no default to make you think about it.
If "greedy", use `greedy_tsp(G, weight)`.
Other common starting cycles are `list(G) + [next(iter(G))]` or the final
result of `simulated_annealing_tsp` when doing `threshold_accepting_tsp`.
weight : string, optional (default="weight")
Edge data key corresponding to the edge weight.
If any edge does not have this attribute the weight is set to 1.
source : node, optional (default: first node in list(G))
Starting node. If None, defaults to ``next(iter(G))``
temp : int, optional (default=100)
The algorithm's temperature parameter. It represents the initial
value of temperature
move : "1-1" or "1-0" or function, optional (default="1-1")
Indicator of what move to use when finding new trial solutions.
Strings indicate two special built-in moves:
- "1-1": 1-1 exchange which transposes the position
of two elements of the current solution.
The function called is :func:`swap_two_nodes`.
For example if we apply 1-1 exchange in the solution
``A = [3, 2, 1, 4, 3]``
we can get the following by the transposition of 1 and 4 elements:
``A' = [3, 2, 4, 1, 3]``
- "1-0": 1-0 exchange which moves an node in the solution
to a new position.
The function called is :func:`move_one_node`.
For example if we apply 1-0 exchange in the solution
``A = [3, 2, 1, 4, 3]``
we can transfer the fourth element to the second position:
``A' = [3, 4, 2, 1, 3]``
You may provide your own functions to enact a move from
one solution to a neighbor solution. The function must take
the solution as input along with a `seed` input to control
random number generation (see the `seed` input here).
Your function should maintain the solution as a cycle with
equal first and last node and all others appearing once.
Your function should return the new solution.
max_iterations : int, optional (default=10)
Declared done when this number of consecutive iterations of
the outer loop occurs without any change in the best cost solution.
N_inner : int, optional (default=100)
The number of iterations of the inner loop.
alpha : float between (0, 1), optional (default=0.01)
Percentage of temperature decrease in each iteration
of outer loop
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
Returns
-------
cycle : list of nodes
Returns the cycle (list of nodes) that a salesman
can follow to minimize total weight of the trip.
Raises
------
NetworkXError
If `G` is not complete the algorithm raises an exception.
Examples
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> cycle = approx.simulated_annealing_tsp(G, "greedy", source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
['D', 'C', 'B', 'A', 'D']
>>> cost
31
>>> incycle = ["D", "B", "A", "C", "D"]
>>> cycle = approx.simulated_annealing_tsp(G, incycle, source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
['D', 'C', 'B', 'A', 'D']
>>> cost
31
Notes
-----
Simulated Annealing is a metaheuristic local search algorithm.
The main characteristic of this algorithm is that it accepts
even solutions which lead to the increase of the cost in order
to escape from low quality local optimal solutions.
This algorithm needs an initial solution. If not provided, it is
constructed by a simple greedy algorithm. At every iteration, the
algorithm selects thoughtfully a neighbor solution.
Consider $c(x)$ cost of current solution and $c(x')$ cost of a
neighbor solution.
If $c(x') - c(x) <= 0$ then the neighbor solution becomes the current
solution for the next iteration. Otherwise, the algorithm accepts
the neighbor solution with probability $p = exp - ([c(x') - c(x)] / temp)$.
Otherwise the current solution is retained.
`temp` is a parameter of the algorithm and represents temperature.
Time complexity:
For $N_i$ iterations of the inner loop and $N_o$ iterations of the
outer loop, this algorithm has running time $O(N_i * N_o * |V|)$.
For more information and how the algorithm is inspired see:
http://en.wikipedia.org/wiki/Simulated_annealing
"""
if move == "1-1":
move = swap_two_nodes
elif move == "1-0":
move = move_one_node
if init_cycle == "greedy":
# Construct an initial solution using a greedy algorithm.
cycle = greedy_tsp(G, weight=weight, source=source)
if G.number_of_nodes() == 2:
return cycle
else:
cycle = list(init_cycle)
if source is None:
source = cycle[0]
elif source != cycle[0]:
raise nx.NetworkXError("source must be first node in init_cycle")
if cycle[0] != cycle[-1]:
raise nx.NetworkXError("init_cycle must be a cycle. (return to start)")
if len(cycle) - 1 != len(G) or len(set(G.nbunch_iter(cycle))) != len(G):
raise nx.NetworkXError("init_cycle should be a cycle over all nodes in G.")
# Check that G is a complete graph
N = len(G) - 1
# This check ignores selfloops which is what we want here.
if any(len(nbrdict) - (n in nbrdict) != N for n, nbrdict in G.adj.items()):
raise nx.NetworkXError("G must be a complete graph.")
if G.number_of_nodes() == 2:
neighbor = next(G.neighbors(source))
return [source, neighbor, source]
# Find the cost of initial solution
cost = sum(G[u][v].get(weight, 1) for u, v in pairwise(cycle))
count = 0
best_cycle = cycle.copy()
best_cost = cost
while count <= max_iterations and temp > 0:
count += 1
for i in range(N_inner):
adj_sol = move(cycle, seed)
adj_cost = sum(G[u][v].get(weight, 1) for u, v in pairwise(adj_sol))
delta = adj_cost - cost
if delta <= 0:
# Set current solution the adjacent solution.
cycle = adj_sol
cost = adj_cost
if cost < best_cost:
count = 0
best_cycle = cycle.copy()
best_cost = cost
else:
# Accept even a worse solution with probability p.
p = math.exp(-delta / temp)
if p >= seed.random():
cycle = adj_sol
cost = adj_cost
temp -= temp * alpha
return best_cycle
[docs]@py_random_state(9)
def threshold_accepting_tsp(
G,
init_cycle,
weight="weight",
source=None,
threshold=1,
move="1-1",
max_iterations=10,
N_inner=100,
alpha=0.1,
seed=None,
):
"""Returns an approximate solution to the traveling salesman problem.
This function uses threshold accepting methods to approximate the minimal cost
cycle through the nodes. Starting from a suboptimal solution, threshold
accepting methods perturb that solution, accepting any changes that make
the solution no worse than increasing by a threshold amount. Improvements
in cost are accepted, but so are changes leading to small increases in cost.
This allows the solution to leave suboptimal local minima in solution space.
The threshold is decreased slowly as iterations proceed helping to ensure
an optimum. In summary, the function returns a cycle starting at `source`
for which the total cost is minimized.
Parameters
----------
G : Graph
`G` should be a complete weighted undirected graph.
The distance between all pairs of nodes should be included.
init_cycle : list or "greedy"
The initial solution (a cycle through all nodes returning to the start).
This argument has no default to make you think about it.
If "greedy", use `greedy_tsp(G, weight)`.
Other common starting cycles are `list(G) + [next(iter(G))]` or the final
result of `simulated_annealing_tsp` when doing `threshold_accepting_tsp`.
weight : string, optional (default="weight")
Edge data key corresponding to the edge weight.
If any edge does not have this attribute the weight is set to 1.
source : node, optional (default: first node in list(G))
Starting node. If None, defaults to ``next(iter(G))``
threshold : int, optional (default=1)
The algorithm's threshold parameter. It represents the initial
threshold's value
move : "1-1" or "1-0" or function, optional (default="1-1")
Indicator of what move to use when finding new trial solutions.
Strings indicate two special built-in moves:
- "1-1": 1-1 exchange which transposes the position
of two elements of the current solution.
The function called is :func:`swap_two_nodes`.
For example if we apply 1-1 exchange in the solution
``A = [3, 2, 1, 4, 3]``
we can get the following by the transposition of 1 and 4 elements:
``A' = [3, 2, 4, 1, 3]``
- "1-0": 1-0 exchange which moves an node in the solution
to a new position.
The function called is :func:`move_one_node`.
For example if we apply 1-0 exchange in the solution
``A = [3, 2, 1, 4, 3]``
we can transfer the fourth element to the second position:
``A' = [3, 4, 2, 1, 3]``
You may provide your own functions to enact a move from
one solution to a neighbor solution. The function must take
the solution as input along with a `seed` input to control
random number generation (see the `seed` input here).
Your function should maintain the solution as a cycle with
equal first and last node and all others appearing once.
Your function should return the new solution.
max_iterations : int, optional (default=10)
Declared done when this number of consecutive iterations of
the outer loop occurs without any change in the best cost solution.
N_inner : int, optional (default=100)
The number of iterations of the inner loop.
alpha : float between (0, 1), optional (default=0.1)
Percentage of threshold decrease when there is at
least one acceptance of a neighbor solution.
If no inner loop moves are accepted the threshold remains unchanged.
seed : integer, random_state, or None (default)
Indicator of random number generation state.
See :ref:`Randomness<randomness>`.
Returns
-------
cycle : list of nodes
Returns the cycle (list of nodes) that a salesman
can follow to minimize total weight of the trip.
Raises
------
NetworkXError
If `G` is not complete the algorithm raises an exception.
Examples
--------
>>> from networkx.algorithms import approximation as approx
>>> G = nx.DiGraph()
>>> G.add_weighted_edges_from({
... ("A", "B", 3), ("A", "C", 17), ("A", "D", 14), ("B", "A", 3),
... ("B", "C", 12), ("B", "D", 16), ("C", "A", 13),("C", "B", 12),
... ("C", "D", 4), ("D", "A", 14), ("D", "B", 15), ("D", "C", 2)
... })
>>> cycle = approx.threshold_accepting_tsp(G, "greedy", source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
['D', 'C', 'B', 'A', 'D']
>>> cost
31
>>> incycle = ["D", "B", "A", "C", "D"]
>>> cycle = approx.threshold_accepting_tsp(G, incycle, source="D")
>>> cost = sum(G[n][nbr]["weight"] for n, nbr in nx.utils.pairwise(cycle))
>>> cycle
['D', 'C', 'B', 'A', 'D']
>>> cost
31
Notes
-----
Threshold Accepting is a metaheuristic local search algorithm.
The main characteristic of this algorithm is that it accepts
even solutions which lead to the increase of the cost in order
to escape from low quality local optimal solutions.
This algorithm needs an initial solution. This solution can be
constructed by a simple greedy algorithm. At every iteration, it
selects thoughtfully a neighbor solution.
Consider $c(x)$ cost of current solution and $c(x')$ cost of
neighbor solution.
If $c(x') - c(x) <= threshold$ then the neighbor solution becomes the current
solution for the next iteration, where the threshold is named threshold.
In comparison to the Simulated Annealing algorithm, the Threshold
Accepting algorithm does not accept very low quality solutions
(due to the presence of the threshold value). In the case of
Simulated Annealing, even a very low quality solution can
be accepted with probability $p$.
Time complexity:
It has a running time $O(m * n * |V|)$ where $m$ and $n$ are the number
of times the outer and inner loop run respectively.
For more information and how algorithm is inspired see:
https://doi.org/10.1016/0021-9991(90)90201-B
See Also
--------
simulated_annealing_tsp
"""
if move == "1-1":
move = swap_two_nodes
elif move == "1-0":
move = move_one_node
if init_cycle == "greedy":
# Construct an initial solution using a greedy algorithm.
cycle = greedy_tsp(G, weight=weight, source=source)
if G.number_of_nodes() == 2:
return cycle
else:
cycle = list(init_cycle)
if source is None:
source = cycle[0]
elif source != cycle[0]:
raise nx.NetworkXError("source must be first node in init_cycle")
if cycle[0] != cycle[-1]:
raise nx.NetworkXError("init_cycle must be a cycle. (return to start)")
if len(cycle) - 1 != len(G) or len(set(G.nbunch_iter(cycle))) != len(G):
raise nx.NetworkXError("init_cycle is not all and only nodes.")
# Check that G is a complete graph
N = len(G) - 1
# This check ignores selfloops which is what we want here.
if any(len(nbrdict) - (n in nbrdict) != N for n, nbrdict in G.adj.items()):
raise nx.NetworkXError("G must be a complete graph.")
if G.number_of_nodes() == 2:
neighbor = list(G.neighbors(source))[0]
return [source, neighbor, source]
# Find the cost of initial solution
cost = sum(G[u][v].get(weight, 1) for u, v in pairwise(cycle))
count = 0
best_cycle = cycle.copy()
best_cost = cost
while count <= max_iterations:
count += 1
accepted = False
for i in range(N_inner):
adj_sol = move(cycle, seed)
adj_cost = sum(G[u][v].get(weight, 1) for u, v in pairwise(adj_sol))
delta = adj_cost - cost
if delta <= threshold:
accepted = True
# Set current solution the adjacent solution.
cycle = adj_sol
cost = adj_cost
if cost < best_cost:
count = 0
best_cycle = cycle.copy()
best_cost = cost
if accepted:
threshold -= threshold * alpha
return best_cycle