Tsp#

class Tsp(graph)[ソース]#

ベースクラス: GraphOptimizationApplication

Optimization application for the 「traveling salesman problem」 [1] based on a NetworkX graph.

参照

[1]: 「Travelling salesman problem」, https://en.wikipedia.org/wiki/Travelling_salesman_problem

パラメータ:

graph (Graph | ndarray | List) – A graph representing a problem. It can be specified directly as a NetworkX graph, or as an array or list format suitable to build out a NetworkX graph.

Attributes

graph#

Getter of the graph

戻り値:

A graph for a problem

Methods

static create_random_instance(n, low=0, high=100, seed=None)[ソース]#

Create a random instance of the traveling salesman problem

パラメータ:
  • n (int) – the number of nodes.

  • low (int) – The minimum value for the coordinate of a node.

  • high (int) – The maximum value for the coordinate of a node.

  • seed (int) – the seed for the random coordinates.

戻り値:

A Tsp instance created from the input information

戻り値の型:

Tsp

draw(result=None, pos=None)#

Draw a graph with the result. When the result is None, draw an original graph without colors.

パラメータ:
interpret(result)[ソース]#

Interpret a result as a list of node indices

パラメータ:

result (OptimizationResult | ndarray) – The calculated result of the problem

戻り値:

A list of nodes whose indices correspond to its order in a prospective cycle.

戻り値の型:

List[int | List[int]]

static parse_tsplib_format(filename)[ソース]#

Read a graph in TSPLIB format from file and return a Tsp instance.

パラメータ:

filename (str) – the name of the file.

例外:
戻り値:

A Tsp instance data.

戻り値の型:

Tsp

static sample_most_likely(state_vector)#

Compute the most likely binary string from state vector.

パラメータ:

state_vector (QuasiDistribution | Statevector | ndarray | Dict) – state vector or counts or quasi-probabilities.

戻り値:

binary string as numpy.ndarray of ints.

例外:

ValueError – if state_vector is not QuasiDistribution, Statevector, np.ndarray, or dict.

戻り値の型:

ndarray

to_quadratic_program()[ソース]#

Convert a traveling salesman problem instance into a QuadraticProgram

戻り値:

The QuadraticProgram created from the traveling salesman problem instance.

戻り値の型:

QuadraticProgram

static tsp_value(z, adj_matrix)[ソース]#

Compute the TSP value of a solution. :param z: list of cities. :param adj_matrix: adjacency matrix.

戻り値:

value of the total length

戻り値の型:

float