Worked examples¶
The examples/ directory holds complete, runnable programs. They depend only on MIP++ (no GoogleTest; only the TSP example also uses the MELON graph library), and each one is built around a technique from the guides. Each example is an independent project folder — main.cpp, a CMakeLists.txt and a conanfile.py — that can be copied as the starting point of a new program.
| Example | Model | Techniques | Read alongside |
|---|---|---|---|
simple_lp/ |
2-variable LP | the whole build → solve → read cycle | A first model |
nqueens/ |
N-Queens | lambda-indexed variables, constraint families over iota ranges |
Variables, Expressions |
sudoku/ |
Sudoku | 3-dimensional indexing, families over cartesian_product |
Expressions |
travelling_salesman_dfj/ |
TSP | branch-and-cut, candidate-solution callback, lazy subtour elimination | Branch-and-cut |
cutting_stock/ |
Cutting stock | column generation: duals by key, add_column, a knapsack pricer |
Column generation |
infeasible_transportation/ |
Transportation | an IIS from the deletion filter, the native routine behind has_iis, members printed by name with their sides |
Diagnosing infeasibility |
Reading them in order¶
simple_lp— the model class, variable handles,sol[x]. Five minutes.nqueens— the first real model: one batch ofn²binaries with an(row, col)id-map, six constraint families built fromiotaandxsum. This is the model behind the benchmark, so it is also the reference for what "no modeling tax" means in practice — see Performance.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.travelling_salesman_dfj— 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).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.infeasible_transportation— a model with no solution, although its depots hold more than the stores order. The deletion filter narrows it down to four figures, printed through the program's own names with the side of each; the native routine runs where the model class has one, and a HiGHS too old for it only skips that step. With its alias changed, the same program runs on every backend of the deletion filter.
Every example selects its backend through the alias at the top of its main.cpp:
Change it to target another solver — see Choosing a solver.
Building and running¶
Each example builds on its own with Conan, once MIP++ has been exported to the cache (see Installation):
cd examples/nqueens
conan build . -of=build -b=missing -pr=<your_profile>
./build/nqueens 8 # board size as an optional argument
or with plain CMake against an installed MIP++ (cmake -S . -B build -DCMAKE_PREFIX_PATH=<install_prefix>). To build them all from the repository root instead:
ENABLE_EXAMPLES=ON conan build . -of=build -b=missing -pr=<your_profile> -c tools.build:skip_test=true
./build/examples/simple_lp/simple_lp
./build/examples/nqueens/nqueens 8
To start a new project, copy an example folder and rename the target in its CMakeLists.txt.
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.