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 .NET. 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¶
Variable duplication¶
\(x=y\)
7.8.2 Linear operations¶
Absolute value¶
\(t\geq |x|\)
1-norm¶
\(t\geq \sum_i |x_i|\)
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)
public static void pow(Model M, Variable t, Variable x, double 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 Variable
public static void pnorm(Model M, Variable t, Variable x, double p) {
int n = (int) x.GetSize();
Variable 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 Variable of length >= 1
public static void geo_mean(Model M, Variable t, Variable x) {
int n = (int) x.GetSize();
if (n==1) {
abs(M, x, t);
}
else {
Variable 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
public static void logsumexp(Model M, Variable t, Variable x) {
int n = (int) x.GetSize();
Variable 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\)
// x = 0 or a <= x <= b
public static void semicontinuous(Model M, Variable x, double a, double b) {
Variable u = M.Variable(x.GetShape(), Domain.Binary());
M.Constraint(Expr.Sub(x, Expr.Mul(a, u)), Domain.GreaterThan(0.0));
M.Constraint(Expr.Sub(x, Expr.Mul(b, u)), Domain.LessThan(0.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.
// x OR y, where x, y are binary
public static void logic_or(Model M, Variable x, Variable y) {
M.Constraint(Expr.Add(x, y), Domain.GreaterThan(1.0));
}
// x_1 OR ... OR x_n, where x is a binary vector
public static void logic_or_vect(Model M, Variable x) {
M.Constraint(Expr.Sum(x), Domain.GreaterThan(1.0));
}
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)
public static void logic_sos1(Model M, Variable x) {
M.Constraint(Expr.Sum(x), Domain.LessThan(1.0));
}
// NOT(x AND y), where x, y are binary
public static void logic_nand(Model M, Variable x, Variable 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\).
// At most k of entries in x are nonzero, assuming in advance |x_i|<=1.
public static void card(Model M, Variable x, int k) {
Variable t = M.Variable(x.GetShape(), Domain.Binary());
abs(M, t, x);
M.Constraint(Expr.Sum(t), Domain.LessThan(k));
}
7.8.6 Model assembly example¶
We now demonstrate how to quickly build a simple optimization model for the problem
or equivalently
public static void testExample() {
Model M = new Model();
Variable x = M.Variable();
Variable y = M.Variable();
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, new double[]{-1,1,-1}));