-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinopt.py
29 lines (22 loc) · 856 Bytes
/
linopt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from mosek.fusion import *
# def main(args):
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]]
c = [3.0, 1.0, 5.0, 1.0]
# Create a model with the name 'lo1'
with Model("lo1") as M:
# Create variable 'x' of length 4
x = M.variable("x", 4, Domain.greaterThan(0.0))
# Create constraints
M.constraint(x.index(1), Domain.lessThan(10.0))
M.constraint("c1", Expr.dot(A[0], x), Domain.equalsTo(30.0))
M.constraint("c2", Expr.dot(A[1], x), Domain.greaterThan(15.0))
M.constraint("c3", Expr.dot(A[2], x), Domain.lessThan(25.0))
# Set the objective function to (c^t * x)
M.objective("obj", ObjectiveSense.Maximize, Expr.dot(c, x))
# Solve the problem
M.solve()
# Get the solution values
sol = x.level()
print('\n'.join(["x[%d] = %f" % (i, sol[i]) for i in range(4)]))