E-SE0004 — Unsupported if-clause in a comprehension

Error Type

Syntax Error

Phase

defining a problem with the decorator API

Python exception

SyntaxError (builtin)

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.

({construct} is the JijModeling function that received the comprehension: currently only genarray)

Cause

Inside a decorated function, the comprehension passed to jijmodeling.genarray 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. This restriction is specific to genarray; comprehensions passed to jijmodeling.gendict, jijmodeling.sum, and the other reduction functions may use if clauses, as in jm.gendict(c[n, k] * 2 for (n, k) in (N, K) if n % 2 == 0) or jm.stream(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, fold the condition into the element expression so every position still receives a value, or build a filtered expression with filter() / map() instead of genarray.