Modelling¶
What a model can use, then three models in Python. The test suite runs the first two models as written; the third is the model part of a whole program.
What a model can use¶
In Python, with m = cbls.Model(), x and y expressions and h an
expression’s .handle. C++ has the same operations in namespace cbls and on
Model, except the two table sums, which are Python’s: C++ passes callables to
lambda_sum and pair_lambda_sum. C++ spells x ** y as x.pow(y).
Kind |
Python |
|---|---|
Variables |
|
Arithmetic |
|
Nonlinear |
|
Piecewise and logic |
|
Rows |
|
List and Set |
|
Objective |
|
Nonlinear terms need no form: non-convex, non-smooth and discontinuous expressions are scored as written;
log,sqrtand fractional powers add domain rows that hold exactly.An inner solver, problem-specific code the search calls on its points, is a C++ plugin: inner-solver hooks.
Lot sizing with set-up costs¶
Bools and Ints with constraints: make at least 60 units on three machines, each with capacity 40, a unit cost and a set-up cost paid when it runs.
import cbls
m = cbls.Model()
cost, setup = [4, 3, 5], [20, 30, 10]
on = [m.Bool() for _ in cost]
n = [m.Int(0, 40) for _ in cost]
for i in range(3):
m.add_constraint(n[i] <= 40 * on[i])
m.add_constraint(sum(n) >= 60)
m.minimize(sum(c * a + f * b for c, a, f, b in zip(cost, n, setup, on)))
m.close()
result = cbls.solve(m, time_limit=1)
print(result.objective, [result.value(a) for a in n])
A tour as one List variable¶
A permutation as one List variable: the shortest closed tour of 12 points. It
needs NumPy, which cbls does not install (pip install numpy).
import cbls
import numpy as np
xy = np.random.default_rng(1).random((12, 2))
dist = np.linalg.norm(xy[:, None] - xy[None], axis=2)
m = cbls.Model()
tour = m.List(12)
m.minimize(m.pair_table_sum(tour.handle, dist, cyclic=True))
m.close()
result = cbls.solve(m, time_limit=1)
print(result.objective, result.elements[0])
Vehicle routes as List variables¶
A capacitated vehicle routing problem (CVRP): each route is one List, the
Lists partition the customers, no route carries more than capacity, and the
total length is minimised. n customers, fleet routes; demand,
from_depot and to_depot are NumPy arrays of length n, between is n by
n, all without the depot, and capacity is a model.constant(...):
routes = [model.list_var(n, 0, n, cbls.ListInit.Empty, f"route{r}") for r in range(fleet)]
model.add_list_partition(routes, cbls.Cover.Exact)
lengths = []
for route in routes:
model.add_constraint(model.leq(model.lambda_table_sum(route, demand), capacity))
lengths.append(model.pair_table_sum(route, between, head=from_depot, tail=to_depot))
model.minimize(model.sum(lengths))
model.close()
The partition is an invariant, not a penalised constraint: the search starts from a random complete partition and its moves keep it complete.
lambda_table_sumadds a route’s demands;pair_table_sumits legs, withheadandtailthe legs from and to the depot. An empty route costs 0.examples/cvrp.pyreads a CVRPLIB instance, solves it and checks the routes;examples/cvrp.cppis the C++ version. The test suite runs the Python one onX-n115-k10:
python examples/cvrp.py benchmarks/instances/cvrp/X/X-n115-k10.vrp