Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
ModelBuilder.java
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
14package com.google.ortools.modelbuilder;
15
16import java.util.LinkedHashMap;
17import java.util.Map;
18
24public final class ModelBuilder {
25 static class ModelBuilderException extends RuntimeException {
26 public ModelBuilderException(String methodName, String msg) {
27 // Call constructor of parent Exception
28 super(methodName + ": " + msg);
29 }
30 }
31
33 public static class MismatchedArrayLengths extends ModelBuilderException {
34 public MismatchedArrayLengths(String methodName, String array1Name, String array2Name) {
35 super(methodName, array1Name + " and " + array2Name + " have mismatched lengths");
36 }
37 }
38
40 public static class WrongLength extends ModelBuilderException {
41 public WrongLength(String methodName, String msg) {
42 super(methodName, msg);
43 }
44 }
45
47 public ModelBuilder() {
48 helper = new ModelBuilderHelper();
49 constantMap = new LinkedHashMap<>();
50 }
51
54 ModelBuilder clonedModel = new ModelBuilder();
55 clonedModel.getHelper().overwriteModel(helper);
56 clonedModel.constantMap.putAll(constantMap);
57 return clonedModel;
58 }
59
60 // Integer variables.
61
63 public Variable newVar(double lb, double ub, boolean isIntegral, String name) {
64 return new Variable(helper, lb, ub, isIntegral, name);
65 }
66
68 public Variable newNumVar(double lb, double ub, String name) {
69 return new Variable(helper, lb, ub, false, name);
70 }
71
73 public Variable newIntVar(double lb, double ub, String name) {
74 return new Variable(helper, lb, ub, true, name);
75 }
76
78 public Variable newBoolVar(String name) {
79 return new Variable(helper, 0, 1, true, name);
80 }
81
83 public Variable newConstant(double value) {
84 if (constantMap.containsKey(value)) {
85 return new Variable(helper, constantMap.get(value));
86 }
87 Variable cste = new Variable(helper, value, value, false, ""); // bounds and name.
88 constantMap.put(value, cste.getIndex());
89 return cste;
90 }
91
93 public Variable varFromIndex(int index) {
94 return new Variable(helper, index);
95 }
96
97 // Linear constraints.
98
100 public LinearConstraint addLinearConstraint(LinearArgument expr, double lb, double ub) {
101 LinearConstraint lin = new LinearConstraint(helper);
102 final LinearExpr e = expr.build();
103 for (int i = 0; i < e.numElements(); ++i) {
105 }
106 double offset = e.getOffset();
107 if (lb == Double.NEGATIVE_INFINITY || lb == Double.POSITIVE_INFINITY) {
108 lin.setLowerBound(lb);
109 } else {
110 lin.setLowerBound(lb - offset);
111 }
112 if (ub == Double.NEGATIVE_INFINITY || ub == Double.POSITIVE_INFINITY) {
113 lin.setUpperBound(ub);
114 } else {
115 lin.setUpperBound(ub - offset);
116 }
117 return lin;
118 }
119
121 public LinearConstraint addEquality(LinearArgument expr, double value) {
122 return addLinearConstraint(expr, value, value);
123 }
124
128 difference.addTerm(left, 1);
129 difference.addTerm(right, -1);
130 return addLinearConstraint(difference, 0.0, 0.0);
131 }
132
134 public LinearConstraint addLessOrEqual(LinearArgument expr, double value) {
135 return addLinearConstraint(expr, Double.NEGATIVE_INFINITY, value);
136 }
137
141 difference.addTerm(left, 1);
142 difference.addTerm(right, -1);
143 return addLinearConstraint(difference, Double.NEGATIVE_INFINITY, 0.0);
144 }
145
148 return addLinearConstraint(expr, value, Double.POSITIVE_INFINITY);
149 }
150
154 difference.addTerm(left, 1);
155 difference.addTerm(right, -1);
156 return addLinearConstraint(difference, 0.0, Double.POSITIVE_INFINITY);
157 }
158
161 return new LinearConstraint(helper, index);
162 }
163
164 // Enforced Linear constraints.
165
168 LinearArgument expr, double lb, double ub, Variable iVar, boolean iValue) {
170 lin.setIndicatorVariable(iVar);
171 lin.setIndicatorValue(iValue);
172 final LinearExpr e = expr.build();
173 for (int i = 0; i < e.numElements(); ++i) {
175 }
176 double offset = e.getOffset();
177 if (lb == Double.NEGATIVE_INFINITY || lb == Double.POSITIVE_INFINITY) {
178 lin.setLowerBound(lb);
179 } else {
180 lin.setLowerBound(lb - offset);
181 }
182 if (ub == Double.NEGATIVE_INFINITY || ub == Double.POSITIVE_INFINITY) {
183 lin.setUpperBound(ub);
184 } else {
185 lin.setUpperBound(ub - offset);
186 }
187 return lin;
188 }
189
192 LinearArgument expr, double value, Variable iVar, boolean iValue) {
193 return addEnforcedLinearConstraint(expr, value, value, iVar, iValue);
194 }
195
198 LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
200 difference.addTerm(left, 1);
201 difference.addTerm(right, -1);
202 return addEnforcedLinearConstraint(difference, 0.0, 0.0, iVar, iValue);
203 }
204
207 LinearArgument expr, double value, Variable iVar, boolean iValue) {
208 return addEnforcedLinearConstraint(expr, Double.NEGATIVE_INFINITY, value, iVar, iValue);
209 }
210
213 LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
215 difference.addTerm(left, 1);
216 difference.addTerm(right, -1);
217 return addEnforcedLinearConstraint(difference, Double.NEGATIVE_INFINITY, 0.0, iVar, iValue);
218 }
219
222 LinearArgument expr, double value, Variable iVar, boolean iValue) {
223 return addEnforcedLinearConstraint(expr, value, Double.POSITIVE_INFINITY, iVar, iValue);
224 }
225
228 LinearArgument left, LinearArgument right, Variable iVar, boolean iValue) {
230 difference.addTerm(left, 1);
231 difference.addTerm(right, -1);
232 return addEnforcedLinearConstraint(difference, 0.0, Double.POSITIVE_INFINITY, iVar, iValue);
233 }
234
237 return new EnforcedLinearConstraint(helper, index);
238 }
239
241 public void minimize(LinearArgument obj) {
242 optimize(obj, false);
243 }
244
246 public void maximize(LinearArgument obj) {
247 optimize(obj, true);
248 }
249
251 public void optimize(LinearArgument obj, boolean maximize) {
252 helper.clearObjective();
253 LinearExpr e = obj.build();
254 LinkedHashMap<Integer, Double> coeffMap = new LinkedHashMap<>();
255 for (int i = 0; i < e.numElements(); ++i) {
256 coeffMap.merge(e.getVariableIndex(i), e.getCoefficient(i), Double::sum);
257 }
258 for (Map.Entry<Integer, Double> entry : coeffMap.entrySet()) {
259 if (entry.getValue() != 0) {
260 helper.setVarObjectiveCoefficient(entry.getKey(), entry.getValue());
261 }
262 }
263 helper.setObjectiveOffset(e.getOffset());
264 helper.setMaximize(maximize);
265 }
266
268 double getObjectiveOffset() {
269 return helper.getObjectiveOffset();
270 }
271
273 void setObjectiveOffset(double offset) {
274 helper.setObjectiveOffset(offset);
275 }
276
278 void clearHints() {
279 helper.clearHints();
280 }
281
286 void addHint(Variable v, double value) {
287 helper.addHint(v.getIndex(), value);
288 }
289
290 // Model getters, import, export.
291
293 public int numVariables() {
294 return helper.numVariables();
295 }
296
298 public int numConstraints() {
299 return helper.numConstraints();
300 }
301
303 public String getName() {
304 return helper.getName();
305 }
306
308 public void setName(String name) {
309 helper.setName(name);
310 }
311
319 public boolean exportToFile(String file) {
320 return helper.writeModelToProtoFile(file);
321 }
322
329 public boolean importFromFile(String file) {
330 return helper.readModelFromProtoFile(file);
331 }
332
333 public String exportToMpsString(boolean obfuscate) {
334 return helper.exportToMpsString(obfuscate);
335 }
336
337 public String exportToLpString(boolean obfuscate) {
338 return helper.exportToLpString(obfuscate);
339 }
340
341 public boolean writeToMpsFile(String filename, boolean obfuscate) {
342 return helper.writeToMpsFile(filename, obfuscate);
343 }
344
345 public boolean importFromMpsString(String mpsString) {
346 return helper.importFromMpsString(mpsString);
347 }
348
349 public boolean importFromMpsFile(String mpsFile) {
350 return helper.importFromMpsString(mpsFile);
351 }
352
353 public boolean importFromLpString(String lpString) {
354 return helper.importFromLpString(lpString);
355 }
356
357 public boolean importFromLpFile(String lpFile) {
358 return helper.importFromMpsString(lpFile);
359 }
360
361 // Getters.
364 return helper;
365 }
366
367 private final ModelBuilderHelper helper;
368 private final Map<Double, Integer> constantMap;
369}
LinearExprBuilder addTerm(LinearArgument expr, double coeff)
void setVarObjectiveCoefficient(int var_index, double coeff)
void addEnforcedConstraintTerm(int ct_index, int var_index, double coeff)
boolean writeToMpsFile(String filename, boolean obfuscate)
void addConstraintTerm(int ct_index, int var_index, double coeff)
MismatchedArrayLengths(String methodName, String array1Name, String array2Name)
Variable newNumVar(double lb, double ub, String name)
EnforcedLinearConstraint addEnforcedLessOrEqual(LinearArgument expr, double value, Variable iVar, boolean iValue)
Variable newIntVar(double lb, double ub, String name)
LinearConstraint addEquality(LinearArgument left, LinearArgument right)
LinearConstraint addLinearConstraint(LinearArgument expr, double lb, double ub)
Linear constraints.
EnforcedLinearConstraint addEnforcedEquality(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue)
int numVariables()
Model getters, import, export.
EnforcedLinearConstraint addEnforcedLinearConstraint(LinearArgument expr, double lb, double ub, Variable iVar, boolean iValue)
Enforced Linear constraints.
EnforcedLinearConstraint addEnforcedLessOrEqual(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue)
boolean writeToMpsFile(String filename, boolean obfuscate)
LinearConstraint addGreaterOrEqual(LinearArgument left, LinearArgument right)
EnforcedLinearConstraint addEnforcedGreaterOrEqual(LinearArgument left, LinearArgument right, Variable iVar, boolean iValue)
LinearConstraint addGreaterOrEqual(LinearArgument expr, double value)
EnforcedLinearConstraint addEnforcedEquality(LinearArgument expr, double value, Variable iVar, boolean iValue)
EnforcedLinearConstraint enforcedConstraintFromIndex(int index)
EnforcedLinearConstraint addEnforcedGreaterOrEqual(LinearArgument expr, double value, Variable iVar, boolean iValue)
Variable newVar(double lb, double ub, boolean isIntegral, String name)
Integer variables.
LinearConstraint addEquality(LinearArgument expr, double value)
LinearConstraint addLessOrEqual(LinearArgument expr, double value)
LinearConstraint addLessOrEqual(LinearArgument left, LinearArgument right)
LinearConstraint constraintFromIndex(int index)
void optimize(LinearArgument obj, boolean maximize)
static LinearExprBuilder newBuilder()