optimaru.knapsack (Python)¶
The pybind11 package of the knapsack use case (optimaru/knapsack/cpp). Chooses the items to
carry — Item.selected is the only decision variable (1 = selected, 0 = not).
Score, at the default weights:
- hard = -(weight over capacity + resource overflow + unselected mandatory entries
- selected forbidden entries + selection-group shortfall/excess + unmet item dependencies) — six constraints, hard x1 each
- soft = +(selected value) —
selected_value_cost, a reward (raw cost-(selected value)), soft x1; it is always soft (level="hard"raises)
The problem carries no weights. Pick, re-weight or flip constraints with
optimaru.knapsack.constraints, and read the defaults with knapsack.constraint_info():
from optimaru import knapsack
from optimaru.knapsack.constraints import MandatoryItemsCost, SelectedValueCost, get_constraints, score
knapsack.constraint_info()
# [ConstraintInfo(name='knapsack_capacity_cost', default_level='hard', default_weight=1.0,
# level_flippable=True, reward=False, default_enabled=True), ...,
# ConstraintInfo(name='selected_value_cost', default_level='soft', default_weight=1.0,
# level_flippable=False, reward=True, default_enabled=True)]
picks = [SelectedValueCost(weight=2) if spec.name == "selected_value_cost" else spec for spec in get_constraints()]
problem = knapsack.solve(problem, time_limit_seconds=1.0, constraints=picks)
raw = knapsack.constraint_costs(problem) # {name: {"costs": [total], "weight": ..., ...}}
score(raw), score(raw, [MandatoryItemsCost(exponent=2.0)])
# A constraint may be picked several times, each pick with its own knobs; offset / clip
# shift the shaping of the total (clip=True: only the misses beyond the first count).
score(raw, [MandatoryItemsCost(), MandatoryItemsCost(exponent=2, offset=1)])
Every knapsack constraint is global: constraint_costs() reports one raw total per
constraint and exponent (with offset / clip) shapes that total, a raw 0
included. clip=False with an offset is refused on hard, where hard 0 must mean no
violation: ValueError: constraint 'mandatory_items_cost': clip=False with a non-zero offset
cannot be hard, because hard 0 must mean no violation; use clip=True (a threshold) or
another level. On soft (and on the soft reward) it is allowed.
constraint_costs(problem, picks) scores a pick list, the second pick of a name keyed
<name>#2. level is None (the default level), "hard" or "soft" (or Level.HARD /
Level.SOFT); the domain has no medium level, so "medium" raises ValueError, and a
repeat carries its own level. Hard contributions are rounded per constraint, the reward
refuses level="hard", and SolverConfig carries the engine's knobs
(spent_limit_seconds, step_count_limit, ...); see the
use-case README.
Install and test¶
OPTIMARU_USE_CASES=knapsack uv sync # builds only this use case's C++ extension
uv run pytest -q tests/knapsack
Usage¶
from optimaru import knapsack
from optimaru.knapsack.models import KnapsackProblemModel
problem = KnapsackProblemModel.model_validate(data) # the canonical JSON
problem = knapsack.solve(problem, time_limit_seconds=5.0, random_seed=42) # a new model: selection, totals, score
print(problem.score_hard, problem.score_soft, problem.total_weight, problem.total_value)
core = problem.to_core() # the engine object
knapsack.solve(core, time_limit_seconds=5.0, random_seed=42) # solved in place
print(core.score, core.assignment(), core.total_value())
For finer control build knapsack.SolverConfig() (time budget, step_count_limit,
multi-start, ALNS knobs, move weights, nearby swaps, core_repack_moves (the move that
re-decides the items around the capacity boundary exactly), the on_new_best(step, score)
callback, best_score_limit early stop, ...) and call knapsack.Solver(cfg).solve(core)
or knapsack.solve(problem, config=cfg). A zero budget (spent_limit_seconds = 0)
returns the greedy construction, or a warm-start seed, unchanged.
Note: core.items[i] returns a copy — write the selection in bulk with
core.set_assignment([...]).
JSON in, JSON out¶
solve_request takes a pydantic request, models.KnapsackRequest: the problem model as
model_input, with the settings of the solve under the names of solve's keywords
(time_limit_seconds, random_seed, parallel_solver_count, stop_at_first_feasible,
full_assert, and solver_parameters for further SolverConfig fields by name). It
returns the solved KnapsackProblemModel, as solve(model, ...) does, and leaves the request
unchanged. constraints= is the selection solve takes.
from optimaru.knapsack import solve_request
from optimaru.knapsack.models import KnapsackRequest
request = KnapsackRequest.model_validate_json(text) # or KnapsackRequest(model_input=model, time_limit_seconds=5.0)
solved = solve_request(request)
Parameters and their costs¶
A request that sets a field which acts only through a cost, while its constraints leave
every such cost out, is refused with ValueError naming the field and the costs to add;
a pick at weight 0 counts. models.parameter_uses(model) lists the fields an input sets,
and Parameters and their costs has the table.
It is checked where the input is the JSON model (solve_request, solve(model)); a
compiled problem is solved as given.