简体   繁体   English

使用Cplex在Java中获得双重价值

[英]Getting dual value in Java using Cplex

I am trying to get the dual values in java using cplex. 我正在尝试使用cplex在Java中获取双重值。 But I am getting the wrong value. 但是我得到了错误的价值。 I know it's wrong because I have checked the dual values using other solver. 我知道这是错误的,因为我已经使用其他求解器检查了对偶值。 I couldn't find any problem with my code. 我的代码找不到任何问题。 Can anybody tell me what's wrong here? 有人可以告诉我这是怎么回事吗? My problem is given below: 我的问题如下:

minimize 100000x1 + 0.869x2 + 299997x3 + 199998x4 + 199998x5
subject to x1 = 1
           x1+x2=1
           x3 = 1
           x3+x4=1
           x5 = 1
           x1+x2+X3+x4+x5 <= 500

And this is my code: 这是我的代码:

public class dual_new {
    public static void main(String[] args) {
    Model_1();
    }
    public static void Model_1() {
        try {
            //create new model
            IloCplex cplex = new IloCplex();
            //define variables
            IloNumVar x1 = cplex.numVar(0, 1,"x1");
            IloNumVar x2 = cplex.numVar(0, 1,"x2");
            IloNumVar x3 = cplex.numVar(0, 1,"x3");
            IloNumVar x4 = cplex.numVar(0, 1,"x4");
            IloNumVar x5 = cplex.numVar(0, 1,"x5");



            //define expressions
            IloLinearNumExpr objective = cplex.linearNumExpr();
            objective.addTerm(100000, x1);
            objective.addTerm(0.869, x2);
            objective.addTerm(299997, x3);
            objective.addTerm(199998, x4);
            objective.addTerm(199998, x5);

            //define objective
            cplex.addMinimize(objective);
            //define constraints
            List<IloRange>constraints = new ArrayList<IloRange>();
            constraints.add(cplex.addEq(cplex.prod(1, x1),1));
            constraints.add(cplex.addEq(cplex.sum(cplex.prod(1, x1),cplex.prod(1, x2)),1));
            constraints.add(cplex.addEq(cplex.prod(1, x3),1));
            constraints.add(cplex.addEq(cplex.sum(cplex.prod(1, x3),cplex.prod(1, x4)),1));
            constraints.add(cplex.addEq(cplex.prod(1, x5),1));
            constraints.add(cplex.addLe(cplex.sum(cplex.prod(1, x1),cplex.prod(1, x2),cplex.prod(1, x3),cplex.prod(1, x4),cplex.prod(1, x5)),500));

            //solve model
            if (cplex.solve()) {
                System.out.println("obj = "+cplex.getObjValue());
                System.out.println("x1   = "+cplex.getValue(x1));
                System.out.println("x2   = "+cplex.getValue(x2));
                System.out.println("x3   = "+cplex.getValue(x3));
                System.out.println("x4   = "+cplex.getValue(x4));
                System.out.println("x5   = "+cplex.getValue(x5));

                for(int i=0; i<constraints.size();i++)
                System.out.println("dual = "+(i+1) +" = " +cplex.getDual(constraints.get(i)));
            }
            else {
                System.out.println("Model not solved");
            }
            cplex.end();
        }
        catch (IloException exc) {
            exc.printStackTrace();
        }
    }
}

There is nothing wrong. 没有任何错误。 The dual problem to your model has multiple optimal solutions; 您模型的双重问题有多个最佳解决方案。 therefore, a solver can give you any of those solutions where none of them is wrong. 因此,求解器可以为您提供其中没有错误的任何解决方案。 A way to check this is to compute the objective of the dual problem using the dual values from the primal, you should get the same objective value for both problems. 一种检查方法是使用来自原始的对偶值来计算对偶问题的目标,对于这两个问题,您应获得相同的目标值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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