Skip to content

Test problems

Guide: Test problems.

Engineering design

ypea.problems.pressure_vessel

Pressure vessel design (Sandgren 1990, Kannan and Kramer 1994).

p = ypea.problems.pressure_vessel(mixed=true)

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:

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 (the original problem): Ts and Th are integer multiples of
0.0625 inch, given by ypea.Integer("thickness", shape=2, lb=1, ub=99) in
those units, and ypea.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 ypea.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.

info holds the best known solution (x_opt), the variant and the source.
The problem is vectorized, with constraints g <= 0.

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.

ypea.problems.welded_beam

Welded beam design, in the seven constraint version of Coello (2000).

p = ypea.problems.welded_beam()

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, all in ypea.Real("x") = (h, l, t, 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. info holds x_opt, the variant and the source.

Reference: C. A. C. Coello, "Use of a self adaptive penalty approach for
engineering optimization problems", Computers in Industry 41 (2000) 113 to 127.

ypea.problems.spring

Tension/compression spring design (Belegundu 1982, Arora 1989).

p = ypea.problems.spring()

Minimize the weight f = (N + 2) D d^2 of a spring with wire diameter d,
mean coil diameter D and N active coils, ypea.Real("x") = (d, D, N),
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 info.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.

ypea.problems.speed_reducer

Speed reducer design (Golinski 1973), with the eleven constraints of Mezura-Montes and Coello (2005).

p = ypea.problems.speed_reducer()

Minimize the weight of a gear box. The variables, in ypea.Real("x"), 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.

ypea.problems.three_bar_truss

Three bar truss design (Nowacki 1973, Ray and Saini 2001).

p = ypea.problems.three_bar_truss()

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), in
ypea.Real("x"), 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.

ypea.problems.gear_train

Gear train design (Sandgren 1990).

p = ypea.problems.gear_train()

Choose the numbers of teeth (Ta, Tb, Td, Tf) of four gears, integers from
12 to 60 in ypea.Integer("teeth"), 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

ypea.problems.tsp

A traveling salesman problem from city coordinates or a distance matrix.

p = ypea.problems.tsp(coords)
p = ypea.problems.tsp(distances=D, name="tsp", optimum=[], source="")

coords     City coordinates, n x 2. Without distances, the distances
           are Euclidean. With distances, they are kept only for plotting.
distances  Distance matrix, n x n. It may be asymmetric: the tour goes
           from tour(i) to tour(i + 1).
name, optimum (known optimal tour length), source (stored in info).

The variable is ypea.Permutation("tour", n), a tour of the cities 1 to n.
Its length includes the edge back from the last city to the first. The
problem is vectorized.

ypea.problems.random_tsp

A TSP with n cities placed uniformly at random in a square.

p = ypea.problems.random_tsp(n, seed=[], size=100)

MATLAB draws its own cities, so the same seed gives a different
instance than the Python version. Pass the coordinates to
ypea.problems.tsp to build a given instance.

ypea.problems.circle_tsp

A TSP with n cities evenly spaced on a circle, in shuffled order.

p = ypea.problems.circle_tsp(n=30, radius=40, seed=[], center=[50 50])

The optimal tour follows the circle, so the optimum is the perimeter of
the regular polygon, 2 n radius sin(pi / n). MATLAB shuffles with its
own generator, so the same seed gives a different city order than the
Python version.

ypea.problems.plot_tour

Plot a closed tour on the city coordinates of a TSP problem.

ax = ypea.problems.plot_tour(problem, tour, ax=gca of a new figure)

tour is a permutation, a solution struct with a tour field, or a
ypea.Result. Returns the axes.

TourLength

Not in this toolbox under this name. Python's TourLength class is not needed: the objective is a function, and its data is in problem.info.

ypea.problems.TSPLIB_OPTIMA

Optimal tour lengths from TSPLIB (TSP-BEST.html), by instance name.

Differs from Python: A function that returns a map: t = ypea.problems.TSPLIB_OPTIMA(); t("berlin52").

t = ypea.problems.TSPLIB_OPTIMA() is a containers.Map, for example
t("berlin52") is 7542.

Knapsack

ypea.problems.knapsack

A 0/1 knapsack problem, or a multidimensional one when weights is a matrix.

p = ypea.problems.knapsack(values, weights, capacity, name="knapsack", optimum=[], source="")

values    1 x n values of the items.
weights   1 x n weights, or m x n for m constraints.
capacity  A number, or m capacities.

The problem maximizes the total value x * values' subject to
weights * x' <= capacity. The variable is ypea.Binary("x", shape=n), and
the objective returns [f, g] with g = weights * x' - capacity. The
problem is vectorized.

ypea.problems.integer_knapsack

A bounded knapsack: item i may be packed from 0 to upper(i) times.

p = ypea.problems.integer_knapsack(values, weights, capacity, upper, name="integer_knapsack", optimum=[], source="")

The variable is ypea.Integer("x", shape=n, lb=0, ub=upper). Otherwise as
ypea.problems.knapsack.

ypea.problems.small_knapsack

A small 0/1 knapsack: 20 items, capacity 20, optimum 73.

info.x_opt holds an optimal solution.

ypea.problems.mknap1

The 7 multidimensional knapsack problems of OR-Library file mknap1 (Petersen 1967), bundled.

Returns a cell array of ypea.Problem. See data/NOTICE.md for the source and license.

KnapsackObjective

Not in this toolbox under this name. Python's KnapsackObjective class is not needed: the objective is a function, and its data is in problem.info. ypea.problems.knapsack_objective builds it.

Quadratic assignment and n-Queens

ypea.problems.qap

A quadratic assignment problem from a flow matrix and a distance matrix.

p = ypea.problems.qap(flow, distance, name="qap", optimum=[], source="")

Facility i goes to location p(i). The cost is the sum over i and j of
flow(i, j) * distance(p(i), p(j)), as in QAPLIB. Both matrices are
n x n. The variable is ypea.Permutation("p", n), the data is in p.info
(flow, distance), and the problem is vectorized.

QAPCost

Not in this toolbox under this name. Python's QAPCost class is not needed: the objective is a function, and its data is in problem.info.

ypea.problems.QAPLIB_OPTIMA

Proven optimal values from QAPLIB, by instance name.

Differs from Python: A function that returns a map.

t = ypea.problems.QAPLIB_OPTIMA() is a containers.Map, for example
t("nug12") is 578.

ypea.problems.nqueens

The n-Queens problem.

p = ypea.problems.nqueens(n=8)

The queen of row i stands in column q(i), with the variable
ypea.Permutation("q", n). A permutation already keeps rows and columns
distinct, so the objective counts the pairs of queens that share a
diagonal. The optimum is 0 for every n except 2 and 3, where it is 1.
The problem is vectorized.

Vehicle routing

ypea.problems.cvrp

A capacitated vehicle routing problem from node coordinates or a distance matrix.

p = ypea.problems.cvrp(coords, demands, capacity, n_vehicles, distances=[],
    name="cvrp", optimum=[], source="")

Node 1 is the depot and nodes 2 to n + 1 are the customers 1 to n. Each
of at most k = n_vehicles 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.

coords      (n + 1) x 2 node coordinates, the depot first. Without
            distances, the distances are Euclidean. May be empty when
            distances is given. Kept in info.coords for plotting.
demands     n + 1 demands. The depot's demand (the first value) must be 0.
capacity    The vehicle capacity.
n_vehicles  The number of vehicles k (the most routes a solution may have).
distances   (n + 1) x (n + 1) distance matrix.

Encoding: one ypea.Permutation("route", n + k - 1). Values 1 to n are
the customers, and values above n 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).
The objective is the total distance, with k capacity constraints
(load - capacity). Use ypea.problems.cvrp_routes to get the routes of a
solution. The data is in p.info. The problem is vectorized.

ypea.problems.cvrp_routes

The routes of a CVRP solution.

routes = ypea.problems.cvrp_routes(problem, solution) returns a 1 x k
cell array: routes{v} holds the customers (1 to n) of vehicle v in
their visiting order. Empty routes are kept. solution is a solution
struct or the permutation itself.

ypea.problems.read_cvrplib

Read a CVRPLIB file (TSPLIB like .vrp format) and return the CVRP problem.

p = ypea.problems.read_cvrplib(path, n_vehicles=[], rounding=true, optimum=[])

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 (the 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. The depot becomes node 1. CVRPLIB files are
not bundled: download them from https://galgos.inf.puc-rio.br/cvrplib/

ypea.problems.plot_routes

Plot the routes of a CVRP solution (needs coordinates).

ax = ypea.problems.plot_routes(problem, solution, ax=gca)

solution is a solution struct with a route field, or the permutation
itself. Each route starts and ends at the depot. Returns the axes.

RouteCost

Not in this toolbox under this name. Python's RouteCost class is not needed: the objective is a function, and its data is in problem.info.

Generalized assignment

ypea.problems.gap

A generalized assignment problem.

p = ypea.problems.gap(cost, resource, capacity, sense="min", name="gap", optimum=[], source="")

Assign each of n jobs to one of m machines. Job j on machine i costs
cost(i, j) (or earns it, with sense="max") and uses resource(i, j) of
the machine's capacity(i). The total resource used on each machine must
not exceed its capacity.

cost      m x n
resource  m x n
capacity  m values

The variable is ypea.Integer("machine", shape=n, lb=1, ub=m): machine(j)
is the machine of job j. The objective returns [f, g] with one capacity
constraint per machine (resource used - capacity). The data is in
p.info (cost, resource, capacity). The problem is vectorized.

ypea.problems.orlib_gap

Problem c515-index (5 machines, 15 jobs, maximization) of the bundled OR-Library file gap1.

p = ypea.problems.orlib_gap(index=1), index from 1 to 5. See
data/NOTICE-allocation.md for the source and license.

ypea.problems.read_orlib_gap

Read an OR-Library GAP file (gap1 to gap12, gapa to gapd).

problems = ypea.problems.read_orlib_gap(path, sense="max") returns a cell
array of ypea.Problem.

The file holds several problems: for each, m and n, the m x n cost
matrix, the m x 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 ypea.problems.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).

AssignmentCost

Not in this toolbox under this name. Python's AssignmentCost class is not needed: the objective is a function, and its data is in problem.info.

ypea.problems.ORLIB_GAP_OPTIMA

Optimal values of the OR-Library gap1 to gap12 problems, solved as maximization problems.

Differs from Python: A function that returns a map.

t = ypea.problems.ORLIB_GAP_OPTIMA() is a containers.Map from the
problem name to its optimum, for example t("c515-1") is 336.

Hub location

ypea.problems.hub_location

An uncapacitated single allocation p-hub median problem.

problem = ypea.problems.hub_location(flows, costs, p, alpha, chi=1, delta=1,
    name="hub_location", optimum=[], source="")

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.

flows   n x n flows W.
costs   n x n unit transport costs C (usually the distances).
p       number of hubs.

Variables: ypea.Selection("hubs", n, p) gives the hub nodes, and
ypea.Integer("alloc", shape=n, lb=1, ub=p) gives each node's hub as an
index into hubs. The allocation of a hub node is ignored, since a hub
serves itself (see ypea.problems.hub_allocation). The data is in info.
The problem is vectorized.

ypea.problems.cab

CAB p-hub median instance (O'Kelly 1987), bundled from OR-Library.

problem = ypea.problems.cab(n=25, p=2, alpha=0.2, normalize=true)

n is 10, 15, 20 or 25: the subsets are the first n cities. p is the
number of hubs. With normalize (the 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. See
ypea.problems.hub_location for the variables, and
data/NOTICE-allocation.md for the source and license.

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.

ypea.problems.read_orlib_cab

Read the OR-Library CAB file (phub4).

[flows, costs] = ypea.problems.read_orlib_cab(path), both 25 x 25.
The file lists "i j W(i, j) C(i, j)" for every pair, after the line
starting with "param".

ypea.problems.hub_allocation

The hub node of every node: hubs(alloc), with each hub allocated to itself.

a = ypea.problems.hub_allocation(hubs, alloc). hubs is a row of p hub
nodes (1 to n), and alloc a row of n indices into hubs (1 to p). Both
may have one row per solution. For example
hub_allocation([4 2], [2 1 1 1 1]) is [2 2 4 4 4].

Transportation

ypea.problems.transportation

A transportation problem.

p = ypea.problems.transportation(cost, supply, demand, name="transportation", optimum=[], source="")

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 total supply must be at least the total demand.

cost    m x n
supply  m values
demand  n values

The variable is ypea.FixedSum("x", [m n], target=supply, dim=2): the
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(x(:, 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 data is in p.info. The problem is vectorized.

ShippingCost

Not in this toolbox under this name. Python's ShippingCost class is not needed: the objective is a function, and its data is in problem.info.

Instance files

ypea.problems.read_tsplib

Read a TSPLIB file (TSP or ATSP) and return the TSP problem.

p = ypea.problems.read_tsplib(path, optimum=[])

optimum defaults to the TSPLIB value for the known instances in
ypea.problems.TSPLIB_OPTIMA. Node coordinates (or display coordinates)
are kept in info.coords for plotting. TSPLIB files are not bundled:
download them from http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/

ypea.problems.read_tsplib_tour

Read a TSPLIB tour file (.opt.tour) and return the tour, a row of cities 1 to n.

ypea.problems.tsplib_distances

TSPLIB distance matrix of coords for the edge weight type kind.

d = ypea.problems.tsplib_distances(coords, kind), coords one node per row.
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 (nint(x)
is floor(x + 0.5)).

ypea.problems.read_qaplib

Read a QAPLIB .dat file (n, then the matrices A and B) and return the QAP problem.

p = ypea.problems.read_qaplib(path, optimum=[])

A is the flow and B the distance, so the cost is the sum of
A(i, j) * B(p(i), p(j)). optimum defaults to the QAPLIB value for the
instances in ypea.problems.QAPLIB_OPTIMA. QAPLIB files are not bundled:
download them from https://coral.ise.lehigh.edu/data-sets/qaplib/

ypea.problems.read_qaplib_solution

Read a QAPLIB .sln file.

[cost, perm] = ypea.problems.read_qaplib_solution(path), with perm a row
of locations 1 to n.

ypea.problems.read_orlib_mknap

Read an OR-Library multidimensional knapsack file (mknap1, mknapcb1 to mknapcb9).

Returns a cell array of ypea.Problem. The optimum is set when the file
gives one (not 0).

Data sets

ypea.problems.port1

The Hang Seng portfolio data of OR-Library (port1): 31 assets.

[mean_return, cov_matrix, frontier] = ypea.problems.port1()

mean_return  the mean weekly return of each asset, 1 x 31.
cov_matrix   the covariance matrix of the returns, 31 x 31.
frontier     2000 points of the unconstrained efficient frontier as
             (return, variance) pairs, 2000 x 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), see data/NOTICE.md.