API
Travelling Salesman Problem with Profits library
Alpha
Bases: IntEnum
Ratio between profit/cost limit and profit/cost of TSP solution
Source code in tspwplib/types.py
304 305 306 307 |
|
BaseTSP
Bases: BaseModel
A pydantic model for tsplib95.
Each field is validated with type hinting.
Source code in tspwplib/problem.py
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 |
|
__add_edges(graph)
Add edges from edge data
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Input graph |
required |
Source code in tspwplib/problem.py
411 412 413 414 415 416 417 418 |
|
__set_edge_attributes(graph)
Set edge attributes for 'weight' and 'is_fixed'
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Input graph |
required |
Source code in tspwplib/problem.py
420 421 422 423 424 425 426 427 428 |
|
__set_graph_attributes(graph)
Set graph attributes such as 'name' and 'comment'
Source code in tspwplib/problem.py
389 390 391 392 393 394 395 396 |
|
__set_node_attributes(graph)
Set node attributes
Source code in tspwplib/problem.py
398 399 400 401 402 403 404 405 406 407 408 409 |
|
from_dataframes(name, comment, problem_type, edges_df, nodes_df, capacity=None, display_data=None, display_data_type=DisplayDataType.NO_DISPLAY, edge_weight_format=EdgeWeightFormat.FULL_MATRIX)
classmethod
Get a TSP base model from edge and node dataframes
Notes
Essential edge columns: [source, target, weight]. Optional edge columns: [is_fixed]. Essential node columns: [node, is_depot]. Optional node columns: [x, y, z, demand]. The edge weight function is explicitly given by the 'weight' column.
Source code in tspwplib/problem.py
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 |
|
from_networkx(name, comment, problem_type, G, capacity=None, display_data=None, display_data_type=DisplayDataType.NO_DISPLAY, edge_weight_format=EdgeWeightFormat.FULL_MATRIX, weight_attr_name='weight')
classmethod
Get a base TSP model from a networkx graph
Source code in tspwplib/problem.py
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 |
|
from_tsplib95(problem)
classmethod
Get a TSP base model from a StandardProblem object
Source code in tspwplib/problem.py
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 |
|
from_yaml(yaml_filepath)
classmethod
Load from a yaml file
Source code in tspwplib/problem.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
get_graph()
Get a networkx graph
Returns:
Type | Description |
---|---|
Graph
|
Undirected networkx graph with node attributes such as 'is_depot' |
Graph
|
and edge attributes such as 'weight' and 'is_fixed'. |
Source code in tspwplib/problem.py
430 431 432 433 434 435 436 437 438 439 440 441 442 |
|
get_weighted_full_matrix()
Get a square weighted adjacency matrix, sorted by node ID
Source code in tspwplib/problem.py
377 378 379 380 381 382 383 384 385 386 387 |
|
to_tsplib95()
Convert to a tsplib95 standard model
Source code in tspwplib/problem.py
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 |
|
to_yaml(yaml_filepath)
Dump the TSP to a YAML file
Source code in tspwplib/problem.py
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 |
|
EdgeFunctionName
Bases: StrEnumMixin
, str
, Enum
Valid names of functions on edges
Source code in tspwplib/types.py
128 129 130 131 132 |
|
EdgeNotFoundException
Bases: NetworkXException
An edge could not be found in the graph
Source code in tspwplib/exception.py
6 7 |
|
EdgeWeightType
Bases: StrEnumMixin
, str
, Enum
Specifies how the edge weights (or distances) are given
Source code in tspwplib/types.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
EdgesNotAdjacentException
Bases: AmbiguousSolution
An edge list has been given non-adjacent edges which is ambiguous
Source code in tspwplib/exception.py
10 11 |
|
Generation
Bases: StrEnumMixin
, str
, Enum
Generations of TSPwP problem instances
Source code in tspwplib/types.py
296 297 298 299 300 301 |
|
GraphName
Bases: StrEnumMixin
, str
, Enum
Names of TSPlib instances
Source code in tspwplib/types.py
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 |
|
LondonaqGraphName
Bases: StrEnumMixin
, str
, Enum
Names of graphs with London air quality forecasts
Source code in tspwplib/types.py
251 252 253 254 255 256 257 258 |
|
LondonaqLocation
Bases: StrEnumMixin
, str
, Enum
Names of locations that the London air quality graph is centered upon
Source code in tspwplib/types.py
276 277 278 279 280 281 282 283 |
|
LondonaqLocationShort
Bases: StrEnumMixin
, str
, Enum
Short codes for londonaq locations
Source code in tspwplib/types.py
286 287 288 289 290 291 292 293 |
|
LondonaqTimestamp
Bases: Enum
Timestamps of the forecasts for London air quality forecasts
Source code in tspwplib/types.py
261 262 263 264 |
|
LondonaqTimestampId
Bases: StrEnumMixin
, str
, Enum
Timestamp IDs for CLI
Source code in tspwplib/types.py
267 268 269 270 |
|
NotSimpleCycleException
Bases: NotSimpleException
The walk was not a simple cycle
Source code in tspwplib/exception.py
18 19 |
|
NotSimpleException
Bases: NetworkXException
A path, cycle or walk is not simple
Source code in tspwplib/exception.py
14 15 |
|
NotSimplePathException
Bases: NotSimpleException
The walk was not a simple path
Source code in tspwplib/exception.py
22 23 |
|
OptimalSolutionTSP
Bases: IntEnum
Value of optimal solutions to TSP instances
Source code in tspwplib/types.py
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 |
|
ProfitsProblem
Bases: StandardProblem
TSP with Profits Problem
Source code in tspwplib/problem.py
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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
|
__set_edge_attributes(graph, names)
Set edge attributes
Source code in tspwplib/problem.py
500 501 502 503 504 505 506 507 |
|
__set_graph_attributes(graph)
Set attributes of the graph such as the name
Source code in tspwplib/problem.py
509 510 511 512 513 514 515 516 |
|
__set_node_attributes(graph, names)
Add node attributes
Source code in tspwplib/problem.py
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
|
get_cost_limit()
Get the cost limit for a TSP with Profits problem
Returns:
Type | Description |
---|---|
int
|
Cost limit |
Source code in tspwplib/problem.py
613 614 615 616 617 618 619 |
|
get_edges(normalize=False)
Get a list of edges in the graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalize |
bool
|
If true use the normalized vertex ids |
False
|
Returns:
Type | Description |
---|---|
EdgeList
|
List of edges in the graph |
Source code in tspwplib/problem.py
664 665 666 667 668 669 670 671 672 673 674 675 |
|
get_graph(normalize=False)
Return a networkx graph instance representing the problem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalize |
bool
|
rename nodes to be zero-indexed |
False
|
Source code in tspwplib/problem.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 |
|
get_node_score()
Get the node scores (profits)
Returns:
Type | Description |
---|---|
VertexLookup
|
Mapping from node to node score (profit) |
Source code in tspwplib/problem.py
621 622 623 624 625 626 627 628 629 630 |
|
get_quota(alpha)
The quota is alpha percent of the total prize
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
int
|
Percent of the total prize |
required |
Returns:
Type | Description |
---|---|
int
|
quota |
Source code in tspwplib/problem.py
594 595 596 597 598 599 600 601 602 603 |
|
get_root_vertex(normalize=False)
Get the root vertex
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalize |
bool
|
If true, vertices start at index 0 |
False
|
Returns:
Type | Description |
---|---|
Vertex
|
The first depot in the list |
Raises:
Type | Description |
---|---|
ValueError
|
If the list of depots is empty |
Source code in tspwplib/problem.py
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
|
get_total_prize()
Get the sum of prize over all vertices
Returns:
Type | Description |
---|---|
int
|
Total prize |
Source code in tspwplib/problem.py
585 586 587 588 589 590 591 592 |
|
get_tsp_optimal_value()
Get the value of the optimal solution to TSP
Returns:
Type | Description |
---|---|
int
|
TSP optimal value |
Source code in tspwplib/problem.py
632 633 634 635 636 637 638 |
|
get_weight(start, end)
Return the weight of the edge between start and end.
This method provides a single way to obtain edge weights regardless of whether the problem uses an explicit matrix or a distance function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start |
int
|
starting node index |
required |
end |
int
|
ending node index |
required |
Returns:
Type | Description |
---|---|
int
|
Weight of edge |
Notes
Over-rides euclidean weight to ensure the costs are metric
Source code in tspwplib/problem.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
number_of_nodes()
Get the number of nodes in the problem
Returns:
Type | Description |
---|---|
int
|
Number of nodes in graph |
Source code in tspwplib/problem.py
605 606 607 608 609 610 611 |
|
VertexFunctionName
Bases: StrEnumMixin
, str
, Enum
Valid names of functions on vertices
Source code in tspwplib/types.py
121 122 123 124 125 |
|
asymmetric_from_directed(G)
Create asymmetric directed graph from directed graph
Split every node u two nodes u1 and u2. We add a directed arc between u1 and u2. Any previous inward edge v->u is now v->u1 and any outward edge from u is now u2->v.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
DiGraph
|
Directed graph |
required |
Returns:
Type | Description |
---|---|
DiGraph
|
Directed asymmetric graph |
Source code in tspwplib/converter.py
38 39 40 41 42 43 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 |
|
asymmetric_from_undirected(G)
Create asymmetric directed graph from undirected graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Undirected graph |
required |
Returns:
Type | Description |
---|---|
DiGraph
|
Directed asymmetric graph |
Source code in tspwplib/converter.py
95 96 97 98 99 100 101 102 103 104 105 |
|
biggest_vertex_id_from_graph(G)
Return the vertex with the largest integer id
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Graph |
required |
Returns:
Type | Description |
---|---|
Vertex
|
Vertex with biggest id |
Source code in tspwplib/converter.py
108 109 110 111 112 113 114 115 116 117 |
|
build_path_to_londonaq_instance(londonaq_root, name)
Build a filepath to a londonaq instance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
londonaq_root |
Path
|
Root directory of the londonaq dataset |
required |
name |
LondonaqGraphName
|
Londonaq graph name |
required |
Returns:
Type | Description |
---|---|
Path
|
Filepath to the londonaq txt |
Source code in tspwplib/utils.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
build_path_to_londonaq_yaml(londonaq_root, name)
Build a filepath to the londonaq yaml file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
londonaq_root |
Path
|
Root directory of the londonaq dataset |
required |
name |
LondonaqGraphName
|
Londonaq graph name |
required |
Returns:
Type | Description |
---|---|
Path
|
Filepath to the londonaq yaml file |
Source code in tspwplib/utils.py
21 22 23 24 25 26 27 28 29 30 31 |
|
build_path_to_oplib_instance(oplib_root, generation, name, alpha=Alpha.fifty.value)
Build a filepath to a oplib instance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
oplib_root |
Path
|
The directory of the clones oplib |
required |
generation |
Generation
|
Generation of OPLib instance |
required |
name |
GraphName
|
Graph instance name |
required |
alpha |
int
|
Percent of the total cost to set the cost limit to. Not useful for instances of Prize-collecting TSPs. Default is 50. Note if you change to a different value, make sure the file exists |
value
|
Returns:
Type | Description |
---|---|
Path
|
Path to the OPLib instance |
Source code in tspwplib/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
build_path_to_tsplib_instance(tsplib_root, name)
Build a filepath to a tsplib instance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tsplib_root |
Path
|
Directory containing TSP txt instances |
required |
name |
GraphName
|
Name of the instance |
required |
Returns:
Type | Description |
---|---|
Path
|
Filepath to the TSP instance |
Source code in tspwplib/utils.py
74 75 76 77 78 79 80 81 82 83 84 85 |
|
edge_list_from_walk(walk)
Get ordered list of edges from an ordered list of vertices
Parameters:
Name | Type | Description | Default |
---|---|---|---|
walk |
VertexList
|
Ordered list of vertices that represent a walk in the graph |
required |
Returns:
Type | Description |
---|---|
EdgeList
|
List of edges in the same order as the walk |
Source code in tspwplib/walk.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
get_original_from_split_vertex(biggest_vertex, split_vertex)
Return the original vertex id given a split vertex (may be head or tail)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
split_vertex |
Vertex
|
A split vertex in asymmetric graph |
required |
Returns:
Type | Description |
---|---|
Vertex
|
ID of the vertex in the original graph |
Source code in tspwplib/converter.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
get_original_path_from_split_path(biggest_vertex, split_path)
Get the path in the original graph given a path of split vertices in the asymmetric graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
split_path |
VertexList
|
A path of split vertices in the asymmetric directed graph |
required |
Returns:
Type | Description |
---|---|
VertexList
|
A path of vertices in the original graph |
Source code in tspwplib/converter.py
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 |
|
get_quota_from_alpha(alpha, total_prize)
The quota is alpha percent of the total prize
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
int
|
Percent of the total prize |
required |
total_prize |
int
|
Total prize of the graph |
required |
Returns:
Type | Description |
---|---|
int
|
quota as an integer |
Source code in tspwplib/problem.py
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
|
head_prize(prize)
Get the prize of the split head
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prize |
int
|
The prize of a vertex |
required |
Returns Split head prize
Source code in tspwplib/converter.py
235 236 237 238 239 240 241 242 243 244 245 246 |
|
is_complete(G)
Check if the graph is complete
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Simple graph |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the graph is complete, false otherwise |
Note
Assumes no self loops
Source code in tspwplib/complete.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
is_complete_with_self_loops(G)
Check if the graph is complete, and every vertex has a self loop
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Simple graph |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the graph is complete, false otherwise |
Source code in tspwplib/complete.py
25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
is_pctsp_yes_instance(graph, quota, root_vertex, edge_list)
Returns true if the list of edges is a solution to the instance of the Prize collecting Travelling Salesman Problem.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Undirected graph with cost function on edges and prize function on vertices |
required |
quota |
int
|
The salesman must collect at least the quota in prize money |
required |
root_vertex |
Vertex
|
Start and finish vertex of the tour |
required |
edge_list |
EdgeList
|
Edges in the solution of the instance |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the total prize of the tour is at least the quota and the tour is a simple |
bool
|
cycle that starts and ends at the root vertex. False otherwise. |
Source code in tspwplib/problem.py
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 |
|
is_simple_cycle(G, cycle)
Is the cycle simple in the graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
input graph |
required |
cycle |
VertexList
|
Ordered sequence of vertices |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the cycle is simple in the graph |
Source code in tspwplib/walk.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
|
is_simple_path(G, path)
Is the path simple in the graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
input graph |
required |
path |
VertexList
|
Ordered sequence of vertices |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the path is simple in the graph |
Source code in tspwplib/walk.py
266 267 268 269 270 271 272 273 274 275 276 |
|
is_split_vertex_pair(biggest_vertex, tail, head)
Does the arc (tail, head) represent a split vertex in the original graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
tail |
Vertex
|
Tail of edge in directed graph |
required |
head |
Vertex
|
Head of edge in directed graph |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the arc (tail, head) represents a split vertex in the original graph |
Source code in tspwplib/converter.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
is_vertex_split_head(biggest_vertex, split_vertex)
Is the vertex a head in the asymmetric graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
split_vertex |
Vertex
|
A potential head of an edge in directed graph |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the vertex is a head |
Source code in tspwplib/converter.py
196 197 198 199 200 201 202 203 204 205 206 |
|
is_vertex_split_tail(biggest_vertex, vertex)
Is the vertex a tail in the asymmetric graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
vertex |
Vertex
|
A potential tail of an edge in directed graph |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the vertex is a tail |
Source code in tspwplib/converter.py
183 184 185 186 187 188 189 190 191 192 193 |
|
is_walk(G, walk)
Is the walk a sequence of adjacent vertices in the graph?
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
input graph |
required |
walk |
VertexList
|
Ordered sequence of vertices |
required |
Returns:
Type | Description |
---|---|
bool
|
True if all vertices are adjacent in the graph |
Source code in tspwplib/walk.py
232 233 234 235 236 237 238 239 240 241 242 243 |
|
londonaq_comment(location_id, timestamp_id)
Get a comment for a londonaq dataset
Source code in tspwplib/utils.py
119 120 121 122 123 124 125 |
|
londonaq_graph_name(location_id, timestamp_id)
Get a londonaq graph name
Source code in tspwplib/utils.py
112 113 114 115 116 |
|
metricness(graph, cost_attr='cost')
Measures how metric a cost function is
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Must be undirected, connected and not a tree |
required |
cost_attr |
str
|
Name of cost attribute |
'cost'
|
Returns:
Type | Description |
---|---|
float
|
If a cost function is metric, return 1.0. |
float
|
If n-1 edges are metric and the remaining edges are non-metric, return 0.0. |
Raises:
Type | Description |
---|---|
NotConnectedException
|
If the graph is not connected |
NoTreesException
|
If the graph is a tree |
Notes
Self loops are ignored from the metricness
Source code in tspwplib/metric.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
mst_cost(G, cost_attr='cost')
Find the minimum spanning tree of G. The cost of edges in the tree remains unchanged. The cost of edges not in the tree is equal to the cost of the minimum spanning tree plus the original cost of the edges.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Undirected, simple graph |
required |
cost_attr |
str
|
Name of the cost attribute of edges |
'cost'
|
Returns A new cost function
Source code in tspwplib/metric.py
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 |
|
order_edge_list(unordered_edges)
Given a list of unordered edges, return an ordered edge list such that every two adjacent edge in the list are also adjacent in the input graph.
Note that the list of edges should form a simple path or cycle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unordered_edges |
EdgeList
|
List of unique edges in no particular order |
required |
Returns:
Type | Description |
---|---|
EdgeList
|
List of unique edges that are adjacent in the graph |
Raises:
Type | Description |
---|---|
NotSimpleException
|
If the list of edges is not a simple path or cycle |
Source code in tspwplib/walk.py
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 |
|
remove_self_loops_from_edge_list(edge_list)
Return a new edge list with no self loops
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list |
EdgeList
|
List of edges |
required |
Returns:
Type | Description |
---|---|
EdgeList
|
Edge list with no self loops |
Source code in tspwplib/walk.py
353 354 355 356 357 358 359 360 361 362 |
|
rename_edge_attributes(graph, renaming, copy_graph=False, del_old_attr=False)
Rename edge attributes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Networkx graph |
required |
renaming |
Dict[str, str]
|
Keys are current attribute names. Values are new attribute names. |
required |
copy_graph |
bool
|
If true, copy the graph before renaming attributes. |
False
|
del_old_attr |
bool
|
If true, delete the old edge attribute. |
False
|
Returns:
Type | Description |
---|---|
Graph
|
Graph with renamed attributes. If |
Graph
|
Otherwise the original graph is returned. |
Source code in tspwplib/utils.py
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 |
|
rename_node_attributes(graph, renaming, copy_graph=False, del_old_attr=False)
Rename node attributes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Networkx graph |
required |
renaming |
Dict[str, str]
|
Keys are current attribute names. Values are new attribute names. |
required |
copy_graph |
bool
|
If true, copy the graph before renaming attributes. |
False
|
del_old_attr |
bool
|
If true, delete the old node attribute. |
False
|
Returns:
Type | Description |
---|---|
Graph
|
Graph with renamed attributes. If |
Graph
|
Otherwise the original graph is returned. |
Source code in tspwplib/utils.py
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 |
|
reorder_edge_list_from_root(edge_list, root)
Reorder a list of edges such that the root vertex is in the first (and last) edge
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list |
EdgeList
|
List of unique, adjacent edges |
required |
root |
Vertex
|
Root vertex |
required |
Returns:
Type | Description |
---|---|
EdgeList
|
List of edges. The first (and last) edge will contain the root vertex. |
Raises:
Type | Description |
---|---|
NodeNotFound
|
If the root vertex is not in any edges. |
Source code in tspwplib/walk.py
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 |
|
semi_mst_cost(G, cost_attr='cost', seed=0)
Half of the non-MST-tree edges are left unchanged. The other half are assigned cost as described in mst_cost.
The half of edges are chosen with uniform and independent probability.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Undirected, simple graph |
required |
cost_attr |
str
|
Name of the cost attribute of edges |
'cost'
|
seed |
int
|
Set the seed of the random number generator |
0
|
Returns A new cost function
Source code in tspwplib/metric.py
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 |
|
sparsify_by_cost(G, kappa, cost_attr='cost', seed=0, remove_self_loops=False)
Given vertex i, remove an edge e=(i,j) with probability P[i,j] where the probability function is weighted according to the cost function:
$P[i,j] = c(i,j) / C_i$
where $C_i$ is the total cost of all edges adjacent to vertex i.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Graph |
required |
kappa |
int
|
Parameter independent of the input size |
required |
cost_attr |
str
|
Name of the cost attribute on edges |
'cost'
|
remove_self_loops |
bool
|
Should self loops have a change of being removed? |
False
|
seed |
int
|
Set the random seed for reproducibility of graphs |
0
|
Returns:
Type | Description |
---|---|
Graph
|
A deep copy of the original graph with edges removed |
Source code in tspwplib/sparse.py
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 |
|
sparsify_uid(G, kappa, remove_self_loops=False, seed=0)
Remove edges with uniform and independent (uid) probability until the number of edges equals kappa * number of nodes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
Graph
|
Graph |
required |
kappa |
int
|
Parameter independent of the input size |
required |
remove_self_loops |
bool
|
Should self loops have a change of being removed? |
False
|
seed |
int
|
Set the random seed |
0
|
Returns:
Type | Description |
---|---|
Graph
|
Graph with kappa * V edges where V is the number of nodes |
Notes
A copy of the graph is made and returned. The original graph is unedited.
Source code in tspwplib/sparse.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
split_graph_from_properties(edge_properties, edge_attr_to_split='cost', edge_attr_to_vertex='length', new_vertex_attr='prize', old_edge_attr='old_edge')
Split edges with properties and create undirected simple graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_properties |
EdgeProperties
|
Keys are edges. Values are dicts of edge attributes. |
required |
edge_attr_to_split |
str
|
Name of edge attribute. Assign half the value to each split edge. |
'cost'
|
edge_attr_to_vertex |
str
|
Name of edge attribute. Assign edge value to a new vertex attribute. |
'length'
|
new_vertex_attr |
str
|
Name of the newly created vertex attribute. |
'prize'
|
old_edge_attr |
str
|
Name of the newly created attribute for the old edge ID. |
'old_edge'
|
Returns:
Type | Description |
---|---|
Graph
|
Undirected simple graph with edge attributes for cost, prize and old_edge |
Notes
To get the original_edge that a split edge represents, access the 'old_edge' attribute
Source code in tspwplib/converter.py
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 |
|
split_head(biggest_vertex, original_vertex)
Get the split head of the vertex
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
original_vertex |
Vertex
|
Vertex in the original graph |
required |
Returns:
Type | Description |
---|---|
Vertex
|
New split vertex that is a head of all arcs in the asymmetric graph |
Source code in tspwplib/converter.py
209 210 211 212 213 214 215 216 217 218 219 |
|
split_tail(biggest_vertex, original_vertex)
Get the split tail of the vertex
Parameters:
Name | Type | Description | Default |
---|---|---|---|
biggest_vertex |
Vertex
|
The vertex with the biggest id in the original graph |
required |
original_vertex |
Vertex
|
Vertex in the original graph |
required |
Returns:
Type | Description |
---|---|
Vertex
|
New split vertex that is a tail of all arcs in the asymmetric graph |
Source code in tspwplib/converter.py
222 223 224 225 226 227 228 229 230 231 232 |
|
tail_prize(prize)
Get the prize of the split tail
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prize |
int
|
The prize of a vertex |
required |
Returns Split tail prize
Source code in tspwplib/converter.py
249 250 251 252 253 254 255 256 257 258 |
|
to_simple_undirected(G)
Given an undirected multigraph, multi edges to create a simple undirected graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
G |
MultiGraph
|
Undirected networkx multi graph. |
required |
Returns:
Type | Description |
---|---|
Graph
|
Undirected networkx simple graph with no multi edges. |
Notes
Assumes the vertex ids are integers.
Source code in tspwplib/converter.py
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 |
|
to_vertex_dataframe(graph)
Convert graph vertices to pandas dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Input graph |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pandas dataframe with vertex set as index |
Source code in tspwplib/converter.py
23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
total_cost(costs, edges)
Total cost of edges
Parameters:
Name | Type | Description | Default |
---|---|---|---|
costs |
EdgeFunction
|
Mapping from edges to costs |
required |
edges |
EdgeList
|
List of edges |
required |
Returns:
Type | Description |
---|---|
int
|
Total cost of edges |
Source code in tspwplib/walk.py
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 |
|
total_cost_networkx(graph, walk)
Get the total cost of edges in a walk of the networkx graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph |
Graph
|
Undirected input graph with cost attribute |
required |
walk |
VertexList
|
A sequence of adjacent vertices |
required |
Returns:
Type | Description |
---|---|
int
|
Total cost of edges in the walk |
Source code in tspwplib/walk.py
338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
total_prize(prizes, vertices)
Total prize of vertices
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prizes |
Mapping[Vertex, int]
|
A mapping from vertices to prizes, e.g. dict, property map |
required |
vertices |
Iterable[Vertex]
|
List of vertices in the prizes map |
required |
Returns:
Type | Description |
---|---|
int
|
Total prize of vertices |
Source code in tspwplib/walk.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
|
total_prize_of_tour(prizes, tour)
Total prize of unique vertices in the tour
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prizes |
Mapping[Vertex, int]
|
A mapping from vertices to prizes, e.g. dict, property map |
required |
tour |
VertexList
|
List of vertices in the prizes map |
required |
Returns:
Type | Description |
---|---|
int
|
Total prize of the tour |
Source code in tspwplib/walk.py
295 296 297 298 299 300 301 302 303 304 305 |
|
uniform_random_cost(edge_list, min_value=1, max_value=100, seed=0)
Generate a cost function for each edge drawn from a uniform and independant probability
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list |
SimpleEdgeList
|
List of edges in graph |
required |
min_value |
int
|
Minimum value the cost can take (inclusive) |
1
|
max_value |
int
|
Maximum value the cost can take (inclusive) |
100
|
seed |
int
|
Set the seed of the random number generator |
0
|
Returns:
Type | Description |
---|---|
SimpleEdgeFunction
|
Edge cost function |
Source code in tspwplib/metric.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
vertex_set_from_edge_list(edge_list)
Get a set of vertices from a list of edges
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list |
EdgeList
|
List of edges |
required |
Returns:
Type | Description |
---|---|
Set[Vertex]
|
Set of vertices in the edge list |
Source code in tspwplib/walk.py
41 42 43 44 45 46 47 48 49 50 |
|
walk_from_edge_list(edge_list)
Get a walk from a list of unique, adjacent edges
Parameters:
Name | Type | Description | Default |
---|---|---|---|
edge_list |
EdgeList
|
List of unique edges that are adjacent in the graph |
required |
Returns:
Type | Description |
---|---|
VertexList
|
List of vertices in walk of edges |
Raises:
Type | Description |
---|---|
EdgesNotAdjacentException
|
When two edges in the walk are not adjacent |
Source code in tspwplib/walk.py
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 |
|