Skip to main content

Goals and Constraints

Rebalancer lets you express what you want from an assignment as goals and constraints, built from a library of 25+ reusable specs. Some specs work only as a goal, some only as a constraint, and many as either.

  • A constraint defines what makes an assignment valid; it must hold.
  • A goal is a quantity to minimize; with several goals, their relative priority decides how they trade off.

To add them to the solver, use addConstraint and addGoal. Only the spec is required; the remaining parameters are optional:

solver.addConstraint(spec, policy, invalidCost, invalidState, tuplePosIfBroken);
solver.addGoal(spec, weight, tuplePos);
  • For a constraint, policy and the invalidCost / invalidState / tuplePosIfBroken tuning control its constraint policy: how a constraint that is already broken in the initial assignment is treated (fixed best-effort by default, or made strictly hard or soft).
  • For a goal, weight and tuplePos control its goal priorities: how it trades off against other goals.

Available specs

Where a spec has its own page, its name links to it. The Type column shows whether a spec can be used as a goal, a constraint, or either. Examples links to the spec's unit tests.

Capacity and limits

SpecTypeDescriptionExamples
CapacitySpecBothLimit each scope item's utilization for a dimension to a max (or min)unit tests
ToFreeSpecBothDrain the listed containers, driving their utilization for a dimension to zerounit tests
GroupCountSpecBothLimit how many objects of a group a scope item holds, or their total for a dimensionunit tests
GroupDiversitySpecBothRequire each scope item to hold at least, or at most, N distinct groupsunit tests
CapacityWithGroupPresenceSpecBothLimit each scope item's utilization, with every present group counting for at least a minimum weightunit tests
GroupCapacitySpecBothLimit each group's total utilization across all scope items in a scopeunit tests
GroupIsolationLimitSpecBothLimit how many groups may exceed their utilization limit in the same scope item (default 1)unit tests
DisasterRecoveryCapacitySpecBothReserve enough spare capacity so scope items can survive correlated failure scenariosunit tests

Balancing and packing

SpecTypeDescriptionExamples
BalanceSpecGoalBalance a dimension's utilization evenly across scope itemsunit tests
UtilIncreaseCostSpecGoalPenalize raising a scope item's utilization above a lower boundunit tests
MinimizeContainersSpecGoalMinimize the number of scope items usedunit tests
MaximizeAllocationSpecGoalMaximize utilization on a set of scope itemsunit tests
MinimizeNthLargestUtilizationSpecGoalMinimize the n-th largest scope-item utilization (n is 0-based)unit tests
MinimizeSquaresSpecGoalMinimize the sum of squared scope-item utilizationsunit tests
WorkingSetSpecGoalMinimize the average or maximum working-set size across scope itemsunit tests

Placement

SpecTypeDescriptionExamples
NonAcceptingSpecConstraintPrevent objects from moving into the listed scope itemsunit tests
AvoidAssignmentsSpecConstraintForbid specific object-to-scope-item assignmentsunit tests
ColocateGroupsSpecBothLimit how many scope items each group spreads across (default: one)unit tests
ExclusiveScopeItemsSpecBothForbid pairs of scope items from being used at the same timeunit tests

Movement

SpecTypeDescriptionExamples
AvoidMovingSpecConstraintPrevent the listed objects from movingunit tests
GroupMoveLimitSpecConstraintLimit how many objects of a group may moveunit tests
MovesInProgressSpecConstraintAccount for objects already moving between containersunit tests
MinimizeMovementSpecBothMinimize the number (or dimension-weighted amount) of objects movedunit tests

Affinities

SpecTypeDescriptionExamples
AssignmentAffinitiesSpecGoalMake specific objects prefer specific scope itemsunit tests
PairAffinitiesSpecBothMake pairs of objects prefer the same scope itemunit tests
GroupAssignmentAffinitiesSpecGoalMake specific groups prefer specific scope itemsunit tests
ScopeAffinitiesSpecGoalReward or penalize each scope item's utilization of a dimension via a per-scope-item weightunit tests