import org.apache.commons.math3.optim.*; import org.apache.commons.math3.optim.linear.*; import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; public class LinearProgrammingExample { public static void main(String[] args) { LinearObjectiveFunction objective = new LinearObjectiveFunction(new double[]{3, 4}, 0); LinearConstraintSet constraints = new LinearConstraintSet( new Constraint[]{new LinearConstraint(new double[]{2, 1}, Relationship.LEQ, 5), new LinearConstraint(new double[]{1, 5}, Relationship.GEQ, 10), new LinearConstraint(new double[]{-1, 3}, Relationship.LEQ, 7) }); PointValuePair solution = new SimplexSolver().optimize(new MaxIter(100), objective, constraints, GoalType.MAXIMIZE, new NonNegativeConstraint(true)); System.out.println("Objective value: " + solution.getValue()); System.out.println("Solution: "); double[] point = solution.getPoint(); for (int i = 0; i < point.length; i++) { System.out.println("x[" + i + "] = " + point[i]); } } }


上一篇:
下一篇:
切换中文