optimaru.max_cut (Python)¶
The pybind11 package of the max-cut use case (optimaru/max_cut/cpp). Splits the vertices of a
weighted graph into two sides — the side of each vertex (0 or 1) is the only
decision variable; a vertex in fixed_partition is pinned.
Score, at the default weights:
- hard = -(fixed vertices off their side + split same-side pairs + different-side pairs left together + side-size shortfall/excess) — four constraints, hard x1 each
- soft = +(cut weight) —
cut_weight_cost, a reward (raw cost-(cut weight)), soft x1; it is always soft (level="hard"raises) partition_imbalance_cost(abs(n0 - n1)) is an opt-in objective: soft, default weight 0
The problem carries no weights. Pick, re-weight or flip constraints with
optimaru.max_cut.constraints, and read the defaults with max_cut.constraint_info():
from optimaru import max_cut
from optimaru.max_cut.constraints import CutWeightCost, FixedPartitionCost, PartitionImbalanceCost, get_constraints, score
max_cut.constraint_info()
# [ConstraintInfo(name='fixed_partition_cost', default_level='hard', default_weight=1.0,
# level_flippable=True, reward=False, default_enabled=True), ...,
# ConstraintInfo(name='partition_imbalance_cost', default_level='soft', default_weight=0.0,
# level_flippable=True, reward=False, default_enabled=False),
# ConstraintInfo(name='cut_weight_cost', default_level='soft', default_weight=1.0,
# level_flippable=False, reward=True, default_enabled=True)]
problem = max_cut.solve(
problem,
time_limit_seconds=1.0,
constraints=[
PartitionImbalanceCost(weight=2) if spec.name == "partition_imbalance_cost" else spec
for spec in get_constraints()
],
)
raw = max_cut.constraint_costs(problem) # {name: {"costs": [side 0, side 1] or [total], "weight": ..., ...}}
score(raw), score(raw, [FixedPartitionCost(exponent=2.0)])
# A constraint may be picked several times, each pick with its own knobs; offset / clip
# shift the shaping (clip=True: only the imbalance beyond 2 is squared).
score(raw, [PartitionImbalanceCost(weight=1), PartitionImbalanceCost(weight=5, exponent=2, offset=2)])
The four unit constraints (fixed, same-side, different-side, size bounds) report one
raw cost per side and exponent (with offset / clip) shapes each side, a side
of raw 0 included (per side, not over the sum); the imbalance and the cut report one
total. clip=False with an offset is refused on hard, where hard 0 must mean no
violation: ValueError: constraint 'fixed_partition_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. calculate_score /
constraint_costs need a complete partition (a partial one raises ValueError). Hard
contributions are rounded per side, 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=max_cut uv sync # builds only this use case's C++ extension
uv run pytest -q tests/max_cut
Usage¶
from optimaru import max_cut
from optimaru.max_cut.models import MaxCutProblemModel
problem = MaxCutProblemModel.model_validate(data) # the canonical JSON
problem = max_cut.solve(problem, time_limit_seconds=5.0, random_seed=42) # a new model: partition, cut, score
print(problem.score_hard, problem.score_soft, problem.cut_weight, problem.partition)
core = problem.to_core() # the engine object
max_cut.solve(core, time_limit_seconds=5.0, random_seed=42)
print(core.score, core.assignment(), core.cut_weight())
For finer control build max_cut.SolverConfig() (time budget, step_count_limit,
multi-start, ALNS knobs, move weights, the on_new_best(step, score) callback,
best_score_limit early stop, ...) and call max_cut.Solver(cfg).solve(core) or
max_cut.solve(problem, config=cfg). A zero budget (spent_limit_seconds = 0)
returns the start partition (vertex i on side i % 2, fixed vertices on their
side), or a warm-start seed, unchanged. Under exact size bounds
(min_partition_size == max_partition_size) the search only swaps sides, so the
start's side sizes are kept, also without a seed: when fixed vertices unbalance the
i % 2 start, the result stays infeasible even if a feasible partition exists. Seed a partition of the right size with with_initial_solution instead.
Note: list attributes of the engine object return copies — assign whole lists and
write the partition in bulk with core.set_assignment([...]).
JSON in, JSON out¶
solve_request takes a pydantic request, models.MaxCutRequest: 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 MaxCutProblemModel, as solve(model, ...) does, and leaves the request
unchanged. constraints= is the selection solve takes.
from optimaru.max_cut import solve_request
from optimaru.max_cut.models import MaxCutRequest
request = MaxCutRequest.model_validate_json(text) # or MaxCutRequest(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.