Error reference¶
This page lists the exceptions and warnings that this release raises, with a short example that
triggers it and what to do about it. All of them are available as ms.<Name> and in
misdp.errors.
- Every error on this page is a subclass of
MisdpError, so oneexcept ms.MisdpErrorcatches all of them, operator mistakes such asy * yincluded (see below). - Every warning is a subclass of
MisdpWarning. - Messages follow the pattern
<Kind> '<name>': <what is wrong>., often followed by a hint that says how to fix it.
The examples are Python sessions. They start with:
>>> import warnings
>>> import misdp as ms
Most errors are raised as soon as you write the faulty line. Some are raised only when the
model is compiled, which compile(), to_cbf() and write() all do first. The entries say which.
Catching errors¶
MisdpError¶
The base class of all errors. Besides the message, every error has three attributes:
message: the message without the hint;element: the name of the variable, constraint or model the error is about (orNone);hint: how to fix it (orNone).
>>> m = ms.Model("demo")
>>> try:
... m.add_var("y", vtype="int")
... except ms.MisdpError as err:
... print(type(err).__name__, "|", err.element, "|", err.hint)
InvalidVTypeError | y | Use 'continuous', 'integer' or 'binary'.
What to do: catch ms.MisdpError where your program can recover from any misdp
error, for example to report it and go on with the next model. Catch a subclass when only
that problem is expected.
Operator mistakes raise a MisdpError too: products of variables, divisions by variables
and powers with variables give a NonlinearExpressionError (y * y, X @ X, 2 / x,
y ** 2), shapes that do not fit give a DimensionError (X + y, y >= np.ones(3),
x >> 0), and operators misdp does not support give an UnsupportedConstruct (2 * y
on a vector variable, x < 1, x // 2, abs(x), x + 1j, and &, |, ~ or
arithmetic on constraints, such as (x <= 1) | (x >= 3)). Python's own TypeError
remains for operands of other types, such as x + "a", x + None, x + True and
x + Decimal(1).
>>> m = ms.Model("demo")
>>> y = m.add_vars(2, "y")
>>> try:
... y * y
... except ms.MisdpError as err:
... print(type(err).__name__, "|", err.element)
NonlinearExpressionError | y * y
Modeling errors¶
These come from building a model with the Python API.
ModelingError¶
The base class of all errors in this section, so except ms.ModelingError catches each
of them.
DimensionError¶
Sizes or shapes do not fit together, most often a matrix expression combined with a matrix of another size.
>>> import numpy as np
>>> m = ms.Model("demo")
>>> X = m.add_matrix_var(2, "X")
>>> _ = m.add_constraint(X >> np.eye(3))
Traceback (most recent call last):
...
misdp.errors.DimensionError: PSD constraint 'X >> 3x3 array': sizes differ (2x2 and 3x3).
What to do: make both sides the same n x n size, here np.eye(2). To compare a
matrix with a scalar x, write x * np.eye(n); only the number 0 stands for the zero
matrix. Other triggers: comparing matrices with ==, <= or >= (use >> or <<), a
matrix or vector as objective (reduce it with ms.trace, ms.inner or y.sum()), and
ms.matrix with rows that do not form a square.
NonSymmetricMatrixError¶
A numeric array in a matrix expression or PSD constraint, or the entries passed to
ms.matrix, are not symmetric.
>>> import numpy as np
>>> m = ms.Model("demo")
>>> X = m.add_matrix_var(2, "X")
>>> A = np.array([[1.0, 2.0], [0.0, 1.0]])
>>> _ = m.add_constraint(X >> A)
Traceback (most recent call last):
...
misdp.errors.NonSymmetricMatrixError: PSD constraint 'X >> 2x2 array': the array is not symmetric; the largest asymmetry is at position (0, 1): 2 against 0 at (1, 0). ...
What to do: pass a symmetric matrix. If the difference is rounding noise, or you meant
the symmetric part, use (A + A.T) / 2. With ms.matrix, write the same expression at
[i][j] and [j][i]. Entries may differ by at most 1e-9 * max(1, max |A|).
NonlinearExpressionError¶
An operation would make the model nonlinear: a product of two expressions that both
contain variables (x * y), a division by an expression with variables (2 / x), or a
power with a variable on either side (x ** 2, 2 ** x). It is raised when the
expression is built. The only power misdp accepts is ** 1: of a variable or scalar
expression it gives a scalar expression, of a matrix variable or matrix expression
(X ** 1) a matrix expression. A vector variable supports no operators except @, so
v ** 1 raises the UnsupportedConstruct that names that.
Other powers without variables, such as (x - x + 2) ** 2 where the variables cancel,
raise an UnsupportedConstruct instead; compute such powers
with plain numbers.
>>> m = ms.Model("demo")
>>> x = m.add_var(name="x")
>>> y = m.add_var(name="y")
>>> m.optimize(x * y)
Traceback (most recent call last):
...
misdp.errors.NonlinearExpressionError: Expression 'x * y': nonlinear expressions are not supported. Multiply and divide expressions only by numbers. ...
What to do: multiply and divide expressions only by numbers. A product of variables
needs an extra variable and linearization constraints (for binaries, McCormick
inequalities). A square such as t >= x**2 can often be written as a PSD constraint:
m.add_constraint(ms.matrix([[t, x], [x, 1]]) >> 0).
InvalidBoundsError¶
The bounds of a variable leave no valid value, most often a lower bound above the upper bound.
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=5, ub=1)
Traceback (most recent call last):
...
misdp.errors.InvalidBoundsError: Variable 'x': lower bound 5 is greater than upper bound 1.
What to do: check the order of lb and ub. For a variable without a bound, leave
lb or ub out or pass -math.inf or math.inf. Also raised for a bound that is not a
number, binary bounds outside [0, 1], and integer bounds with no integer between them.
InvalidVTypeError¶
vtype is not exactly one of "continuous", "integer" or "binary".
>>> m = ms.Model("demo")
>>> y = m.add_var("y", vtype="int")
Traceback (most recent call last):
...
misdp.errors.InvalidVTypeError: Variable 'y': invalid variable type 'int'. Use 'continuous', 'integer' or 'binary'.
What to do: spell the type out in lower case. Short forms such as "int" and Python
types such as int are not accepted.
DuplicateNameError¶
A name is already used in the model, or a label list contains the same label twice. Variables share one set of names; constraints and groups share another.
>>> m = ms.Model("demo")
>>> x = m.add_var("x")
>>> x2 = m.add_var("x", vtype="integer")
Traceback (most recent call last):
...
misdp.errors.DuplicateNameError: Variable 'x': the name is already used by a variable. Choose a different name.
What to do: give every variable its own name, or leave the name out and let misdp
choose one. To change a variable you already have, keep it and call m.set_vtype(x,
"integer") or m.set_bounds(...) instead of adding it again.
InvalidNameError¶
A name is not an ASCII identifier (letters A-Z and a-z, digits and underscores, not
starting with a digit; umlauts such as ä or ß are not allowed),
a name is not a str at all (also the name of a Model, such as ms.Model(1)),
or a label of a vector or matrix variable is not a valid label.
>>> m = ms.Model("demo")
>>> x = m.add_var("x[1]")
Traceback (most recent call last):
...
misdp.errors.InvalidNameError: Variable 'x[1]': invalid name. Names must start with an ASCII letter (A-Z, a-z) or underscore and contain only ASCII letters, digits and underscores.
What to do: use an identifier such as x_1. For indexed variables, create one vector or
matrix variable, x = m.add_vars(3, "x"), and index it (x[1]) instead of building
names like "x[1]" yourself.
IndexOutOfRangeError¶
An index or label of a vector or matrix variable does not exist.
>>> m = ms.Model("demo")
>>> y = m.add_vars(3, "y")
>>> y[3]
Traceback (most recent call last):
...
misdp.errors.IndexOutOfRangeError: Vector variable 'y': index 3 is out of range for size 3. Valid labels: 0, 1, 2.
What to do: use one of the labels in the message. A size n gives the labels 0 to
n - 1; a variable created with a list of labels accepts exactly those.
ModelMismatchError¶
Objects from two different models are combined, for example a constraint built from the variables of one model is added to another.
>>> m1 = ms.Model("first")
>>> m2 = ms.Model("second")
>>> x = m1.add_var("x")
>>> _ = m2.add_constraint(x >= 1)
Traceback (most recent call last):
...
misdp.errors.ModelMismatchError: Model 'second': the constraint uses variables of model 'first'. Add each constraint to the model whose variables it uses.
What to do: build each expression, constraint and objective only from variables of the model you add it to. A common cause in notebooks: the model was created again, but old variables from an earlier cell are still in use. Create the variables again too.
InfeasibleConstantConstraintError¶
A linear constraint has no variables left (all coefficients are zero or cancel) and is
false. It is raised when the model is compiled, not when the constraint is added. The
message shows the constraint moved to the form <constant> <sense> 0.
>>> m = ms.Model("demo")
>>> x = m.add_var("x")
>>> _ = m.add_constraint(0 * x >= 1, "c")
>>> m.compile()
Traceback (most recent call last):
...
misdp.errors.InfeasibleConstantConstraintError: Constraint 'c': the constraint has no variables and is false (-1 >= 0). ...
What to do: check the data that builds this constraint; often its coefficients are all
zero by mistake. As written, the model is infeasible, so fix the constraint or drop it. A
constant constraint that is true is dropped with a
TrivialConstraintWarning.
ChainedComparisonError¶
Python asked a constraint for a truth value. This happens with a chained comparison such
as 0 <= x <= 1, and when a constraint is used in if, and, or or not.
>>> m = ms.Model("demo")
>>> x = m.add_var("x")
>>> _ = m.add_constraint(0 <= x <= 1)
Traceback (most recent call last):
...
misdp.errors.ChainedComparisonError: Constraint 'x >= 0': a constraint has no truth value; this happens with a chained comparison such as 0 <= x <= 1 or ...
What to do: write the two sides as bounds, m.add_var("x", lb=0, ub=1) when you create
the variable or m.set_bounds(x, lb=0, ub=1) for an existing one, or as two constraints,
m.add_constraint([x >= 0, x <= 1]).
NonFiniteCoefficientError¶
An inf or nan reached an expression, a coefficient array or the objective, a NaN
bound was given, an expression was divided by zero, or a coefficient overflowed to inf.
>>> m = ms.Model("demo")
>>> x = m.add_var(name="x")
>>> cost = float("nan") # e.g. a missing value in a data file
>>> m.optimize(cost * x)
Traceback (most recent call last):
...
misdp.errors.NonFiniteCoefficientError: Number 'nan': expressions accept only finite numbers. Check the data for inf or nan values.
What to do: clean the input data before you build the model: fill in or drop missing
values and remove infinite ones (np.isfinite finds them). Infinite bounds are allowed;
a bound must not be NaN. Rescale data that overflows.
EmptyModelError¶
The model has no variables. It is raised when the model is compiled.
>>> m = ms.Model("demo")
>>> m.compile()
Traceback (most recent call last):
...
misdp.errors.EmptyModelError: Model 'demo': the model has no variables. Add variables with add_var, add_vars or add_matrix_var.
What to do: add at least one variable. In a script, check that the code creating the variables runs at all, for example that a loop does not run over empty data.
UnsupportedConstruct¶
Valid Python that has no meaning in an MISDP, most often a != constraint. Other
triggers: an operator misdp does not support (see MisdpError), such as
x < 1 or (x - x + 2) ** 2, add_constraint() given something that is not a
constraint, a constraint as objective, a sense other than "min" or "max", or an
argument of the wrong type.
>>> m = ms.Model("demo")
>>> x = m.add_var(name="x", vtype="integer")
>>> m.add_constraint(x != 3)
Traceback (most recent call last):
...
misdp.errors.UnsupportedConstruct: Expression 'x': '!=' constraints are not supported. Use ==, <= or >=. For an integer variable, x != a can be written with a binary variable and two inequalities.
What to do: follow the hint in the message. Use ==, <= or >= constraints, give
add_constraint() only constraints, and pass optimize a scalar linear expression.
A different cause: the file would be too large. ms.write_cbf,
to_cbf() and write() refuse a file with more than 10**6 variables, more than
10**6 constraint rows (bound rows included), or more than 10**6 lower-triangle
entries of all PSD blocks together.
As far as PSD entries go, a single block of size at most 1413 fits (a larger one is
already an IRValidationError, I9), but two blocks of size 1000 do
not:
misdp.errors.UnsupportedConstruct: Model 'big': the file would hold 1001000 PSD block entries (PSDCON), more than misdp supports (1000000). Make the model smaller.
What to do: make the model smaller.
The problem data (IR)¶
IRValidationError¶
A ProblemIR breaks one of its rules. ProblemIR.validate() (which
write_cbf runs first) collects all problems before it raises; ProblemIR.from_dict
reports the first wrong field.
>>> ms.ProblemIR.from_dict({"name": "demo", "sense": "min", "variables": []})
Traceback (most recent call last):
...
misdp.errors.IRValidationError: ProblemIR is invalid (1 problem):
- from_dict: ir.sense: expected 'MIN' or 'MAX', got 'min'.
What to do: fix the fields the listed problems name; the attribute problems holds
them as a list. You only meet this error when you build or edit a ProblemIR yourself. It
is easier to build the problem with ms.Model and call compile(), or to start from the
output of ProblemIR.to_dict().
Warnings¶
A warning does not stop your program: Python prints it and the code goes on. To see a
warning as an error with a traceback, as in the examples below, turn misdp warnings into
errors. The examples on this page run this way, and it is a good setting for your own
tests too:
>>> warnings.simplefilter("error", ms.MisdpWarning)
MisdpWarning¶
The base class of all warnings, so one filter covers them all:
warnings.simplefilter("error", ms.MisdpWarning) makes them errors, and
warnings.filterwarnings("ignore", category=ms.MisdpWarning) hides them. misdp also
issues plain MisdpWarning for problems that need no class of their own, for example a
PSD constraint without variables. It is written unchanged, but SCIP-SDP 4.4.0 refuses to
read a file with such a block:
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=0)
>>> m.optimize(x)
>>> _ = m.add_constraint(ms.matrix([[1, 0], [0, 1]]) >> 0)
>>> ir = m.compile()
Traceback (most recent call last):
...
misdp.errors.MisdpWarning: PSD constraint 'psd0': the constraint has no variables. It is written unchanged and not checked, ...
A PSD constraint whose variable coefficients all have magnitude at most 1e-9 gets a similar
warning from compile(): SCIP-SDP 4.4.0 drops such values while reading, which leaves a
block without variables, and then refuses the file. Such coefficients also get a
NumericsWarning.
What to do: read the message; it says what was ignored or dropped. Here: remove the
constant constraint (check the matrix yourself, e.g. with numpy.linalg.eigvalsh). For
a block of tiny coefficients, rescale the constraint or remove it.
ObjectiveReplacedWarning¶
optimize was called on a model that already has an objective. The last
call wins.
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=0)
>>> m.optimize(x)
>>> m.optimize(2 * x, sense="max")
Traceback (most recent call last):
...
misdp.errors.ObjectiveReplacedWarning: Model 'demo': the objective 'maximize 2*x' replaces the earlier objective 'minimize x'.
What to do: set the objective once. A second optimize does not add to the first:
build the whole expression first (for example with ms.quicksum) and pass it once. If
you replaced it on purpose, ignore the warning.
NoObjectiveWarning¶
The model has no objective when it is compiled. It is written as a feasibility problem
(OBJSENSE MIN with a zero objective).
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=0)
>>> _ = m.add_constraint(x >= 1)
>>> ir = m.compile()
Traceback (most recent call last):
...
misdp.errors.NoObjectiveWarning: Model 'demo': no objective was set; it is written as a feasibility problem (OBJSENSE MIN, zero objective). ...
What to do: call m.optimize(...) or m.optimize(..., sense="max"). For a feasibility
problem on purpose, write m.optimize(0); that says so and stops the warning.
BoundsRoundedWarning¶
An integer or binary variable got fractional bounds; misdp rounded them inward.
>>> m = ms.Model("demo")
>>> n = m.add_var("n", vtype="integer", lb=0.5, ub=3.7)
Traceback (most recent call last):
...
misdp.errors.BoundsRoundedWarning: Variable 'n': fractional bounds [0.5, 3.7] of an integer variable were rounded inward to [1, 3].
What to do: give integer variables whole-number bounds, here lb=1, ub=3. Rounding
inward allows the same integers, so if the fractional values are what your data says, the
warning is harmless. If the variable should be continuous, use vtype="continuous".
TrivialConstraintWarning¶
A linear constraint has no variables left (for example because terms cancel) and always holds. It is dropped when the model is compiled.
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=0)
>>> m.optimize(x)
>>> _ = m.add_constraint(x - x <= 1)
>>> ir = m.compile()
Traceback (most recent call last):
...
misdp.errors.TrivialConstraintWarning: Model 'demo': dropped 1 constraint without variables that always hold: 'c0'.
What to do: usually harmless. Check the named constraints: did terms cancel, or did coefficients turn out zero when a loop built them? Fix the expression or leave the constraint out.
NumericsWarning¶
A value that SCIP-SDP cannot handle reliably. compile() warns about a coefficient (of a
constraint or the objective) or a constraint constant with magnitude above 1e8 or a
nonzero magnitude below 1e-8, such as a big-M constant of 1e9. Bounds and the objective
constant are not part of this check (ub=1e9 is often meant as a big-M bound):
>>> m = ms.Model("demo")
>>> x = m.add_var("x", lb=0)
>>> b = m.add_var("b", vtype="binary")
>>> m.optimize(x)
>>> _ = m.add_constraint(x <= 1e9 * b)
>>> ir = m.compile()
Traceback (most recent call last):
...
misdp.errors.NumericsWarning: Model 'demo': 1 value is outside the range 1e-08 <= |value| <= 1e+08 that SCIP-SDP handles reliably ...
SCIP-SDP 4.4.0 drops a nonzero coefficient or constraint constant of magnitude 1e-9 or
less while reading the file, and says so only in its log
(ACOORD: Remove 1 coefficients with absolute value less than epsilon = 1e-09.). A row
of such values, like 1e-10 * x >= 1e-10, becomes 0 >= 0 and constrains nothing:
minimizing x with lb=0 gives 0 instead of 1. Rescaled to x >= 1, it gives 1.
compile() and writing a CBF file also warn about any value written to the file with
magnitude 1e20 or more, bounds and the objective constant included (for example
ub=1e20; an infinite bound is not written): SCIP-SDP 4.4.0 reads such values as
infinite and refuses the file. An objective constant of that size is read, but SCIP-SDP
then reports a meaningless objective value and status; leave the constant out and add it
to the result yourself. to_cbf() and write() compile and then write, but Python's
default warning filter shows this warning only once, because both warnings have the same
text and point to the same line.
What to do: rescale the model so that values stay between 1e-8 and 1e8: change units, make big-M constants as small as is still valid, and replace near-zero coefficients (often rounding noise) with 0. For a variable without a bound, leave the bound out instead of writing 1e20.