Skip to content

Worked examples

The examples/ directory holds complete, runnable programs. They depend only on MIP++ (no GoogleTest, and only the TSP example needs a graph library), and each one is built around a technique from the guides.

Example Model Techniques Read alongside
simple_lp.cpp 2-variable LP the whole build → solve → read cycle A first model
nqueens.cpp N-Queens lambda-indexed variables, constraint families over iota ranges Variables, Expressions
sudoku.cpp Sudoku 3-dimensional indexing, families over cartesian_product Expressions
tsp_lazy_constraints.cpp TSP branch-and-cut, candidate-solution callback, lazy subtour elimination Branch-and-cut
cutting_stock.cpp Cutting stock column generation: duals by key, add_column, a knapsack pricer Column generation

Reading them in order

  1. simple_lp — the api/model split, variable handles, sol[x]. Five minutes.
  2. nqueens — the first real model: one batch of binaries with an (row, col) id-map, six constraint families built from iota and xsum. This is the model behind the benchmark, so it is also the reference for what "no modeling tax" means in practice — see Performance.
  3. sudoku — the same ideas one dimension up, and a good template for assignment-style models: X(i, j, v), families over cartesian products, hints fixed with single constraints.
  4. tsp_lazy_constraints — an algorithm, not just a model: the callback receives a candidate, the code searches it for subtours, and injects the violated constraints. Needs a backend with callback support (Gurobi, CPLEX or COPT).
  5. cutting_stock — the other classic: a restricted master, dual prices read back by order id, a dynamic-programming pricer, and columns streamed in as lazy ranges.

Every example selects its backend through the two aliases at the top of the file:

using api_type = highs_api;
using milp_type = highs_milp;

Change them to target another solver — see Choosing a solver.

Building and running

cmake -S . -B build -DENABLE_EXAMPLES=ON
cmake --build build

./build/examples/example_simple_lp
./build/examples/example_nqueens 8      # board size as an optional argument

The corresponding solver's shared library must be discoverable at run time — see Installation.

Beyond the examples

The test suites are the second body of usage code, and they cover features no example does — MIP starts, model modification, reduced costs, and the column_manager for large-scale pricing. They are written against the concepts, so each of them is itself an example of solver-generic code.