E-TE0611 — Boolean conversion of a modeling object

Error Type

Type Error

Phase

using a modeling object in a boolean context

Python exception

TypeError (builtin)

Message

A `{type_name}` has no truth value, so it cannot be used with Python `if`, `and`, `or`, `not`, `bool()`, or chained comparisons such as `a <= x <= b`.

Possible fix: keep it as a symbolic expression and restructure the surrounding Python code instead.

({type_name} is the kind of modeling object, such as Expression or Placeholder)

Cause

Python asked for the truth value of a JijModeling modeling object — an expression, placeholder, decision variable, named expression, or category label. This happens whenever such an object appears where Python needs a plain True/False: an if or while condition, an operand of and/or/not, bool(...), assert, or a chained comparison such as 0 <= x[i] <= 1 (which Python evaluates as (0 <= x[i]) and (x[i] <= 1)). Modeling objects are symbolic: a comparison like x[i] <= 1 builds an expression whose value depends on instance data and decision variables, so it has no truth value at the time the model is being built, and JijModeling rejects the conversion rather than silently guessing one.

Fix

Keep comparisons symbolic instead of testing them with Python control flow:

# instead of: if x[0] + y[0] <= b: ...
problem += problem.Constraint("cap", x[0] + y[0] <= b)

Split a chained comparison into separate comparisons, since Python’s chaining inserts an implicit and:

# instead of: problem.Constraint("bounds", 0 <= x[i] <= 1)
problem += problem.Constraint("lower", 0 <= x[i])
problem += problem.Constraint("upper", x[i] <= 1)

To combine conditions inside a single symbolic expression (for example a filter predicate), use the & and | operators instead of and/or. To check whether two modeling objects are structurally the same — for example in a test — use jm.is_same(a, b), which returns a Python bool, instead of if a == b: (== builds a symbolic comparison rather than comparing structure).