Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cplex java api ifThen结构_Java_If Statement_Cplex - Fatal编程技术网

cplex java api ifThen结构

cplex java api ifThen结构,java,if-statement,cplex,Java,If Statement,Cplex,我试图在cplex中建模一个约束,它是IF-THEN结构。在二维数组中,如果rowsm行的和是1,那么列的和应该是0。在cplex java api中,IF-THEN结构将两个约束cplex.ifThenIloconstraint arg0和ILONSTRAINT arg2作为参数。1如何在不将约束添加到模型的情况下创建约束,即不使用cplex.addEq1变量,而是将其作为约束存储在某个位置?2我们有一个表达式,然后我们添加相等或不相等检查,它也会自动添加到模型中,对吗 for(int

我试图在cplex中建模一个约束,它是IF-THEN结构。在二维数组中,如果rowsm行的和是1,那么列的和应该是0。在cplex java api中,IF-THEN结构将两个约束cplex.ifThenIloconstraint arg0和ILONSTRAINT arg2作为参数。1如何在不将约束添加到模型的情况下创建约束,即不使用cplex.addEq1变量,而是将其作为约束存储在某个位置?2我们有一个表达式,然后我们添加相等或不相等检查,它也会自动添加到模型中,对吗

    for(int i=0; i<m; i++){
                drArrRow[i] =  cplex.linearNumExpr();
                    for(int j=0; j<m;j++){
                        drArrRow[i].addTerm(1, x[i][j]);

                    }
                    cplex.addEq(1, drArrRow[i]);
                }

            //second part "then"
               for(int j=0; j<m; j++){
                   drArrCol[j] =  cplex.linearNumExpr();
                    for(int i=0; i<m;i++){
                        drArrCol[i].addTerm(1, x[i][j]);

                    }
                    cplex.addEq(1, drArrCol[j]);
                }

    //cplex.ifThen(constraint, constraint);
    //Here i got stuck
谢谢大家!

这是正确的答案吗

            IloLinearNumExpr[] drArrCol= new IloLinearNumExpr[m];
            IloLinearNumExpr[] drArrRow= new IloLinearNumExpr[m];
            IloConstraint[] drColConstr= new IloConstraint[m];
            IloConstraint[] drRowConstr= new IloConstraint[m];
            // first part "if"  
            for(int i=0; i<m; i++){
                drArrRow[i] =  cplex.linearNumExpr();
                    for(int j=0; j<m;j++){
                        drArrRow[i].addTerm(1, x[i][j]);

                    }
                    drColConstr[i] = cplex.eq(1, drArrRow[i]);
                }

            //second part "then"
               for(int j=0; j<m; j++){
                   drArrCol[j] =  cplex.linearNumExpr();
                    for(int i=0; i<m;i++){
                        drArrCol[j].addTerm(1, x[i][j]);

                    }
                    drRowConstr[j] = cplex.eq(1, drArrRow[j]);
                }
             //end of 3 constraint 

               for(int i=0; i<m; i++){
                        cplex.ifThen(drRowConstr[i], drColConstr[i]);
                    }

看起来你的答案是正确的。我以前使用过约束,但没有将它们添加到模型中,它的工作原理与您在代码中编写的一样