I am trying to apply linear programming for my problem using Apache commons Math library. I saw an example online to solve the following example
max. 3X + 5Y
s.t.
2X + 8Y <= 13
5X - Y <= 11
X >= 0, Y >= 0
The code is like
LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 3, 5}, 0);
Collection 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));
constraints.add(new LinearConstraint(new double[] { 1, 0}, Relationship.GEQ, 0));
constraints.add(new LinearConstraint(new double[] { 0, 1}, Relationship.GEQ, 0));
//create and run solver
RealPointValuePair solution = null;
try {
solution = new SimplexSolver().optimize(f, constraints, GoalType.MAXIMIZE, false);
}
catch (OptimizationException e) {
e.printStackTrace();
}
if (solution != null) {
//get solution
double max = solution.getValue();
System.out.println("Opt: " + max);
//print decision variables
for (int i = 0; i < 2; i++) {
System.out.print(solution.getPoint()[i] + "\t");
}
However, what if I want the value of X and Y can only be either 0 or 1? Is apache libs allow us do this? If no, any other libs I can use to achieve this?
Thanks in advance.