How to solve a linear programming q u e s t i o n, that gives alternate optimum solutions using JOptimizer java API?

396 Views Asked by At

My question to be solved is:

/** Maximize 4x+3Y
 * Subject to
 *  8x+6y <= 25
 *  3x+4y <= 15
 *  x,y >= 0
 */

In theory LP Optimum of this question has unlimited # of solutions.

all required libraries, dependancies available at my google drive: https://drive.google.com/file/d/0B84k1fZRHSMdak00TjZKNXBKSFU/view?usp=sharing

My code:

package testJOptimizer;

import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;

/**
 *
 * @author K.P.L.Kanchana
 */
public class test_4_alternateOptimum {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
//        BasicConfigurator.configure();

        // Objective function (plane)
        LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {-4.0, -3.0}, 0); // maximize 4x+3y

        //inequalities (polyhedral feasible set G.X<H )
        ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
        // 8x+6y <= 25
        inequalities[0] = new LinearMultivariateRealFunction(new double[]{8.0, 6.0}, -25); // 8x+6y-25<=0
        // 3x+4y <= 15
        inequalities[1] = new LinearMultivariateRealFunction(new double[]{1.0, 4.0}, -15); // 3x+4y-15<=0
        // x >= 0
        inequalities[2] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.0}, 0);
        // y >= 0
        inequalities[3] = new LinearMultivariateRealFunction(new double[]{0.0, -1.0}, 0);

        //optimization problem
        OptimizationRequest or = new OptimizationRequest();
        or.setF0(objectiveFunction);
        or.setFi(inequalities);
        //or.setInitialPoint(new double[] {0.0, 0.0});//initial feasible point, not mandatory
        or.setToleranceFeas(1.E-9);
        or.setTolerance(1.E-9);

        //optimization
        JOptimizer opt = new JOptimizer();
        opt.setOptimizationRequest(or);
        try {
            int returnCode = opt.optimize();
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        // get the solution
        double[] sol = opt.getOptimizationResponse().getSolution();

        // display the solution
        System.out.println("Length: " + sol.length);
        for (int i = 0; i < sol.length; i++) {
                System.out.println("answer " + (i+1) + ": " + (sol[i]));
        }
    }

}
1

There are 1 best solutions below

0
On BEST ANSWER

I found the issue with my code. To tell the truth I had some help from alberto trivellato. He is the person who develop JOptimizer as I know. I really really appreciate him for wasting his time to find the issue. As he mentioned The issue was not with multiple solutions but with the high accuracy I'm asking to the solver. it is a best practice to not ask for more precision than you really need. Also remember that inequalities are always in the form of G.x < h, i.e. strictly less than(not less htan or EQUAL), because JOptimizer implements an interior points method solver.

Corrected code:

package testJOptimizer;

import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;

/**
 *
 * @author K.P.L.Kanchana
 */
public class test_4_alternateOptimum {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args){
//        BasicConfigurator.configure();

        // Objective function (plane)
        LinearMultivariateRealFunction objectiveFunction = new LinearMultivariateRealFunction(new double[] {-4.0, -3.0}, 0); // maximize 4x+3y

        //inequalities (polyhedral feasible set G.X<H )
        ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
        // 8x+6y < 25(no equal sign)
        inequalities[0] = new LinearMultivariateRealFunction(new double[]{8.0, 6.0}, -25); // 8x+6y-25<0
        // 3x+4y < 15
        inequalities[1] = new LinearMultivariateRealFunction(new double[]{1.0, 4.0}, -15); // 3x+4y-15<0
        // x > 0
        inequalities[2] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.0}, 0);
        // y > 0
        inequalities[3] = new LinearMultivariateRealFunction(new double[]{0.0, -1.0}, 0);

        //optimization problem
        OptimizationRequest or = new OptimizationRequest();
        or.setF0(objectiveFunction);
        or.setFi(inequalities);
        //or.setInitialPoint(new double[] {0.0, 0.0});//initial feasible point, not mandatory
        or.setToleranceFeas(JOptimizer.DEFAULT_FEASIBILITY_TOLERANCE / 10); // There was the issue
        or.setTolerance(JOptimizer.DEFAULT_TOLERANCE / 10);  // There was the issue

        //optimization
        JOptimizer opt = new JOptimizer();
        opt.setOptimizationRequest(or);
        try {
            int returnCode = opt.optimize();
        }
        catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        // get the solution
        double[] sol = opt.getOptimizationResponse().getSolution();

        // display the solution
        System.out.println("Length: " + sol.length);
        for (int i = 0; i < sol.length; i++) {
                System.out.println("answer " + (i+1) + ": " + (sol[i]));
        }
    }

}