@File : init.py @Time : 2023/10/30 12:35:54 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2023, Alejandro Marrero @Desc : None
BPP
Bases: Problem
Bin Packing Problem
Source code in digneapy/domains/bpp.py
26 27 28 29 30 31 32 33 34 35 36 37 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 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 | |
__array__(dtype=np.int32, copy=None)
Return a NumPy array representation of the Bin Packing Problem.
The representation stores the capacity first and then all the items.
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
156 157 158 159 160 161 162 163 164 165 166 | |
__call__(individual)
Evaluates the candidate individual with the information of the Bin Packing.
The fitness of the solution is the amount of unused space, as well as the number of bins for a specific solution. Falkenauer (1998) performance metric defined as: (x) = \frac{\sum_{k=1}^{N} \left(\frac{fill_k}{C}\right)^2}{N}
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
__init__(items, maximum_capacity, seed=None, *args, **kwargs)
Creates a new Bin Packing Problem (BPP) object
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/bpp.py
29 30 31 32 33 34 35 36 37 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 | |
create_solution()
Creates a random BPP solution
The solution is created with variables equal to [0, dimension]. Which means that each item is stored in a independent bin. Also, the number of objectives is set to 1.
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
evaluate(individual)
Evaluates the candidate individual with the information of the Bin Packing.
The fitness of the solution is the amount of unused space, as well as the number of bins for a specific solution. Falkenauer (1998) performance metric defined as: (x) = \frac{\sum_{k=1}^{N} \left(\frac{fill_k}{C}\right)^2}{N}
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
from_file(filename)
classmethod
Loads a BPP problem from a file
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
to_file(filename='instance.bpp')
Saves the BPP problem to a file
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/bpp.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
to_instance()
Generates an Instance with the information of the Problem
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
231 232 233 234 235 236 237 238 | |
BPPDomain
Bases: Domain
Source code in digneapy/domains/bpp.py
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 | |
__init__(number_of_items=50, minimum_weight=np.uint32(1), maximum_weight=np.uint32(1000), maximum_capacity=np.uint32(100), capacity_approach='fixed', capacity_ratio=0.8, seed=None)
Bin Packing Problem Domain
Creates a new domain to generate instances for the Bin Packing Problem (BPP).
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
extract_features(instances)
Extract the features of the instance based on the BPP domain.
For the BPP domain, the features consist of: - N as the number of items, - Capacity as the maximum capacity of each bin, MeanWeights of the items, - MedianWeights of the items, VarianceWeights of the weights of the items, - MaxWeight of the items in the instance, MinWeight of the items, - Huge as the ratio of items with normalised weights > 0.5, - Large as the ratio of items with normalised weights between 0.333 and 0.5, - Medium as the ratio of items with normalised weights between 0.25 and 0.333, - Small as the ratio of items with normalised weights >= 0.25, - Tiny as the ratio of items with normalised weights >= 0.1
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
extract_features_as_dict(instances)
Creates a dictionary with the features of the instances.
The key are the names of each feature and the values are the values extracted from instance. For the BPP domain, the features consist of: - N as the number of items, - Capacity as the maximum capacity of each bin, MeanWeights of the items, - MedianWeights of the items, VarianceWeights of the weights of the items, - MaxWeight of the items in the instance, MinWeight of the items, - Huge as the ratio of items with normalised weights > 0.5, - Large as the ratio of items with normalised weights between 0.333 and 0.5, - Medium as the ratio of items with normalised weights between 0.25 and 0.333, - Small as the ratio of items with normalised weights >= 0.25, - Tiny as the ratio of items with normalised weights >= 0.1
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
generate_instances(n=np.uint32(1))
Generates N new instances for the BPP domain.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.py
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 | |
generate_problems_from_instances(instances)
Generates BPP problems from the given instances
This method is used to generate a collection of (objects) of the BPP class ready to be solved from the definition of the instances.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/bpp.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 | |
Knapsack
Bases: Problem
Representation of a 0/1 Knapsack Problem.
Each item contributes a profit and consumes a weight. A solution is encoded as a binary vector where each entry indicates whether the corresponding item is selected.
The objective rewards profit while penalizing solutions that exceed the assigned capacity.
Source code in digneapy/domains/kp.py
27 28 29 30 31 32 33 34 35 36 37 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 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 | |
bounds
property
Return the binary bounds for every decision variable in the problem.
__array__(dtype=np.uint32, copy=None)
Return a NumPy array representation of the Knapsack Problem.
The representation stores the capacity first and then alternates weight/profit pairs for each item, which is convenient for serialization and downstream processing.
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
__call__(individual)
Evaluate a candidate solution and compute its objective value.
The score is the total profit of the selected items minus a penalty for any excess weight beyond the knapsack capacity. This makes infeasible solutions receive a lower fitness than feasible ones.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
__init__(capacity, profits, weights, seed=None, penalty_factor=100.0, *args, **kwargs)
Create a new knapsack problem from the given profit/weight data.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/kp.py
37 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 | |
__len__()
Return the number of items defined by the problem.
Source code in digneapy/domains/kp.py
184 185 186 187 | |
__str__()
Return a compact string representation of the problem instance.
Source code in digneapy/domains/kp.py
179 180 181 182 | |
create_solution()
Create a random initial solution for the Knapsack Problem.
The returned solution is a binary vector that can be used as a starting point for an optimizer, although it may be infeasible if the selected items exceed the capacity.
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
189 190 191 192 193 194 195 196 197 198 199 200 201 | |
evaluate(individual)
Evaluate a candidate solution and compute its objective value.
The score is the total profit of the selected items minus a penalty for any excess weight beyond the knapsack capacity. This makes infeasible solutions receive a lower fitness than feasible ones.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
from_file(filename)
classmethod
Load a Knapsack Problem from a text file.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
get_bounds_at(i)
Return the valid bounds for one decision variable.
Each item is handled as a binary decision: selecting it corresponds to value 1, while leaving it out corresponds to value 0.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
to_file(filename='instance.kp')
Stores the Knapsack Problem in a plain text file.
The file format contains the number of items and the capacity on the first line, followed by one row per item containing its weight and profit.
| Parameters: |
|
|---|
Source code in digneapy/domains/kp.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
to_instance()
Convert the Knapsack Problem into an Instance object used by Digneapy.
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
245 246 247 248 249 250 251 252 253 254 255 256 | |
KnapsackDomain
Bases: Domain
Knapsack Domain for synthesizing Knapsack Problem instances.
This class allows to create benchmark instances by sampling item weights and profits and then assigning a capacity using one of several strategies. Note that the number of dimensions defined produces instances of N = dimension items. Which means that the results Instance objects will have 2 * dimension + 1 variables: - Q, w_0, p_0, w_1, p_1, ..., w_N-1, p_N-1
It also provides utilities to extract descriptive features and build concrete Knapsack problems from the generated data.
Source code in digneapy/domains/kp.py
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 | |
capacity_approach
property
Return the strategy currently used to assign capacities to generated instances.
capacity_ratio
property
Returns the ratio to which the capacity is update when using percentage approach
__init__(number_of_items=np.uint32(50), minimum_weight=np.uint32(1), maximum_weight=np.uint32(1000), minimum_profit=np.uint32(1), maximum_profit=np.uint32(1000), maximum_capacity=np.uint32(100000.0), capacity_approach='evolved', capacity_ratio=0.8, seed=None)
Create a domain that can generate knapsack instances with configurable difficulty.
| Parameters: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
extract_features(instances)
Compute a compact set of numerical features for the supplied instances.
These features summarize the Knapsack instance structure, they include: - Capacity - Maximum profit - Maximum weight - Minimum profit - Minimum weight - Average efficiency as the average ratio of profits / weights - Mean of the values (both profits and weights) - Standard deviation of the values (both profits and weights)
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
extract_features_as_dict(instances)
Return the extracted features as dictionaries.
These features summarize the Knapsack instance structure, they include: - Capacity - Maximum profit - Maximum weight - Minimum profit - Minimum weight - Average efficiency as the average ratio of profits / weights - Mean of the values (both profits and weights) - Standard deviation of the values (both profits and weights)
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
generate_instances(n=np.uint32(1))
Generate a batch of knapsack instances.
The method samples item weights and profits for each instance and then assigns a capacity according to the selected strategy. This creates instances with varying levels of difficulty and tightness.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
generate_problems_from_instances(instances)
Create Knapsack Problem objects from the given instances.
This method converts the numerical representation of each instance into a fully functional Knapsack Problem that can be passed directly to a solver.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/kp.py
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 | |
Sphere
Bases: Problem
Minimises the shifted sphere: f(x) = Σ (xᵢ − centerᵢ)²
Fitness is returned as −f(x) so that higher is better, matching the maximisation convention used throughout digneapy.
Source code in digneapy/domains/sphere.py
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 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 | |
__call__(individual)
Alias for evaluate — makes SphereProblem directly callable.
Source code in digneapy/domains/sphere.py
62 63 64 | |
create_solution()
Creates a random feasible solution.
Source code in digneapy/domains/sphere.py
50 51 52 53 54 55 56 57 58 59 60 | |
evaluate(individual)
Returns (−sphere_value,) so higher fitness = closer to centre.
Source code in digneapy/domains/sphere.py
42 43 44 45 46 47 48 | |
to_file(filename)
Persists the problem centre to a plain text file.
Source code in digneapy/domains/sphere.py
70 71 72 | |
to_instance()
Converts this problem back to an Instance (center as variables).
Source code in digneapy/domains/sphere.py
66 67 68 | |
SphereDomain
Bases: Domain
Domain of 2-D shifted sphere problems.
Each instance is a 2-D point (x₀, x₁) that serves as the centre of a sphere. The two coordinates are also the features used as descriptors, so they map directly onto a 2-D GridArchive.
| Parameters: |
|
|---|
Source code in digneapy/domains/sphere.py
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 | |
extract_features(instances)
Returns the raw instance coordinates as features.
Shape: (n_instances, dimension). For a 2-D domain these directly populate a 2-D GridArchive.
Source code in digneapy/domains/sphere.py
131 132 133 134 135 136 137 138 139 140 141 142 | |
extract_features_as_dict(instances)
Returns a list of {feat_name: value} dicts, one per instance.
Source code in digneapy/domains/sphere.py
144 145 146 147 148 149 150 151 | |
generate_instances(n=np.uint32(1))
Generates n random sphere centre points as Instance objects.
Source code in digneapy/domains/sphere.py
115 116 117 118 119 120 121 122 123 | |
generate_problems_from_instances(instances)
Creates one SphereProblem per instance (using variables as centre).
Source code in digneapy/domains/sphere.py
125 126 127 128 129 | |
TSP
Bases: Problem
Representation of the Symmetric Travelling Salesman Problem (TSP).
Given a set of cities, each described by a pair of 2D coordinates, the objective is to find the shortest possible tour that visits every city exactly once and returns to the starting city. This implementation uses the Euclidean distance between coordinate pairs as the inter-city travel cost.
A candidate solution is encoded as a sequence of city indices of length N + 1, where N is the number of cities. The first and last elements must both be 0 (the depot / starting city), and every city index from 1 to N-1 must appear exactly once in between.
The objective value is the reciprocal of the total tour length (1 / distance), so higher values correspond to shorter, better tours. Infeasible tours are those that violate the cyclic constraint (meaning that the start and end of the tour should be the node number 0) or those that visit a node more than once.
Source code in digneapy/domains/tsp.py
26 27 28 29 30 31 32 33 34 35 36 37 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 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 | |
__array__(dtype=np.float64, copy=None)
Return a NumPy array representation of the TSP instance.
The returned array is the (N, 2) coordinate matrix, where each row stores
the [x, y] position of one city. This is useful for serialisation and
for passing the instance to downstream NumPy-based tools.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
__call__(individual)
Evaluate a candidate tour and compute its objective value.
Delegates directly to :meth:evaluate. This makes the problem instance
callable, allowing it to be used wherever a plain function is expected
(e.g. as an argument to a solver).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
__init__(number_of_nodes, coords, penalty_factor=10.0, postpone_dist_comp=False, save_distances_as_1d=False, seed=None, *args, **kwargs)
Create a new Symmetric Travelling Salesman Problem instance.
The full Euclidean distance matrix between all pairs of cities is pre-computed during initialisation so that repeated evaluations are fast.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
create_solution(random=False, start_node=0)
Create a trivial initial solution for the TSP.
The solution visits cities in natural order: 0 → 1 → 2 → … → N-1 → 0. This is a feasible but almost certainly non-optimal tour that can be used as a starting point for local search or population initialisation.
| Parameters: |
|
|---|
Returns:
Solution: A Solution object whose variables encode the tour,
with zeroed objectives and constraints arrays.
Source code in digneapy/domains/tsp.py
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 | |
evaluate(individual)
Evaluate a candidate tour and compute its objective value.
The fitness of a feasible solution is the reciprocal of the total Euclidean
tour length (1.0 / distance). This formulation turns the minimisation
problem into a maximisation problem, which is consistent with the Digneapy
framework's convention. Fitness, always to maximise.
Infeasible tours—those that repeat cities are assigned a penalty proportional to the number of repetitions.
If the supplied individual is a Solution object, its fitness,
objectives, and constraints attributes are updated in-place.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
from_file(filename)
classmethod
Load a TSP instance from a text file produced by :meth:to_file.
The expected file format is:
- Line 1: number of cities.
- Line 2: blank separator.
- Lines 3+: one city per line as x<TAB>y.
| Parameters: |
|
|---|
| Raises: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
to_file(filename='instance.tsp')
Serialise the TSP instance to a plain text file.
The file will follow this format:
- Line 1: number of cities.
- Line 2: blank separator.
- Lines3+: one city per line as x<TAB>y.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
to_instance()
Convert the TSP problem into an Instance object used by Digneapy.
The instance variables are the coordinate array flattened into a
one-dimensional sequence: [x_0, y_0, x_1, y_1, …, x_{N-1}, y_{N-1}].
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
390 391 392 393 394 395 396 397 398 399 400 | |
TSPDomain
Bases: Domain
Domain for synthesising Symmetric TSP instances.
This class generates benchmark TSP instances by sampling city coordinates
uniformly within configurable rectangular bounds. It also provides utilities
for extracting a rich set of geometric and structural features from the
generated instances and for converting raw instance data back into
TSP problem objects ready for a solver.
Each generated Instance contains 2 * number_of_nodes variables arranged as
interleaved x/y coordinates: x_0, y_0, x_1, y_1, …, x_{N-1}, y_{N-1}.
The eleven descriptive features extracted by this domain are:
+-----+----------------------+-----------------------------------------------+
| # | Name | Description |
+=====+======================+===============================================+
| 0 | size | Number of cities (eq. number_of_nodes * 2)|
+-----+----------------------+-----------------------------------------------+
| 1 | std_distances | Standard deviation of all pairwise distances |
+-----+----------------------+-----------------------------------------------+
| 2 | centroid_x | x coordinate of the geometric centroid |
+-----+----------------------+-----------------------------------------------+
| 3 | centroid_y | y coordinate of the geometric centroid |
+-----+----------------------+-----------------------------------------------+
| 4 | radius | Mean distance from each city to the centroid |
+-----+----------------------+-----------------------------------------------+
| 5 | fraction_distances | Fraction of unique pairwise distances |
+-----+----------------------+-----------------------------------------------+
| 6 | area | Bounding-box area of all city coordinates |
+-----+----------------------+-----------------------------------------------+
| 7 | variance_nnNds | Variance of normalised nearest-neighbour |
| | | distances (top-5) |
+-----+----------------------+-----------------------------------------------+
| 8 | variation_nnNds | Coefficient of variation of the normalised |
| | | nearest-neighbour distances |
+-----+----------------------+-----------------------------------------------+
| 9 | cluster_ratio | Ratio of DBSCAN clusters to number of cities |
+-----+----------------------+-----------------------------------------------+
| 10 | mean_cluster_radius | Mean radius of the DBSCAN-identified clusters |
+-----+----------------------+-----------------------------------------------+
Source code in digneapy/domains/tsp.py
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 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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 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 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
__init__(number_of_nodes=np.uint32(100), x_range=(0, 1000), y_range=(0, 1000), seed=None)
Create a new TSPDomain for generating Symmetric TSP instances.
| Parameters: |
|
|---|
| Raises: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
extract_features(instances)
Compute an eleven-dimensional feature vector for each supplied instance.
The features capture the geometric structure of the city layout and are intended for use in instance-space analysis and algorithm selection. All computations are performed in a vectorised, batch-friendly manner where possible; the DBSCAN-based cluster features require a per-instance loop.
Feature descriptions:
- size – Number of cities. Constant across all instances generated by the same domain, but included for completeness.
- std_distances – Standard deviation of all pairwise Euclidean distances, excluding self-distances. Captures the overall spread of city separations.
- centroid_x / centroid_y – The x and y coordinates of the geometric centroid (mean position of all cities).
- radius – Mean Euclidean distance from each city to the centroid. Indicates how tightly the cities cluster around their centre of mass.
- fraction_distances – The number of unique pairwise distances divided
by the total number of city pairs
N*(N-1)/2. Values close to 1 indicate that few distances are identical. - area – Bounding-box area, calculated as
(x_max - x_min) * (y_max - y_min). - variance_nnNds – Variance of the top-5 normalised nearest-neighbour distances (normalised by the maximum distance in the instance).
- variation_nnNds – Coefficient of variation (variance / mean) of the top-5 normalised nearest-neighbour distances.
- cluster_ratio – Ratio of the number of clusters found by DBSCAN to the total number of cities. A low ratio indicates dense clustering; a ratio close to 1 indicates that every city forms its own cluster.
- mean_cluster_radius – Average radius of the DBSCAN clusters, where each cluster radius is the mean distance of its member cities to the cluster centroid.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
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 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
extract_features_as_dict(instances)
Return the extracted features as a list of named dictionaries.
This is a convenience wrapper around :meth:extract_features that pairs
each numeric value with its human-readable feature name, making the
output easier to inspect, log, or pass to downstream tools that expect
labelled data (e.g. pandas.DataFrame).
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 | |
generate_instances(n=np.uint32(1))
Generate a batch of TSP instances by sampling city coordinates at random.
City x-coordinates are drawn uniformly from x_range and y-coordinates
from y_range. Each instance stores the coordinates as a flat vector of
length 2 * number_of_nodes in the interleaved form
[x_0, y_0, x_1, y_1, …, x_{N-1}, y_{N-1}].
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |
generate_problems_from_instances(instances)
Create TSP problem objects from a collection of raw instances.
Each instance is converted from its flat interleaved representation into
an (N, 2) coordinate array and wrapped in a TSP object.
The instances variables are expected to be in the interleaved format
[x_0, y_0, x_1, y_1, …] produced by :meth:generate_instances.
The number of cities is inferred as len(instance) // 2.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in digneapy/domains/tsp.py
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 | |