Test problems¶
ypea.problems has ready-made problems from engineering design, combinatorial optimization and allocation, and readers for the standard instance libraries. Each is a vectorized ypea.Problem with its constraints, its best-known value as optimum when known, and its origin in info["source"].
Engineering design¶
Six design problems in their most cited formulations, all but gear_train constrained. Each has its best-known feasible solution in info["x_opt"].
| Function | Variables | Constraints | Best-known f | Source |
|---|---|---|---|---|
pressure_vessel() |
2 integer thicknesses, 2 reals | 4 | 6059.714335 | Sandgren 1990, Kannan and Kramer 1994 |
pressure_vessel(mixed=False) |
4 reals | 4 | 5885.332774 | the continuous version |
welded_beam() |
4 reals | 7 | 1.724852 | Coello 2000 |
spring() |
3 reals | 4 | 0.012665233 | Belegundu 1982, Arora 1989 |
speed_reducer() |
7 reals | 11 | 2994.471066 | Golinski 1973, Mezura-Montes and Coello 2005 |
three_bar_truss() |
2 reals | 3 | 263.8958434 | Nowacki 1973, Ray and Saini 2001 |
gear_train() |
4 integers | none | 2.700857e-12 | Sandgren 1990 |
The example solves the welded beam problem with L-SHADE and prints the best value, whether it meets all constraints, and the best-known value:
import ypea
problem = ypea.problems.welded_beam()
result = ypea.LSHADE(max_nfe=20_000).solve(problem, seed=1)
print(result.best.obj_value, result.best.feasible, problem.optimum)
1.7248523085973648 True 1.7248523085975365
problem = ypea.problems.welded_beam();
result = ypea.LSHADE(max_nfe=20000).solve(problem, seed=1);
fprintf("%.16g %d %.16g\n", result.best.obj_value, result.best.feasible, problem.optimum);
1.724852308597365 1 1.724852308597536
Each docstring gives the formulas, the bounds and the variants found in the literature.
Combinatorial problems¶
| Function | Variable | Notes |
|---|---|---|
tsp(coords=None, distances=None) |
Permutation("tour", n) |
Euclidean distances from coordinates, or any (also asymmetric) distance matrix |
random_tsp(n, seed) |
Permutation("tour", n) |
cities placed at random in a square |
circle_tsp(n=30, radius=40, seed=None) |
Permutation("tour", n) |
n cities evenly spaced on a circle, in shuffled order. The optimum is the perimeter of the polygon. |
knapsack(values, weights, capacity) |
Binary("x", n) |
0/1 knapsack, or multidimensional when weights is a matrix (maximization) |
integer_knapsack(values, weights, capacity, upper) |
Integer("x", n) |
each item may be packed up to upper[i] times |
small_knapsack() |
Binary("x", 20) |
a small 0/1 knapsack with 20 items and capacity 20 (optimum 73) |
mknap1() |
Binary("x", n) |
the 7 multidimensional knapsack problems of OR-Library file mknap1 (Petersen 1967) |
qap(flow, distance) |
Permutation("p", n) |
quadratic assignment, with the cost of QAPLIB |
nqueens(n=8) |
Permutation("q", n) |
the number of queen pairs on a common diagonal (optimum 0) |
The example places 12 queens with SA and its native permutation moves (see Native operators). A value of 0 means no two queens attack each other, and q[i] is the row of the queen in column i:
import ypea
problem = ypea.problems.nqueens(12)
sa = ypea.SA(max_nfe=20_000, initial_temp=1, final_temp=0.01, space="native")
result = sa.solve(problem, seed=1)
print(result.best.obj_value, result.best.solution.q)
0.0 [ 8 1 3 6 0 9 11 4 10 7 5 2]
Differs from Python: rows count from 1, and MATLAB finds its own solution.
problem = ypea.problems.nqueens(12);
sa = ypea.SA(max_nfe=20000, initial_temp=1, final_temp=0.01, space="native");
result = sa.solve(problem, seed=1);
disp(result.best.obj_value)
disp(result.best.solution.q)
0
7 3 11 6 1 9 12 5 2 8 10 4
plot_tour(problem, result) draws the best tour of a TSP with coordinates (needs matplotlib).
random_tsp(50, seed=2): a random tour (left) and the tour found by GA(max_nfe=50_000, space="native") with seed 1 (right), drawn with plot_tour.Allocation and assignment¶
| Function | Variables | Notes |
|---|---|---|
cvrp(coords, demands, capacity, n_vehicles) |
Permutation("route", n + k - 1) |
capacitated vehicle routing. Node 0 is the depot. Values n and above separate the routes. |
gap(cost, resource, capacity, sense="min") |
Integer("machine", n) |
generalized assignment of n jobs to m machines with capacities |
orlib_gap(index=1) |
Integer("machine", 15) |
problem c515-index of OR-Library gap1 (5 machines, 15 jobs, maximization) |
hub_location(flows, costs, p, alpha) |
Selection("hubs", n, p) and Integer("alloc", n) |
single allocation p-hub median |
cab(n=25, p=2, alpha=0.2) |
as above | the CAB data set of O'Kelly (1987), from OR-Library |
transportation(cost, supply, demand) |
FixedSum("x", (m, n), target=supply, axis=1) |
every source ships all of its supply, and the demands are constraints |
cvrp_routes(problem, solution) turns a CVRP solution into its list of routes, and hub_allocation(hubs, alloc) gives the hub of every node. plot_routes(problem, solution) draws the routes.
The example solves the first OR-Library generalized assignment problem with GA in the native space. It prints the name, the best value, whether it is feasible, and the known optimum:
import ypea
problem = ypea.problems.orlib_gap(1)
ga = ypea.GA(max_nfe=20_000, mutation_prob=0.5, space="native")
result = ga.solve(problem, seed=1)
print(problem.name, result.best.obj_value, result.best.feasible, problem.optimum)
c515-1 325.0 True 336
Differs from Python: MATLAB draws its own random numbers, so the value differs from Python's.
problem = ypea.problems.orlib_gap(1);
ga = ypea.GA(max_nfe=20000, mutation_prob=0.5, space="native");
result = ga.solve(problem, seed=1);
fprintf("%s %g %d %g\n", problem.name, result.best.obj_value, result.best.feasible, problem.optimum);
c515-1 324 1 336
Tightly constrained assignment problems are hard for general methods, so give them a larger budget or several runs.
Instance files¶
| Reader | Library | Returns |
|---|---|---|
read_tsplib(path), read_tsplib_tour(path) |
TSPLIB (TSP and ATSP) | a TSP problem, with the optimum of the known instances in TSPLIB_OPTIMA, and an optimal tour (zero-based) |
tsplib_distances(coords, kind) |
TSPLIB | the distance matrix for an edge weight type: EUC_2D, EUC_3D, MAN_2D, MAN_3D, MAX_2D, MAX_3D, CEIL_2D, ATT or GEO |
read_qaplib(path), read_qaplib_solution(path) |
QAPLIB | a QAP problem (optima in QAPLIB_OPTIMA), and a solution (cost, p) |
read_orlib_mknap(path) |
OR-Library mknap | a list of multidimensional knapsack problems |
read_orlib_gap(path, sense="max") |
OR-Library gap | a list of GAP problems (optima in ORLIB_GAP_OPTIMA) |
read_orlib_cab(path) |
OR-Library phub | the flows and costs of the CAB data |
read_cvrplib(path) |
CVRPLIB | a CVRP problem |
Data sets¶
ypea.problems.port1() returns the Hang Seng portfolio data of OR-Library (Chang et al. 2000): the mean returns and the covariance matrix of 31 assets, and 2000 points of the unconstrained efficient frontier. The portfolio example uses it.
Sources and licenses¶
Five OR-Library files ship unchanged with the package: mknap1, gap1, phub4 (the CAB data), port1 and portef1. OR-Library (J. E. Beasley) is under the MIT license, and the notices in the package data folder give the full text and the papers to cite.
TSPLIB, QAPLIB and CVRPLIB give no clear redistribution terms, so their files are not included. Download the instances and use the readers above.