简体   繁体   中英

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.

Best wishes!

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM