OR-Tools  7.1
SimpleLpProgram.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 GLOP solver.
15 // [START program]
16 // [START import]
21 // [END import]
22 
24 public class SimpleLpProgram {
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 GLOP backend.
32  MPSolver solver =
34  // [END solver]
35 
36  // [START variables]
37  // Create the variables x and y.
38  MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
39  MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
40 
41  System.out.println("Number of variables = " + solver.numVariables());
42  // [END variables]
43 
44  // [START constraints]
45  // Create a linear constraint, 0 <= x + y <= 2.
46  MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
47  ct.setCoefficient(x, 1);
48  ct.setCoefficient(y, 1);
49 
50  System.out.println("Number of constraints = " + solver.numConstraints());
51  // [END constraints]
52 
53  // [START objective]
54  // Create the objective function, 3 * x + y.
55  MPObjective objective = solver.objective();
56  objective.setCoefficient(x, 3);
57  objective.setCoefficient(y, 1);
58  objective.setMaximization();
59  // [END objective]
60 
61  // [START solve]
62  solver.solve();
63  // [END solve]
64 
65  // [START print_solution]
66  System.out.println("Solution:");
67  System.out.println("Objective value = " + objective.value());
68  System.out.println("x = " + x.solutionValue());
69  System.out.println("y = " + y.solutionValue());
70  // [END print_solution]
71  }
72 }
73 // [END program]
Minimal Linear Programming example to showcase calling the solver.
void setCoefficient(MPVariable var, double coeff)
MPConstraint makeConstraint(double lb, double ub)
Definition: MPSolver.java:131
static void main(String[] args)
void setCoefficient(MPVariable var, double coeff)
MPVariable makeNumVar(double lb, double ub, String name)
Definition: MPSolver.java:107