Constraints' expression for linear optimizer functions

17 views
Skip to first unread message

Linh Dang

unread,
Feb 13, 2025, 5:03:25 AMFeb 13
to mosek
Dear all,

I'm using Mosek Fusion for python to solve this linear programming problem

Screenshot 2025-02-13 163205.png
with the shape of each variable as follows:
s - (n,)
q - (m,)
z - scalar variable
τ - scalar
R - (m, n)
r_bar - (n,)

I have setup the model as:
m, n = R.shape
M = Model('model1')
s = M.variable('s', n)
q = M.variable('q', m, Domain.greaterThan(0))
z = M.variable('z', 1, Domain.greaterThan(0))

objective = Expr.sub(Expr.dot(s, r_bar), Expr.mul(z, tau))
M.objective('objective', ObjectiveSense.Maximize, objective)

M.constraint('cond1'
,        Expr.hstack(q, Expr.sub(Expr.mul(z, tau), Expr.mul(s.T, return_matrix))),
        Domain.greaterThan(0)
        )
M.constraint('cond2', Expr.sum(q), Domain.equalsTo(1))
M.constraint('
cond3', Expr.sum(s), Domain.equalsTo(z))
M.constraint('min_weight', Expr.mul(z, min_weight) <= s)
M.constraint('max_weight', s <= Expr.mul(z, max_weight))
M.constraint('cond4', Expr.dot(s, 
r_bar) >= Expr.mul(z, tau))
M.solve()

I keep having errors with cond1, where it's setting conditions for each row in q. I tried looping through each row using this constraint but it didn't work either.
for i in range(n_days):
    M.constraint(
        Expr.hstack(q.index(i), Expr.sub(Expr.mul(z, tau), Expr.dot(s, return_matrix[i, :]))),
        Domain.greaterThan(0)
    )

Can you please help?

Michal Adamaszek

unread,
Feb 13, 2025, 5:12:56 AMFeb 13
to mosek
You want to sub and not hstack, and repeat the variable to correct shape

M.constraint('cond1'
,        Expr.sub(q, Expr.sub(Expr.mul(Var.repeat(z,m), tau), Expr.mul(s.T, return_matrix))),
        Domain.greaterThan(0)
        )



M.constraint('cond1',  q >= Var.repeat(z,m) @ tau - s.T @ return_matrix )

Michal
Reply all
Reply to author
Forward
0 new messages