7.8 Library of basic functions¶
This section contains a library of small models of basic functions frequently appearing in optimization models. It is essentially an implementation of the mathematical models from the MOSEK Modeling Cookbook using Fusion API for Python. These short code snippets can be seen as illustrative examples, can be copy-pasted to other code, and can even be directly called when assembling optimization models as we show in Sec. 7.8.6 (Model assembly example) (although this may be more suitable for prototyping; also note that additional variables and constraints will be introduced and there is no error checking).
7.8.1 Variable and constraint management¶
7.8.3 Quadratic and power operations¶
Square¶
\(t\geq x^2\)
2-norm¶
\(t\geq \sqrt{\sum_i x_i^2}\)
Powers¶
\(t\geq |x|^p\), \(p>1\)
# t >= |x|^p (where p>1)
def pow(M, t, x, p):
M.constraint(Expr.hstack(t, 1, x), Domain.inPPowerCone(1.0/p))
\(t\geq 1/x^p,\ x>0\), \(p>0\)
p-norm¶
\(t\geq (\sum_i |x_i|^p)^{1/p}\), \(p>1\)
# t >= \|x\|_p (where p>1), x is a vector expression
def pnorm(M, t, x, p):
n = int(x.getSize())
r = M.variable(n)
M.constraint(Expr.sub(t, Expr.sum(r)), Domain.equalsTo(0.0))
M.constraint(Expr.hstack(Var.repeat(t,n), r, x), Domain.inPPowerCone(1.0-1.0/p))
Geometric mean¶
\(t\leq (x_1\cdot\cdots\cdot x_n)^{1/n}\), \(x_i>0\)
# |t| <= (x_1...x_n)^(1/n), x_i>=0, x is a vector expression of length >= 1
def geo_mean(M, t, x):
n = int(x.getSize())
if n==1:
abs(M, x, t)
else:
t2 = M.variable()
M.constraint(Expr.hstack(t2, x.index(n-1), t), Domain.inPPowerCone(1.0-1.0/n))
geo_mean(M, t2, x.slice(0,n-1))
7.8.4 Exponentials and logarithms¶
log¶
\(t\leq \log{x},\ x>0\)
exp¶
\(t\geq e^{x}\)
Entropy¶
\(t\geq x\log{x},\ x>0\)
Relative entropy¶
\(t\geq x\log{x/y},\ x,y>0\)
Log-sum-exp¶
\(\log{\sum_i e^{x_i}}\leq t\)
# log( sum_i(exp(x_i)) ) <= t, where x is a vector
def logsumexp(M, t, x):
n = int(x.getSize())
u = M.variable(n)
M.constraint(Expr.hstack(u, Expr.constTerm(n, 1.0), Expr.sub(x, Var.repeat(t, n))), Domain.inPExpCone())
M.constraint(Expr.sum(u), Domain.lessThan(1.0))
7.8.5 Integer Modeling¶
Semicontinuous variable¶
\(x\in\{0\}\cup[a,b]\), \(b>a>0\)
Indicator variable¶
\(x\neq 0 \implies t=1\). We assume \(x\) is a priori normalized so \(|x_i|\leq 1\).
Logical OR¶
At least one of the conditions is true.
Logical NAND¶
At most one of the conditions is true (also known as SOS1).
# at most one of x_1,...,x_n, where x is a binary vector (SOS1 constraint)
def logic_sos1(M, x):
M.constraint(Expr.sum(x), Domain.lessThan(1.0))
# NOT(x AND y), where x, y are binary
def logic_nand(M, x, y):
M.constraint(Expr.add(x, y), Domain.lessThan(1.0))
Cardinality bound¶
At most \(k\) of the continuous variables are nonzero. We assume \(x\) is a priori normalized so \(|x_i|\leq 1\).
7.8.6 Model assembly example¶
We now demonstrate how to quickly build a simple optimization model for the problem
or equivalently
def testExample():
M = Model()
x = M.variable()
y = M.variable()
t = M.variable(3)
M.constraint(Expr.sub(x, y), Domain.greaterThan(3.0))
norm2(M, t.index(0), Var.vstack(x,y))
log (M, t.index(1), y)
pow (M, t.index(2), x, 1.5)
M.objective(ObjectiveSense.Maximize, Expr.dot(t, [-1,1,-1]))