Java Reference

Java Reference

ModelBuilderTest.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 static com.google.common.truth.Truth.assertThat;
17 
18 import com.google.ortools.Loader;
19 import java.time.Duration;
20 import org.junit.jupiter.api.BeforeEach;
21 import org.junit.jupiter.api.Test;
22 
23 public final class ModelBuilderTest {
24  @BeforeEach
25  public void setUp() {
27  }
28 
29  @Test
31  ModelBuilder model = new ModelBuilder();
32  model.setName("minimal_linear_example");
33  double infinity = Double.POSITIVE_INFINITY;
34  Variable x1 = model.newNumVar(0.0, infinity, "x1");
35  Variable x2 = model.newNumVar(0.0, infinity, "x2");
36  Variable x3 = model.newNumVar(0.0, infinity, "x3");
37 
38  assertThat(model.numVariables()).isEqualTo(3);
39  assertThat(x1.getIntegrality()).isFalse();
40  assertThat(x1.getLowerBound()).isEqualTo(0.0);
41  assertThat(x2.getUpperBound()).isEqualTo(infinity);
42  x1.setLowerBound(1.0);
43  assertThat(x1.getLowerBound()).isEqualTo(1.0);
44 
45  LinearConstraint c0 = model.addLessOrEqual(LinearExpr.sum(new Variable[] {x1, x2, x3}), 100.0);
46  assertThat(c0.getUpperBound()).isEqualTo(100.0);
47  LinearConstraint c1 =
48  model
50  LinearExpr.newBuilder().addTerm(x1, 10.0).addTerm(x2, 4.0).addTerm(x3, 5.0), 600.0)
51  .withName("c1");
52  assertThat(c1.getName()).isEqualTo("c1");
54  LinearExpr.newBuilder().addTerm(x1, 2.0).addTerm(x2, 2.0).addTerm(x3, 6.0), 300.0);
55  assertThat(c2.getUpperBound()).isEqualTo(300.0);
56 
57  model.maximize(
58  LinearExpr.weightedSum(new Variable[] {x1, x2, x3}, new double[] {10.0, 6, 4.0}));
59  assertThat(x3.getObjectiveCoefficient()).isEqualTo(4.0);
60  assertThat(model.getObjectiveOffset()).isEqualTo(0.0);
61  model.setObjectiveOffset(-5.5);
62  assertThat(model.getObjectiveOffset()).isEqualTo(-5.5);
63 
64  ModelSolver solver = new ModelSolver("glop");
65  assertThat(solver.solverIsSupported()).isTrue();
66  solver.setTimeLimit(Duration.ofSeconds(1));
67  assertThat(solver.solve(model)).isEqualTo(SolveStatus.OPTIMAL);
68 
69  assertThat(solver.getObjectiveValue())
70  .isWithin(1e-5)
71  .of(733.333333 + model.getObjectiveOffset());
72  assertThat(solver.getValue(x1)).isWithin(1e-5).of(33.333333);
73  assertThat(solver.getValue(x2)).isWithin(1e-5).of(66.6666673);
74  assertThat(solver.getValue(x3)).isWithin(1e-5).of(0.0);
75 
76  double dualObjectiveValue = solver.getDualValue(c0) * c0.getUpperBound()
77  + solver.getDualValue(c1) * c1.getUpperBound()
78  + solver.getDualValue(c2) * c2.getUpperBound() + model.getObjectiveOffset();
79  assertThat(solver.getObjectiveValue()).isWithin(1e-5).of(dualObjectiveValue);
80 
81  assertThat(solver.getReducedCost(x1)).isWithin(1e-5).of(0.0);
82  assertThat(solver.getReducedCost(x2)).isWithin(1e-5).of(0.0);
83  assertThat(solver.getReducedCost(x3))
84  .isWithin(1e-5)
85  .of(4.0 - 1.0 * solver.getDualValue(c0) - 5.0 * solver.getDualValue(c1));
86 
87  assertThat(solver.getActivity(c0)).isWithin(1e-5).of(100.0);
88  assertThat(solver.getActivity(c1)).isWithin(1e-5).of(600.0);
89  assertThat(solver.getActivity(c2)).isWithin(1e-5).of(200.0);
90 
91  assertThat(model.exportToLpString(false)).contains("minimal_linear_example");
92  assertThat(model.exportToMpsString(false)).contains("minimal_linear_example");
93  }
94 
95  @Test
96  public void importFromMpsString() {
97  ModelBuilder model = new ModelBuilder();
98  String mpsData = "* Generated by MPModelProtoExporter\n"
99  + "* Name : SupportedMaximizationProblem\n"
100  + "* Format : Free\n"
101  + "* Constraints : 0\n"
102  + "* Variables : 1\n"
103  + "* Binary : 0\n"
104  + "* Integer : 0\n"
105  + "* Continuous : 1\n"
106  + "NAME SupportedMaximizationProblem\n"
107  + "OBJSENSE\n"
108  + " MAX\n"
109  + "ROWS\n"
110  + " N COST\n"
111  + "COLUMNS\n"
112  + " X_ONE COST 1\n"
113  + "BOUNDS\n"
114  + " UP BOUND X_ONE 4\n"
115  + "ENDATA";
116  assertThat(model.importFromMpsString(mpsData)).isTrue();
117  assertThat(model.getName()).isEqualTo("SupportedMaximizationProblem");
118  }
119 
120  @Test
121  public void importFromLpString() {
122  ModelBuilder model = new ModelBuilder();
123  String lpData = "min: x + y;\n"
124  + "bin: b1, b2, b3;\n"
125  + "1 <= x <= 42;\n"
126  + "constraint_num1: 5 b1 + 3b2 + x <= 7;\n"
127  + "4 y + b2 - 3 b3 <= 2;\n"
128  + "constraint_num2: -4 b1 + b2 - 3 z <= -2;\n";
129  assertThat(model.importFromLpString(lpData)).isTrue();
130  assertThat(model.numVariables()).isEqualTo(6);
131  assertThat(model.numConstraints()).isEqualTo(3);
132  assertThat(model.varFromIndex(0).getLowerBound()).isEqualTo(1.0);
133  assertThat(model.varFromIndex(0).getUpperBound()).isEqualTo(42.0);
134  assertThat(model.varFromIndex(0).getName()).isEqualTo("x");
135  }
136 }
Load native libraries needed for using ortools-java.
Definition: Loader.java:33
static synchronized void loadNativeLibraries()
Definition: Loader.java:104
Wrapper around a linear constraint stored in the ModelBuilderHelper instance.
LinearConstraint withName(String name)
Inline setter.
double getUpperBound()
Returns the upper bound of the variable.
String getName()
Returns the name of the variable given upon creation.
LinearExprBuilder addTerm(LinearArgument expr, double coeff)
int numVariables()
Returns the number of variables in the model.
Variable newNumVar(double lb, double ub, String name)
Creates an continuous variable with domain [lb, ub].
boolean importFromMpsString(String mpsString)
Variable varFromIndex(int index)
Rebuilds a variable from its index.
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.
void setName(String name)
Sets the name of the model.
void maximize(LinearArgument obj)
Minimize expression.
double getDualValue(LinearConstraint ct)
Checks that the solver has found a solution, and returns the dual value of the given constraint.
double getReducedCost(Variable var)
Checks that the solver has found a solution, and returns the reduced cost of the given variable.
double getObjectiveValue()
Checks that the solver has found a solution, and returns the objective value.
SolveStatus solve(ModelBuilder model)
Solves given model, and returns the status of the response.
double getValue(Variable var)
Checks that the solver has found a solution, and returns the value of the given variable.
double getActivity(LinearConstraint ct)
Checks that the solver has found a solution, and returns the activity of the given constraint.
void setTimeLimit(Duration limit)
Sets the time limit for the solve in seconds.
boolean solverIsSupported()
Returns whether solver specified during the ctor was found and correctly installed.
double getObjectiveCoefficient()
Returns the objective coefficient of the variable.
Definition: Variable.java:74
double getLowerBound()
Returns the lower bound of the variable.
Definition: Variable.java:44
double getUpperBound()
Returns the upper bound of the variable.
Definition: Variable.java:54
String getName()
Returns the name of the variable given upon creation.
Definition: Variable.java:84
boolean getIntegrality()
Returns whether the variable is integral.
Definition: Variable.java:64
void setLowerBound(double lowerBound)
Sets the lower bound of the variable.
Definition: Variable.java:49
A linear expression (sum (ai * xi) + b).
static LinearExpr weightedSum(LinearArgument[] exprs, double[] coeffs)
Shortcut for newBuilder().addWeightedSum(exprs, coeffs).build()
static LinearExprBuilder newBuilder()
Returns a builder.
static LinearExpr sum(LinearArgument[] exprs)
Shortcut for newBuilder().addSum(exprs).build()