Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and improve TSP code #148

Merged
merged 6 commits into from
Dec 13, 2019
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions dwave_networkx/algorithms/tsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


@binary_quadratic_model_sampler(1)
def traveling_salesperson(G, sampler=None, lagrange=2, weight='weight',
def traveling_salesperson(G, sampler=None, lagrange=None, weight='weight',
start=None, **sampler_args):
"""Returns an approximate minimum traveling salesperson route.

Expand All @@ -57,7 +57,7 @@ def traveling_salesperson(G, sampler=None, lagrange=2, weight='weight',
sampler is provided, one must be provided using the
`set_default_sampler` function.

lagrange : number, optional (default 2)
lagrange : number, optional (default None)
Lagrange parameter to weight constraints (visit every city once)
versus objective (shortest distance route).

Expand Down Expand Up @@ -117,7 +117,7 @@ def traveling_salesperson(G, sampler=None, lagrange=2, weight='weight',
traveling_salesman = traveling_salesperson


def traveling_salesperson_qubo(G, lagrange=2, weight='weight'):
def traveling_salesperson_qubo(G, lagrange=None, weight='weight'):
"""Return the QUBO with ground states corresponding to a minimum TSP route.

If :math:`|G|` is the number of nodes in the graph, the resulting qubo will have:
Expand All @@ -130,7 +130,7 @@ def traveling_salesperson_qubo(G, lagrange=2, weight='weight'):
G : NetworkX graph
A complete graph in which each edge has a attribute giving its weight.

lagrange : number, optional (default 2)
lagrange : number, optional (default None)
Lagrange parameter to weight constraints (no edges within set)
versus objective (largest set possible).

Expand All @@ -148,6 +148,13 @@ def traveling_salesperson_qubo(G, lagrange=2, weight='weight'):
"""
N = G.number_of_nodes()

if lagrange is None:
# if no lagrange parameter provided, set to 'average' tour length
if G.number_of_edges()>0:
lagrange = G.size(weight=weight)*G.number_of_nodes()/G.number_of_edges()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add few words in the comment about why it makes sense to set this value for the parameter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

else:
lagrange = 2

# some input checking
if N in (1, 2) or len(G.edges) != N*(N-1)//2:
msg = "graph must be a complete graph with at least 3 nodes or empty"
Expand Down Expand Up @@ -211,4 +218,4 @@ def is_hamiltonian_path(G, route):

"""

return (len(route) == len(set(G)))
return (set(route) == set(G))