E-SE0004 — Unsupported if-clause in a comprehension
Error Type |
|
|---|---|
Phase |
defining a problem with the decorator API |
Python exception |
|
Message
A `{construct}` comprehension cannot have if-clauses.
Possible fix: remove the `if` and fold the condition into the element expression instead, since a `{construct}` comprehension must produce an entry for every index or key.
({construct} is the JijModeling function that received the comprehension: genarray or gendict)
Cause
Inside a decorated function, the comprehension passed to jijmodeling.genarray or jijmodeling.gendict contains an if clause, for example jm.genarray(c[i] * x[t, i] for (t, i) in (T, N) if t < i).
A generated array must define a value at every position of its shape, so entries cannot be skipped with a condition, and generated dictionaries do not support filtered comprehensions either.
This restriction is specific to genarray and gendict; comprehensions passed to jijmodeling.sum and the other reduction functions may use if clauses, as in jm.set(i for i in N if i != 0).
Fix
Remove the if clause from the comprehension.
If only part of the index set should contribute, restrict the set you iterate over before the comprehension (for gendict, pass the narrowed key set), or fold the condition into the element expression so every position still receives a value.