This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
forked from rte-france/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_solver.i
475 lines (416 loc) · 20.1 KB
/
linear_solver.i
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright 2010-2018 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This .i file exposes the linear programming and integer programming
//
// The python API is enriched by custom code defined here, making it
// extremely intuitive, like:
// solver = pywraplp.Solver(
// 'Example Solver', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)
// x1 = solver.NumVar(0.0, 1.0, 'x1')
// x2 = solver.NumVar(-3.0, 2.0, 'x2')
// c1 = solver.Add(2 * x1 - 3.2 * x2 + 1 <= 2.5)
// solver.Maximize(10 * x1 + 6 * x2)
//
// USAGE EXAMPLES:
// - examples/python/linear_programming.py
// - ./pywraplp_test.py
//
// TODO(user): test all the APIs that are currently marked as 'untested'.
%include "std_string.i"
%include "stdint.i"
%include "ortools/base/base.i"
%include "ortools/util/python/proto.i"
%import "ortools/util/python/vector.i"
// We need to forward-declare the proto here, so that the PROTO_* macros
// involving them work correctly. The order matters very much: this declaration
// needs to be before the %{ #include ".../linear_solver.h" %}.
namespace operations_research {
class MPModelProto;
class MPModelRequest;
class MPSolutionResponse;
} // namespace operations_research
%{
#include "ortools/linear_solver/linear_solver.h"
#include "ortools/linear_solver/model_exporter.h"
#include "ortools/linear_solver/model_exporter_swig_helper.h"
%}
%pythoncode {
import numbers
from ortools.linear_solver.linear_solver_natural_api import OFFSET_KEY
from ortools.linear_solver.linear_solver_natural_api import inf
from ortools.linear_solver.linear_solver_natural_api import LinearExpr
from ortools.linear_solver.linear_solver_natural_api import ProductCst
from ortools.linear_solver.linear_solver_natural_api import Sum
from ortools.linear_solver.linear_solver_natural_api import SumArray
from ortools.linear_solver.linear_solver_natural_api import SumCst
from ortools.linear_solver.linear_solver_natural_api import LinearConstraint
from ortools.linear_solver.linear_solver_natural_api import VariableExpr
} // %pythoncode
%extend operations_research::MPVariable {
std::string __str__() {
return $self->name();
}
std::string __repr__() {
return $self->name();
}
%pythoncode {
def __getattr__(self, name):
return getattr(VariableExpr(self), name)
} // %pythoncode
}
%extend operations_research::MPSolver {
// Change the API of LoadModelFromProto() to simply return the error message:
// it will always be empty iff the model was valid.
std::string LoadModelFromProto(const operations_research::MPModelProto& input_model) {
std::string error_message;
$self->LoadModelFromProto(input_model, &error_message);
return error_message;
}
std::string ExportModelAsLpFormat(bool obfuscated) {
operations_research::MPModelExportOptions options;
options.obfuscate = obfuscated;
operations_research::MPModelProto model;
$self->ExportModelToProto(&model);
return ExportModelAsLpFormat(model, options).value_or("");
}
std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscated) {
operations_research::MPModelExportOptions options;
options.obfuscate = obfuscated;
operations_research::MPModelProto model;
$self->ExportModelToProto(&model);
return ExportModelAsMpsFormat(model, options).value_or("");
}
/// Set a hint for solution.
///
/// If a feasible or almost-feasible solution to the problem is already known,
/// it may be helpful to pass it to the solver so that it can be used. A
/// solver that supports this feature will try to use this information to
/// create its initial feasible solution.
///
/// Note that it may not always be faster to give a hint like this to the
/// solver. There is also no guarantee that the solver will use this hint or
/// try to return a solution "close" to this assignment in case of multiple
/// optimal solutions.
void SetHint(const std::vector<operations_research::MPVariable*>& variables,
const std::vector<double>& values) {
if (variables.size() != values.size()) {
LOG(FATAL) << "Different number of variables and values when setting "
<< "hint.";
}
std::vector<std::pair<const operations_research::MPVariable*, double> >
hint(variables.size());
for (int i = 0; i < variables.size(); ++i) {
hint[i] = std::make_pair(variables[i], values[i]);
}
$self->SetHint(hint);
}
/// Sets the number of threads to be used by the solver.
bool SetNumThreads(int num_theads) {
return $self->SetNumThreads(num_theads).ok();
}
%pythoncode {
def Add(self, constraint, name=''):
if isinstance(constraint, bool):
if constraint:
return self.RowConstraint(0, 0, name)
else:
return self.RowConstraint(1, 1, name)
else:
return constraint.Extract(self, name)
def Sum(self, expr_array):
result = SumArray(expr_array)
return result
def RowConstraint(self, *args):
return self.Constraint(*args)
def Minimize(self, expr):
objective = self.Objective()
objective.Clear()
objective.SetMinimization()
if isinstance(expr, numbers.Number):
objective.SetOffset(expr)
else:
coeffs = expr.GetCoeffs()
objective.SetOffset(coeffs.pop(OFFSET_KEY, 0.0))
for v, c, in list(coeffs.items()):
objective.SetCoefficient(v, float(c))
def Maximize(self, expr):
objective = self.Objective()
objective.Clear()
objective.SetMaximization()
if isinstance(expr, numbers.Number):
objective.SetOffset(expr)
else:
coeffs = expr.GetCoeffs()
objective.SetOffset(coeffs.pop(OFFSET_KEY, 0.0))
for v, c, in list(coeffs.items()):
objective.SetCoefficient(v, float(c))
} // %pythoncode
// Catch runtime exceptions in class methods
%exception MPSolver {
try {
$action
} catch ( std::runtime_error& e ) {
SWIG_exception(SWIG_RuntimeError, e.what());
}
}
static double Infinity() { return operations_research::MPSolver::infinity(); }
void SetTimeLimit(int64 x) { $self->set_time_limit(x); }
int64 WallTime() const { return $self->wall_time(); }
int64 Iterations() const { return $self->iterations(); }
} // extend operations_research::MPSolver
%extend operations_research::MPVariable {
double SolutionValue() const { return $self->solution_value(); }
bool Integer() const { return $self->integer(); }
double Lb() const { return $self->lb(); }
double Ub() const { return $self->ub(); }
void SetLb(double x) { $self->SetLB(x); }
void SetUb(double x) { $self->SetUB(x); }
double ReducedCost() const { return $self->reduced_cost(); }
} // extend operations_research::MPVariable
%extend operations_research::MPConstraint {
double Lb() const { return $self->lb(); }
double Ub() const { return $self->ub(); }
void SetLb(double x) { $self->SetLB(x); }
void SetUb(double x) { $self->SetUB(x); }
double DualValue() const { return $self->dual_value(); }
} // extend operations_research::MPConstraint
%extend operations_research::MPObjective {
double Offset() const { return $self->offset();}
} // extend operations_research::MPObjective
PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver_pb2,
MPModelProto,
operations_research::MPModelProto);
PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver_pb2,
MPSolutionResponse,
operations_research::MPSolutionResponse);
// Actual conversions. This also includes the conversion to std::vector<Class>.
%define PY_CONVERT(Class)
%{
// Forcing SWIGTYPE_p_.... $descriptor( ) does not work outside of typemaps.
template<>
bool PyObjAs(PyObject *py_obj, operations_research::Class** b) {
return SWIG_ConvertPtr(py_obj, reinterpret_cast<void**>(b),
SWIGTYPE_p_operations_research__ ## Class,
SWIG_POINTER_EXCEPTION) >= 0;
}
PyObject* FromObject ## Class(operations_research::Class* obj) {
return SWIG_NewPointerObj(obj,
SWIGTYPE_p_operations_research__ ## Class,
SWIG_POINTER_NOSHADOW);
}
bool CanConvertTo ## Class(PyObject *py_obj) {
operations_research::Class* tmp;
return PyObjAs(py_obj, &tmp);
}
%}
%typemap(in) operations_research::Class* const {
if (!PyObjAs($input, &$1)) SWIG_fail;
}
%typecheck(SWIG_TYPECHECK_POINTER) operations_research::Class* const {
$1 = CanConvertTo ## Class($input);
if ($1 == 0) PyErr_Clear();
}
PY_LIST_OUTPUT_TYPEMAP(operations_research::Class*, CanConvertTo ## Class,
FromObject ## Class);
%enddef
PY_CONVERT(MPConstraint);
PY_CONVERT(MPVariable);
#undef PY_CONVERT
%ignoreall
%unignore operations_research;
// Strip the "MP" prefix from the exposed classes.
%rename (Solver) operations_research::MPSolver;
%rename (Solver) operations_research::MPSolver::MPSolver;
%rename (Constraint) operations_research::MPConstraint;
%rename (Variable) operations_research::MPVariable;
%rename (Objective) operations_research::MPObjective;
// Expose the MPSolver::OptimizationProblemType enum.
%unignore operations_research::MPSolver::OptimizationProblemType;
%unignore operations_research::MPSolver::GLOP_LINEAR_PROGRAMMING;
%unignore operations_research::MPSolver::CLP_LINEAR_PROGRAMMING;
%unignore operations_research::MPSolver::GLPK_LINEAR_PROGRAMMING;
%unignore operations_research::MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING;
%unignore operations_research::MPSolver::CBC_MIXED_INTEGER_PROGRAMMING;
%unignore operations_research::MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING;
%unignore operations_research::MPSolver::BOP_INTEGER_PROGRAMMING;
%unignore operations_research::MPSolver::SAT_INTEGER_PROGRAMMING;
// These aren't unit tested, as they only run on machines with a Gurobi license.
%unignore operations_research::MPSolver::GUROBI_LINEAR_PROGRAMMING;
%unignore operations_research::MPSolver::GUROBI_MIXED_INTEGER_PROGRAMMING;
%unignore operations_research::MPSolver::CPLEX_LINEAR_PROGRAMMING;
%unignore operations_research::MPSolver::CPLEX_MIXED_INTEGER_PROGRAMMING;
// Expose the MPSolver::ResultStatus enum.
%unignore operations_research::MPSolver::ResultStatus;
%unignore operations_research::MPSolver::OPTIMAL;
%unignore operations_research::MPSolver::FEASIBLE; // No unit test
%unignore operations_research::MPSolver::INFEASIBLE;
%unignore operations_research::MPSolver::UNBOUNDED; // No unit test
%unignore operations_research::MPSolver::ABNORMAL;
%unignore operations_research::MPSolver::NOT_SOLVED; // No unit test
// Expose the MPSolver's basic API, with some renames.
%rename (Objective) operations_research::MPSolver::MutableObjective;
%rename (BoolVar) operations_research::MPSolver::MakeBoolVar; // No unit test
%rename (IntVar) operations_research::MPSolver::MakeIntVar;
%rename (NumVar) operations_research::MPSolver::MakeNumVar;
%rename (Var) operations_research::MPSolver::MakeVar;
// We intentionally don't expose MakeRowConstraint(LinearExpr), because this
// "natural language" API is specific to C++: other languages may add their own
// syntactic sugar on top of MPSolver instead of this.
%rename (Constraint) operations_research::MPSolver::MakeRowConstraint(double, double);
%rename (Constraint) operations_research::MPSolver::MakeRowConstraint();
%rename (Constraint) operations_research::MPSolver::MakeRowConstraint(double, double, const std::string&);
%rename (Constraint) operations_research::MPSolver::MakeRowConstraint(const std::string&);
%unignore operations_research::MPSolver::~MPSolver;
%unignore operations_research::MPSolver::Solve;
%unignore operations_research::MPSolver::VerifySolution;
%unignore operations_research::MPSolver::infinity;
%unignore operations_research::MPSolver::set_time_limit; // No unit test
// Proto-based API of the MPSolver. Use is encouraged.
%unignore operations_research::MPSolver::SolveWithProto;
%unignore operations_research::MPSolver::ExportModelToProto;
%unignore operations_research::MPSolver::FillSolutionResponseProto;
// LoadModelFromProto() is also visible: it's overridden by an %extend, above.
%unignore operations_research::MPSolver::LoadSolutionFromProto; // No test
// Expose some of the more advanced MPSolver API.
%unignore operations_research::MPSolver::InterruptSolve;
%unignore operations_research::MPSolver::SupportsProblemType; // No unit test
%unignore operations_research::MPSolver::wall_time; // No unit test
%unignore operations_research::MPSolver::Clear; // No unit test
%unignore operations_research::MPSolver::constraints;
%unignore operations_research::MPSolver::variables;
%unignore operations_research::MPSolver::NumConstraints;
%unignore operations_research::MPSolver::NumVariables;
%unignore operations_research::MPSolver::EnableOutput; // No unit test
%unignore operations_research::MPSolver::SuppressOutput; // No unit test
%rename (LookupConstraint)
operations_research::MPSolver::LookupConstraintOrNull;
%rename (LookupVariable) operations_research::MPSolver::LookupVariableOrNull;
%unignore operations_research::MPSolver::SetSolverSpecificParametersAsString;
%unignore operations_research::MPSolver::NextSolution;
%unignore operations_research::MPSolver::ExportModelAsLpFormat;
%unignore operations_research::MPSolver::ExportModelAsMpsFormat;
// Expose very advanced parts of the MPSolver API. For expert users only.
%unignore operations_research::MPSolver::ComputeConstraintActivities;
%unignore operations_research::MPSolver::ComputeExactConditionNumber;
%unignore operations_research::MPSolver::nodes;
%unignore operations_research::MPSolver::iterations; // No unit test
%unignore operations_research::MPSolver::BasisStatus;
%unignore operations_research::MPSolver::FREE; // No unit test
%unignore operations_research::MPSolver::AT_LOWER_BOUND;
%unignore operations_research::MPSolver::AT_UPPER_BOUND;
%unignore operations_research::MPSolver::FIXED_VALUE; // No unit test
%unignore operations_research::MPSolver::BASIC;
// MPVariable: writer API.
%unignore operations_research::MPVariable::SetLb;
%unignore operations_research::MPVariable::SetUb;
%unignore operations_research::MPVariable::SetBounds;
// MPVariable: reader API.
%unignore operations_research::MPVariable::solution_value;
%unignore operations_research::MPVariable::lb;
%unignore operations_research::MPVariable::ub;
%unignore operations_research::MPVariable::integer; // No unit test
%unignore operations_research::MPVariable::name; // No unit test
%unignore operations_research::MPVariable::index; // No unit test
%unignore operations_research::MPVariable::basis_status;
%unignore operations_research::MPVariable::reduced_cost; // For experts only.
// MPConstraint: writer API.
%unignore operations_research::MPConstraint::SetCoefficient;
%unignore operations_research::MPConstraint::SetLb;
%unignore operations_research::MPConstraint::SetUb;
%unignore operations_research::MPConstraint::SetBounds;
%unignore operations_research::MPConstraint::set_is_lazy;
// MPConstraint: reader API.
%unignore operations_research::MPConstraint::GetCoefficient;
%unignore operations_research::MPConstraint::lb;
%unignore operations_research::MPConstraint::ub;
%unignore operations_research::MPConstraint::name;
%unignore operations_research::MPConstraint::index;
%unignore operations_research::MPConstraint::basis_status;
%unignore operations_research::MPConstraint::dual_value; // For experts only.
// MPObjective: writer API.
%unignore operations_research::MPObjective::SetCoefficient;
%unignore operations_research::MPObjective::SetMinimization;
%unignore operations_research::MPObjective::SetMaximization;
%unignore operations_research::MPObjective::SetOptimizationDirection;
%unignore operations_research::MPObjective::Clear; // No unit test
%unignore operations_research::MPObjective::SetOffset;
%unignore operations_research::MPObjective::AddOffset; // No unit test
// MPObjective: reader API.
%unignore operations_research::MPObjective::Value;
%unignore operations_research::MPObjective::GetCoefficient;
%unignore operations_research::MPObjective::minimization;
%unignore operations_research::MPObjective::maximization;
%unignore operations_research::MPObjective::offset;
%unignore operations_research::MPObjective::Offset;
%unignore operations_research::MPObjective::BestBound;
// MPSolverParameters API. For expert users only.
// TODO(user): also strip "MP" from the class name.
%unignore operations_research::MPSolverParameters;
%unignore operations_research::MPSolverParameters::MPSolverParameters;
// Expose the MPSolverParameters::DoubleParam enum.
%unignore operations_research::MPSolverParameters::DoubleParam;
%unignore operations_research::MPSolverParameters::RELATIVE_MIP_GAP;
%unignore operations_research::MPSolverParameters::PRIMAL_TOLERANCE;
%unignore operations_research::MPSolverParameters::DUAL_TOLERANCE;
%unignore operations_research::MPSolverParameters::GetDoubleParam;
%unignore operations_research::MPSolverParameters::SetDoubleParam;
%unignore operations_research::MPSolverParameters::kDefaultRelativeMipGap;
%unignore operations_research::MPSolverParameters::kDefaultPrimalTolerance;
%unignore operations_research::MPSolverParameters::kDefaultDualTolerance;
// TODO(user): unit test kDefaultPrimalTolerance.
// Expose the MPSolverParameters::IntegerParam enum.
%unignore operations_research::MPSolverParameters::IntegerParam;
%unignore operations_research::MPSolverParameters::PRESOLVE;
%unignore operations_research::MPSolverParameters::LP_ALGORITHM;
%unignore operations_research::MPSolverParameters::INCREMENTALITY;
%unignore operations_research::MPSolverParameters::SCALING;
%unignore operations_research::MPSolverParameters::GetIntegerParam;
%unignore operations_research::MPSolverParameters::SetIntegerParam;
%unignore operations_research::MPSolverParameters::RELATIVE_MIP_GAP;
%unignore operations_research::MPSolverParameters::kDefaultPrimalTolerance;
// TODO(user): unit test kDefaultPrimalTolerance.
// Expose the MPSolverParameters::PresolveValues enum.
%unignore operations_research::MPSolverParameters::PresolveValues;
%unignore operations_research::MPSolverParameters::PRESOLVE_OFF;
%unignore operations_research::MPSolverParameters::PRESOLVE_ON;
%unignore operations_research::MPSolverParameters::kDefaultPresolve;
// Expose the MPSolverParameters::LpAlgorithmValues enum.
%unignore operations_research::MPSolverParameters::LpAlgorithmValues;
%unignore operations_research::MPSolverParameters::DUAL;
%unignore operations_research::MPSolverParameters::PRIMAL;
%unignore operations_research::MPSolverParameters::BARRIER;
// Expose the MPSolverParameters::IncrementalityValues enum.
%unignore operations_research::MPSolverParameters::IncrementalityValues;
%unignore operations_research::MPSolverParameters::INCREMENTALITY_OFF;
%unignore operations_research::MPSolverParameters::INCREMENTALITY_ON;
%unignore operations_research::MPSolverParameters::kDefaultIncrementality;
// Expose the MPSolverParameters::ScalingValues enum.
%unignore operations_research::MPSolverParameters::ScalingValues;
%unignore operations_research::MPSolverParameters::SCALING_OFF;
%unignore operations_research::MPSolverParameters::SCALING_ON;
// Expose the model exporters.
%rename (ModelExportOptions) operations_research::MPModelExportOptions;
%rename (ModelExportOptions) operations_research::MPModelExportOptions::MPModelExportOptions;
%rename (ExportModelAsLpFormat) operations_research::ExportModelAsLpFormatReturnString;
%rename (ExportModelAsMpsFormat) operations_research::ExportModelAsMpsFormatReturnString;
%include "ortools/linear_solver/linear_solver.h"
%include "ortools/linear_solver/model_exporter.h"
%include "ortools/linear_solver/model_exporter_swig_helper.h"
%unignoreall
%pythoncode {
def setup_variable_operator(opname):
setattr(Variable, opname,
lambda self, *args: getattr(VariableExpr(self), opname)(*args))
for opname in LinearExpr.OVERRIDDEN_OPERATOR_METHODS:
setup_variable_operator(opname)
} // %pythoncode