Skip to content

Concepts

This page explains how misdp sees a problem and what happens between your Python code and the CBF file.

The problem class: dual form

misdp writes every problem in dual form: scalar variables x_1, ..., x_n and constraints that are affine in them.

min or max   Σ_j c_j x_j + c_0
s.t.         a_i · x + b_i ∈ K_i          for every row i, K_i ∈ {= 0, ≥ 0, ≤ 0}
             Σ_j x_j H_kj + D_k ⪰ 0       for every PSD block k
             lb_j ≤ x_j ≤ ub_j,  x_j integer where requested

A row is one scalar linear constraint. A PSD block is one linear matrix inequality (LMI) with symmetric matrices H_kj and D_k. In CBF the rows are CON with the cones L=, L+, L-, and the PSD blocks are PSDCON with HCOORD and DCOORD.

Why dual form only? CBF can mark only scalar variables as integer, so an integer matrix must be built from integer scalars anyway. SCIP-SDP also converts matrix variables (PSDVAR) to scalars internally. misdp therefore never writes PSDVAR.

Matrix variables are containers of scalars

A matrix variable of size n is a symmetric container of n(n+1)/2 scalar variables, one per entry of the lower triangle, created row by row:

import misdp as ms

m = ms.Model("concepts")
X = m.add_matrix_var(3, name="X")

assert X[0, 1] is X[1, 0]  # one variable for both entries
names = [v.name for v in m.variables]
assert names == ["X[0,0]", "X[1,0]", "X[1,1]", "X[2,0]", "X[2,1]", "X[2,2]"]

So you can use an entry anywhere a scalar variable fits, and you can make single entries integer:

m.set_vtype(X.diagonal(), "integer")
m.set_bounds(X[2, 0], lb=-1, ub=1)

X >> 0 turns the whole matrix into one PSD block: each entry variable gets the matrix that has a 1 at its position (two positions off the diagonal).

Expressions

Arithmetic on variables builds expressions right away; there are no expression trees.

  • A LinExpr is a coefficient per variable plus a constant: 2 * x + y - 1.
  • A MatExpr is a symmetric coefficient matrix per variable plus a constant matrix: ms.matrix([[x, 1], [1, y]]), x * A + D with numpy arrays, or X - 2 * Y with matrix variables.

Comparisons make constraints:

Code Meaning
expr == 3, expr <= 3, expr >= 3 linear constraint (one row)
M >> 0, M >> A, M << A M ⪰ 0, M - A ⪰ 0, A - M ⪰ 0 (one PSD block)

A - B >> 0 means (A - B) >> 0, because >> binds weaker than + and -.

A chained comparison such as 0 <= x <= 1 does not work in Python the way it reads; misdp raises ChainedComparisonError instead of silently dropping one side:

x = m.add_var("x")
try:
    m.add_constraint(0 <= x <= 1)
except ms.ChainedComparisonError as error:
    print(error)

m.set_bounds(x, lb=0, ub=1)  # bounds belong on the variable

Products of two variables are not linear and raise NonlinearExpressionError.

Long sums: use quicksum

The built-in sum creates a new expression for every term, so it takes time quadratic in the number of terms (about 0.2 s for 10 000 terms). ms.quicksum builds the sum in place:

v = m.add_vars(1000, name="v")
total = ms.quicksum(v[i] for i in range(1000))

numpy scalars

Write the variable first, x * 2, or convert numpy scalars with float(...). A numpy scalar on the left, as in np.int64(2) * x or np.float64(2) <= x, works at run time, but type checkers such as mypy can reject it or infer the wrong type because of numpy's type stubs.

Names and constraint groups

Every variable and constraint has a name. Names start with an ASCII letter or underscore and contain only ASCII letters, digits and underscores. Variable names are unique across scalar, vector and matrix variables, and constraint names are unique among the constraints. When you leave them out, misdp uses x0, x1, ... for scalars, v0, X0, ... for vectors and matrices, and c0, psd0, ... for constraints.

m.add_constraint(...) with a name (or latex) creates a constraint group, also for a single constraint. With several constraints the members are called name[0], name[1], ..., and the group keeps the latex string that describes all of them. Use m.add_constraint(constraint, name="cap", group=False) to name one constraint without creating a group.

Bounds and binary variables

Bounds live on the variable (add_var(..., lb=0, ub=10) or set_bounds). CBF has no bounds section, so the writer adds one row per finite bound. CBF also has no binary type: a binary variable is an integer variable with bounds 0 and 1.

The pipeline: user API → IR → writer

Your model never goes straight to the file. It passes through an intermediate representation (IR), the class misdp.ProblemIR:

 your code ─► Model ─► compile() ─► ProblemIR ─► CBF writer ─► .cbf file ─► SCIP-SDP
  1. User API (misdp.core): Model, variables, expressions and constraints. This layer checks your input and raises errors with the names you chose.
  2. Canonicalizer (Model.compile()): turns the model into a ProblemIR. It collects coefficients, drops exact zeros, turns constraints into rows and PSD blocks in dual form, and orders the rows by cone (L=, L+, L-). Bounds stay on the variables; the writer turns them into rows.
  3. IR (misdp.ir): frozen dataclasses that hold the mathematical problem plus metadata (names, vector and matrix structure, groups). The writer reads only the IR.
  4. Writer (misdp.io): write_cbf turns the IR into CBF version 2 text. The same model built in the same order always gives a byte-identical file.

You can run each step yourself:

m.optimize(x + X[0, 0])
m.add_constraint(X >> 0)

ir = m.compile()  # ProblemIR
text = ms.write_cbf(ir)  # CBF text (or ms.write_cbf(ir, "file.cbf"))
assert text == m.to_cbf()  # the same file as the model writes

Annotations

CBF has no place for names or structure, so misdp stores them in comment lines at the top of the file, before VER:

# misdp-cbf 1
# misdp-model {"name": "beispiel_a", "sense": "MIN", "has_objective": true}
# misdp-var 0 "y1"

Solvers skip comment lines. They keep your names, vector and matrix variables and constraint groups in the file. m.write(path, annotate=False) leaves them out.

Errors and warnings

All errors derive from ms.MisdpError; modeling mistakes raise a subclass of ms.ModelingError. Each message names the element by your name and usually suggests a fix. Warnings derive from ms.MisdpWarning, for example NoObjectiveWarning when a model has no objective. The API reference lists all classes (on the built site).