E-SE0002 — More than one for-clause in a comprehension

Error Type

Syntax Error

Phase

defining a problem with the decorator API

Python exception

SyntaxError (builtin)

Message

A `{construct}` comprehension must have exactly one for-clause.

Possible fix: use a single `for` that iterates over the whole shape or key set at once.

({construct} is the JijModeling function that received the comprehension: genarray or gendict)

Cause

Inside a decorated function, a comprehension passed to jijmodeling.genarray or jijmodeling.gendict contains two or more for clauses, for example jm.genarray(c[i] * x[t, i] for t in T for i in N). A generated array or dictionary is defined by a single for clause: its iterable is the whole shape (for genarray) or the whole key set (for gendict), and multi-dimensional indices are bound by a tuple target. This restriction is specific to genarray and gendict; comprehensions passed to jijmodeling.sum and the other reduction functions may chain several for clauses.

Fix

Merge the loops into a single for clause with a tuple target that iterates over the whole shape or key set:

A = problem.NamedExpr(
    jm.genarray(c[i] * x[t, i] for t, i in (T, N)),  # instead of `for t in T for i in N`
)

For gendict, iterate once over the key set, for example jm.gendict(c[k] * 2 for k in K).