1 // Copyright 2010-2021 Google LLC 2 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // you may not use this file except in compliance with the License. 4 // You may obtain a copy of the License at 5 // 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // This .i file is only used in the open-source export of 15 // util/operations_research (github or-tools) 16 // 17 // It exposes the linear programming and integer programming solver. 18 // 19 // The C# API is quite different from the C++ API; mostly because it 20 // supports the same "Natural language" API as the python wrapper. See the 21 // usage examples. 22 // 23 // This .i file is complemented by C# extensions (using partial classes) 24 // of some wrapped C++ classes, like Solver or Variable. There are also 25 // other C# helper classes. See src/com/google/ortools/linearsolver. 26 // 27 // USAGE EXAMPLES: 28 // - examples/csharp/cslinearprogramming.cs 29 // - examples/csharp/csintegerprogramming.cs 30 31 %include "enums.swg" 32 %include "stdint.i" 33 %include "std_vector.i" 34 35 %include "ortools/base/base.i" 36 %include "ortools/util/csharp/vector.i" 37 38 %{ 39 #include "ortools/linear_solver/linear_solver.h" 40 #include "ortools/linear_solver/linear_solver.pb.h" 41 #include "ortools/linear_solver/model_exporter.h" 42 %} 43 44 45 // We need to forward-declare the proto here, so that the PROTO_* macros 46 // involving them work correctly. The order matters very much: this declaration 47 // needs to be before the %{ #include ".../linear_solver.h" %}. 48 namespace operations_research { 49 class MPModelProto; 50 class MPModelRequest; 51 class MPSolutionResponse; 52 } // namespace operations_research 53 54 // Allow partial C# classes. That way, we can put our C# code extension in C# 55 // files instead of using typemap(cscode). 56 /* allow partial c# classes */ 57 %typemap(csclassmodifiers) SWIGTYPE "public partial class" 58 59 %typemap(csclassmodifiers) operations_research::MPVariable "public partial class" 60 %typemap(csclassmodifiers) operations_research::MPSolver "public partial class" 61 62 %template(DoubleVector) std::vector<double>; 63 VECTOR_AS_CSHARP_ARRAY(double, double, double, DoubleVector); 64 65 %define CONVERT_VECTOR(CTYPE, TYPE) 66 SWIG_STD_VECTOR_ENHANCED(CTYPE*); 67 %template(TYPE ## Vector) std::vector<CTYPE*>; 68 %enddef // CONVERT_VECTOR 69 70 CONVERT_VECTOR(operations_research::MPConstraint, MPConstraint) 71 CONVERT_VECTOR(operations_research::MPVariable, MPVariable) 72 73 #undef CONVERT_VECTOR 74 75 %ignoreall 76 77 %unignore operations_research; 78 79 // Rename all the exposed classes, by removing the "MP" prefix. 80 %rename (Solver) operations_research::MPSolver; 81 %rename (Variable) operations_research::MPVariable; 82 %rename (Constraint) operations_research::MPConstraint; 83 %rename (Objective) operations_research::MPObjective; 84 %rename (SolverParameters) operations_research::MPSolverParameters; 85 86 // Expose the MPSolver::OptimizationProblemType enum. 87 %unignore operations_research::MPSolver::OptimizationProblemType; 88 %unignore operations_research::MPSolver::CLP_LINEAR_PROGRAMMING; 89 %unignore operations_research::MPSolver::GLOP_LINEAR_PROGRAMMING; 90 %unignore operations_research::MPSolver::GLPK_LINEAR_PROGRAMMING; 91 %unignore operations_research::MPSolver::SCIP_MIXED_INTEGER_PROGRAMMING; 92 %unignore operations_research::MPSolver::CBC_MIXED_INTEGER_PROGRAMMING; 93 %unignore operations_research::MPSolver::GLPK_MIXED_INTEGER_PROGRAMMING; 94 %unignore operations_research::MPSolver::GUROBI_LINEAR_PROGRAMMING; 95 %unignore operations_research::MPSolver::GUROBI_MIXED_INTEGER_PROGRAMMING; 96 %unignore operations_research::MPSolver::CPLEX_LINEAR_PROGRAMMING; 97 %unignore operations_research::MPSolver::CPLEX_MIXED_INTEGER_PROGRAMMING; 98 %unignore operations_research::MPSolver::XPRESS_LINEAR_PROGRAMMING; 99 %unignore operations_research::MPSolver::XPRESS_MIXED_INTEGER_PROGRAMMING; 100 %unignore operations_research::MPSolver::BOP_INTEGER_PROGRAMMING; 101 %unignore operations_research::MPSolver::SAT_INTEGER_PROGRAMMING; 102 103 // Expose the MPSolver::ResultStatus enum. 104 %unignore operations_research::MPSolver::ResultStatus; 105 %unignore operations_research::MPSolver::OPTIMAL; 106 %unignore operations_research::MPSolver::FEASIBLE; 107 %unignore operations_research::MPSolver::INFEASIBLE; 108 %unignore operations_research::MPSolver::UNBOUNDED; 109 %unignore operations_research::MPSolver::ABNORMAL; 110 %unignore operations_research::MPSolver::NOT_SOLVED; 111 112 // Expose the MPSolver's basic API, with some non-trivial renames. 113 // We intentionally don't expose MakeRowConstraint(LinearExpr), because this 114 // "natural language" API is specific to C++: other languages may add their own 115 // syntactic sugar on top of MPSolver instead of this. 116 %rename (MakeConstraint) operations_research::MPSolver::MakeRowConstraint(double, double); 117 %rename (MakeConstraint) operations_research::MPSolver::MakeRowConstraint(); 118 %rename (MakeConstraint) operations_research::MPSolver::MakeRowConstraint(double, double, const std::string&); 119 %rename (MakeConstraint) operations_research::MPSolver::MakeRowConstraint(const std::string&); 120 121 %rename (Objective) operations_research::MPSolver::MutableObjective; 122 123 // Expose the MPSolver's basic API, with trivial renames when needed. 124 %unignore operations_research::MPSolver::MPSolver; 125 %unignore operations_research::MPSolver::~MPSolver; 126 %newobject operations_research::MPSolver::CreateSolver; 127 %unignore operations_research::MPSolver::CreateSolver; 128 %unignore operations_research::MPSolver::MakeBoolVar; 129 %unignore operations_research::MPSolver::MakeIntVar; 130 %unignore operations_research::MPSolver::MakeNumVar; 131 %unignore operations_research::MPSolver::MakeVar; 132 %unignore operations_research::MPSolver::Solve; 133 %unignore operations_research::MPSolver::VerifySolution; 134 %unignore operations_research::MPSolver::Reset; 135 %rename (SetTimeLimit) operations_research::MPSolver::set_time_limit; 136 137 // Expose some of the more advanced MPSolver API. 138 %unignore operations_research::MPSolver::InterruptSolve; 139 %unignore operations_research::MPSolver::SupportsProblemType; 140 %unignore operations_research::MPSolver::SetSolverSpecificParametersAsString; 141 %rename (WallTime) operations_research::MPSolver::wall_time; 142 %unignore operations_research::MPSolver::Clear; 143 %rename (Constraint) operations_research::MPSolver::constraint; 144 %unignore operations_research::MPSolver::constraints; 145 %unignore operations_research::MPSolver::NumConstraints; 146 %unignore operations_research::MPSolver::NumVariables; 147 %rename (Variable) operations_research::MPSolver::variable; 148 %unignore operations_research::MPSolver::variables; 149 %unignore operations_research::MPSolver::EnableOutput; 150 %unignore operations_research::MPSolver::SuppressOutput; 151 %unignore operations_research::MPSolver::LookupConstraintOrNull; 152 %unignore operations_research::MPSolver::LookupVariableOrNull; 153 154 // Expose very advanced parts of the MPSolver API. For expert users only. 155 %unignore operations_research::MPSolver::ComputeConstraintActivities; 156 %unignore operations_research::MPSolver::ComputeExactConditionNumber; 157 %rename (Nodes) operations_research::MPSolver::nodes; 158 %rename (Iterations) operations_research::MPSolver::iterations; 159 %unignore operations_research::MPSolver::BasisStatus; 160 %unignore operations_research::MPSolver::FREE; 161 %unignore operations_research::MPSolver::AT_LOWER_BOUND; 162 %unignore operations_research::MPSolver::AT_UPPER_BOUND; 163 %unignore operations_research::MPSolver::FIXED_VALUE; 164 %unignore operations_research::MPSolver::BASIC; 165 166 // Extend code. 167 %unignore operations_research::MPSolver::ExportModelAsLpFormat(bool); 168 %unignore operations_research::MPSolver::ExportModelAsMpsFormat(bool, bool); 169 %unignore operations_research::MPSolver::SetHint( 170 const std::vector<operations_research::MPVariable*>&, 171 const std::vector<double>&); 172 %unignore operations_research::MPSolver::SetNumThreads; 173 %extend operations_research::MPSolver { ExportModelAsLpFormat(bool obfuscated)174 std::string ExportModelAsLpFormat(bool obfuscated) { 175 operations_research::MPModelExportOptions options; 176 options.obfuscate = obfuscated; 177 operations_research::MPModelProto model; 178 $self->ExportModelToProto(&model); 179 return ExportModelAsLpFormat(model, options).value_or(""); 180 } 181 ExportModelAsMpsFormat(bool fixed_format,bool obfuscated)182 std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscated) { 183 operations_research::MPModelExportOptions options; 184 options.obfuscate = obfuscated; 185 operations_research::MPModelProto model; 186 $self->ExportModelToProto(&model); 187 return ExportModelAsMpsFormat(model, options).value_or(""); 188 } 189 SetHint(const std::vector<operations_research::MPVariable * > & variables,const std::vector<double> & values)190 void SetHint(const std::vector<operations_research::MPVariable*>& variables, 191 const std::vector<double>& values) { 192 if (variables.size() != values.size()) { 193 LOG(FATAL) << "Different number of variables and values when setting " 194 << "hint."; 195 } 196 std::vector<std::pair<const operations_research::MPVariable*, double> > 197 hint(variables.size()); 198 for (int i = 0; i < variables.size(); ++i) { 199 hint[i] = std::make_pair(variables[i], values[i]); 200 } 201 $self->SetHint(hint); 202 } 203 SetNumThreads(int num_theads)204 bool SetNumThreads(int num_theads) { 205 return $self->SetNumThreads(num_theads).ok(); 206 } 207 } 208 209 // MPVariable: writer API. 210 %unignore operations_research::MPVariable::SetInteger; 211 %rename (SetLb) operations_research::MPVariable::SetLB; 212 %rename (SetUb) operations_research::MPVariable::SetUB; 213 %unignore operations_research::MPVariable::SetBounds; 214 215 // MPVariable: reader API. 216 %rename (SolutionValue) operations_research::MPVariable::solution_value; 217 %rename (Lb) operations_research::MPVariable::lb; 218 %rename (Ub) operations_research::MPVariable::ub; 219 %rename (Name) operations_research::MPVariable::name; 220 %rename (BasisStatus) operations_research::MPVariable::basis_status; 221 %rename (ReducedCost) operations_research::MPVariable::reduced_cost; // expert 222 223 // MPConstraint: writer API. 224 %unignore operations_research::MPConstraint::SetCoefficient; 225 %rename (SetLb) operations_research::MPConstraint::SetLB; 226 %rename (SetUb) operations_research::MPConstraint::SetUB; 227 %unignore operations_research::MPConstraint::SetBounds; 228 %rename (Index) operations_research::MPConstraint::index; 229 %rename (SetIsLazy) operations_research::MPConstraint::set_is_lazy; 230 231 // MPConstraint: reader API. 232 %unignore operations_research::MPConstraint::GetCoefficient; 233 %rename (Lb) operations_research::MPConstraint::lb; 234 %rename (Ub) operations_research::MPConstraint::ub; 235 %rename (Name) operations_research::MPConstraint::name; 236 %rename (BasisStatus) operations_research::MPConstraint::basis_status; 237 %rename (DualValue) operations_research::MPConstraint::dual_value; // expert 238 %rename (IsLazy) operations_research::MPConstraint::is_lazy; // expert 239 240 // MPObjective: writer API. 241 %unignore operations_research::MPObjective::SetCoefficient; 242 %unignore operations_research::MPObjective::SetMinimization; 243 %unignore operations_research::MPObjective::SetMaximization; 244 %unignore operations_research::MPObjective::SetOptimizationDirection; 245 %unignore operations_research::MPObjective::Clear; 246 %unignore operations_research::MPObjective::SetOffset; 247 248 // MPObjective: reader API. 249 %unignore operations_research::MPObjective::Value; 250 %unignore operations_research::MPObjective::GetCoefficient; 251 %rename (Minimization) operations_research::MPObjective::minimization; 252 %rename (Maximization) operations_research::MPObjective::maximization; 253 %rename (Offset) operations_research::MPObjective::offset; 254 %unignore operations_research::MPObjective::BestBound; 255 256 // MPSolverParameters API. For expert users only. 257 // TODO(user): unit test all of it. 258 259 %unignore operations_research::MPSolverParameters; // no test 260 %unignore operations_research::MPSolverParameters::MPSolverParameters; // no test 261 262 // Expose the MPSolverParameters::DoubleParam enum. 263 %unignore operations_research::MPSolverParameters::DoubleParam; // no test 264 %unignore operations_research::MPSolverParameters::RELATIVE_MIP_GAP; // no test 265 %unignore operations_research::MPSolverParameters::PRIMAL_TOLERANCE; // no test 266 %unignore operations_research::MPSolverParameters::DUAL_TOLERANCE; // no test 267 %unignore operations_research::MPSolverParameters::GetDoubleParam; // no test 268 %unignore operations_research::MPSolverParameters::SetDoubleParam; // no test 269 %unignore operations_research::MPSolverParameters::kDefaultRelativeMipGap; // no test 270 %unignore operations_research::MPSolverParameters::kDefaultPrimalTolerance; // no test 271 %unignore operations_research::MPSolverParameters::kDefaultDualTolerance; // no test 272 273 // Expose the MPSolverParameters::IntegerParam enum. 274 %unignore operations_research::MPSolverParameters::IntegerParam; // no test 275 %unignore operations_research::MPSolverParameters::PRESOLVE; // no test 276 %unignore operations_research::MPSolverParameters::LP_ALGORITHM; // no test 277 %unignore operations_research::MPSolverParameters::INCREMENTALITY; // no test 278 %unignore operations_research::MPSolverParameters::SCALING; // no test 279 %unignore operations_research::MPSolverParameters::GetIntegerParam; // no test 280 %unignore operations_research::MPSolverParameters::SetIntegerParam; // no test 281 282 // Expose the MPSolverParameters::PresolveValues enum. 283 %unignore operations_research::MPSolverParameters::PresolveValues; // no test 284 %unignore operations_research::MPSolverParameters::PRESOLVE_OFF; // no test 285 %unignore operations_research::MPSolverParameters::PRESOLVE_ON; // no test 286 %unignore operations_research::MPSolverParameters::kDefaultPresolve; // no test 287 288 // Expose the MPSolverParameters::LpAlgorithmValues enum. 289 %unignore operations_research::MPSolverParameters::LpAlgorithmValues; // no test 290 %unignore operations_research::MPSolverParameters::DUAL; // no test 291 %unignore operations_research::MPSolverParameters::PRIMAL; // no test 292 %unignore operations_research::MPSolverParameters::BARRIER; // no test 293 294 // Expose the MPSolverParameters::IncrementalityValues enum. 295 %unignore operations_research::MPSolverParameters::IncrementalityValues; // no test 296 %unignore operations_research::MPSolverParameters::INCREMENTALITY_OFF; // no test 297 %unignore operations_research::MPSolverParameters::INCREMENTALITY_ON; // no test 298 %unignore operations_research::MPSolverParameters::kDefaultIncrementality; // no test 299 300 // Expose the MPSolverParameters::ScalingValues enum. 301 %unignore operations_research::MPSolverParameters::ScalingValues; // no test 302 %unignore operations_research::MPSolverParameters::SCALING_OFF; // no test 303 %unignore operations_research::MPSolverParameters::SCALING_ON; // no test 304 305 %include "ortools/linear_solver/linear_solver.h" 306 %include "ortools/linear_solver/model_exporter.h" 307 308 %unignoreall 309