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

时间:2016-07-21 19:12:14

标签: java cplex

我正在尝试使用cplex在java中获取双值。但我得到了错误的价值。我知道这是错的,因为我使用其他求解器检查了双值。我发现我的代码没有任何问题。谁能告诉我这里有什么问题?我的问题如下:

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

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

没有错。模型的双重问题有多种最优解;因此,解算器可以为您提供任何解决方案,其中没有一个是错误的。检查这个的一种方法是使用原始的双重值来计算双重问题的目标,你应该为这两个问题得到相同的目标值。