Test problems¶
Guide: Test problems.
Engineering design¶
Constrained engineering design problems.
Each function returns a vectorized Problem (minimization) in the most cited formulation,
with constraints g <= 0, the best-known value as optimum, and info holding the
best-known solution (x_opt), the variant and the source. The best-known points are stored with
full precision: each was computed on the active constraints of the formulation below, and it
rounds to the values published in the cited papers.
pressure_vessel ¶
pressure_vessel(mixed=True)
Pressure vessel design (Sandgren 1990, Kannan and Kramer 1994).
Minimize the cost of material, forming and welding of a cylindrical vessel with hemispherical heads. The design is the shell thickness Ts, the head thickness Th, the inner radius R and the length L of the cylinder:
f = 0.6224 Ts R L + 1.7781 Th R^2 + 3.1661 Ts^2 L + 19.84 Ts^2 R, subject to g1 = -Ts + 0.0193 R, g2 = -Th + 0.00954 R, g3 = -pi R^2 L - 4/3 pi R^3 + 1296000, g4 = L - 240, and 10 <= R, L <= 200.
mixed=True (default, the original problem): Ts and Th are integer multiples of 0.0625 inch,
given by Integer("thickness", 2, lb=1, ub=99) in those units, and Real("x") holds
(R, L). Best-known f = 6059.714335 at Ts = 0.8125, Th = 0.4375, R = 42.098446,
L = 176.636596.
mixed=False: the continuous version, with Real("x") = (Ts, Th, R, L) and
0 <= Ts, Th <= 99. Best-known f = 5885.332774 at (0.778169, 0.384649, 40.319619, 200),
often reported as 5885.3328.
References: E. Sandgren, "Nonlinear integer and discrete programming in mechanical design optimization", Journal of Mechanical Design 112 (1990) 223 to 229. B. K. Kannan and S. N. Kramer, "An augmented Lagrange multiplier based method for mixed integer discrete continuous optimization", Journal of Mechanical Design 116 (1994) 405 to 411.
welded_beam ¶
welded_beam()
Welded beam design, in the seven constraint version of Coello (2000).
Minimize the fabrication cost f = 1.10471 h^2 l + 0.04811 t b (14 + l) of a beam welded to a support, with weld thickness h, weld length l, bar height t and bar thickness b. The constraints bound the shear stress (tau <= 13600 psi), the bending stress (sigma <= 30000 psi), the end deflection (delta <= 0.25 inch) and the buckling load (P <= Pc), with h <= b, h >= 0.125 and a cost limit of 5. The polar moment of inertia is J = 2 (sqrt(2) h l (l^2 / 12 + ((h + t) / 2)^2)) and the buckling load is Pc = 4.013 E sqrt(t^2 b^6 / 36) / L^2 (1 - t / (2 L) sqrt(E / (4 G))), with P = 6000 lb, L = 14 inch, E = 30e6 psi and G = 12e6 psi. Bounds: 0.1 <= h, b <= 2 and 0.1 <= l, t <= 10.
Best-known f = 1.724852 at (h, l, t, b) = (0.205730, 3.470489, 9.036624, 0.205730). Other versions of this problem use other constants or constraints, with other best values.
Reference: C. A. C. Coello, "Use of a self adaptive penalty approach for engineering optimization problems", Computers in Industry 41 (2000) 113 to 127.
spring ¶
spring()
Tension/compression spring design (Belegundu 1982, Arora 1989).
Minimize the weight f = (N + 2) D d^2 of a spring with wire diameter d, mean coil diameter D and N active coils, subject to limits on deflection, shear stress, surge frequency and outer diameter (four constraints). Bounds: 0.05 <= d <= 2, 0.25 <= D <= 1.3, 2 <= N <= 15. N is continuous, as in most papers.
Best-known f = 0.012665233 at (d, D, N) = (0.051689, 0.356718, 11.288966). The rounded
point violates the first constraint by about 4e-6, the stored x_opt is feasible.
Reference: J. S. Arora, Introduction to Optimum Design, McGraw-Hill, 1989. A. D. Belegundu, "A study of mathematical programming methods for structural optimization", PhD thesis, University of Iowa, 1982.
speed_reducer ¶
speed_reducer()
Speed reducer design (Golinski 1973), with the eleven constraints of Mezura-Montes and Coello (2005).
Minimize the weight of a gear box. The variables are the face width x1, the tooth module x2, the number of pinion teeth x3 (continuous, as in most papers), the shaft lengths x4 and x5 between bearings, and the shaft diameters x6 and x7. The constraints bound the bending and surface stress of the teeth, the deflection and stress of the shafts, and the dimensions. Bounds: 2.6 <= x1 <= 3.6, 0.7 <= x2 <= 0.8, 17 <= x3 <= 28, 7.3 <= x4, x5 <= 8.3, 2.9 <= x6 <= 3.9, 5.0 <= x7 <= 5.5.
Best-known f = 2994.471066 at (3.5, 0.7, 17, 7.3, 7.715320, 3.350215, 5.286654). Some papers use 7.8 as the lower bound of x5, which makes this point infeasible.
References: J. Golinski, "An adaptive optimization system applied to machine synthesis", Mechanism and Machine Theory 8 (1973) 419 to 436. E. Mezura-Montes and C. A. C. Coello, "Useful infeasible solutions in engineering optimization with evolutionary algorithms", MICAI 2005, LNAI 3789, 652 to 662.
three_bar_truss ¶
three_bar_truss()
Three bar truss design (Nowacki 1973, Ray and Saini 2001).
Minimize the volume f = (2 sqrt(2) x1 + x2) l of a symmetric truss with bar cross sections x1 (outer bars) and x2 (middle bar), subject to three stress constraints, with l = 100 cm, P = 2 kN/cm^2 and sigma = 2 kN/cm^2. Bounds: 0 <= x1, x2 <= 1.
Best-known f = 263.8958434 at (0.7886751, 0.4082483).
Reference: T. Ray and P. Saini, "Engineering design optimization using a swarm with an intelligent information sharing among individuals", Engineering Optimization 33 (2001) 735 to 748.
gear_train ¶
gear_train()
Gear train design (Sandgren 1990).
Choose the numbers of teeth (Ta, Tb, Td, Tf) of four gears, integers from 12 to 60, so that the gear ratio Ta Tb / (Td Tf) is as close as possible to 1/6.931: f = (1/6.931 - Ta Tb / (Td Tf))^2. Only the bounds constrain the problem.
Best-known f = 2.700857e-12 at (16, 19, 43, 49), which is the global optimum (checked by enumeration). Swapping Ta with Tb, or Td with Tf, gives the same value.
Reference: E. Sandgren, "Nonlinear integer and discrete programming in mechanical design optimization", Journal of Mechanical Design 112 (1990) 223 to 229.
Traveling salesman¶
Traveling salesman problem (TSP).
A tour is a permutation of the cities (zero-based). Its length is the sum of the distances between consecutive cities, including the edge back from the last city to the first.
TSPLIB_OPTIMA
module-attribute
¶
TSPLIB_OPTIMA = {'att48': 10628, 'bayg29': 1610, 'bays29': 2020, 'berlin52': 7542, 'burma14': 3323, 'eil51': 426, 'eil76': 538, 'gr17': 2085, 'gr21': 2707, 'gr24': 1272, 'kroA100': 21282, 'pr76': 108159, 'st70': 675, 'ulysses16': 6859, 'ulysses22': 7013}
TourLength ¶
TourLength(distances)
Objective: length of the closed tour sol.tour on the distance matrix distances.
Works on one tour with shape (n,) and on a batch with shape (m, n).
tsp ¶
tsp(coords=None, distances=None, *, name='tsp', optimum=None, source=None)
A TSP from city coordinates or from a distance matrix.
Parameters
coords(array of shape (n, 2), optional): City coordinates. Withoutdistances, the distances are Euclidean. Withdistances, they are kept only for plotting.distances(array of shape (n, n), optional): Distance matrix. It may be asymmetric: the tour goes fromtour[i]totour[i + 1].name: stroptimum(float, optional): Known optimal tour length.source(str, optional): Where the instance comes from, stored ininfo["source"].
The variable is Permutation("tour", n) and the problem is vectorized.
random_tsp ¶
random_tsp(n, seed=None, *, size=100.0)
A TSP with n cities placed uniformly at random in a square of side size.
circle_tsp ¶
circle_tsp(n=30, *, radius=40.0, seed=None, center=(50.0, 50.0))
A TSP with n cities evenly spaced on a circle, in shuffled order.
The optimal tour follows the circle, so the optimum is the perimeter of the regular
polygon, 2 n radius sin(pi / n).
plot_tour ¶
plot_tour(problem, tour, ax=None, **kwargs)
Plot a closed tour on the city coordinates of problem (needs matplotlib).
tour is a permutation, a Solution with a tour attribute, or a Result.
Returns the axes.
Knapsack¶
Knapsack problems: 0/1, multidimensional and generalized integer.
Each problem maximizes the total value x @ values subject to weights @ x <= capacity.
The constraints are returned as g = weights @ x - capacity, satisfied when g <= 0.
KnapsackObjective ¶
KnapsackObjective(values, weights, capacity)
Objective: total value of sol.x, with one constraint per row of weights.
Works on one solution with shape (n,) and on a batch with shape (m, n).
knapsack ¶
knapsack(values, weights, capacity, *, name='knapsack', optimum=None, source=None)
A 0/1 knapsack problem, or a multidimensional one when weights is a matrix.
Parameters
values: array of shape (n,)weights: array of shape (n,), or (m, n) for m constraintscapacity: float, or array of shape (m,)optimum(float, optional): Known optimal value.
The variable is Binary("x", n), the sense is "max", and the problem is vectorized.
integer_knapsack ¶
integer_knapsack(values, weights, capacity, upper, *, name='integer_knapsack', optimum=None, source=None)
A generalized (bounded) knapsack: item i may be packed from 0 to upper[i] times.
The variable is Integer("x", n, lb=0, ub=upper). Otherwise as knapsack.
mknap1 ¶
mknap1()
The 7 multidimensional knapsack problems of OR-Library file mknap1 (Petersen 1967), bundled.
Quadratic assignment and n-Queens¶
Quadratic assignment problem (QAP).
Facility i goes to location p[i]. The cost is
sum over i, j of flow[i, j] * distance[p[i], p[j]], as in QAPLIB.
QAPLIB_OPTIMA
module-attribute
¶
QAPLIB_OPTIMA = {'chr12a': 9552, 'esc16a': 68, 'had12': 1652, 'nug12': 578, 'nug14': 1014, 'nug15': 1150, 'scr12': 31410, 'tai12a': 224416}
QAPCost ¶
QAPCost(flow, distance)
Objective: QAP cost of the assignment sol.p, for one (n,) or a batch (m, n).
qap ¶
qap(flow, distance, *, name='qap', optimum=None, source=None)
A QAP from a flow matrix and a distance matrix, both (n, n).
The variable is Permutation("p", n) and the problem is vectorized.
Vehicle routing¶
Capacitated vehicle routing problem (CVRP).
Node 0 is the depot and nodes 1 to n are the customers. Each of at most k vehicles leaves the depot, visits some customers and comes back. Every customer is visited once, and the demand served on one route must not exceed the vehicle capacity.
Encoding: one Permutation("route", n + k - 1). Values 0 to n - 1 are the customers (node
value + 1), and values n and above are separators between routes. Reading the permutation from
left to right gives up to k routes in their visiting order, and a route may be empty (fewer
vehicles used). This is the same code as Partition(n, k) with the order kept inside each
group, but it decodes to an array, so the problem is vectorized.
RouteCost ¶
RouteCost(distances, demands, capacity, n_vehicles)
Objective: total route length, with one capacity constraint per vehicle (load - capacity).
cvrp ¶
cvrp(coords=None, demands=None, capacity=None, n_vehicles=None, *, distances=None, name='cvrp', optimum=None, source=None)
A CVRP from node coordinates or a distance matrix.
Parameters
coords(array of shape (n + 1, 2), optional): Node coordinates, the depot first. Withoutdistances, distances are Euclidean.demands(array of shape (n + 1,)): Demand of each node. The depot's demand (the first value) must be 0.capacity(float): Vehicle capacity.n_vehicles(int): Number of vehicles k (the most routes a solution may have).distances: array of shape (n + 1, n + 1), optionalname,optimum,source: optional metadata. The coordinates go toinfo["coords"].
The variable is Permutation("route", n + k - 1), the objective is the total distance,
and there are k capacity constraints. Use cvrp_routes to get the routes of a solution.
cvrp_routes ¶
cvrp_routes(problem, solution)
The routes of a solution: a list of k arrays of node indices (customers are 1 to n).
Empty routes are kept, so route i is vehicle i. solution is a Solution or the
permutation itself.
read_cvrplib ¶
read_cvrplib(path, n_vehicles=None, *, rounding=True, optimum=None)
Read a CVRPLIB file (TSPLIB like .vrp format) and return the CVRP Problem.
Needs NODE_COORD_SECTION (EUC_2D) or an EXPLICIT edge weight section, DEMAND_SECTION and
DEPOT_SECTION with one depot. n_vehicles defaults to the -kN part of the name (for
example A-n32-k5), else to ceil(total demand / capacity). With rounding (default, the
CVRPLIB convention for EUC_2D), distances are rounded to the nearest integer. optimum
defaults to a value given in the COMMENT line ("Optimal value: 784"). In some sets the
-kN number is the fewest vehicles possible, and the best-known solution uses more
routes, so pass a larger n_vehicles when the instance needs it.
plot_routes ¶
plot_routes(problem, solution, ax=None)
Plot the routes of a CVRP solution (needs coordinates and matplotlib). Returns the axes.
Generalized assignment¶
Generalized assignment problem (GAP).
Assign each of n jobs to one of m machines. Job j on machine i costs cost[i, j] and uses
resource[i, j] of the machine's capacity capacity[i]. The total resource used on each
machine must not exceed its capacity.
The variable is Integer("machine", n, lb=0, ub=m - 1): machine[j] is the machine of
job j. The problem is vectorized, with one capacity constraint per machine.
ORLIB_GAP_OPTIMA
module-attribute
¶
ORLIB_GAP_OPTIMA = {'c515-1': 336, 'c515-2': 327, 'c515-3': 339, 'c515-4': 341, 'c515-5': 326, 'c520-1': 434, 'c520-2': 436, 'c520-3': 420, 'c520-4': 419, 'c520-5': 428, 'c525-1': 580, 'c525-2': 564, 'c525-3': 573, 'c525-4': 570, 'c525-5': 564, 'c530-1': 656, 'c530-2': 644, 'c530-3': 673, 'c530-4': 647, 'c530-5': 664, 'c824-1': 563, 'c824-2': 558, 'c824-3': 564, 'c824-4': 568, 'c824-5': 559, 'c832-1': 761, 'c832-2': 759, 'c832-3': 758, 'c832-4': 752, 'c832-5': 747, 'c840-1': 942, 'c840-2': 949, 'c840-3': 968, 'c840-4': 945, 'c840-5': 951, 'c848-1': 1133, 'c848-2': 1134, 'c848-3': 1141, 'c848-4': 1117, 'c848-5': 1127, 'c1030-1': 709, 'c1030-2': 717, 'c1030-3': 712, 'c1030-4': 723, 'c1030-5': 706, 'c1040-1': 958, 'c1040-2': 963, 'c1040-3': 960, 'c1040-4': 947, 'c1040-5': 947, 'c1050-1': 1139, 'c1050-2': 1178, 'c1050-3': 1195, 'c1050-4': 1171, 'c1050-5': 1171, 'c1060-1': 1451, 'c1060-2': 1449, 'c1060-3': 1433, 'c1060-4': 1447, 'c1060-5': 1446}
AssignmentCost ¶
AssignmentCost(cost, resource, capacity)
Objective: total cost of the assignment sol.machine, with resource used - capacity per machine.
gap ¶
gap(cost, resource, capacity, *, sense='min', name='gap', optimum=None, source=None)
A generalized assignment problem.
Parameters
cost(array of shape (m, n)): Cost (or profit, withsense="max") of each job on each machine.resource(array of shape (m, n)): Capacity used by each job on each machine.capacity: array of shape (m,)sense: "min" or "max"name,optimum,source: optional metadata.
orlib_gap ¶
orlib_gap(index=1)
Problem c515-index (5 machines, 15 jobs, maximization) of the bundled OR-Library file gap1.
read_orlib_gap ¶
read_orlib_gap(path, sense='max')
Read an OR-Library GAP file (gap1 to gap12, gapa to gapd) and return a list of Problem.
The file holds several problems: for each, m and n, the (m, n) cost matrix, the (m, n)
resource matrix and the m capacities. gap1 to gap12 are usually solved as maximization
problems (the default here), and their problems get the optimum from ORLIB_GAP_OPTIMA.
The type A to D files are minimization problems, so read them with sense="min".
Problems are named as in OR-Library, for example "c515-1" (5 machines, 15 jobs, number 1).
Hub location¶
Uncapacitated single allocation p-hub median problem.
Choose p of the n nodes as hubs and allocate every node to one hub (a hub to itself). The flow W[i, j] from node i to node j travels i -> hub(i) -> hub(j) -> j, and its cost per unit is chi C[i, hub(i)] + alpha C[hub(i), hub(j)] + delta C[hub(j), j]. The objective is the total cost over all pairs. The discount factor alpha (< 1) models the cheaper transport between hubs.
Variables: Selection("hubs", n, p) gives the hub nodes, and Integer("alloc", n, 0, p - 1)
gives each node's hub as an index into hubs. The allocation of a hub node is ignored, since
a hub serves itself. The problem is vectorized.
The CAB data set (25 US cities, O'Kelly 1987) from OR-Library is bundled, see cab.
hub_location ¶
hub_location(flows, costs, p, alpha, *, chi=1.0, delta=1.0, name='hub_location', optimum=None, source=None)
A single allocation p-hub median problem.
Parameters
flows(array of shape (n, n)): Flow W[i, j] from node i to node j.costs(array of shape (n, n)): Unit transport cost (usually the distance) C[i, j].p(int): Number of hubs.alpha(float): Discount factor for transport between hubs.chi,delta(float): Factors for collection (node to hub) and distribution (hub to node), 1 by default.
cab ¶
cab(n=25, p=2, alpha=0.2, *, normalize=True)
CAB p-hub median instance (O'Kelly 1987), bundled from OR-Library.
n is 10, 15, 20 or 25: the subsets are the first n cities. With normalize (default,
as in most papers), the flows are divided by their total, so the objective is the mean cost
per unit of flow. Collection and distribution factors are 1.
Reference: M. E. O'Kelly, "A quadratic integer program for the location of interacting hub facilities", European Journal of Operational Research 32 (1987) 393 to 404.
read_orlib_cab ¶
read_orlib_cab(path)
Read the OR-Library CAB file (phub4). Returns (flows, costs), both 25 by 25.
The file lists i j W[i, j] C[i, j] for every pair, after the line starting with "param".
hub_allocation ¶
hub_allocation(hubs, alloc)
The hub node of every node: hubs[alloc], with each hub allocated to itself.
Transportation¶
Transportation problem.
Ship goods from m sources with supply[i] to n destinations with demand[j] at unit cost
cost[i, j], and minimize the total cost. Each source ships all of its supply, and each
destination receives at least its demand.
The variable is FixedSum("x", (m, n), target=supply, axis=1): the decoded shipments x[i, j]
are nonnegative and each row sums to the supply, so the supply constraints always hold. The
demand constraints demand[j] - sum_i x[i, j] <= 0 are the problem's constraints. When the
total supply equals the total demand, these hold only with equality, which a metaheuristic
reaches only approximately, so give some spare supply for search runs. The problem is vectorized.
ShippingCost ¶
ShippingCost(cost, demand)
Objective: total shipping cost of sol.x, with one demand constraint per destination.
transportation ¶
transportation(cost, supply, demand, *, name='transportation', optimum=None, source=None)
A transportation problem.
Parameters
cost(array of shape (m, n)): Unit shipping cost from source i to destination j.supply: array of shape (m,)demand(array of shape (n,)): The total supply must be at least the total demand.
Instance files¶
Readers for standard instance files: TSPLIB, QAPLIB and OR-Library (mknap).
The files are not bundled (except OR-Library mknap1). Download them from the libraries:
- TSPLIB: http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/
- QAPLIB: https://coral.ise.lehigh.edu/data-sets/qaplib/
- OR-Library: https://people.brunel.ac.uk/~mastjjb/jeb/orlib/mknapinfo.html
read_tsplib ¶
read_tsplib(path, optimum=None)
Read a TSPLIB file (TSP or ATSP) and return the TSP Problem.
optimum defaults to the TSPLIB value for the known instances in TSPLIB_OPTIMA.
Node coordinates (or display coordinates) are kept in info["coords"] for plotting.
read_tsplib_tour ¶
read_tsplib_tour(path)
Read a TSPLIB tour file (.opt.tour) and return the tour as a zero-based array.
tsplib_distances ¶
tsplib_distances(coords, kind)
TSPLIB distance matrix of coords for the edge weight type kind.
Supports EUC_2D, EUC_3D, MAN_2D, MAN_3D, MAX_2D, MAX_3D, CEIL_2D, ATT and GEO, with the rounding rules of the TSPLIB documentation.
read_qaplib ¶
read_qaplib(path, optimum=None)
Read a QAPLIB .dat file (n, then the matrices A and B) and return the QAP Problem.
A is the flow and B the distance, so the cost is sum A[i, j] * B[p[i], p[j]].
optimum defaults to the QAPLIB value for the instances in QAPLIB_OPTIMA.
read_qaplib_solution ¶
read_qaplib_solution(path)
Read a QAPLIB .sln file. Returns (cost, p) with p zero-based.
read_orlib_mknap ¶
read_orlib_mknap(path)
Read an OR-Library multidimensional knapsack file (mknap1, mknapcb1 to mknapcb9).
Returns a list of Problem objects. The optimum is set when the file gives one (not 0).
Data sets¶
Small real data sets for examples.
port1 ¶
port1()
The Hang Seng portfolio data of OR-Library (port1): 31 assets.
Returns (mean, cov, frontier): the mean weekly return of each asset (31,), the
covariance matrix of the returns (31, 31), and 2000 points of the unconstrained efficient
frontier as (return, variance) pairs (2000, 2), from the file portef1.
T.-J. Chang, N. Meade, J. E. Beasley and Y. M. Sharaiha, "Heuristics for cardinality constrained portfolio optimisation", Computers and Operations Research 27, 2000, 1271 to 1302. Data from OR-Library (J. E. Beasley, MIT license).