E-MD0007 — Constraint expression is not a comparison
Error Type |
|
|---|---|
Phase |
declaring a constraint |
Python exception |
|
Message
A constraint must be defined by a comparison expression such as `x + y <= b`, but got `{expression}`.
Possible fix: write the constraint as a `<=`, `>=`, or `==` comparison.
({expression} is the expression you passed, as JijModeling renders it)
Cause
problem.Constraint(name, expression) was called without the domain= argument, and the expression argument is not a comparison: its outermost operation is not a comparison operator such as <=, >=, or ==.
Typical triggers are passing an arithmetic expression such as jm.sum(x) or a plain number where the constraint condition was meant, or passing a function such as lambda i: x[i] <= 1 without also passing domain=.
Fix
State the constraint condition with a comparison operator relating two expressions:
import jijmodeling as jm
problem = jm.Problem("example")
x = problem.BinaryVar("x", shape=(3,))
# Raises E-MD0007: `jm.sum(x)` is not a comparison
# problem += problem.Constraint("limit", jm.sum(x))
problem += problem.Constraint("limit", jm.sum(x) <= 2)
If you passed a function in order to declare one constraint per index, also pass the index set via domain=:
N = problem.Natural("N")
y = problem.BinaryVar("y", shape=(N,))
problem += problem.Constraint("each", lambda i: y[i] <= 1, domain=N)