@Test public void testSolve() { logger.info("solve"); double[] linearObjectiveFunction = {143.0, 60.0, 195.0}; List<LPSolver.LPConstraint> linearConstraintsList = new ArrayList<>(); linearConstraintsList.add(new LPSolver.LPConstraint(new double[]{120.0, 210.0, 150.75}, LPSolver.LEQ, 15000.0)); linearConstraintsList.add(new LPSolver.LPConstraint(new double[]{110.0, 30.0, 125.0}, LPSolver.LEQ, 4000.0)); linearConstraintsList.add(new LPSolver.LPConstraint(new double[]{1.0, 1.0, 1.0}, LPSolver.LEQ, 75.0)); LPSolver.LPResult expResult = new LPSolver.LPResult(); expResult.setObjectiveValue(6986.8421052632); expResult.setVariableValues(new double[]{0.0, 56.578947368421, 18.421052631579}); LPSolver.LPResult result = LPSolver.solve(linearObjectiveFunction, linearConstraintsList, true, true); assertEquals(expResult.getObjectiveValue(), result.getObjectiveValue(), Constants.DOUBLE_ACCURACY_HIGH); assertArrayEquals(expResult.getVariableValues(), result.getVariableValues(), Constants.DOUBLE_ACCURACY_HIGH); }
public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for(LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if(LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if(LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if(LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints.add( new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue()) ); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize( new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize?GoalType.MAXIMIZE:GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND ); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }
LPSolver { public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for(LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if(LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if(LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if(LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints.add( new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue()) ); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize( new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize?GoalType.MAXIMIZE:GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND ); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; } }
LPSolver { public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for(LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if(LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if(LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if(LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints.add( new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue()) ); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize( new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize?GoalType.MAXIMIZE:GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND ); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }  }
LPSolver { public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for(LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if(LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if(LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if(LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints.add( new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue()) ); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize( new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize?GoalType.MAXIMIZE:GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND ); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }  static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize); }
LPSolver { public static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize) { int m = linearConstraintsList.size(); List<LinearConstraint> constraints = new ArrayList<>(m); for(LPSolver.LPConstraint constraint : linearConstraintsList) { String sign = constraint.getSign(); Relationship relationship = null; if(LPSolver.GEQ.equals(sign)) { relationship = Relationship.GEQ; } else if(LPSolver.LEQ.equals(sign)) { relationship = Relationship.LEQ; } else if(LPSolver.EQ.equals(sign)) { relationship = Relationship.EQ; } constraints.add( new LinearConstraint(constraint.getContraintBody(), relationship, constraint.getValue()) ); } SimplexSolver solver = new SimplexSolver(); PointValuePair solution = solver.optimize( new LinearObjectiveFunction(linearObjectiveFunction, 0.0), new LinearConstraintSet(constraints), maximize?GoalType.MAXIMIZE:GoalType.MINIMIZE, new NonNegativeConstraint(nonNegative), PivotSelectionRule.BLAND ); LPResult result = new LPResult(); result.setObjectiveValue(solution.getValue()); result.setVariableValues(solution.getPoint()); return result; }  static LPResult solve(double[] linearObjectiveFunction, List<LPSolver.LPConstraint> linearConstraintsList, boolean nonNegative, boolean maximize); static String GEQ; static String LEQ; static String EQ; }