如何使用JOptimizer获得一个可行的解决方案,以解决具有多个或无限数量解决方案的线性编程问题?

时间:2017-01-03 19:37:32

标签: java linear-programming joptimizer

JOptimizer是一个开源java库,可帮助开发大多数决策支持系统。 见:joptimizer.com/ 我使用JOptimizer来获得线性编程问题的最佳解决方案。 请参阅:joptimizer.com/linearProgramming.html

我可以使用它成功地获得大多数线性编程问题的答案。 例如:最小化3x + 4y使得2x + 3y> = 8,5x + 2y> = 12,x> = 0,y> = 0可以使用JOptimizer如下解决。

import com.joptimizer.functions.ConvexMultivariateRealFunction;
import com.joptimizer.functions.LinearMultivariateRealFunction;
import com.joptimizer.optimizers.JOptimizer;
import com.joptimizer.optimizers.OptimizationRequest;
import org.apache.log4j.BasicConfigurator;

/**
 * @author K.P.L.Kanchana
 */

public class Main {

    public static void main(String[] args) throws Exception {

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

        //inequalities (polyhedral feasible set G.X<H )
        ConvexMultivariateRealFunction[] inequalities = new ConvexMultivariateRealFunction[4];
        // x >= 0
        inequalities[0] = new LinearMultivariateRealFunction(new double[]{-1.0, 0.00}, 0.0);  // focus: -x+0 <= 0 
        // y >= 0
        inequalities[1] = new LinearMultivariateRealFunction(new double[]{0.0, -1.00}, 0.0);  // focus: -y+0 <= 0
        // 2x+3y >= 8
        inequalities[2] = new LinearMultivariateRealFunction(new double[]{-2.0, -3.00}, 8.0); // focus: -2x-3y+8 <= 0
        // 5x+2y >= 12
        inequalities[3] = new LinearMultivariateRealFunction(new double[]{-5.0, -2.00}, 12.0);// focus: -5x-2y+12 <= 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);
        int returnCode = opt.optimize();

        double[] sol = opt.getOptimizationResponse().getSolution();

        System.out.println("Length: " + sol.length);
        for (int i=0; i<sol.length/2; i++){
            System.out.println( "X" + (i+1) + ": " + Math.round(sol[i]) + "\ty" + (i+1) + ": " + Math.round(sol[i+1]) );
        }
    }

}

但是有一些线性编程问题具有多个或无限数量的可行解决方案。例如,最大化4x + 3Y,受8x + 6y <= 25,3x + 4y <= 15,x> = 0,y> = 0。 当我试图如下解决起诉JOptimizer时,它会出错。

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]));
        }
    }

}

我想通过使用JOptimizer在无限数量的解决方案中获得一个可行解决方案来解决此错误。 但我不知道怎么做? JOptimizer lib中是否有speccial命令?有人可以这么说吗? 所有必需的库,我的google驱动器提供的依赖项:https://drive.google.com/file/d/0B84k1fZRHSMdak00TjZKNXBKSFU/view?usp=sharing Java Doc可在此处获取:http://joptimizer.com/apidocs/index.html 对不起,如果这是一个奇怪的问题,感谢所有花时间考虑它的人。

1 个答案:

答案 0 :(得分:1)

我发现我的代码存在问题。说实话我得到了alberto trivellato的一些帮助。据我所知,他是开发JOptimizer的人。我非常感谢他浪费时间去寻找问题。正如他所提到的那样,问题不在于多种解决方案,而在于我向求解者提出的高精度问题。最好的做法是不要求比你真正需要的更精确。还要记住,不等式总是以G.x的形式出现。 h,即严格小于(不小于htan或EQUAL),因为JOptimizer实现了内点法解算器。

更正后的代码:

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); // Here was the issue
        or.setTolerance(JOptimizer.DEFAULT_TOLERANCE / 10);  // Here 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]));
        }
    }

}