Files
ortools-clone/examples/tests/LinearSolverTest.java

212 lines
7.5 KiB
Java
Raw Normal View History

2021-04-02 10:08:51 +02:00
// Copyright 2010-2021 Google LLC
2019-04-23 13:16:20 +02: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.
2020-09-14 23:49:23 +02:00
package com.google.ortools;
2019-04-23 13:16:20 +02:00
2020-09-14 23:49:23 +02:00
import com.google.ortools.Loader;
2019-04-23 13:16:20 +02: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;
import java.util.ArrayList;
2020-09-23 12:10:52 +02:00
import java.util.Arrays;
2019-04-23 13:16:20 +02:00
import java.util.logging.Logger;
2020-09-14 23:49:23 +02:00
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
2019-04-23 13:16:20 +02:00
2020-09-14 23:49:23 +02:00
public class LinearSolverTest {
2019-04-23 13:16:20 +02:00
static {
2020-09-23 12:10:52 +02:00
System.setProperty(
"java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] %5$s %n");
2019-04-23 13:16:20 +02:00
}
2020-09-14 23:49:23 +02:00
private static final Logger logger = Logger.getLogger(LinearSolverTest.class.getName());
2019-04-23 13:16:20 +02:00
2020-09-23 12:10:52 +02:00
private static void solveAndPrint(
MPSolver solver, MPVariable[] variables, MPConstraint[] constraints) {
2019-04-23 13:16:20 +02:00
logger.info("Number of variables = " + solver.numVariables());
2020-09-23 12:10:52 +02:00
logger.info("Number of constraints = " + solver.numConstraints());
2019-04-23 13:16:20 +02:00
final MPSolver.ResultStatus status = solver.solve();
// Check that the problem has an optimal solution.
if (status != MPSolver.ResultStatus.OPTIMAL) {
logger.severe("The problem does not have an optimal solution!");
}
logger.info("Solution:");
ArrayList<MPVariable> vars = new ArrayList<>(Arrays.asList(variables));
2020-09-23 12:10:52 +02:00
vars.forEach(var -> logger.info(var.name() + " = " + var.solutionValue()));
2019-04-23 13:16:20 +02:00
logger.info("Optimal objective value = " + solver.objective().value());
logger.info("");
logger.info("Advanced usage:");
logger.info("Problem solved in " + solver.wallTime() + " milliseconds");
logger.info("Problem solved in " + solver.iterations() + " iterations");
2020-09-23 12:10:52 +02:00
if (solver.isMip())
return;
2020-09-23 12:10:52 +02:00
vars.forEach(var -> logger.info(var.name() + ": reduced cost " + var.reducedCost()));
2019-04-23 13:16:20 +02:00
final double[] activities = solver.computeConstraintActivities();
ArrayList<MPConstraint> cts = new ArrayList<>(Arrays.asList(constraints));
2020-09-23 12:10:52 +02:00
cts.forEach(ct
-> logger.info(ct.name() + ": dual value = " + ct.dualValue()
+ " activity = " + activities[ct.index()]));
2019-04-23 13:16:20 +02:00
}
2020-09-14 23:49:23 +02:00
@ParameterizedTest
2020-09-23 12:10:52 +02:00
@ValueSource(strings = {"GLOP", "GLPK_LP", "CLP", "GUROBI_LP"})
2020-09-14 23:49:23 +02:00
private static void testLinearProgramming(String problem_type) {
logger.info("------ Linear programming example with " + problem_type + " ------");
2020-08-19 11:42:52 +02:00
MPSolver solver = MPSolver.createSolver(problem_type);
2020-09-23 12:10:52 +02:00
if (solver == null)
return;
2019-04-23 13:16:20 +02:00
// x and y are continuous non-negative variables.
MPVariable x = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "x");
MPVariable y = solver.makeNumVar(0.0, Double.POSITIVE_INFINITY, "y");
// Objectif function: Maximize 3x + 4y).
MPObjective objective = solver.objective();
objective.setCoefficient(x, 3);
objective.setCoefficient(y, 4);
objective.setMaximization();
// x + 2y <= 14.
final MPConstraint c0 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 14.0, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 2);
// 3x - y >= 0.
final MPConstraint c1 = solver.makeConstraint(0.0, Double.POSITIVE_INFINITY, "c1");
c1.setCoefficient(x, 3);
c1.setCoefficient(y, -1);
// x - y <= 2.
final MPConstraint c2 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 2.0, "c2");
c2.setCoefficient(x, 1);
c2.setCoefficient(y, -1);
solveAndPrint(solver, new MPVariable[] {x, y}, new MPConstraint[] {c0, c1, c2});
}
2020-09-14 23:49:23 +02:00
@ParameterizedTest
2020-09-23 12:10:52 +02:00
@ValueSource(strings = {"GLPK", "CBC", "SCIP", "SAT"})
2020-09-14 23:49:23 +02:00
private static void testMixedIntegerProgramming(String problem_type) {
logger.info("------ Mixed integer programming example with " + problem_type + " ------");
2020-09-23 12:10:52 +02:00
MPSolver solver = MPSolver.createSolver(problem_type);
if (solver == null)
return;
2019-04-23 13:16:20 +02:00
// x and y are continuous non-negative variables.
MPVariable x = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "x");
MPVariable y = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "y");
// Objectif function: Maximize x + 10 * y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 1);
objective.setCoefficient(y, 10);
objective.setMaximization();
// x + 7 * y <= 17.5.
final MPConstraint c0 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 17.5, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 7);
// x <= 3.5.
final MPConstraint c1 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 3.5, "c1");
c1.setCoefficient(x, 1);
c1.setCoefficient(y, 0);
solveAndPrint(solver, new MPVariable[] {x, y}, new MPConstraint[] {c0, c1});
}
2020-09-14 23:49:23 +02:00
@ParameterizedTest
2020-09-23 12:10:52 +02:00
@ValueSource(strings = {"SAT", "BOP"})
2020-09-14 23:49:23 +02:00
private static void testBooleanProgramming(String problem_type) {
logger.info("------ Boolean programming example with " + problem_type + " ------");
2020-09-23 12:10:52 +02:00
MPSolver solver = MPSolver.createSolver(problem_type);
if (solver == null)
return;
2019-04-23 13:16:20 +02:00
// x and y are continuous non-negative variables.
MPVariable x = solver.makeBoolVar("x");
MPVariable y = solver.makeBoolVar("y");
// Objectif function: Maximize 2 * x + y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 2);
objective.setCoefficient(y, 1);
objective.setMinimization();
// 1 <= x + 2 * y <= 3.
final MPConstraint c0 = solver.makeConstraint(1, 3, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 2);
solveAndPrint(solver, new MPVariable[] {x, y}, new MPConstraint[] {c0});
}
2020-09-14 23:49:23 +02:00
@Test
public void testSameConstraintName() {
Loader.loadNativeLibraries();
2020-08-19 11:42:52 +02:00
MPSolver solver = MPSolver.createSolver("CBC");
2019-04-23 13:16:20 +02:00
boolean success = true;
solver.makeConstraint("my_const_name");
try {
solver.makeConstraint("my_const_name");
2020-09-23 12:10:52 +02:00
} catch (Throwable e) {
2019-04-23 13:16:20 +02:00
System.out.println(e);
success = false;
}
logger.info("Success = " + success);
}
2020-09-14 23:49:23 +02:00
@Test
public void testSetHintAndSolverGetters() {
Loader.loadNativeLibraries();
2020-09-23 12:10:52 +02:00
MPSolver solver = MPSolver.createSolver("GLOP");
// x and y are continuous non-negative variables.
MPVariable x = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "x");
MPVariable y = solver.makeIntVar(0.0, Double.POSITIVE_INFINITY, "y");
// Objectif function: Maximize x + 10 * y.
MPObjective objective = solver.objective();
objective.setCoefficient(x, 1);
objective.setCoefficient(y, 10);
objective.setMaximization();
// x + 7 * y <= 17.5.
final MPConstraint c0 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 17.5, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 7);
// x <= 3.5.
final MPConstraint c1 = solver.makeConstraint(-Double.POSITIVE_INFINITY, 3.5, "c1");
c1.setCoefficient(x, 1);
c1.setCoefficient(y, 0);
if (solver.constraints().length != 2) {
throw new RuntimeException("WrongConstraintLength");
}
if (solver.variables().length != 2) {
throw new RuntimeException("WrongConstraintLength");
}
solver.setHint(new MPVariable[] {x, y}, new double[] {2.0, 3.0});
}
2019-04-23 13:16:20 +02:00
}