# 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 | `m.Bool()`, `m.Int(lb, ub)`, `m.Float(lb, ub)`, `m.List(n)`, `m.Set(n)`; `m.Constant(v)` | | Arithmetic | `x + y`, `x - y`, `x * y`, `x / y`, `-x`, `x ** y`, `cbls.pow(x, y)` | | Nonlinear | `cbls.sin`, `cos`, `tan`, `exp`, `log`, `sqrt`, `abs`; `m.tanh_expr(h)`, `m.signpower_expr(h, h2)` | | Piecewise and logic | `cbls.min([x, y])`, `cbls.max([x, y])`, `cbls.if_then_else(c, x, y)`, `cbls.ceil`, `floor`, `round`, `cbls.element(table, i)` | | Rows | `m.add_constraint(...)` of `x <= y`, `x >= y`, `x < y`, `x > y`, `x.eq(y)`, `x.neq(y)` | | List and Set | `m.at(h, hi)` (the element at position `hi`, a handle), `m.count(h)` (the size), `m.lambda_table_sum`, `m.pair_table_sum`, `m.lambda_sum`, `m.pair_lambda_sum`, `m.add_list_partition` | | Objective | `m.minimize(x)`, `m.maximize(x)` | - **Nonlinear terms need no form:** non-convex, non-smooth and discontinuous expressions are scored as written; `log`, `sqrt` and 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](../cpp.md#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. ```python 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`). ```python 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(...)`: ```python 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_sum` adds a route's demands; `pair_table_sum` its legs, with `head` and `tail` the legs from and to the depot. An empty route costs 0. - [`examples/cvrp.py`](https://github.com/spoorendonk/cbls/blob/main/examples/cvrp.py) reads a CVRPLIB instance, solves it and checks the routes; [`examples/cvrp.cpp`](https://github.com/spoorendonk/cbls/blob/main/examples/cvrp.cpp) is the C++ version. The test suite runs the Python one on `X-n115-k10`: ```bash python examples/cvrp.py benchmarks/instances/cvrp/X/X-n115-k10.vrp ```