/
xpress.py
281 lines (233 loc) · 11.9 KB
/
xpress.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
""" FICO XPRESS module
:Author: Jonathan Karr <jonrkarr@gmail.com>
:Date: 2017-11-22
:Copyright: 2017, Karr Lab
:License: MIT
"""
from ..core import (ModelType, ObjectiveDirection, Presolve,
SolveOptions, Solver, StatusCode, VariableType, Verbosity,
Constraint, LinearTerm, Model, QuadraticTerm, Term, Variable, Result, ConvOptError,
SolverModel)
try:
import capturer
except ModuleNotFoundError: # pragma: no cover
capturer = None # pragma: no cover
import numpy
import sys
try:
import xpress
except (ImportError, RuntimeError): # pragma: no cover
import warnings # pragma: no cover
warnings.warn('FICO XPRESS is not installed', UserWarning) # pragma: no cover
class XpressModel(SolverModel):
""" FICO XPRESS solver """
def load(self, conv_opt_model):
""" Load a model to XPRESS' data structure
Args:
conv_opt_model (:obj:`Model`): model
Returns:
:obj:`object`: the model in XPRESS' data structure
Raises:
:obj:`ConvOptError`: if the presolve mode is unsupported, a variable has an unsupported type,
an objective has an unsupported term, a constraint
has an unsupported term, or a constraint is unbounded
"""
solver_model = xpress.problem(name=conv_opt_model.name)
# variables
for variable in conv_opt_model.variables:
if variable.type == VariableType.binary:
vartype = xpress.binary
elif variable.type == VariableType.integer:
vartype = xpress.integer
elif variable.type == VariableType.continuous:
vartype = xpress.continuous
elif variable.type == VariableType.semi_integer:
vartype = xpress.semiinteger
elif variable.type == VariableType.semi_continuous:
vartype = xpress.semicontinuous
elif variable.type == VariableType.partially_integer:
vartype = xpress.partiallyinteger
else:
raise ConvOptError('Unsupported variable of type "{}"'.format(variable.type))
if variable.lower_bound is None:
lb = -float('inf')
else:
lb = variable.lower_bound
if variable.upper_bound is None:
ub = float('inf')
else:
ub = variable.upper_bound
solver_model.addVariable(xpress.var(name=variable.name, lb=lb, ub=ub, vartype=vartype))
# objective
if conv_opt_model.objective_direction in [ObjectiveDirection.max, ObjectiveDirection.maximize]:
sense = xpress.maximize
elif conv_opt_model.objective_direction in [ObjectiveDirection.min, ObjectiveDirection.minimize]:
sense = xpress.minimize
else:
raise ConvOptError('Unsupported objective direction "{}"'.format(conv_opt_model.objective_direction))
terms = []
for term in conv_opt_model.objective_terms:
if isinstance(term, LinearTerm):
i_variable = conv_opt_model.variables.index(term.variable)
terms.append(term.coefficient * solver_model.getVariable(index=i_variable))
elif isinstance(term, QuadraticTerm):
i_variable_1 = conv_opt_model.variables.index(term.variable_1)
i_variable_2 = conv_opt_model.variables.index(term.variable_2)
terms.append(term.coefficient
* solver_model.getVariable(index=i_variable_1)
* solver_model.getVariable(index=i_variable_2))
else:
raise ConvOptError('Unsupported objective term of type "{}"'.format(term.__class__.__name__))
solver_model.setObjective(xpress.Sum(terms), sense)
# constraints
for constraint in conv_opt_model.constraints:
body = []
for term in constraint.terms:
if isinstance(term, LinearTerm):
i_variable = conv_opt_model.variables.index(term.variable)
body.append(term.coefficient * solver_model.getVariable(index=i_variable))
# elif isinstance(term, QuadraticTerm):
# :todo: implement quadratic constraints
# raise ConvOptError('Unsupported constraint term of type "{}"'.format(term.__class__.__name__))
else:
raise ConvOptError('Unsupported constraint term of type "{}"'.format(term.__class__.__name__))
if constraint.lower_bound is None and constraint.upper_bound is None:
raise ConvOptError('Constraints must have at least one bound')
elif constraint.lower_bound is None:
solver_model.addConstraint(xpress.constraint(body=xpress.Sum(
body), ub=constraint.upper_bound, sense=xpress.leq, name=constraint.name))
elif constraint.upper_bound is None:
solver_model.addConstraint(xpress.constraint(body=xpress.Sum(
body), lb=constraint.lower_bound, sense=xpress.geq, name=constraint.name))
elif constraint.lower_bound == constraint.upper_bound:
solver_model.addConstraint(xpress.constraint(body=xpress.Sum(
body), rhs=constraint.lower_bound, sense=xpress.eq, name=constraint.name))
else:
# :todo: figure out how to use xpress.range type
solver_model.addConstraint(xpress.constraint(body=xpress.Sum(body), sense=xpress.geq,
rhs=constraint.lower_bound, name=constraint.name + '__lower__'))
solver_model.addConstraint(xpress.constraint(body=xpress.Sum(body), sense=xpress.leq,
rhs=constraint.upper_bound, name=constraint.name + '__upper__'))
return solver_model
def solve(self):
""" Solve the model
Returns:
:obj:`Result`: result
"""
model = self._model
# verbosity
if self._options.verbosity == Verbosity.off:
model.setlogfile('xpress.log')
# presolve
if self._options.presolve == Presolve.on:
if capturer and self._options.verbosity.value <= Verbosity.error.value:
capture_output = capturer.CaptureOutput(merged=False, relay=False)
capture_output.start_capture()
model.presolve()
if capturer and self._options.verbosity.value <= Verbosity.error.value:
capture_output.finish_capture()
err = capture_output.stderr.get_text()
if err and self._options.verbosity == Verbosity.error:
sys.stderr.write(err)
else:
model.presolve()
elif self._options.presolve != Presolve.off:
raise ConvOptError('Unsupported presolve mode "{}"'.format(self._options.presolve))
# tune
if self._options.tune:
raise ConvOptError('Unsupported tuning mode "{}"'.format(self._options.tune))
# solve
model.solve()
# get status
if self.is_mixed_integer():
if model.getProbStatus() == xpress.mip_optimal:
status_code = StatusCode.optimal
elif model.getProbStatus() == xpress.mip_infeas:
status_code = StatusCode.infeasible
else:
status_code = StatusCode.other
elif self.is_non_linear():
if model.getProbStatus() in [xpress.nlp_locally_optimal]:
status_code = StatusCode.optimal
elif model.getProbStatus() in [xpress.lp_infeas, xpress.nlp_infeasible]:
status_code = StatusCode.infeasible
else:
status_code = StatusCode.other
else:
if model.getProbStatus() == xpress.lp_optimal:
status_code = StatusCode.optimal
elif model.getProbStatus() == xpress.lp_infeas:
status_code = StatusCode.infeasible
else:
status_code = StatusCode.other
status_message = model.getProbStatusString()
# get solution
if status_code == StatusCode.optimal:
value = model.getObjVal()
primals = numpy.array([model.getSolution(var) for var in model.getVariable()])
if self.is_mixed_integer():
reduced_costs = numpy.full((len(model.getVariable()), ), numpy.nan)
duals = numpy.array([numpy.nan
for constraint in model.getConstraint() if not constraint.name.endswith('__upper__')])
else:
reduced_costs = numpy.array([model.getRCost(var) for var in model.getVariable()])
duals = numpy.array([model.getDual(constraint)
for constraint in model.getConstraint() if not constraint.name.endswith('__upper__')])
else:
value = numpy.nan
primals = numpy.full((len(model.getVariable()), ), numpy.nan)
reduced_costs = numpy.full((len(model.getVariable()), ), numpy.nan)
duals = numpy.array([numpy.nan
for constraint in model.getConstraint() if not constraint.name.endswith('__upper__')])
return Result(status_code, status_message, value, primals, reduced_costs, duals)
def get_stats(self):
""" Get diagnostic information about the model
Returns:
:obj:`dict`: diagnostic information about the model
"""
model = self._model
sol = model.getSolution()
stats = {}
stats['slacks'] = [numpy.nan] * len(model.getConstraint())
model.calcslacks(sol, stats['slacks'])
if not self.is_non_linear():
stats['row_status'] = [0] * len(model.getConstraint())
stats['col_status'] = [0] * len(model.getVariable())
model.getbasis(stats['row_status'], stats['col_status'])
stats['primal_infeasible_variables'] = []
stats['primal_infeasible_constraints'] = []
stats['dual_infeasible_constraints'] = []
stats['dual_infeasible_variables'] = []
if not self.is_mixed_integer():
model.getinfeas(
stats['primal_infeasible_variables'], stats['primal_infeasible_constraints'],
stats['dual_infeasible_constraints'], stats['dual_infeasible_variables'])
duals = model.getDual()
stats['abs_primal_infeas'] = model.calcsolinfo(sol, duals, xpress.solinfo_absprimalinfeas)
stats['rel_primal_infeas'] = model.calcsolinfo(sol, duals, xpress.solinfo_relprimalinfeas)
stats['abs_dual_infeas'] = model.calcsolinfo(sol, duals, xpress.solinfo_absdualinfeas)
stats['rel_dual_infeas'] = model.calcsolinfo(sol, duals, xpress.solinfo_reldualinfeas)
stats['max_mip_fractional'] = model.calcsolinfo(sol, duals, xpress.solinfo_maxmipfractional)
stats['sos'] = model.getSOS()
return stats
def is_mixed_integer(self):
""" Determine if the model has at least one binary or integer variable
Returns:
:obj:`bool`: :obj:`True` if the model has at least one binary or integer variable, and false otherwise
"""
for variable in self._model.getVariable():
if variable.vartype in [xpress.binary, xpress.integer, xpress.semiinteger, xpress.partiallyinteger]:
return True
return False
def is_non_linear(self):
""" Determine if the model is non-linear
Returns:
:obj:`bool`: :obj:`True` if the model is non-linear and false otherwise
"""
n_var = len(self._model.getVariable())
size = int((n_var + 1) * n_var / 2)
mstart = [0.] * size
mclind = [0.] * size
dobjval = [0.] * size
self._model.getmqobj(mstart, mclind, dobjval, size, 0, n_var - 1)
return len(dobjval) != 0