OR-Tools  7.1
SimpleMipProgram.java
Go to the documentation of this file.
1 // Copyright 2010-2018 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 // Minimal example to call the MIP solver.
15 // [START program]
16 // [START import]
21 // [END import]
22 
24 public class SimpleMipProgram {
25  static {
26  System.loadLibrary("jniortools");
27  }
28 
29  public static void main(String[] args) throws Exception {
30  // [START solver]
31  // Create the linear solver with the CBC backend.
32  MPSolver solver = new MPSolver(
34  // [END solver]
35 
36  // [START variables]
37  double infinity = java.lang.Double.POSITIVE_INFINITY;
38  // x and y are integer non-negative variables.
39  MPVariable x = solver.makeIntVar(0.0, infinity, "x");
40  MPVariable y = solver.makeIntVar(0.0, infinity, "y");
41 
42  System.out.println("Number of variables = " + solver.numVariables());
43  // [END variables]
44 
45  // [START constraints]
46  // x + 7 * y <= 17.5.
47  MPConstraint c0 = solver.makeConstraint(-infinity, 17.5, "c0");
48  c0.setCoefficient(x, 1);
49  c0.setCoefficient(y, 7);
50 
51  // x <= 3.5.
52  MPConstraint c1 = solver.makeConstraint(-infinity, 3.5, "c1");
53  c1.setCoefficient(x, 1);
54  c1.setCoefficient(y, 0);
55 
56  System.out.println("Number of constraints = " + solver.numConstraints());
57  // [END constraints]
58 
59  // [START objective]
60  // Maximize x + 10 * y.
61  MPObjective objective = solver.objective();
62  objective.setCoefficient(x, 1);
63  objective.setCoefficient(y, 10);
64  objective.setMaximization();
65  // [END objective]
66 
67  // [START solve]
68  final MPSolver.ResultStatus resultStatus = solver.solve();
69  // Check that the problem has an optimal solution.
70  if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
71  System.err.println("The problem does not have an optimal solution!");
72  return;
73  }
74  // Verify that the solution satisfies all constraints (when using solvers
75  // others than GLOP_LINEAR_PROGRAMMING, this is highly recommended!).
76  if (!solver.verifySolution(/*tolerance=*/1e-7, /*log_errors=*/true)) {
77  System.err.println("The solution returned by the solver violated the"
78  + " problem constraints by at least 1e-7");
79  return;
80  }
81  // [END solve]
82 
83  // [START print_solution]
84  System.out.println("Solution:");
85  System.out.println("Objective value = " + objective.value());
86  System.out.println("x = " + x.solutionValue());
87  System.out.println("y = " + y.solutionValue());
88  // [END print_solution]
89 
90  // [START advanced]
91  System.out.println("\nAdvanced usage:");
92  System.out.println("Problem solved in " + solver.wallTime() + " milliseconds");
93  System.out.println("Problem solved in " + solver.iterations() + " iterations");
94  System.out.println("Problem solved in " + solver.nodes() + " branch-and-bound nodes");
95  // [END advanced]
96  }
97 }
98 // [END program]
Minimal Mixed Integer Programming example to showcase calling the solver.
MPVariable makeIntVar(double lb, double ub, String name)
Definition: MPSolver.java:112
static void main(String[] args)
void setCoefficient(MPVariable var, double coeff)
MPConstraint makeConstraint(double lb, double ub)
Definition: MPSolver.java:131
boolean verifySolution(double tolerance, boolean log_errors)
Definition: MPSolver.java:168
void setCoefficient(MPVariable var, double coeff)