E-SE0000 — JijModeling object iterated at runtime
Error Type |
|
|---|---|
Phase |
building a model from Python (inside or outside the decorator API) |
Python exception |
|
Message
JijModeling objects cannot be iterated at runtime.
Typical triggers:
- comprehension syntax used outside the decorator API
- Python's builtin `sum` used instead of `jijmodeling.sum`
- a plain `for` loop, `list(...)`, or unpacking over a JijModeling object
Possible fix: move reductions into a function decorated with `@problem.update` or `@jijmodeling.Problem.define` and use JijModeling's own functions such as `jijmodeling.sum`.
Cause
JijModeling expression objects (placeholders, decision variables, and expressions built from them) cannot be iterated by plain Python, so this error is raised as soon as Python’s iteration protocol touches one.
Comprehension syntax such as jm.sum(x[i] for i in N) is only supported inside a function decorated with @jijmodeling.Problem.define or @problem.update, where JijModeling translates it into its own functions before the code runs.
Typical triggers are:
writing a comprehension or generator expression over an expression outside a decorated function, such as
jm.sum(x[i] for i in N)or[x[i] for i in N]at module level (Python callsiter(N)the moment the generator expression is created);using Python’s builtin
sumon a generator of expressions, even inside a decorated function — only JijModeling’s own functions such asjijmodeling.sumaccept comprehension arguments;placing a comprehension in a position the decorator API does not translate, such as
jm.min((x[i] for i in N), axis=0)— the supported form passes the comprehension as the only argument, with no extra arguments or keywords;iterating an expression directly in any other way, for example with a plain
forloop,list(...), or tuple unpacking.
Fix
Move the comprehension into a decorated function and pass it directly to one of JijModeling’s comprehension-aware functions (jijmodeling.sum, prod, min, max, set, genarray, gendict, or problem.Constraint), replacing Python’s builtin sum with jijmodeling.sum:
import jijmodeling as jm
@jm.Problem.define("model")
def problem(problem: jm.DecoratedProblem):
N = problem.Length("N")
x = problem.BinaryVar("x", shape=N)
problem += jm.sum(x[i] for i in N)
Outside the decorator API, use the functional forms instead of comprehension syntax, for example jm.sum(N, lambda i: x[i]).
If you were iterating a JijModeling object directly (a plain for loop, list(...), or unpacking), remove the iteration and express the computation with these functions inside the model instead.