Element Constraints for Variable Indexing

Introduces the element constraint in constraint programming, which lets a model express y = v[i] when the index i is itself a decision variable. Covers element constraints with constant arrays, with variable arrays, parallel element constraints for selection modeling, and using element constraints together with restrictions on the target to identify feasible indices.

Step 1 of 157%

Tutorial

The Element Constraint

In constraint programming, we often need to express the value at position ii of a list, where the index ii is itself a decision variable. Standard indexing v[i]v[i] requires ii to be a known integer at modeling time, but a solver only fixes ii at solve time.

The element constraint resolves this. Given an integer-valued list v=[v0,v1,,vn1]v=[v_0, v_1, \ldots, v_{n-1}], an index variable ii, and a target variable yy, the call

AddElement(i,  [v0,v1,,vn1],  y)\texttt{AddElement}(i,\; [v_0, v_1, \ldots, v_{n-1}],\; y)

enforces the relationship

y=vi.y = v_i.

The index ii is restricted to {0,1,,n1}\{0, 1, \ldots, n-1\}. Whenever the solver fixes ii to a particular value, yy is forced to equal the entry of vv at that position.

For example, the constraint

AddElement(i,  [3,8,1,6,4],  y)\texttt{AddElement}(i,\; [3, 8, 1, 6, 4],\; y)

forces y=3y=3 when i=0i=0, y=1y=1 when i=2i=2, and y=4y=4 when i=4i=4.

The same logical effect could be obtained with reified constraints, one per possible index value (y=3y=3 OnlyEnforceIf\texttt{OnlyEnforceIf} (i=0i=0), y=8y=8 OnlyEnforceIf\texttt{OnlyEnforceIf} (i=1i=1), and so on). The element constraint is the compact, solver-friendly way to express the same idea.

navigate · Enter open · Esc close · ⌘K/Ctrl K toggle