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.
1 Answer
This worked for me:
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(); 1