From 09487025a8b167cefb2e23bf9ba67b809bb6eee5 Mon Sep 17 00:00:00 2001 From: "lperron@google.com" Date: Thu, 8 Dec 2011 16:47:57 +0000 Subject: [PATCH] improve getSolverEnum on java --- .../samples/IntegerProgramming.java | 93 ++++++----- .../samples/LinearProgramming.java | 151 ++++++++++-------- linear_solver/linear_solver.swig | 5 +- 3 files changed, 138 insertions(+), 111 deletions(-) diff --git a/com/google/ortools/linearsolver/samples/IntegerProgramming.java b/com/google/ortools/linearsolver/samples/IntegerProgramming.java index c1288799e3..452d7fc554 100644 --- a/com/google/ortools/linearsolver/samples/IntegerProgramming.java +++ b/com/google/ortools/linearsolver/samples/IntegerProgramming.java @@ -29,50 +29,63 @@ public class IntegerProgramming { } - private static void runIntegerProgrammingExample(String solverType) { + private static MPSolver createSolver (String solverType) { try { - MPSolver solver = new MPSolver("IntegerProgrammingExample", - MPSolver.getSolverEnum(solverType)); - 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.setObjectiveCoefficient(x1, 1); - solver.setObjectiveCoefficient(x2, 2); - - // 2 * x2 + 3 * x1 >= 17. - MPConstraint ct = solver.makeConstraint(17, infinity); - ct.setCoefficient(x1, 3); - ct.setCoefficient(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"); - } catch (java.lang.Exception exc) { - System.out.println("- Solver not supported."); + return new MPSolver("IntegerProgrammingExample", + MPSolver.getSolverEnum(solverType)); + } catch (java.lang.ClassNotFoundException e) { + throw new Error(e); + } catch (java.lang.NoSuchFieldException e) { + return null; + } catch (java.lang.IllegalAccessException e) { + throw new Error(e); } } + private static void runIntegerProgrammingExample(String solverType) { + MPSolver solver = createSolver(solverType); + if (solver == null) { + System.out.println("Could not create solver " + solverType); + return; + } + 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.setObjectiveCoefficient(x1, 1); + solver.setObjectiveCoefficient(x2, 2); + + // 2 * x2 + 3 * x1 >= 17. + MPConstraint ct = solver.makeConstraint(17, infinity); + ct.setCoefficient(x1, 3); + ct.setCoefficient(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 ----"); diff --git a/com/google/ortools/linearsolver/samples/LinearProgramming.java b/com/google/ortools/linearsolver/samples/LinearProgramming.java index 5fc7076539..5ca8e8bf90 100644 --- a/com/google/ortools/linearsolver/samples/LinearProgramming.java +++ b/com/google/ortools/linearsolver/samples/LinearProgramming.java @@ -29,80 +29,91 @@ public class LinearProgramming { } - private static void runLinearProgrammingExample(String solverType) { + private static MPSolver createSolver (String solverType) { try { - MPSolver solver = new MPSolver("LinearProgrammingExample", - MPSolver.getSolverEnum(solverType)); - 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.setObjectiveCoefficient(x1, 10); - solver.setObjectiveCoefficient(x2, 6); - solver.setObjectiveCoefficient(x3, 4); - solver.setMaximization(); - - // x1 + x2 + x3 <= 100. - MPConstraint c0 = solver.makeConstraint(-infinity, 100.0); - c0.setCoefficient(x1, 1); - c0.setCoefficient(x2, 1); - c0.setCoefficient(x3, 1); - - // 10 * x1 + 4 * x2 + 5 * x3 <= 600. - MPConstraint c1 = solver.makeConstraint(-infinity, 600.0); - c1.setCoefficient(x1, 10); - c1.setCoefficient(x2, 4); - c1.setCoefficient(x3, 5); - - // 2 * x1 + 2 * x2 + 6 * x3 <= 300. - MPConstraint c2 = solver.makeConstraint(-infinity, 300.0); - c2.setCoefficient(x1, 2); - c2.setCoefficient(x2, 2); - c2.setCoefficient(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()); - } catch (java.lang.Exception exc) { - System.out.println("- Solver not supported."); + return new MPSolver("IntegerProgrammingExample", + MPSolver.getSolverEnum(solverType)); + } catch (java.lang.ClassNotFoundException e) { + throw new Error(e); + } catch (java.lang.NoSuchFieldException e) { + return null; + } catch (java.lang.IllegalAccessException e) { + throw new Error(e); } } + private static void runLinearProgrammingExample(String solverType) { + MPSolver solver = createSolver(solverType); + if (solver == null) { + System.out.println("Could not create solver " + solverType); + return; + } + 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.setObjectiveCoefficient(x1, 10); + solver.setObjectiveCoefficient(x2, 6); + solver.setObjectiveCoefficient(x3, 4); + solver.setMaximization(); + + // x1 + x2 + x3 <= 100. + MPConstraint c0 = solver.makeConstraint(-infinity, 100.0); + c0.setCoefficient(x1, 1); + c0.setCoefficient(x2, 1); + c0.setCoefficient(x3, 1); + + // 10 * x1 + 4 * x2 + 5 * x3 <= 600. + MPConstraint c1 = solver.makeConstraint(-infinity, 600.0); + c1.setCoefficient(x1, 10); + c1.setCoefficient(x2, 4); + c1.setCoefficient(x3, 5); + + // 2 * x1 + 2 * x2 + 6 * x3 <= 300. + MPConstraint c2 = solver.makeConstraint(-infinity, 300.0); + c2.setCoefficient(x1, 2); + c2.setCoefficient(x2, 2); + c2.setCoefficient(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("GLPK_LINEAR_PROGRAMMING"); diff --git a/linear_solver/linear_solver.swig b/linear_solver/linear_solver.swig index 2f044931ad..d0f1190321 100644 --- a/linear_solver/linear_solver.swig +++ b/linear_solver/linear_solver.swig @@ -504,7 +504,10 @@ namespace operations_research { return makeVarArray(count, 0.0, 1.0, true, var_name); } - public static final int getSolverEnum(String solver_name) throws java.lang.Exception { + public static final int getSolverEnum(String solver_name) + throws java.lang.ClassNotFoundException, + java.lang.NoSuchFieldException, + java.lang.IllegalAccessException { Class c = Class.forName("com.google.ortools.linearsolver.MPSolver"); Field field = c.getField(solver_name); return field.getInt(null);