-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcplexinterface.py
More file actions
197 lines (165 loc) · 5.32 KB
/
cplexinterface.py
File metadata and controls
197 lines (165 loc) · 5.32 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import cplex
import numpy as np
from scipy.sparse import *
"""
Converts a sparse matrix to column compressed format of CPLEX
"""
infinity = cplex.infinity
def sparseToList(A):
if type(A) == list:
return []
(n, m) = A.shape
# Going through columns
Aout = list()
for k in range(m):
rowind = A.indices[A.indptr[k] : A.indptr[k+1]]
first = []
for j in rowind:
first.append(int(j))
col = list()
for j in A.data[A.indptr[k] : A.indptr[k+1]]:
col.append(float(j))
second = col
Aout.append([first, second])
return Aout
class CplexQPSolver:
def __init__(self, verbose = False):
# Problem object
self.p = cplex.Cplex()
self.is_warm = False
self.verbose = verbose
# Setting options
self.p.parameters.qpmethod.set(0)
# Enabling pure convex objectives (only for barrier)
self.p.parameters.solutiontarget.set(2)
# Setting tolerance
self.p.parameters.barrier.convergetol.set(1E-12)
if not verbose:
self.p.parameters.barrier.display.set(self.p.parameters.barrier.display.values.none)
self.p.parameters.simplex.display.set(self.p.parameters.simplex.display.values.none)
self.p.set_results_stream(None)
else:
self.p.parameters.barrier.display.set(self.p.parameters.barrier.display.values.normal)
self.p.parameters.simplex.display.set(self.p.parameters.simplex.display.values.normal)
self.p.parameters.threads.set(1)
self.p.objective.set_sense(self.p.objective.sense.minimize)
self.p.parameters.barrier.crossover.set(1)######################################################### 1 = crossover active, 0 = crossover inactive
#---------------------------------------------------------------------------------
"""
Solves the QP with CPLEX
min 0.5 * x' Q x + c'x
s.t. A <= b
Aeq = b
lbx <= x <= ubx
"""
def cplex_solve(self, Q, c, A = [], b = [], Aeq = [], beq = [], lbx = [], ubx = [], outfile = ''):
p = self.p
# Preprocessing vectors a bit
if type(c) == np.ndarray:
if c.ndim == 2:
c = (c.T)[0].astype(float)
else:
c = c.astype(float)
if type(b) == np.ndarray:
if b.ndim == 2:
b = (b.T)[0].astype(float)
else:
b = b.astype(float)
if type(beq) == np.ndarray:
if beq.ndim == 2:
beq = (beq.T)[0].astype(float)
else:
beq = beq.astype(float)
if type(lbx) == np.ndarray:
if lbx.ndim == 2:
lbx = (lbx.T)[0].astype(float)
else:
lbx = lbx.astype(float)
for k in range(lbx.size):
if lbx[k] == -np.inf:
lbx[k] = -cplex.infinity
if type(ubx) == np.ndarray:
if ubx.ndim == 2:
ubx = (ubx.T)[0].astype(float)
else:
ubx = ubx.astype(float)
for k in range(ubx.size):
if ubx[k] == np.inf:
ubx[k] = cplex.infinity
if c != []:
c = c.tolist()
if b != []:
b = b.tolist()
if beq != []:
beq = beq.tolist()
if lbx == []:
lbx = [-cplex.infinity] * len(c)
else:
lbx = list(lbx)
if ubx == []:
ubx = [+cplex.infinity] * len(c)
else:
ubx = list(ubx)
Qconv = sparseToList(Q)
if A == []:
Ash = [0, 0]
else:
Ash = A.shape
if Aeq == []:
Aeqsh = [0, 0]
else:
Aeqsh = Aeq.shape
Aunited = []
if A == []:
Aunited = Aeq
elif Aeq == []:
Aunited = A
else:
Aunited = lil_matrix((Ash[0] + Aeqsh[0], Ash[1]))
Aunited[0 : Ash[0], 0 : Ash[1]] = A
Aunited[Ash[0] : Ash[0] + Aeqsh[0], 0 : Ash[1]] = Aeq
Aunited = Aunited.tocsc()
# Aconv = sparseToList(A)
# Aeqconv = sparseToList(Aeq)
Aconv = sparseToList(Aunited)
rhs = np.concatenate([b, beq])
sense = len(b) * 'L' + len(beq) * 'E'
# Setting up everything
if not self.is_warm:
p.linear_constraints.add(rhs = rhs, senses = sense)
p.variables.add(obj = c, lb = lbx, ub = ubx, columns = Aconv )
p.objective.set_quadratic(Qconv)
else:
p.parameters.qpmethod.set(0)
# Changing only linear term
linterm = []
for k in range(len(c)):
linterm.append((k, c[k]))
p.objective.set_linear(linterm)
# Changing quadratic term
p.objective.set_quadratic(Qconv)
# Injecting initial basis
p.start.set_start(self.basis_pr, self.basis_slack, self.x_opt, [], self.dual_opt, [])
if outfile != '':
p.write(outfile)
# Solving QP...
p.solve()
# Obtaining dual variables
mu = []
for k in range(Ash[0]):
mu.append(p.solution.get_dual_values(k))
lamb = []
for k in range(Ash[0], Ash[0] + Aeqsh[0]):
lamb.append(p.solution.get_dual_values(k))
psi = p.solution.get_reduced_costs()
self.x_opt = p.solution.get_values()
self.dual_opt = p.solution.get_dual_values()
f_opt = p.solution.get_objective_value()
sol_status = p.solution.get_status()
sol_string = p.solution.get_status_string()
# Obtaining basis
(self.basis_pr, self.basis_slack) = p.solution.basis.get_basis()
# for k in range(len(c)):
# x_opt.append(p.solution.get_values(k))
self.is_warm = True
return (self.x_opt, f_opt, mu, lamb, psi, sol_status, sol_string)