OR-Tools  7.1
LinearProgrammingExample.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 // [START program]
19 
22  static {
23  System.loadLibrary("jniortools");
24  }
25 
26  public static void main(String[] args) throws Exception {
27  // [START solver]
28  MPSolver solver = new MPSolver(
29  "LinearProgrammingExample", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
30  // [END solver]
31 
32  // [START variables]
33  double infinity = java.lang.Double.POSITIVE_INFINITY;
34  // x and y are continuous non-negative variables.
35  MPVariable x = solver.makeNumVar(0.0, infinity, "x");
36  MPVariable y = solver.makeNumVar(0.0, infinity, "y");
37  System.out.println("Number of variables = " + solver.numVariables());
38  // [END variables]
39 
40  // [START constraints]
41  // x + 2*y <= 14.
42  MPConstraint c0 = solver.makeConstraint(-infinity, 14.0, "c0");
43  c0.setCoefficient(x, 1);
44  c0.setCoefficient(y, 2);
45 
46  // 3*x - y >= 0.
47  MPConstraint c1 = solver.makeConstraint(0.0, infinity, "c1");
48  c1.setCoefficient(x, 3);
49  c1.setCoefficient(y, -1);
50 
51  // x - y <= 2.
52  MPConstraint c2 = solver.makeConstraint(-infinity, 2.0, "c2");
53  c2.setCoefficient(x, 1);
54  c2.setCoefficient(y, -1);
55  System.out.println("Number of constraints = " + solver.numConstraints());
56  // [END constraints]
57 
58  // [START objective]
59  // Maximize 3 * x + 4 * y.
60  MPObjective objective = solver.objective();
61  objective.setCoefficient(x, 3);
62  objective.setCoefficient(y, 4);
63  objective.setMaximization();
64  // [END objective]
65 
66  // [START solve]
67  final MPSolver.ResultStatus resultStatus = solver.solve();
68  // Check that the problem has an optimal solution.
69  if (resultStatus != MPSolver.ResultStatus.OPTIMAL) {
70  System.err.println("The problem does not have an optimal solution!");
71  return;
72  }
73  // [END solve]
74 
75  // [START print_solution]
76  // The value of each variable in the solution.
77  System.out.println("Solution");
78  System.out.println("x = " + x.solutionValue());
79  System.out.println("y = " + y.solutionValue());
80 
81  // The objective value of the solution.
82  System.out.println("Optimal objective value = " + solver.objective().value());
83  // [END print_solution]
84  }
85 }
86 // [END program]
void setCoefficient(MPVariable var, double coeff)
static void main(String[] args)
MPConstraint makeConstraint(double lb, double ub)
Definition: MPSolver.java:131
void setCoefficient(MPVariable var, double coeff)
MPVariable makeNumVar(double lb, double ub, String name)
Definition: MPSolver.java:107
Simple linear programming example.