diff --git a/Makefile.java.mk b/Makefile.java.mk index 0ef1e4be7c..040419b4b3 100644 --- a/Makefile.java.mk +++ b/Makefile.java.mk @@ -281,18 +281,18 @@ $(LIBPREFIX)jnilinearsolver.$(JNILIBEXT): objs/linear_solver_java_wrap.$O $(LP_L # Java Algorithms Examples -compile_LinearSolverExample: objs/com/google/ortools/linearsolver/samples/LinearSolverExample.class +compile_LinearProgramming: objs/com/google/ortools/linearsolver/samples/LinearProgramming.class -objs/com/google/ortools/linearsolver/samples/LinearSolverExample.class: javalp com/google/ortools/linearsolver/samples/LinearSolverExample.java - $(JAVAC_BIN) -d objs -cp com.google.ortools.linearsolver.jar com/google/ortools/linearsolver/samples/LinearSolverExample.java +objs/com/google/ortools/linearsolver/samples/LinearProgramming.class: javalp com/google/ortools/linearsolver/samples/LinearProgramming.java + $(JAVAC_BIN) -d objs -cp com.google.ortools.linearsolver.jar com/google/ortools/linearsolver/samples/LinearProgramming.java -run_LinearSolverExample: compile_LinearSolverExample - $(JAVA_BIN) -Djava.library.path=. -cp objs$(CPSEP)com.google.ortools.linearsolver.jar com.google.ortools.linearsolver.samples.LinearSolverExample +run_LinearProgramming: compile_LinearProgramming + $(JAVA_BIN) -Djava.library.path=. -cp objs$(CPSEP)com.google.ortools.linearsolver.jar com.google.ortools.linearsolver.samples.LinearProgramming -compile_IntegerSolverExample: objs/com/google/ortools/linearsolver/samples/IntegerSolverExample.class +compile_IntegerProgramming: objs/com/google/ortools/linearsolver/samples/IntegerProgramming.class -objs/com/google/ortools/linearsolver/samples/IntegerSolverExample.class: javalp com/google/ortools/linearsolver/samples/IntegerSolverExample.java - $(JAVAC_BIN) -d objs -cp com.google.ortools.linearsolver.jar com/google/ortools/linearsolver/samples/IntegerSolverExample.java +objs/com/google/ortools/linearsolver/samples/IntegerProgramming.class: javalp com/google/ortools/linearsolver/samples/IntegerProgramming.java + $(JAVAC_BIN) -d objs -cp com.google.ortools.linearsolver.jar com/google/ortools/linearsolver/samples/IntegerProgramming.java -run_IntegerSolverExample: compile_IntegerSolverExample - $(JAVA_BIN) -Xss2048k -Djava.library.path=. -cp objs$(CPSEP)com.google.ortools.linearsolver.jar com.google.ortools.linearsolver.samples.IntegerSolverExample +run_IntegerProgramming: compile_IntegerProgramming + $(JAVA_BIN) -Xss2048k -Djava.library.path=. -cp objs$(CPSEP)com.google.ortools.linearsolver.jar com.google.ortools.linearsolver.samples.IntegerProgramming diff --git a/com/google/ortools/linearsolver/samples/IntegerProgramming.java b/com/google/ortools/linearsolver/samples/IntegerProgramming.java new file mode 100644 index 0000000000..c87998e1f7 --- /dev/null +++ b/com/google/ortools/linearsolver/samples/IntegerProgramming.java @@ -0,0 +1,80 @@ +// Copyright 2010-2011 Google +// 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. + +package com.google.ortools.linearsolver.samples; + +import com.google.ortools.linearsolver.MPConstraint; +import com.google.ortools.linearsolver.MPSolver; +import com.google.ortools.linearsolver.MPVariable; + +/** + * Integer programming example that shows how to use the API. + * + */ + +public class IntegerProgramming { + + static { + System.loadLibrary("jnilinearsolver"); + } + + + private static void runIntegerProgrammingExample(int optimizationProblemType) { + MPSolver solver = new MPSolver("IntegerProgrammingExample", + optimizationProblemType); + double infinity = solver.infinity(); + // x1 and x2 are integer non-negative variables. + MPVariable x1 = solver.makeIntVar(0.0, infinity, "x1"); + MPVariable x2 = solver.makeIntVar(0.0, infinity, "x2"); + + // Minimize x1 + 2 * x2. + solver.addObjectiveTerm(x1, 1); + solver.addObjectiveTerm(x2, 2); + + // 2 * x2 + 3 * x1 >= 17. + MPConstraint ct = solver.makeConstraint(17, infinity); + ct.addTerm(x1, 3); + ct.addTerm(x2, 2); + + int resultStatus = solver.solve(); + + // Check that the problem has an optimal solution. + if (resultStatus != MPSolver.OPTIMAL) { + System.err.println("The problem does not have an optimal solution!"); + return; + } + + System.out.println("Problem solved in " + solver.wallTime() + + " milliseconds"); + + // The objective value of the solution. + System.out.println("Optimal objective value = " + solver.objectiveValue()); + + // The value of each variable in the solution. + System.out.println("x1 = " + x1.solutionValue()); + System.out.println("x2 = " + x2.solutionValue()); + + System.out.println("Advanced usage:"); + System.out.println("Problem solved in " + solver.nodes() + + " branch-and-bound nodes"); + } + + public static void main(String[] args) throws Exception { + System.out.println("---- Integer programming example with GLPK ----"); + runIntegerProgrammingExample(MPSolver.GLPK_MIXED_INTEGER_PROGRAMMING); + System.out.println("---- Integer programming example with CBC ----"); + runIntegerProgrammingExample(MPSolver.CBC_MIXED_INTEGER_PROGRAMMING); + System.out.println("---- Integer programming example with SCIP ----"); + runIntegerProgrammingExample(MPSolver.SCIP_MIXED_INTEGER_PROGRAMMING); + } +} diff --git a/com/google/ortools/linearsolver/samples/IntegerSolverExample.java b/com/google/ortools/linearsolver/samples/IntegerSolverExample.java deleted file mode 100644 index 13429c68aa..0000000000 --- a/com/google/ortools/linearsolver/samples/IntegerSolverExample.java +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2010-2011 Google -// 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. - -package com.google.ortools.linearsolver.samples; - -import com.google.ortools.linearsolver.MPConstraint; -import com.google.ortools.linearsolver.MPSolver; -import com.google.ortools.linearsolver.MPVariable; - -/** - * Example of linear solver usage. - * - */ - -public class IntegerSolverExample { - - static { - System.loadLibrary("jnilinearsolver"); - } - - - private static void runFirstIntegerExample(int solverType) { - MPSolver solver = new MPSolver("runFirstIntegerExample", solverType); - double infinity = solver.infinity(); - MPVariable x1 = solver.makeIntVar(0.0, infinity, "x1"); - MPVariable x2 = solver.makeIntVar(0.0, infinity, "x2"); - - solver.addObjectiveTerm(x1, 1); - solver.addObjectiveTerm(x2, 2); - - MPConstraint ct = solver.makeConstraint(17, infinity); - ct.addTerm(x1, 3); - ct.addTerm(x2, 2); - - // Check the solution. - solver.solve(); - System.out.println("Objective value = " + solver.objectiveValue()); - } - - public static void main(String[] args) throws Exception { - runFirstIntegerExample(MPSolver.CBC_MIXED_INTEGER_PROGRAMMING); - runFirstIntegerExample(MPSolver.GLPK_MIXED_INTEGER_PROGRAMMING); - runFirstIntegerExample(MPSolver.SCIP_MIXED_INTEGER_PROGRAMMING); - } -} diff --git a/com/google/ortools/linearsolver/samples/LinearProgramming.java b/com/google/ortools/linearsolver/samples/LinearProgramming.java new file mode 100644 index 0000000000..6d28f25c9c --- /dev/null +++ b/com/google/ortools/linearsolver/samples/LinearProgramming.java @@ -0,0 +1,107 @@ +// Copyright 2010-2011 Google +// 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. + +package com.google.ortools.linearsolver.samples; + +import com.google.ortools.linearsolver.MPConstraint; +import com.google.ortools.linearsolver.MPSolver; +import com.google.ortools.linearsolver.MPVariable; + +/** + * Linear programming example that shows how to use the API. + * + */ + +public class LinearProgramming { + + static { + System.loadLibrary("jnilinearsolver"); + } + + + private static void runLinearProgrammingExample(int optimizationProblemType) { + MPSolver solver = new MPSolver("LinearProgrammingExample", + optimizationProblemType); + double infinity = solver.infinity(); + // x1, x2 and x3 are continuous non-negative variables. + MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1"); + MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2"); + MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3"); + + // Maximize 10 * x1 + 6 * x2 + 4 * x3. + solver.addObjectiveTerm(x1, 10); + solver.addObjectiveTerm(x2, 6); + solver.addObjectiveTerm(x3, 4); + solver.setMaximization(); + + // x1 + x2 + x3 <= 100. + MPConstraint c0 = solver.makeConstraint(-infinity, 100.0); + c0.addTerm(x1, 1); + c0.addTerm(x2, 1); + c0.addTerm(x3, 1); + + // 10 * x1 + 4 * x2 + 5 * x3 <= 600. + MPConstraint c1 = solver.makeConstraint(-infinity, 600.0); + c1.addTerm(x1, 10); + c1.addTerm(x2, 4); + c1.addTerm(x3, 5); + + // 2 * x1 + 2 * x2 + 6 * x3 <= 300. + MPConstraint c2 = solver.makeConstraint(-infinity, 300.0); + c2.addTerm(x1, 2); + c2.addTerm(x2, 2); + c2.addTerm(x3, 6); + + System.out.println("Number of variables = " + solver.numVariables()); + System.out.println("Number of constraints = " + solver.numConstraints()); + + int resultStatus = solver.solve(); + + // Check that the problem has an optimal solution. + if (resultStatus != MPSolver.OPTIMAL) { + System.err.println("The problem does not have an optimal solution!"); + return; + } + + System.out.println("Problem solved in " + solver.wallTime() + + " milliseconds"); + + // The objective value of the solution. + System.out.println("Optimal objective value = " + solver.objectiveValue()); + + // The value of each variable in the solution. + System.out.println("x1 = " + x1.solutionValue()); + System.out.println("x2 = " + x2.solutionValue()); + System.out.println("x3 = " + x3.solutionValue()); + + System.out.println("Advanced usage:"); + System.out.println("Problem solved in " + solver.iterations() + + " iterations"); + System.out.println("x1: reduced cost = " + x1.reducedCost()); + System.out.println("x2: reduced cost = " + x2.reducedCost()); + System.out.println("x3: reduced cost = " + x3.reducedCost()); + System.out.println("c0: dual value = " + c0.dualValue()); + System.out.println(" activity = " + c0.activity()); + System.out.println("c1: dual value = " + c1.dualValue()); + System.out.println(" activity = " + c1.activity()); + System.out.println("c2: dual value = " + c2.dualValue()); + System.out.println(" activity = " + c2.activity()); + } + + public static void main(String[] args) throws Exception { + System.out.println("---- Linear programming example with GLPK ----"); + runLinearProgrammingExample(MPSolver.GLPK_LINEAR_PROGRAMMING); + System.out.println("---- Linear programming example with CLP ----"); + runLinearProgrammingExample(MPSolver.CLP_LINEAR_PROGRAMMING); + } +} diff --git a/com/google/ortools/linearsolver/samples/LinearSolverExample.java b/com/google/ortools/linearsolver/samples/LinearSolverExample.java deleted file mode 100644 index 0e980499fb..0000000000 --- a/com/google/ortools/linearsolver/samples/LinearSolverExample.java +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2010-2011 Google -// 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. - -package com.google.ortools.linearsolver.samples; - -import com.google.ortools.linearsolver.MPConstraint; -import com.google.ortools.linearsolver.MPSolver; -import com.google.ortools.linearsolver.MPVariable; - -/** - * Example of linear solver usage. - * - */ - -public class LinearSolverExample { - - static { - System.loadLibrary("jnilinearsolver"); - } - - - private static void runFirstLinearExample(int solverType) { - MPSolver solver = new MPSolver("runFirstLinearExample", solverType); - double infinity = solver.infinity(); - MPVariable x1 = solver.makeNumVar(0.0, infinity, "x1"); - MPVariable x2 = solver.makeNumVar(0.0, infinity, "x2"); - MPVariable x3 = solver.makeNumVar(0.0, infinity, "x3"); - System.out.println("Number of variables = " + solver.numVariables()); - - solver.addObjectiveTerm(x1, 10); - solver.addObjectiveTerm(x2, 6); - solver.addObjectiveTerm(x3, 4); - solver.setMaximization(); - - MPConstraint c0 = solver.makeConstraint(-infinity, 100.0); - c0.addTerm(x1, 1); - c0.addTerm(x2, 1); - c0.addTerm(x3, 1); - MPConstraint c1 = solver.makeConstraint(-infinity, 600.0); - c1.addTerm(x1, 10); - c1.addTerm(x2, 4); - c1.addTerm(x3, 5); - MPConstraint c2 = solver.makeConstraint(-infinity, 300.0); - c2.addTerm(x1, 2); - c2.addTerm(x2, 2); - c2.addTerm(x3, 6); - System.out.println("Number of constraints = " + solver.numConstraints()); - - // The problem has an optimal solution. - solver.solve(); - - System.out.println("Optimal value = " + solver.objectiveValue()); - System.out.println("x1 = " + x1.solutionValue() + - " reduced cost = " + x1.reducedCost()); - System.out.println("x2 = " + x2.solutionValue() + - " reduced cost = " + x2.reducedCost()); - System.out.println("x3 = " + x3.solutionValue() + - " reduced cost = " + x3.reducedCost()); - System.out.println("c0 dual value = " + c0.dualValue() + - " activity = " + c0.activity()); - System.out.println("c1 dual value = " + c1.dualValue() + - " activity = " + c1.activity()); - System.out.println("c2 dual value = " + c2.dualValue() + - " activity = " + c2.activity()); - } - - public static void main(String[] args) throws Exception { - runFirstLinearExample(MPSolver.CLP_LINEAR_PROGRAMMING); - runFirstLinearExample(MPSolver.GLPK_LINEAR_PROGRAMMING); - } -}