Java Reference

Java Reference

ModelBuilder.java
Go to the documentation of this file.
1 // Copyright 2010-2022 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 package com.google.ortools.modelbuilder;
15 
16 import java.util.LinkedHashMap;
17 import java.util.Map;
18 
24 public 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 
46  public ModelBuilder() {
47  helper = new ModelBuilderHelper();
48  constantMap = new LinkedHashMap<>();
49  }
50 
51  // Integer variables.
52 
54  public Variable newVar(double lb, double ub, boolean isIntegral, String name) {
55  return new Variable(helper, lb, ub, isIntegral, name);
56  }
57 
59  public Variable newNumVar(double lb, double ub, String name) {
60  return new Variable(helper, lb, ub, false, name);
61  }
62 
64  public Variable newIntVar(double lb, double ub, String name) {
65  return new Variable(helper, lb, ub, true, name);
66  }
67 
69  public Variable newBoolVar(String name) {
70  return new Variable(helper, 0, 1, true, name);
71  }
72 
74  public Variable newConstant(double value) {
75  if (constantMap.containsKey(value)) {
76  return new Variable(helper, constantMap.get(value));
77  }
78  Variable cste = new Variable(helper, value, value, false, ""); // bounds and name.
79  constantMap.put(value, cste.getIndex());
80  return cste;
81  }
82 
84  public Variable varFromIndex(int index) {
85  return new Variable(helper, index);
86  }
87 
88  // Linear constraints.
89 
91  public LinearConstraint addLinearConstraint(LinearArgument expr, double lb, double ub) {
92  LinearConstraint lin = new LinearConstraint(helper);
93  final LinearExpr e = expr.build();
94  for (int i = 0; i < e.numElements(); ++i) {
95  helper.addConstraintTerm(lin.getIndex(), e.getVariableIndex(i), e.getCoefficient(i));
96  }
97  double offset = e.getOffset();
98  if (lb == Double.NEGATIVE_INFINITY || lb == Double.POSITIVE_INFINITY) {
99  lin.setLowerBound(lb);
100  } else {
101  lin.setLowerBound(lb - offset);
102  }
103  if (ub == Double.NEGATIVE_INFINITY || ub == Double.POSITIVE_INFINITY) {
104  lin.setUpperBound(ub);
105  } else {
106  lin.setUpperBound(ub - offset);
107  }
108  return lin;
109  }
110 
112  public int numVariables() {
113  return helper.numVariables();
114  }
115 
117  public LinearConstraint addEquality(LinearArgument expr, double value) {
118  return addLinearConstraint(expr, value, value);
119  }
120 
123  LinearExprBuilder difference = LinearExpr.newBuilder();
124  difference.addTerm(left, 1);
125  difference.addTerm(right, -1);
126  return addLinearConstraint(difference, 0.0, 0.0);
127  }
128 
130  public LinearConstraint addLessOrEqual(LinearArgument expr, double value) {
131  return addLinearConstraint(expr, Double.NEGATIVE_INFINITY, value);
132  }
133 
136  LinearExprBuilder difference = LinearExpr.newBuilder();
137  difference.addTerm(left, 1);
138  difference.addTerm(right, -1);
139  return addLinearConstraint(difference, Double.NEGATIVE_INFINITY, 0.0);
140  }
141 
143  public LinearConstraint addGreaterOrEqual(LinearArgument expr, double value) {
144  return addLinearConstraint(expr, value, Double.POSITIVE_INFINITY);
145  }
146 
149  LinearExprBuilder difference = LinearExpr.newBuilder();
150  difference.addTerm(left, 1);
151  difference.addTerm(right, -1);
152  return addLinearConstraint(difference, 0.0, Double.POSITIVE_INFINITY);
153  }
154 
156  public int numConstraints() {
157  return helper.numConstraints();
158  }
159 
161  public void minimize(LinearArgument obj) {
162  optimize(obj, false);
163  }
164 
166  public void maximize(LinearArgument obj) {
167  optimize(obj, true);
168  }
169 
171  public void optimize(LinearArgument obj, boolean maximize) {
172  helper.clearObjective();
173  LinearExpr e = obj.build();
174  LinkedHashMap<Integer, Double> coeffMap = new LinkedHashMap<>();
175  for (int i = 0; i < e.numElements(); ++i) {
176  coeffMap.merge(e.getVariableIndex(i), e.getCoefficient(i), Double::sum);
177  }
178  for (Map.Entry<Integer, Double> entry : coeffMap.entrySet()) {
179  if (entry.getValue() != 0) {
180  helper.setVarObjectiveCoefficient(entry.getKey(), entry.getValue());
181  }
182  }
183  helper.setObjectiveOffset(e.getOffset());
184  helper.setMaximize(maximize);
185  }
186 
188  double getObjectiveOffset() {
189  return helper.getObjectiveOffset();
190  }
191 
193  void setObjectiveOffset(double offset) {
194  helper.setObjectiveOffset(offset);
195  }
196 
197  // Model getters, import, export.
198 
200  public String getName() {
201  return helper.getName();
202  }
203 
205  public void setName(String name) {
206  helper.setName(name);
207  }
208 
216  public boolean exportToFile(String file) {
217  return helper.writeModelToFile(file);
218  }
219 
220  public String exportToMpsString(boolean obfuscate) {
221  return helper.exportToMpsString(obfuscate);
222  }
223 
224  public String exportToLpString(boolean obfuscate) {
225  return helper.exportToLpString(obfuscate);
226  }
227 
228  public boolean importFromMpsString(String mpsString) {
229  return helper.importFromMpsString(mpsString);
230  }
231 
232  public boolean importFromMpsFile(String mpsFile) {
233  return helper.importFromMpsString(mpsFile);
234  }
235 
236  public boolean importFromLpString(String lpString) {
237  return helper.importFromLpString(lpString);
238  }
239 
240  public boolean importFromLpFile(String lpFile) {
241  return helper.importFromMpsString(lpFile);
242  }
243 
244  // Getters.
246  public ModelBuilderHelper getHelper() {
247  return helper;
248  }
249 
250  private final ModelBuilderHelper helper;
251  private final Map<Double, Integer> constantMap;
252 }
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
int getIndex()
Returns the index of the constraint in the model.
void setUpperBound(double ub)
Returns the upper bound of the variable.
void setLowerBound(double lb)
Returns the lower bound of the variable.
LinearExprBuilder addTerm(LinearArgument expr, double coeff)
Exception thrown when parallel arrays have mismatched lengths.
MismatchedArrayLengths(String methodName, String array1Name, String array2Name)
Exception thrown when an array has a wrong length.
int numVariables()
Returns the number of variables in the model.
LinearConstraint addGreaterOrEqual(LinearArgument left, LinearArgument right)
Adds.
LinearConstraint addEquality(LinearArgument left, LinearArgument right)
Adds.
Variable newNumVar(double lb, double ub, String name)
Creates an continuous variable with domain [lb, ub].
boolean importFromMpsString(String mpsString)
boolean exportToFile(String file)
Write the model as a protocol buffer to 'file'.
void optimize(LinearArgument obj, boolean maximize)
Sets the objective expression.
ModelBuilderHelper getHelper()
Returns the model builder helper.
Variable varFromIndex(int index)
Rebuilds a variable from its index.
LinearConstraint addLessOrEqual(LinearArgument left, LinearArgument right)
Adds.
int numConstraints()
Returns the number of constraints in the model.
LinearConstraint addLessOrEqual(LinearArgument expr, double value)
Adds.
String getName()
Returns the name of the model.
LinearConstraint addEquality(LinearArgument expr, double value)
Adds.
Variable newConstant(double value)
Creates a constant variable.
LinearConstraint addLinearConstraint(LinearArgument expr, double lb, double ub)
Adds.
Variable newBoolVar(String name)
Creates a Boolean variable with the given name.
LinearConstraint addGreaterOrEqual(LinearArgument expr, double value)
Adds.
void setName(String name)
Sets the name of the model.
void minimize(LinearArgument obj)
Minimize expression.
void maximize(LinearArgument obj)
Minimize expression.
Variable newIntVar(double lb, double ub, String name)
Creates an integer variable with domain [lb, ub].
Variable newVar(double lb, double ub, boolean isIntegral, String name)
Creates a variable with domain [lb, ub].
int getIndex()
Returns the index of the variable in the underlying ModelBuilderHelper.
Definition: Variable.java:33
A object that can build a LinearExpr object.
LinearExpr build()
Builds a linear expression.
A linear expression (sum (ai * xi) + b).
int numElements()
Returns the number of terms (excluding the constant one) in this expression.
double getCoefficient(int index)
Returns the ith coefficient.
static LinearExprBuilder newBuilder()
Returns a builder.
double getOffset()
Returns the constant part of the expression.
int getVariableIndex(int index)
Returns the index of the ith variable.