Tutorial¶
This tutorial builds three models step by step: the stable set example of the project
slides, Beispiel A, and Max-Cut. All three are also in the example collection
misdp.examples (see Example collection at the end). Install
misdp first (Getting started).
Each section is one Python script; run the code blocks of a section in order.
Stable set: matrix variables and constraint groups¶
The minimal example of the project slides works on a graph G = (V, E). It looks for a
symmetric 0/1 matrix X that is positive semidefinite, is zero on every edge, and has the
largest trace:
max tr(X)
s.t. X ⪰ 0
X_uv = 0 for every edge {u, v} in E
X ∈ {0, 1}^(n×n) symmetric
In misdp the model reads almost like the formula. The graph is the path 0–1–2:
import misdp as ms
E = [(0, 1), (1, 2)]
stable_set = ms.Model("stable_set")
X = stable_set.add_matrix_var(3, vtype="binary", name="X")
stable_set.add_constraint(X >> 0, name="psd")
stable_set.add_constraint(
(X[u, v] == 0 for u, v in E),
name="edge",
latex=r"X_{uv} = 0 \quad \forall \{u,v\} \in E",
)
stable_set.optimize(ms.trace(X), sense="max")
stable_set.write("stable_set.cbf")
What happens here:
add_matrix_var(3, ...)creates a symmetric 3×3 matrix variable. Behind the scenes it is six scalar variables, one per entry of the lower triangle;X[0, 1]andX[1, 0]are the same variable.vtype="binary"makes every entry 0 or 1.X >> 0is the constraint "X is positive semidefinite".stable_set.add_constraint(...)with a generator adds one constraint per edge. Because you gave aname, they form a constraint group callededge, with membersedge[0]andedge[1]. The optionallatexstring describes the whole group; the CBF file stores it in its annotation comments.ms.trace(X)is the sum of the diagonal entries.
print(stable_set.summary()) shows what the model contains:
print(stable_set.summary())
Warning: this model is a modeling demo, not an exact stable set model. The identity matrix
X = Isatisfies every constraint for every graph, so the optimum of this model is the number of nodes (here 3), not the size of the largest stable set (here 2). An exact formulation needsrank(X) = 1; rank-1 constraints are out of scope formisdp. The example stays because it shows the modeling features in a few lines.
Beispiel A: matrices from variables and numbers¶
Beispiel A comes from the project briefing. It has two integer variables and one 2×2 linear matrix inequality (LMI):
min y1 + y2
s.t. [ y1 1.5 ]
[ 1.5 y2 ] ⪰ 0
y1, y2 integer
ms.matrix builds a matrix expression from a nested list of variables, expressions and
numbers. The list must be square and symmetric.
import misdp as ms
beispiel_a = ms.Model("beispiel_a")
y1 = beispiel_a.add_var("y1", vtype="integer")
y2 = beispiel_a.add_var("y2", vtype="integer")
beispiel_a.add_constraint(ms.matrix([[y1, 1.5], [1.5, y2]]) >> 0)
beispiel_a.optimize(y1 + y2)
Papers often write an LMI in the form y1 A1 + y2 A2 + D ⪰ 0 with constant matrices. You
can write it exactly like that with numpy arrays; a variable times an array is a matrix
expression:
import numpy as np
A1 = np.array([[1.0, 0.0], [0.0, 0.0]])
A2 = np.array([[0.0, 0.0], [0.0, 1.0]])
D = np.array([[0.0, 1.5], [1.5, 0.0]])
paper_form = ms.Model("beispiel_a")
z1 = paper_form.add_var("y1", vtype="integer")
z2 = paper_form.add_var("y2", vtype="integer")
paper_form.add_constraint(z1 * A1 + z2 * A2 + D >> 0)
paper_form.optimize(z1 + z2)
assert paper_form.to_cbf() == beispiel_a.to_cbf()
print("Both forms give the same CBF file.")
Both models give the same file: misdp stores every matrix expression as one coefficient
matrix per variable plus a constant matrix, which is exactly the LMI form that CBF uses.
The optimum is 4. Without integrality it would be 3 (at y1 = y2 = 1.5): the matrix is
positive semidefinite exactly when y1, y2 >= 0 and y1 * y2 >= 2.25, and the best
integer points are (1, 3), (2, 2) and (3, 1).
To limit the search, add bounds when you create a variable (lb=0, ub=10) or later with
beispiel_a.set_bounds(y1, lb=0, ub=10).
Max-Cut: matrix expressions of binary variables¶
Max-Cut splits the nodes of a graph into two sides so that as many edges as possible run
between them. With x_v ∈ {-1, 1} for the side of node v, the matrix X = x xᵀ has a
unit diagonal, is positive semidefinite, and X_uv = -1 exactly when the edge uv is cut.
With one binary z_uv per node pair (z_uv = 1 means "cut") that gives:
max Σ_{uv ∈ E} z_uv
s.t. X ⪰ 0 where X_vv = 1 and X_uv = 1 - 2 z_uv for u < v
z_uv ∈ {0, 1}
A PSD matrix with unit diagonal and entries ±1 always has the form x xᵀ, so this model is
exact. On the 5-cycle the optimum is 4.
Here X is not a matrix variable but a matrix expression in the z variables:
import misdp as ms
E = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 0)]
n = 5
max_cut = ms.Model("max_cut")
z = {
(u, v): max_cut.add_var(f"z_{u}_{v}", vtype="binary")
for u in range(n)
for v in range(u + 1, n)
}
def entry(u, v):
"""Return X_uv as a number or an expression in z."""
if u == v:
return 1.0
return 1 - 2 * z[min(u, v), max(u, v)]
X = ms.matrix([[entry(u, v) for v in range(n)] for u in range(n)])
max_cut.add_constraint(X >> 0, name="psd")
max_cut.optimize(ms.quicksum(z[min(u, v), max(u, v)] for u, v in E), sense="max")
max_cut.write("max_cut.cbf")
print(max_cut.summary())
Two things to note:
ms.quicksumadds many terms quickly. The built-insumworks too, but it copies the sum for every term and gets slow for thousands of terms.- You could also model
Xas a matrix variable with extra rowsX[v, v] == 1andX[u, v] == 1 - 2 * z_uv. That gives the same problem with more variables; the expression is simpler.
Example collection¶
misdp.examples.REGISTRY holds these three examples. Each entry is a module with a
build(...) function, the expected optimum of its default instance (EXPECTED_OPTIMUM),
a title and a reference (TITLE, REFERENCE) and the LaTeX formulation (LATEX):
from misdp.examples import REGISTRY
for name, example in REGISTRY.items():
print(f"{name}: {example.TITLE} (optimum {example.EXPECTED_OPTIMUM})")
cut = REGISTRY["max_cut"].build([(0, 1), (1, 2), (2, 0)]) # a triangle
cut.write("triangle_cut.cbf")
The graph examples accept an edge list or a networkx graph
(python -m pip install "misdp[examples] @ git+https://github.com/tpse-tuda/MISDP.git@v0.1.0"
installs networkx). Two things to know before you pass your own graph:
- In an edge list the nodes are the numbers
0ton - 1, wherenis the largest node plus one.[(1, 2), (2, 3)]therefore has four nodes: node0is there too, without an edge, and changes the result. Number your nodes from 0. - For other node names, such as strings, pass a networkx graph.
help(REGISTRY["max_cut"].build) prints the parameters of an example; on the built site
they are in the API reference.