● optimaru Engine
One search loop for every use case.
Every use case runs the same search: build a first solution, then improve it one move at a time, each move judged by an acceptor, with perturbation to restart progress when the search stalls, until a limit is reached.
The loop
How a solve runs
Construction runs once. Everything in the dashed frame is one step, repeated until a stop condition holds; the best solution is kept apart, so a step that makes things worse never loses it.
Each box names the file its step is in; the links open it on GitHub.
- Begin
- Construction → s₀Each unassigned entity gets its best placement. A solution you pass in is kept; only the rest is built.The code of this step: construct/
construction.hpp (opens in a new window) - Current s := s₀ · Best s_best := s₀The code of this step: solution/
snapshot.hpp (opens in a new window) Each step, until a stop condition holds
- Stop condition met?Time limit, step limit, no new best for a while, or a target scoreThe code of this step: stop/
stop_condition.hpp (opens in a new window) YesNoReturn s_best - Search stalled?No new best for as many steps as it usually takes, and at least the triggerThe code of this step: solver.hpp (opens in a new window)YesNoPerturbationTo get the search moving again, the solver removes part of the solution (10–30% by default), reinserts the removed items, and briefly improves the result with hill climbing. This becomes the current solution s even if its score is worse. When late acceptance runs in stages, a stall can advance it to the next stage.The code of this step: search/
perturbation/ stagnation_kick.hpp (opens in a new window) - Built-in strategy drawn?In a fixed order, the solver draws once per strategy at its own probability (0–1) and uses the first strategy drawnThe code of this step: search/
strategies/ strategies.hpp (opens in a new window) YesNoThat strategy makes s′The strategy explores more changes than a basic move, such as repeatedly improving the solution, removing a route, or removing and reinserting several items. - Use-case moves’ turn?On a set share of the steps left, if the use case adds moves of its ownYesNoAn adaptive selector picks the moveOne of the use case’s moves, or the move mix, by what each has found so farThe code of this step: search/
operators.hpp (opens in a new window) - Move mix: draw one move → s′By relative weight; what it moves is chosen by the selection rules (nearby first, the use case’s hints)The code of this step: search/
move_mix/ move_mix.hpp (opens in a new window) - Score s′ incrementallyOnly what the move touched is recomputedThe code of this step: solution/
editor.hpp (opens in a new window) - Acceptor takes s′?Late acceptance is the default. It compares the candidate with the current score and a score from an earlier step.The code of this step: accept/
(opens in a new window) NoYes · s := s′Undo the move - s better than s_best?The code of this step: solver.hpp (opens in a new window)Yess_best := s
Next step
- End
- Construction
- An empty or partial solution becomes complete. A solution you pass in is kept as it is. In the code (opens in a new window)
- Moves
- Each step asks, in this order, for a built-in strategy (a large neighbourhood), then for one of the use case’s own moves, and otherwise draws an ordinary small move. Only what the move touched is re-scored, which is what makes a step cheap. In the code (opens in a new window)
- Acceptor
- Late acceptance by default: a move is taken if it is no worse than the current solution or than the one a fixed number of steps ago. The engine has seven rules; some use cases let you choose one. In the code (opens in a new window)
- Perturbation
- When the best solution stops improving, the engine removes and reinserts some items to explore alternatives. The engine adjusts how it selects removal operators based on their past results. Most use cases enable perturbation by default. In the code (opens in a new window)
Contributing
Follow it in the code, and add to it
The engine’s code is on GitHub. The loop above is Solver::run in core/optimaru/solver.hpp, and the parts a contribution adds to (moves, ruins, constraints) are plugins with an interface of their own, so most contributions plug into one use case without touching the engine.
- Add a move (opens in a new window) A step the built-in moves cannot express, such as “empty the lightest bin”: a MoveOperator in the use case’s search/moves/, registered in its solver.cpp. The engine weighs it against its own moves as it solves.
- Add a constraint (opens in a new window) A cost of one unit (a route, an employee, a bin) in the use case’s constraints/, with its default weight and level, and a spec class so Python can pick and reweight it.
- Add a removal operator (opens in a new window) Register a RuinOperator to choose which items to remove. The engine selects it alongside the built-in removal operators based on performance.
- Change the engine (opens in a new window) The loop and its parts serve all use cases: a change keeps them correct and as fast as before, and a refactor keeps every seeded result.
Contributing guide (opens in a new window)Propose a move or a constraint (opens in a new window)