2018-11-09 09:21:33 +01:00
|
|
|
// Copyright 2010-2018 Google LLC
|
2018-11-06 14:36:21 +01:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
2018-11-09 09:21:33 +01:00
|
|
|
// Minimal example to call the GLOP solver.
|
2018-11-06 14:36:21 +01:00
|
|
|
// [START program]
|
2020-05-26 09:30:42 +02:00
|
|
|
package com.google.ortools.linearsolver.samples;
|
2019-05-13 11:05:42 +02:00
|
|
|
// [START import]
|
2018-11-06 14:36:21 +01:00
|
|
|
import com.google.ortools.linearsolver.MPConstraint;
|
|
|
|
|
import com.google.ortools.linearsolver.MPObjective;
|
|
|
|
|
import com.google.ortools.linearsolver.MPSolver;
|
|
|
|
|
import com.google.ortools.linearsolver.MPVariable;
|
2019-05-13 11:05:42 +02:00
|
|
|
// [END import]
|
2018-11-06 14:36:21 +01:00
|
|
|
|
2018-11-26 17:30:10 +01:00
|
|
|
/** Minimal Linear Programming example to showcase calling the solver.*/
|
2018-11-09 09:21:33 +01:00
|
|
|
public class SimpleLpProgram {
|
2018-11-22 09:29:22 +01:00
|
|
|
static {
|
|
|
|
|
System.loadLibrary("jniortools");
|
|
|
|
|
}
|
2018-11-06 14:36:21 +01:00
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2019-05-13 11:05:42 +02:00
|
|
|
// [START solver]
|
2018-11-09 09:21:33 +01:00
|
|
|
// Create the linear solver with the GLOP backend.
|
2019-05-28 15:28:01 +02:00
|
|
|
MPSolver solver =
|
|
|
|
|
new MPSolver("SimpleLpProgram", MPSolver.OptimizationProblemType.GLOP_LINEAR_PROGRAMMING);
|
2019-05-13 11:05:42 +02:00
|
|
|
// [END solver]
|
2018-11-09 09:21:33 +01:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
// [START variables]
|
2018-11-06 14:36:21 +01:00
|
|
|
// Create the variables x and y.
|
|
|
|
|
MPVariable x = solver.makeNumVar(0.0, 1.0, "x");
|
|
|
|
|
MPVariable y = solver.makeNumVar(0.0, 2.0, "y");
|
2018-11-09 09:21:33 +01:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
System.out.println("Number of variables = " + solver.numVariables());
|
|
|
|
|
// [END variables]
|
|
|
|
|
|
|
|
|
|
// [START constraints]
|
2018-11-09 09:21:33 +01:00
|
|
|
// Create a linear constraint, 0 <= x + y <= 2.
|
|
|
|
|
MPConstraint ct = solver.makeConstraint(0.0, 2.0, "ct");
|
|
|
|
|
ct.setCoefficient(x, 1);
|
|
|
|
|
ct.setCoefficient(y, 1);
|
|
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
System.out.println("Number of constraints = " + solver.numConstraints());
|
|
|
|
|
// [END constraints]
|
|
|
|
|
|
|
|
|
|
// [START objective]
|
2018-11-09 09:21:33 +01:00
|
|
|
// Create the objective function, 3 * x + y.
|
2018-11-06 14:36:21 +01:00
|
|
|
MPObjective objective = solver.objective();
|
2018-11-09 09:21:33 +01:00
|
|
|
objective.setCoefficient(x, 3);
|
2018-11-06 14:36:21 +01:00
|
|
|
objective.setCoefficient(y, 1);
|
|
|
|
|
objective.setMaximization();
|
2019-05-13 11:05:42 +02:00
|
|
|
// [END objective]
|
2018-11-09 09:21:33 +01:00
|
|
|
|
2019-05-13 11:05:42 +02:00
|
|
|
// [START solve]
|
2018-11-06 14:36:21 +01:00
|
|
|
solver.solve();
|
2019-05-13 11:05:42 +02:00
|
|
|
// [END solve]
|
|
|
|
|
|
|
|
|
|
// [START print_solution]
|
2018-11-06 14:36:21 +01:00
|
|
|
System.out.println("Solution:");
|
2019-05-13 11:05:42 +02:00
|
|
|
System.out.println("Objective value = " + objective.value());
|
2018-11-06 14:36:21 +01:00
|
|
|
System.out.println("x = " + x.solutionValue());
|
|
|
|
|
System.out.println("y = " + y.solutionValue());
|
2019-05-13 11:05:42 +02:00
|
|
|
// [END print_solution]
|
2018-11-06 14:36:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// [END program]
|