Skip to content

Max-cut

Weighted Max-Cut with fixed vertex sides, same-side and different-side pairs, side-size bounds and an optional balance term, on this repository's C++ engine (core/optimaru/): split the vertices into two sides so that the total weight of the edges between the sides is as large as possible while every side constraint holds. The base formulation is the standard weighted Max-Cut problem. benchmarks/max-cut/cpsat holds the exact CP-SAT reference.

Usage

OPTIMARU_USE_CASES=max_cut uv sync      # builds only this use case's C++ extension
uv run pytest -q tests/max_cut
from optimaru import max_cut
from optimaru.max_cut.models import EdgeModel, MaxCutProblemModel

problem = MaxCutProblemModel(
    vertices=["A", "B", "C"],
    edges=[EdgeModel(source="A", target="B", weight=2), EdgeModel(source="B", target="C", weight=3),
           EdgeModel(source="A", target="C", weight=4)],
)
problem = max_cut.solve(problem, time_limit_seconds=1.0, random_seed=42)   # a new, solved model
print(problem.score_hard, problem.score_soft)                             # 0 7.0
print(problem.partition, problem.cut_weight)

core = problem.to_core()                                                # or the engine object
print(max_cut.solve(core, time_limit_seconds=1.0, random_seed=42).score)   # 0hard/7soft
print(MaxCutProblemModel.from_core(core).cut_weight)

solve takes time_limit_seconds, random_seed, parallel_solver_count, stop_at_first_feasible, full_assert, on_new_best, a full config (max_cut.SolverConfig) and constraints:

from optimaru.max_cut.constraints import CutWeightCost, PartitionImbalanceCost, PartitionSizeBoundsCost, get_constraints, score

max_cut.constraint_info()     # [ConstraintInfo(name='fixed_partition_cost', default_level='hard', ...), ...]
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)             # raw costs, re-priced in pure Python:
score(raw), score(raw, [PartitionSizeBoundsCost(level="soft"), CutWeightCost()])
# A constraint may be picked several times, each pick with its own knobs; offset / clip
# shift the shaping (clip=True: only the cut beyond 5 earns the second pick's bonus).
score(raw, [CutWeightCost(), CutWeightCost(weight=0.5, offset=5)])

Domain (assignment shape)

  • The side of each vertex — the only decision variable: 0 or 1 (the value range is {0, 1}, so the engine's change move is a flip and its swap move exchanges a side-0 and a side-1 vertex). On the model it is partition ({vertex: side}); on the engine object assignment() / set_assignment() (one entry per vertex, -1 = unassigned).
  • A vertex in fixed_partition is pinned: the engine never moves, swaps or ruins it.
  • Facts: vertices (unique ids), weighted undirected edges (weight >= 0; a self-loop is never cut, parallel edges add up), fixed_partition, same_partition_pairs, different_partition_pairs (a pair may repeat — each entry counts — and may name one vertex twice: a same self-pair is never split, a different self-pair is always joined), min_partition_size / max_partition_size (inclusive bounds on EACH side; None = 0 / |V|).
  • warm_start — start from partition; vertices it leaves out take the default start. A plain solve ignores the input partition. Every solve resets it.
  • Start partition: vertex i on side i % 2, a fixed vertex on its fixed side, a warm-start partition over both.
  • Exact size (min_partition_size == max_partition_size, both given): the search only swaps sides (change moves, ruin-and-recreate and descent are forced off, whatever SolverConfig says), so the side sizes of the start partition are kept — a seed of the wrong size keeps that size and its hard penalty. This holds for an unseeded solve too: 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. With fewer than two free vertices there is no search.
  • An unassigned vertex (-1, transient in the engine's ruin-and-recreate or set by a C++ caller) is in no side: it adds nothing to the cut, the sizes, the imbalance, fixed_partition_cost or different_partition_pairs_cost, and a same-side pair is split while its first vertex is assigned and its second is not. calculate_score / constraint_costs refuse a partial partition (ValueError); solve fills it in.

Constraints

The score is the engine's Score type as it is, with no mapping.

Constraint (Python name) C++ component Default level Default weight Raw cost (0 = no violation)
fixed_partition_cost FixedPartitionCost hard 1 per side: fixed vertices on that side that belong on the other one
same_partition_pairs_cost SamePartitionPairsCost hard 1 per side: same-side pairs split, each charged to its first vertex's side
different_partition_pairs_cost DifferentPartitionPairsCost hard 1 per side: different-side pairs with both vertices on that side
partition_size_bounds_cost PartitionSizeBoundsCost hard 1 per side: max(0, min - n) + max(0, n - max), n = the side's size
partition_imbalance_cost PartitionImbalanceCost soft 0 (opt-in) one total: abs(n0 - n1)
cut_weight_cost CutWeightCost soft 1 reward, one total: -(cut weight), always <= 0, so the soft score is +cut weight (0 = nothing cut, the worst value)

The first four are unit constraints (constraint_costs() reports [side 0, side 1]), the last two globals (one total). Five are scored by default; partition_imbalance_cost has default weight 0 (default_enabled is False): give it a weight to trade cut weight for balance. Every constraint but the reward is flippable (level="hard" / level="soft", or Level.HARD / Level.SOFT; level="medium" raises ValueError, the domain has no medium level). cut_weight_cost is a reward: it is always soft — level="hard" raises ValueError in solve, calculate_score, constraints.score() and the CP-SAT reference — and its level cannot change. max_cut.constraint_info() reports exactly this table from the C++ components (kDefaultLevel / kDefaultWeight).

Knobs: exponent, offset, clip, repeats

Every constraint takes weight, level and exponent, plus offset and clip (keyword-only), and may be picked several times. The four unit constraints shape each side's raw cost x, the imbalance and the cut their single total: sign(x) * max(0, |x| - offset) ** exponent (clip=True, the default) or (x - offset) ** exponent, sign-preserving (clip=False, which charges both sides, a side of raw 0 included, and rewards a side below the offset); offset=0 is the plain shaping. Both apply per side (or to the one total), not to the sum over the sides, so three fixed vertices off their side on side 0 cost 3 ** 2 = 9 at exponent=2. Hard contributions are rounded per side (size bounds [1, 1] at weight 0.4: 0). clip=False with an offset is refused on hard (the four unit constraints by default), because hard 0 must mean no violation: a clean side would otherwise earn a hard reward that cancels the other side's violation. On the cut_weight_cost reward (raw -cut) a threshold offset rewards only the cut beyond it and a literal one adds offset to the reward. Each pick is its own instance with its own knobs, its level included, and the instances add up:

problem = max_cut.solve(problem, constraints=[
    *(spec for spec in get_constraints() if spec.name != "partition_imbalance_cost"),
    PartitionImbalanceCost(weight=1),                          # |n0 - n1|
    PartitionImbalanceCost(weight=5, exponent=2, offset=2),    # + 5 x (imbalance beyond 2)^2
])

max_cut.constraint_costs(problem, picks) returns {name: {"costs", "weight", "exponent", "offset", "clip", "global", "level", "reward", "level_flippable", "name", "label", ...}}, with [side 0, side 1] for the four unit constraints and one total for the two globals; it keys the second pick partition_imbalance_cost#2 and reports every entry's offset, clip and the level in effect. calculate_score and constraint_costs raise ValueError on a partial partition (solve fills it in), and the model refuses partition entries for unknown vertices and sides other than 0 / 1. The CP-SAT reference takes the same picks (integral offsets; it rejects an exponent other than 1).

Python API

  • SolverConfig is the engine's (the other C++ packages' knobs: spent_limit_seconds, step_count_limit, unimproved_*, random_seed, parallel_solver_count, late_acceptance_size (the first stage's: late acceptance in stages), full_assert, ruin_recreate*, change/swap_move_weight, descent_weight, best_score_limit, on_new_best), set by attribute. Zero budget = spent_limit_seconds = 0 (step_count_limit = 0 means "no limit"); a bounded search = step_count_limit = N. Without a time limit a solve runs the 30 s budget. on_new_best(step, score). Multi-start runs independent engine searches from the same start.
  • solve() returns the solved problem: the engine object it was given, solved in place (its score is the engine Score: .hard, .soft, is_feasible()), or, for a model, a new model with the result and score_hard / score_soft (the model passed in is not changed). calculate_score(problem, constraints) returns the engine Score for the defaults and a constraints.Score(hard, soft) named tuple for a selection; constraints.score() re-prices raw costs. With constraints=... a solve is single-start (parallel_solver_count is ignored), as in every C++ package.
  • MaxCutProblemModel.to_core() / from_core() convert to and from the real engine object; from_core reports cut_weight and the score once the core was scored, and lists fixed_partition in vertex order.

C++

  • optimaru/max_cut/cpp/mc/model/domain.hpp — domain + optimaru::SolutionTraits specialization (with is_pinned for fixed vertices) and the -1 rules
  • optimaru/max_cut/cpp/mc/constraints/ — one component per constraint: fixed_partition_cost.hpp, same_partition_pairs_cost.hpp, different_partition_pairs_cost.hpp, partition_size_bounds_cost.hpp, partition_imbalance_cost.hpp, cut_weight_cost.hpp (+ common.hpp: the aliases, the prepare guard and the member-change base of the globals); optimaru/max_cut/cpp/mc/constraints.hpp assembles the default set (defaultConstraints(), also the catalog order)
  • score_calculator — prepare() (validation + CSR adjacency / pair incidence) and the full recompute (the reference, rawCosts())
  • incremental_calculator — O(degree + pair degree of the vertex) per move
  • solver — engine wrapper (the alternating start partition + Late Acceptance over flip / swap moves + adaptive ruin & recreate; swaps only under exact size bounds; independent parallel starts on worker threads, an exception in one of them is rethrown by solve())
  • optimaru/max_cut/cpp/main.cpp — the web demo solved and explained per constraint
cmake -B build -DCMAKE_BUILD_TYPE=Release -DOPTIMARU_USE_CASES=max_cut
cmake --build build -j4
ctest --test-dir build --output-on-failure --no-tests=error
./build/max_cut/mc_demo 2

mc_test: per-constraint hand checks (positive and zero) on the full recompute and the incremental calculator, the edge cases (self-loops, parallel and zero-weight edges, self-pairs, one-sided bounds, the -1 rules), incremental == full recompute over random flip / swap / unassign / batch moves (the score and every constraint's raw total after each move, each constraint non-zero during the walk), ScoreChecks::Full solves (ALNS kicks and exact size), the exact-size contract, pinning under multi-start, a worker exception rethrown instead of terminating the process, the zero budget, an empty graph, brute-force optima, determinism and validation. mc_plugin_test: default assembly == monolith bit for bit (score and unit costs), plugin incremental == full recompute per constraint (with the opt-in imbalance switched on too), partial assemblies, overrides (exponent per side, per-side hard rounding), constraint_totals(), ScoreChecks::Full plugin solves, the builder, raw costs by hand, the catalog and the reward guards, and repeated / shifted components (a hand-calculated repeat, threshold and literal offsets by hand per side and on the totals, the literal formula refused on hard at reset and kept on soft, incremental == a fresh recompute over a random walk, a ScoreChecks::Full solve).

Starting from an existing solution

with_initial_solution(problem, solution) returns a copy of problem whose search starts from solution, a solved model or the same thing as a JSON dict. Vertices the partition leaves out take the default start. Any result of solve is accepted, feasible or not. Under exact size bounds the search only swaps sides, so a seed of the wrong size keeps that size and its hard penalty. Unknown vertices, sides other than 0 or 1 and sides that contradict fixed_partition raise ValueError. With a zero budget (spent_limit_seconds = 0) the seed comes back unchanged.

from optimaru.max_cut import with_initial_solution

warm = with_initial_solution(problem, problem.model_dump(mode="json"))
warm = max_cut.solve(warm, time_limit_seconds=1.0, random_seed=42)
print(warm.partition, warm.cut_weight)

Extending

Engine contracts and code skeletons: docs/extending.md. The solution has the assignment shape; the domain solver that configures the engine is optimaru/max_cut/cpp/mc/solver.hpp + optimaru/max_cut/cpp/mc/solver.cpp.

Adding a move. The engine already mixes flip (change) and swap moves (weights in Solver::Config) and runs ruin-and-recreate on stagnation (and, with ruin_recreate_as_move_weight > 0, as an ordinary move — it moves a cluster of vertices chained by same-side pairs at once). A new move (a gain-guided Kernighan–Lin flip, say) is an optimaru::MoveOperator<…> registered with engine.add_move_operator(...) on every engine optimaru/max_cut/cpp/mc/solver.cpp builds: the one in searchOneStart() (the single start and each parallel start) and the one of the constraint-assembly overload. Under exact size bounds a move must keep the side sizes.

Changing the construction heuristic. construct() in optimaru/max_cut/cpp/mc/solver.cpp gives every vertex a side before the engine starts (the alternating start); the Python reference-parity tests pin it.

Adding a hard or soft cost. Adding a constraint, step by step takes one new constraint through this use case's files in six steps: changed from Python, written as a component, tested, made cheaper per move, moved to the default fast path and measured.

Benchmark

benchmarks/max-cut compares this use case with OR-Tools CP-SAT on the same instances, rescored by the same evaluator.

Latest run (30 s per solver, 20 instances): best score by the engine on 20, CP-SAT on 9 (ties count for each). Full table: benchmarks/max-cut/README.md.