E-TE0004 — Type mismatch

Error Type

Type Error

Phase

type checking the model

Python exception

jijmodeling.TypeError

Message

Could not match actual type `{actual}` with expected `{expected}`{...}

When the mismatched expression is available, {...} is replaced by on an expression `{term}`; otherwise it is omitted. The traceback may also show the check that supplied the expected type, for example, while checking if expression `W` has type `natural` . When a precise source location is available, the source excerpt highlights only that expression.

Cause

An expression has a different type than the context requires. Typical examples are passing a continuous (float) expression where a natural number is required (such as a shape or an index bound), or supplying an element of the wrong type to a typed container. The expected part may be a concrete type or a description of the requirement (for example, indexable type or literal natural number < 3). When the mismatched (sub)expression is known, the message appends on an expression `{term}` .

Fix

Compare the expected and actual types in the message and adjust the expression named by the on an expression `{term}` clause when present. For example, declare the placeholder with the required data type (dtype=jm.DataType.INTEGER instead of FLOAT) or convert the expression so it produces the expected type.

If the message says Could not match actual type `float` with expected `natural` on an expression `W` , the mismatched expression is usually a value used as an array subscript (or in another place that requires a nonnegative integer, such as a shape dimension) even though it is declared as a continuous value. For example:

@jm.Problem.define("Knapsack Problem")
def problem(problem: jm.DecoratedProblem):
    W = problem.Float()
    N = problem.Length()
    x = problem.BinaryVar(shape=N)
    problem += x[W]  # error: `W` is float, but a subscript must be natural

If the variable itself is meant to be an index or a size, declare it as a natural number — for example with problem.Natural() or problem.Length() instead of problem.Float(). If the continuous declaration is correct, the subscript refers to the wrong variable, so pass the variable you actually intended to index with.

A set-valued subscript can fail in the same way. For example, W = problem.CategoryLabel() represents the set of labels in category W, so using x[W] on an array whose shape is numeric compares a set containing category labels with a set containing natural numbers. The traceback highlights W as the mismatched subscript. If x is keyed by those labels, declare it with dict_keys=W and subscript it with each bound label through a decorator comprehension or W.map(...); if x is numerically indexed, use a natural-valued index or set instead.