I had problems to find a simple example for the uasge of Java Apache Commons-Math to solve a simplex problem.
The Problem is given by the maximization of this eqation:
max z= 300 * x_1 + 500 * x_2
The constraints are given by:
1 * x_1 + 2 * x_2 <= 170
1 * x_1 + 1 * x_2 <= 150
0 * x_1 + 3 * x_2 <= 180
Additional information about the problem background can be found here Wikipedia article about simplex (german language)
2
The optimization problem from above can be solved by this solution in Java using the Apache library.
package SimplexAlgorithmExample;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.apache.commons.math3.optim.MaxIter;
import org.apache.commons.math3.optim.PointValuePair;
import org.apache.commons.math3.optim.linear.LinearConstraint;
import org.apache.commons.math3.optim.linear.LinearConstraintSet;
import org.apache.commons.math3.optim.linear.LinearObjectiveFunction;
import org.apache.commons.math3.optim.linear.NonNegativeConstraint;
import org.apache.commons.math3.optim.linear.Relationship;
import org.apache.commons.math3.optim.linear.SimplexSolver;
import org.apache.commons.math3.optim.nonlinear.scalar.GoalType;
public class SimplexAlgorithmExample
{
public static void main(String[] args)
{
LinearObjectiveFunction oFunc = new LinearObjectiveFunction(new double[] {300, 500}, 0);
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
constraints.add(new LinearConstraint(new double[] {1, 2}, Relationship.LEQ, 170));
constraints.add(new LinearConstraint(new double[] {1, 1}, Relationship.LEQ, 150));
constraints.add(new LinearConstraint(new double[] {0, 3}, Relationship.LEQ, 180));
SimplexSolver solver = new SimplexSolver();
PointValuePair solution = solver.optimize(new MaxIter(100), oFunc, new LinearConstraintSet(constraints), GoalType.MAXIMIZE, new NonNegativeConstraint(true));
System.out.println(Arrays.toString(solution.getPoint()) + " : " + solution.getSecond());
}
}
2