0

How does apache commons math library calculate minimum and maximum value of objective function given inequality constraint.

e.g

maximize 3x1+5x2 
given - 2x1+8x2<=13
5x1-x2<=11
x1>=0,x2>=0

what algorithm does apache commons library applies for this.

1
  • Could you give me some feedback on my answer to your question? Negative or positive feedback is very much welcome, better than no feedback at all. Commented Oct 16, 2017 at 9:14

1 Answer 1

1

There is a Simplex solver in Apache commons math.

You can solve your problem like this using this library:

@Test
public void testMathStackOverflow() {
    //      maximize 3x1+5x2
    //      subject to
    //          2x1+8x2<=13
    //          5x1-x2<=11
    //              x1>=0
    // x2>=0

    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);

    ArrayList<LinearConstraint> constraints = new ArrayList<>();

    constraints.add(new LinearConstraint(new double[] {2, 8}, Relationship.LEQ, 13));
    constraints.add(new LinearConstraint(new double[] {5, -1}, Relationship.LEQ, 11));

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints),
            GoalType.MAXIMIZE,
            new NonNegativeConstraint(true),
            PivotSelectionRule.BLAND);
    System.out.printf("x1: %f; x2: %f; max: %f", solution.getPoint()[0], solution.getPoint()[1], solution.getValue());
}

The result is:

x1: 2.404762; x2: 1.023810; max: 12.333333

I have used these dependencies:

<groupId>org.apache.commons</groupId>
<artifactId>commons-math4</artifactId>
<version>4.0-SNAPSHOT</version>

For more examples of the usage of the Simplex solver in this library download the source code and check the unit tests in package:

org.apache.commons.math4.optim.linear
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.