forked from XiongPengNUS/rsome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msk_solver.py
137 lines (108 loc) · 4.74 KB
/
msk_solver.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""
This module is used as an interface to call the MOSEK solver for solving
(mixed-integer) linear or second-order cone programs.
"""
import mosek
import numpy as np
from scipy.sparse import coo_matrix
from .socp import SOCProg
import warnings
import time
from .lp import Solution
def solve(form, display=True, export=False, params={}):
numlc, numvar = form.linear.shape
if isinstance(form, SOCProg):
qmat = form.qmat
else:
qmat = []
ind_int = np.where(form.vtype == 'I')[0]
ind_bin = np.where(form.vtype == 'B')[0]
if ind_bin.size:
form.ub[ind_bin] = 1
form.lb[ind_bin] = 0
ind_ub = np.where((form.ub != np.inf) & (form.lb == -np.inf))[0]
ind_lb = np.where((form.lb != -np.inf) & (form.ub == np.inf))[0]
ind_ra = np.where((form.lb != -np.inf) & (form.ub != np.inf))[0]
ind_fr = np.where((form.lb == -np.inf) & (form.ub == np.inf))[0]
ind_eq = np.where(form.sense)[0]
ind_ineq = np.where(form.sense == 0)[0]
with mosek.Env() as env:
with env.Task(0, 0) as task:
task.appendvars(numvar)
task.appendcons(numlc)
if ind_ub.size:
task.putvarboundlist(ind_ub,
[mosek.boundkey.up] * len(ind_ub),
form.lb[ind_ub], form.ub[ind_ub])
if ind_lb.size:
task.putvarboundlist(ind_lb,
[mosek.boundkey.lo] * len(ind_lb),
form.lb[ind_lb], form.ub[ind_lb])
if ind_ra.size:
task.putvarboundlist(ind_ra,
[mosek.boundkey.ra] * len(ind_ra),
form.lb[ind_ra], form.ub[ind_ra])
if ind_fr.size:
task.putvarboundlist(ind_fr,
[mosek.boundkey.fr] * len(ind_fr),
form.lb[ind_fr], form.ub[ind_fr])
if ind_int.size:
task.putvartypelist(ind_int,
[mosek.variabletype.type_int]
* len(ind_int))
if ind_bin.size:
task.putvartypelist(ind_bin,
[mosek.variabletype.type_int]
* len(ind_bin))
task.putcslice(0, numvar, form.obj.flatten())
task.putobjsense(mosek.objsense.minimize)
coo = coo_matrix(form.linear)
task.putaijlist(coo.row, coo.col, coo.data)
if ind_eq.size:
task.putconboundlist(ind_eq, [mosek.boundkey.fx] * len(ind_eq),
form.const[ind_eq], form.const[ind_eq])
if ind_ineq.size:
task.putconboundlist(ind_ineq,
[mosek.boundkey.up] * len(ind_ineq),
[-np.inf] * len(ind_ineq),
form.const[ind_ineq])
for cone in qmat:
task.appendcone(mosek.conetype.quad,
0.0, cone)
if display:
print('Being solved by Mosek...', flush=True)
time.sleep(0.2)
try:
for param, value in params.items():
if isinstance(value, float):
task.putdouparam(getattr(mosek.dparam, param), value)
if isinstance(value, int):
task.putintparam(getattr(mosek.iparam, param), value)
if isinstance(value, str):
task.putstrparam(getattr(mosek.sparam, param), value)
except (TypeError, ValueError):
raise ValueError('Incorrect parameters or values.')
t0 = time.time()
task.optimize()
stime = time.time() - t0
soltype = mosek.soltype
solsta = None
for stype in [soltype.bas, soltype.itr, soltype.itg]:
try:
solsta = task.getsolsta(stype)
if display:
print('Solution status: {0}'.format(solsta.__repr__()))
print('Running time: {0:0.4f}s'.format(stime))
break
except:
pass
xx = [0.] * numvar
task.getxx(stype, xx)
if export:
task.writedata("out.mps")
if solsta in [mosek.solsta.optimal, mosek.solsta.integer_optimal]:
solution = Solution(xx @ form.obj.flatten(), xx, solsta)
else:
warnings.warn('No feasible solution can be found.')
solution = None
return solution