Skip to content

Instantly share code, notes, and snippets.

[[_ ♕ _ _ _ _ _ _]
[_ _ _ _ _ ♕ _ _]
[♕ _ _ _ _ _ _ _]
[_ _ _ _ _ _ ♕ _]
[_ _ _ ♕ _ _ _ _]
[_ _ _ _ _ _ _ ♕]
[_ _ ♕ _ _ _ _ _]
[_ _ _ _ ♕ _ _ _]]
from mosek.fusion import *
import numpy as np
import sys
n = int(sys.argv[1])
M = Model()
x = M.variable("x", [n,n], Domain.binary())
M.objective(ObjectiveSense.Maximize, Expr.sum(x))
M.constraint(Expr.sum(x, 0), Domain.lessThan(1))
M.constraint(Expr.sum(x, 1), Domain.lessThan(1))
from mosek.fusion import *
import numpy as np
M = Model()
x = M.variable(6)
groups = [ [0,1,2], [3], [4,5] ]
# Some data
bounds = [ 1.0, 2.0, 3.0 ]
# Membership matrix
Mi = [0, 0, 0, 1, 2, 2] # Rows, Group number for entries of x
Mj = [0, 1, 2, 3, 4, 5] # Columns
Mv = [1.0] * 6 # Values
Memb = Matrix.sparse(3, 6, Mi, Mj, Mv)
# Bounded sums
M.constraint( Expr.mul(Memb, x), Domain.lessThan(bounds) )
# Volatility bound
M.constraint( Expr.vstack(gamma,
Expr.mul(G, Expr.sub( Expr.mul(Memb, x), ind_weights))),
Domain.inQCone() )
z = M.variable(3, Domain.binary()) # Indicates sign for each group
bigM = 100
# All items in one group have the same sign
zMatch = Expr.mul(Memb.transpose(), z)
M.constraint( Expr.sub(x, Expr.mul(bigM, zMatch)), Domain.lessThan(0) )
M.constraint( Expr.sub(Expr.mul(-bigM, Expr.sub(1, zMatch)), x), Domain.lessThan(0) )
import mosek.fusion.*;
c = [ 3.0, 1.0, 5.0, 1.0 ];
A = [ 3.0, 1.0, 2.0, 0.0 ; ...
2.0, 1.0, 3.0, 1.0 ; ...
0.0, 2.0, 0.0, 3.0 ];
b = [ 2.0, 5.0, 3.0 ];
x.index(0), x.index(1), x.index(2), x.index(3)
x = M.variable('x', 4, Domain.greaterThan(0.0));
javaaddpath mosek/9.0/tools/platform/linux64x86/bin/mosek.jar