Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello World example for SimplexSolver

I am trying to solve a optimization problem using Apache Commons. I found a "Hello World" example here for Commons Math 2. But, I would like to use Commons Math 3.2 and I couldn't find any example on how to use this part of the code:

PointValuePair solution = null;
SimplexSolver solver = new SimplexSolver();
solution = solver.optimize(optData);

Specificaly, I don't know what is optData and where I put the constraints. I would appreciate if someone indicate me one "Hello World" example of how to use the org.apache.commons.math3.optim library.

like image 908
Esther Avatar asked Jul 30 '13 18:07

Esther


1 Answers

This worked for me:

http://mail-archives.apache.org/mod_mbox/commons-user/201307.mbox/%3CCAJSjvws+9uC=jMP_A_Mbc4szbJL2VXAwp=Q2A+zZ=51mLeRw6g@mail.gmail.com%3E

My version for max cx : Ax <= b, x >= 0. Maybe not "hello world" but I hope it helps:

        LinearObjectiveFunction f = new LinearObjectiveFunction(c, 0);
        Collection<LinearConstraint> constraints = new
                ArrayList<LinearConstraint>();
        for(int i=0; i<A.length; i++) {
            double[] Av = new double[A[i].length];
            for(int j=0; j<A[i].length; j++) {
                Av[j] = A[i][j];
            }
            constraints.add(new LinearConstraint(Av, Relationship.LEQ, b[i]));
        }

        SimplexSolver solver = new SimplexSolver();
        PointValuePair optSolution = solver.optimize(new MaxIter(100), f, new
                LinearConstraintSet(constraints),
                GoalType.MAXIMIZE, new
                NonNegativeConstraint(true));


        double[] solution;
        solution = optSolution.getPoint();
like image 143
Sonia Avatar answered Nov 08 '22 07:11

Sonia