quast_decisiontree.problems.classes.tsp
quast_decisiontree.problems.classes.tsp
TSP problem framework.
logger
module-attribute
logger = logging.getLogger('dt_logger')
TSPBadState
Bases: Exception
raised if a TSP instance is found in an invalid state
Source code in src/quast_decisiontree/problems/classes/tsp.py
40 41 | |
TSP
Bases: OptimizationProblem
class representing an instance of the TSP problem
Source code in src/quast_decisiontree/problems/classes/tsp.py
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
direct_encoding_modes
class-attribute
instance-attribute
direct_encoding_modes = ('QUBO_condensed', 'QUBO')
distance_tol
class-attribute
instance-attribute
distance_tol = 1e-05
optimal_path_length
instance-attribute
optimal_path_length = None
graph
instance-attribute
graph = from_numpy_array(self.adjacency_matrix)
adjacency_matrix
property
writable
adjacency_matrix
num_nodes
property
num_nodes
positions
property
writable
positions
__init__
__init__(adjacency_matrix, positions=None)
Constructs a TSP instance from an adjacency matrix, possibly also setting a positions array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adjacency_matrix
|
ndarray
|
The adjacency matrix containing the distance between the cities of the TSP. |
required |
positions
|
Optional[ndarray]
|
An optional list of positions with two coordinates for each city. It is not necessary since the TSP is fully defined via the adjacency matrix. Positions will be used for plotting. Defaults to None. |
None
|
Source code in src/quast_decisiontree/problems/classes/tsp.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
tsp_value
staticmethod
tsp_value(city_list, adjacency_matrix)
Calculate the total path length for a given city ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
city_list
|
Sequence[int]
|
Sequence of city indices representing the tour order. |
required |
adjacency_matrix
|
ndarray
|
Distance matrix between cities. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Total round-trip path length. |
Source code in src/quast_decisiontree/problems/classes/tsp.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
__eq__
__eq__(other)
checks equality of the underlying graphs (understood as isomorphism respecting weights)
Source code in src/quast_decisiontree/problems/classes/tsp.py
120 121 122 123 124 125 126 127 128 129 130 | |
from_coordinate_list
classmethod
from_coordinate_list(coordinate_list)
Constructs a TSP instance from a coordinate list by inferring the adjacency matrix from the Euclidean distance of the coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinate_list
|
ndarray
|
A list of points with two coordinates each. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
TSP |
TSP
|
The TSP instance defined by the coordinate list. |
Source code in src/quast_decisiontree/problems/classes/tsp.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
create_random_instance
classmethod
create_random_instance(size, seed=None)
create a random TSP instance of the specified size.
Parameters: size how many nodes (cities) to create seed an optional seed for the random number generator
Returns: the newly created TSP instance
Source code in src/quast_decisiontree/problems/classes/tsp.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
display
display()
draws the problem graph of the TSP instance
Source code in src/quast_decisiontree/problems/classes/tsp.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
display_solution
display_solution(result)
Draws the TSP instance with a given solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Sequence
|
The sequence of cities to draw alongside the TSP instance. |
required |
Source code in src/quast_decisiontree/problems/classes/tsp.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
evaluate_objective
evaluate_objective(result)
returns the path length of a proposed path.
Accepts a one-hot bitstring or an integer city sequence in condensed or full form
(see :meth:_as_tour for the accepted conventions).
Source code in src/quast_decisiontree/problems/classes/tsp.py
267 268 269 270 271 272 273 274 | |
tsp_length
tsp_length(eigenstate, num_nodes, encoding='one-hot')
Calculates the expectation value of the path length for a given state. Infeasible basis states are ignored, for feasible ones the TSP length is calculated and added to a weighted average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eigenstate
|
Mapping
|
A dictionary of key : val pairs where the keys correspond to the bitstrings (computational basis states) and the vals may be amplitudes, probabilities or shot counts associated with the bitstrings. |
required |
num_nodes
|
int
|
The number of nodes in the associated TSP. |
required |
encoding
|
str
|
The encoding the bitstrings contained in the eigenstate come from. Defaults to "one-hot". |
'one-hot'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If an unknown encoding is given. |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The expectation value of the path length in the projection of the eigenstate on the feasible subspace. |
Source code in src/quast_decisiontree/problems/classes/tsp.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
convert_between_frames
classmethod
convert_between_frames(path)
Converts a path from the time-frame to the city-frame or vice versa.
Note: this only works if the given path is valid, i.e. all integers i in range(len(path)) occur only once.
Parameter: path list of integers
Returns: converted_path list of integers with switched interpretation
Source code in src/quast_decisiontree/problems/classes/tsp.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
formulate_qubo
formulate_qubo(penalty_factor=100, scaling_factor=1)
returns offset and qubo tensor for the TSP instance
Parameters: penalty_factor The factor penalizing the TSP constraints scaling_factor A generic scaling factor for the cost function
Returns: offset The constant contribution to the objective function qubo_tensor A qubo tensor encapsulating the interaction between the binary variables
Source code in src/quast_decisiontree/problems/classes/tsp.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
formulate_qubo_condensed
formulate_qubo_condensed(
penalty_factor=100, scaling_factor=1
)
returns offset and qubo tensor while removing the cyclic permutation freedom by fixing the first city to be visited first.
Args and return values as for formulate_qubo method.
Source code in src/quast_decisiontree/problems/classes/tsp.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 | |
decode_result
decode_result(sol_bitstring, mode='QUBO_condensed')
the inverse function for formulate_problem(). Takes a solution bitstring of the given formulation and converts it to an integer solution vector and the path length.
Source code in src/quast_decisiontree/problems/classes/tsp.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
formulate_problem
formulate_problem(
mode="QUBO_condensed",
penalty_factor=100,
scaling_factor=1,
)
returns an offset and QUBO tensor for the TSP instance
Source code in src/quast_decisiontree/problems/classes/tsp.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
to_dict
to_dict()
Converts the TSP instance to a dictionary.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with problem_class, distance_matrix, and optionally coordinate_list. |
Source code in src/quast_decisiontree/problems/classes/tsp.py
468 469 470 471 472 473 474 475 476 477 478 479 480 | |
from_dict
classmethod
from_dict(problem_dict)
constructs a TSP instance from a problem dictionary
Either the key "distance_matrix" or "coordinate_list" must be present.
Source code in src/quast_decisiontree/problems/classes/tsp.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
is_feasible
classmethod
is_feasible(
solution_string, num_nodes=None, encoding="one-hot"
)
Determines whether a solution bitstring represents a feasible solution.
For convenience, returns False if solution_string = None.
Source code in src/quast_decisiontree/problems/classes/tsp.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 | |
internal_loops
classmethod
internal_loops(x)
Returns the loops contained in a (candidate) TSP solution given in edge encoding
Source code in src/quast_decisiontree/problems/classes/tsp.py
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | |
get_ising_offset
get_ising_offset(penalty_factor=100, scaling_factor=1)
calculates the offset between QUBO and Ising formulation of the problem.
This is the analytic expression for the normal qubo formulation of TSP.
Source code in src/quast_decisiontree/problems/classes/tsp.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |