Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelBuilder.cs
Go to the documentation of this file.
1// Copyright 2010-2024 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
15{
16
18using System;
19using System.Collections;
20using System.Collections.Generic;
21using System.Linq;
22using System.Runtime.CompilerServices;
23using Google.Protobuf.Collections;
24
30public class Model
35 public Model()
36 {
37 helper_ = new ModelBuilderHelper();
38 constantMap_ = new Dictionary<double, int>();
39 tmp_var_value_map_ = new SortedDictionary<int, double>();
40 tmp_terms_ = new Queue<Term>();
41 }
42
47 public Model Clone()
48 {
49 Model clonedModel = new Model();
50 clonedModel.Helper.OverwriteModel(Helper);
51 foreach (KeyValuePair<double, int> entry in constantMap_)
52 {
53 clonedModel.constantMap_[entry.Key] = entry.Value;
54 }
55 return clonedModel;
56 }
57
58 // Integer variables.
59
68 public Variable NewVar(double lb, double ub, bool isIntegral, String name)
69 {
70 return new Variable(helper_, lb, ub, isIntegral, name);
71 }
72
80 public Variable NewNumVar(double lb, double ub, String name)
81 {
82 return new Variable(helper_, lb, ub, false, name);
83 }
84
92 public Variable NewIntVar(double lb, double ub, String name)
93 {
94 return new Variable(helper_, lb, ub, true, name);
95 }
96
102 public Variable NewBoolVar(String name)
104 return new Variable(helper_, 0, 1, true, name);
105 }
106
112 public Variable NewConstant(double value)
114 if (constantMap_.TryGetValue(value, out int index))
115 {
116 return new Variable(helper_, index);
117 }
118 Variable cste = new Variable(helper_, value, value, false, ""); // bounds and name.
119 constantMap_.Add(value, cste.Index);
120 return cste;
121 }
122
124 public Variable VarFromIndex(int index)
126 return new Variable(helper_, index);
127 }
128
137 switch (lin.CtType)
138 {
139 case BoundedLinearExpression.Type.BoundExpression: {
140 return AddLinearConstraint(lin.Left, lin.Lb, lin.Ub);
141 }
142 case BoundedLinearExpression.Type.VarEqVar: {
143 return AddLinearConstraint(lin.Left - lin.Right, 0, 0);
144 }
145 case BoundedLinearExpression.Type.VarEqCst: {
146 return AddLinearConstraint(lin.Left, lin.Lb, lin.Lb);
147 }
148 default: {
149 throw new ArgumentException("Cannot use '" + lin.ToString() + "' as a linear constraint.");
150 }
151 }
152 }
153
161 public LinearConstraint AddLinearConstraint(LinearExpr expr, double lb, double ub)
163 LinearConstraint lin = new LinearConstraint(helper_);
164 var dict = tmp_var_value_map_;
165 dict.Clear();
166 double offset = LinearExpr.GetVarValueMap(expr, dict, tmp_terms_);
167 foreach (KeyValuePair<int, double> term in dict)
168 {
169 helper_.AddConstraintTerm(lin.Index, term.Key, term.Value);
170 }
171 if (lb == Double.NegativeInfinity || lb == Double.PositiveInfinity)
172 {
173 lin.LowerBound = lb;
174 }
175 else
176 {
177 lin.LowerBound = lb - offset;
178 }
179 if (ub == Double.NegativeInfinity || ub == Double.PositiveInfinity)
180 {
181 lin.UpperBound = ub;
182 }
183 else
184 {
185 lin.UpperBound = ub - offset;
186 }
187 return lin;
188 }
189
191 public LinearConstraint ConstraintFromIndex(int index)
193 return new LinearConstraint(helper_, index);
194 }
195
206 switch (lin.CtType)
207 {
208 case BoundedLinearExpression.Type.BoundExpression: {
209 return AddEnforcedLinearConstraint(lin.Left, lin.Lb, lin.Ub, iVar, iValue);
210 }
211 case BoundedLinearExpression.Type.VarEqVar: {
212 return AddEnforcedLinearConstraint(lin.Left - lin.Right, 0, 0, iVar, iValue);
213 }
214 case BoundedLinearExpression.Type.VarEqCst: {
215 return AddEnforcedLinearConstraint(lin.Left, lin.Lb, lin.Lb, iVar, iValue);
216 }
217 default: {
218 throw new ArgumentException("Cannot use '" + lin.ToString() + "' as a linear constraint.");
219 }
220 }
221 }
222
232 public EnforcedLinearConstraint AddEnforcedLinearConstraint(LinearExpr expr, double lb, double ub, Variable iVar,
233 bool iValue)
234 {
236 lin.IndicatorVariable = iVar;
237 lin.IndicatorValue = iValue;
238 var dict = tmp_var_value_map_;
239 dict.Clear();
240 double offset = LinearExpr.GetVarValueMap(expr, dict, tmp_terms_);
241 foreach (KeyValuePair<int, double> term in dict)
242 {
243 helper_.AddEnforcedConstraintTerm(lin.Index, term.Key, term.Value);
244 }
245 if (lb == Double.NegativeInfinity || lb == Double.PositiveInfinity)
246 {
247 lin.LowerBound = lb;
248 }
249 else
250 {
251 lin.LowerBound = lb - offset;
252 }
253 if (ub == Double.NegativeInfinity || ub == Double.PositiveInfinity)
254 {
255 lin.UpperBound = ub;
256 }
257 else
258 {
259 lin.UpperBound = ub - offset;
260 }
261 return lin;
262 }
263
267 return new EnforcedLinearConstraint(helper_, index);
268 }
269
270 // Objective.
271
276 public void Minimize(LinearExpr obj)
278 Optimize(obj, false);
279 }
280
285 public void Maximize(LinearExpr obj)
287 Optimize(obj, true);
288 }
289
295 public void Optimize(LinearExpr obj, bool maximize)
297 helper_.ClearObjective();
298 var dict = tmp_var_value_map_;
299 dict.Clear();
300 double offset = LinearExpr.GetVarValueMap(obj, dict, tmp_terms_);
301 foreach (KeyValuePair<int, double> term in dict)
302 {
303 if (term.Value != 0.0)
304 {
305 helper_.SetVarObjectiveCoefficient(term.Key, term.Value);
306 }
307 }
308 helper_.SetObjectiveOffset(offset);
309 helper_.SetMaximize(maximize);
310 }
311
315 public double ObjectiveOffset
317 get {
318 return helper_.ObjectiveOffset();
319 }
320 set {
321 helper_.SetObjectiveOffset(value);
322 }
323 }
324
328 public void ClearHints()
330 helper_.ClearHints();
331 }
332
337 public void AddHint(Variable var, double value)
339 helper_.AddHint(var.Index, value);
340 }
341
345 public int VariablesCount()
347 return helper_.VariablesCount();
348 }
349
353 public int ConstraintsCount()
355 return helper_.ConstraintsCount();
356 }
357
361 public String Name
363 get {
364 return helper_.Name();
365 }
366 set {
367 helper_.SetName(value);
368 }
369 }
370
377 public bool ExportToFile(String file)
379 return helper_.WriteModelToProtoFile(file);
380 }
381
387 public bool ImportFromFile(String file)
389 return helper_.ReadModelFromProtoFile(file);
390 }
391
392 public String ExportToMpsString(bool obfuscate)
394 return helper_.ExportToMpsString(obfuscate);
395 }
396
397 public String ExportToLpString(bool obfuscate)
399 return helper_.ExportToLpString(obfuscate);
400 }
401
402 public bool WriteToMpsFile(String filename, bool obfuscate)
404 return helper_.WriteToMpsFile(filename, obfuscate);
405 }
406
407 public bool ImportFromMpsString(String mpsString)
409 return helper_.ImportFromMpsString(mpsString);
410 }
411
412 public bool ImportFromMpsFile(String mpsFile)
414 return helper_.ImportFromMpsString(mpsFile);
415 }
416
417 public bool ImportFromLpString(String lpString)
419 return helper_.ImportFromLpString(lpString);
420 }
421
422 public bool ImportFromLpFile(String lpFile)
424 return helper_.ImportFromMpsString(lpFile);
425 }
426
432 get {
433 return helper_;
434 }
435 }
436
437 private ModelBuilderHelper helper_;
438 private Dictionary<double, int> constantMap_;
439
440 // Used to process linear expressions.
441 private SortedDictionary<int, double> tmp_var_value_map_;
442 private Queue<Term> tmp_terms_;
443}
444
445} // namespace Google.OrTools.ModelBuilder
Holds a linear constraint: expression ∈ domain
Wrapper around an enforced linear constraint stored in the ModelBuilderHelper instance.
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
Holds a linear expression: sum (ai * xi) + b.
bool WriteToMpsFile(string filename, bool obfuscate)
void AddEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
void OverwriteModel(ModelBuilderHelper other_helper)
void AddConstraintTerm(int ct_index, int var_index, double coeff)
void AddHint(int var_index, double var_value)
void SetVarObjectiveCoefficient(int var_index, double coeff)
String Name
The name of the model.
void Minimize(LinearExpr obj)
Objective.
int ConstraintsCount()
Returns the number of constraints in the model.
void Optimize(LinearExpr obj, bool maximize)
Sets the objective expression.
Variable VarFromIndex(int index)
Rebuilds a variable from its index.
String ExportToMpsString(bool obfuscate)
void ClearHints()
Remove all hints from the model.
Variable NewBoolVar(String name)
Creates a bool variable with the given name.
LinearConstraint ConstraintFromIndex(int index)
Rebuilds a linear constraint from its index.
EnforcedLinearConstraint AddEnforcedLinearConstraint(LinearExpr expr, double lb, double ub, Variable iVar, bool iValue)
Adds the constraint iVar == iValue => expr in [lb, ub].
double ObjectiveOffset
The offset of the objective.
bool ImportFromMpsFile(String mpsFile)
void Maximize(LinearExpr obj)
Maximize expression.
EnforcedLinearConstraint AddEnforced(BoundedLinearExpression lin, Variable iVar, bool iValue)
Adds an enforced Linear constraint to the model.
Variable NewVar(double lb, double ub, bool isIntegral, String name)
Integer variables.
bool WriteToMpsFile(String filename, bool obfuscate)
String ExportToLpString(bool obfuscate)
bool ExportToFile(String file)
Write the model as a protocol buffer to 'file'.
Variable NewIntVar(double lb, double ub, String name)
Creates an integer variable with domain [lb, ub].
void AddHint(Variable var, double value)
Adds var == value as a hint to the model. Note that variables must not appear more than once in the l...
bool ImportFromMpsString(String mpsString)
bool ImportFromLpFile(String lpFile)
bool ImportFromLpString(String lpString)
LinearConstraint Add(BoundedLinearExpression lin)
Adds a Linear constraint to the model.
Model Clone()
Returns a cloned model.
Variable NewConstant(double value)
Creates a constant variable.
LinearConstraint AddLinearConstraint(LinearExpr expr, double lb, double ub)
Adds the constraint expr in [lb, ub].
Variable NewNumVar(double lb, double ub, String name)
Creates a continuous variable with domain [lb, ub].
ModelBuilderHelper Helper
The model builder helper.
bool ImportFromFile(String file)
load the model as a protocol buffer from 'file'.
int VariablesCount()
Returns the number of variables in the model.
EnforcedLinearConstraint EnforcedConstraintFromIndex(int index)
Rebuilds a linear constraint from its index.