E-MD0004 — Jagged array constraint not supported

Error Type

Modeling Error

Phase

building the model (adding a constraint)

Python exception

jijmodeling.ModelingError

Message

Constraints comparing jagged arrays are not supported.

Possible fix: compare regular arrays or scalar elements instead.

Cause

The comparison used as a constraint has jagged arrays on its sides. Because rows of a jagged array have different lengths, JijModeling cannot expand such a comparison into a family of element-wise constraints.

Fix

Rewrite the constraint element-wise, comparing individual scalar values — or compare regular (rectangular) arrays. With the decorated API (@problem.update), iterate over the rows and the (row-dependent) inner elements with a generator comprehension:

@problem.update
def _(problem: jm.DecoratedProblem):
    A = problem.Natural("A", ndim=2, jagged=True)
    x = problem.BinaryVar("x", shape=(10,))
    problem += problem.Constraint(
        "c", (x[e] <= 1 for row in A.rows() for e in row)
    )

Outside the decorated API, pass a lambda together with domain=:

problem += problem.Constraint(
    "c", lambda e: x[e] <= 1, domain=A.rows().flat_map(lambda row: row)
)