# C++ reference The supported part of the C++ library, in namespace `cbls`: what `` brings in, plus `` for AMPL `.nl` models. This page has the main entry point of each concept and links the headers that declare the rest. What else `` brings in (the DAG, the violation manager, the moves) is documented in its headers. Headers outside that set, such as `structural_batch.h` and `arg_parse.h`, serve this repository's runners and tests. ## Using it from CMake Build it inside your project and link the target `cbls::cbls`, which brings the headers and C++17: ```cmake cmake_minimum_required(VERSION 3.15) project(app LANGUAGES CXX) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) # else cbls compiles unoptimised endif() include(FetchContent) FetchContent_Declare(cbls GIT_REPOSITORY https://github.com/spoorendonk/cbls.git GIT_TAG main) # or a commit, to pin it FetchContent_MakeAvailable(cbls) add_executable(app main.cpp) target_link_libraries(app PRIVATE cbls::cbls) ``` - **From an install** (`cmake --install build`): `find_package(cbls 0.1 CONFIG REQUIRED)` replaces the `FetchContent` lines. - **Clone or submodule** in your source tree: `add_subdirectory(cbls)` replaces them. - **Needs** zlib, found with `find_package(ZLIB)`; the first configure fetches nlohmann/json's headers. - **Versions**: the library is static and 0.x, with no source or ABI compatibility between releases. ## Model `Model` builds variables, expressions and constraints. [`model.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/model.h) also declares `ListPartition` and `Cover`, for Lists that partition a ground set. ```{doxygenclass} cbls::Model ``` ### Expression functions An `Expr` composes with the arithmetic and comparison operators (equality and inequality are the members `eq` and `neq`), and with `sin`, `cos`, `tan`, `exp`, `log`, `sqrt`, `abs`, `pow`, `min`, `max`, `if_then_else`, `ceil`, `floor`, `round` and `element` (a table lookup, by one index or by row and column), declared in [`expr.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/expr.h). ## Solving `solve` runs one search, under a `SearchConfig`, and returns a `SearchResult`. [`search.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/search.h) also declares `TerminationReason`, `WorkerFailure`, and the `SolveCallback` that receives a `SolveProgress`. [`stop.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/stop.h) has `StopToken` and `StopRef`, to stop a solve from outside. ```{doxygenfunction} cbls::solve ``` ```{doxygenstruct} cbls::SearchConfig ``` ```{doxygenstruct} cbls::SearchResult ``` ## Portfolio `ParallelSearch` runs the search on several threads. `ParallelConfig` is beside it in [`pool.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/pool.h). ```{doxygenclass} cbls::ParallelSearch ``` ## Extension points The hooks that `solve`, `SearchConfig` and the portfolio take: | Type | What it extends | Header | |---|---|---| | `InnerSolverHook`, `FloatIntensifyHook` | an inner solver run on the search's incumbents | [`inner_solver.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/inner_solver.h) | | `LNS` | large neighbourhood search | [`lns.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/lns.h) | | `MoveGenerator`, `MoveContext`, `NeighbourList` | extra moves | [`move_generator.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/move_generator.h) | | `Tracer` | a record of the search | [`tracer.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/tracer.h) | | `ExecutorRef` | the caller's thread pool for the portfolio | [`executor.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/executor.h) | ### Inner-solver hooks A problem-specific algorithm runs on the search's points through a subclass of `InnerSolverHook`; the engine stays problem-blind. - **Subclass** `InnerSolverHook` and override `solve(model, vm, last_changed_vars, stop)`; it reads the point from `model`, may change any value, and returns with the model fully evaluated (after bulk writes: `full_evaluate(model)`, then `vm.invalidate_cache()`). - **Called** after every batch that ends real-feasible (`vm.max_real_violation()` within `SearchConfig::feasibility_tolerance`; the objective-bound row does not count). With `wants_infeasible()` true, also after every other batch, uncapped: such a hook budgets itself. Not started once the run is stopping. - **Result**: recorded only if real-feasible and strictly better than the worker's incumbent (or its first feasible point). The search never rolls the point back, so a hook that cannot improve it leaves it as found. - **Stop**: the call cannot be pre-empted; a hook whose work can outlast the budget polls `stop.requested()` and returns promptly. `stop` is valid only during the call; a hook that reads no clock keeps an iteration-budgeted run bit-reproducible. - **Pass one** to `solve` as a pointer (`cbls::solve(model, time_limit, seed, &hook)`), or to `ParallelSearch::solve` as a factory returning a distinct `std::shared_ptr` per worker. A worker keeps its hook across pool restarts, so its state must stay valid then. - **C++ only**: a Python subclass is never called (no nanobind trampoline). `SearchCounters::inner_solver_calls` counts the calls. - **Worked example**: the unit-commitment hook, [`uc_hook.h`](https://github.com/spoorendonk/cbls/blob/main/benchmarks/uc-chped/uc_hook.h), on the [application page](uc-chped.md#a-problem-specific-inner-solver). ```{doxygenclass} cbls::InnerSolverHook ``` ## Model files `save_model` and `load_model` write and read `.cbls` files, each by path or by stream, declared in [`io.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/io.h). ```{doxygenfunction} cbls::load_model(const std::string &path) ``` ### MPS `mps_to_model(read_mps(path))` gives a model. They and `MpsProblem`, `MpsToModelOptions` and `MpsToModelResult` are in [`io_mps.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/io_mps.h). ```{doxygenfunction} cbls::mps_to_model ``` ### AMPL `.nl` `nl_to_model(read_nl(path))` gives a model, and `parse_nl` parses an already-loaded buffer, text or binary. They and `NlProblem`, `NlToModelOptions` and `NlToModelResult` are in [`io_nl.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/io_nl.h). ```{doxygenfunction} cbls::nl_to_model ``` ## Checking a solution `verify_model` re-checks the point a model is left at and returns a `VerifyResult` of `VerifyError`s, from [`verify.h`](https://github.com/spoorendonk/cbls/blob/main/include/cbls/verify.h). ```{doxygenfunction} cbls::verify_model ```