0

I am using CPLEX concert technology in C++. As you can see in the code, when I want to employ "while" with the expression "LP-LB <=0", I face with the error of "expression must have bool type". What's the problem? If you need the whole parts of the code, I can share them here.

        //***********************************************************
        //***********************************************************
        //**************** Lagragian Dual ***************************
        //***********************************************************

        // Define Lagragian Relaxation Model

        IloModel LRDual(env);

        // **** Parameter setting

        IloArray< IloNumArray > Lambda(env, k);
        for (int b = 0; b < k; b++)
        {
            Lambda[b] = IloNumArray(env, m, 0, IloIntMax);
        }

        for (int b = 0; b < k; b++)
        {
            for (int o = 0; o < m; o++)
            {
                Lambda[b][o] = 0;
            }
        }


        // **** End Parameter Setting

        //***** Define LR Objective Function *********
        //********************************************
        IloNumVar Makespan_Dual(env, 0, IloInfinity);
        IloNumVar Z_LR1(env, 0, IloInfinity);
        LRDual.add(IloMinimize(env, Z_LR1));


        //************ Lagrangian Relaxation Constraint *************
        //***********************************************************
        IloExpr S_D_RCon(env);
        IloExpr S_D_LaMa(env);
        for (int b = 0; b < k; b++)
        {
            for (int o = 0; o < m; o++)
            {
                for (int j = 0; j < n; j++)
                {
                    S_D_RCon += s[j] * x[j][b][o];

                }
                S_D_LaMa += Lambda[b][o] * (B[o] - S_D_RCon);

                S_D_RCon.end();
                S_D_RCon = IloExpr(env);


            }
        }
        LRDual.add(Z_LR1 == Makespan_Dual - S_D_LaMa);
        //**************************************************
        //**************** End LR Constraint ***************


        // Constraint Set 2
        IloExpr S_D1(env);

        for (int j = 0; j < n; j++)
        {
            for (int b = 0; b < k; b++)
            {
                for (int M1 = 0; M1 < m; M1++)
                {
                    S_D1 += x[j][b][M1];
                }
            }

            LRDual.add(S_D1 == 1);
            S_D1.end();
            S_D1 = IloExpr(env);
        }
        //End Constraint Set 2


        // Constraint Set 4

        for (int j = 0; j < n; j++)
        {

            for (int b = 0; b < k; b++)
            {

                for (int M = 0; M < m; M++)
                {
                    LRDual.add(P[b][M] >= (p[j] * x[j][b][M]));
                }
            }
        }

        //End Constraint Set 4

        // Constraint Set 5
        IloExpr S_D3(env);

        for (int M = 0; M < m; M++)
        {

            for (int b = 0; b < k; b++)
                S_D3 += P[b][M];

            LRDual.add(Makespan_Dual >= S_D3);
            S_D3.end();
            S_D3 = IloExpr(env);
        }
        //End Constraint Set 5

        //*******************************************************
        //*************** END CONSTRAINT DEFINITION**************

        // *******************************************************
        //********************************************************
        //************* LR Algorithm *****************************
        //********************************************************
        cplex.extract(LRDual);

        // Parameters 
        IloInt N_repeat = 15;
        IloInt intercount = 0;
        IloInt Sigma = 2;
        IloNumVar LB(env, 0, IloInfinity);
        IloNumVar LP(env, 0, IloInfinity);
        LB = Z_LR1;
        LP = Makespan;
        IloInt UB = 0;

        int total = 0;
        for (int i = 0; i < n; i++)
        {
            total += p[i];
        }
        UB = total;

        while (LP-LB <=0)
        {

        }

I would be thankful if you help me to get rid of this error.

PedBel
  • 1
  • 1
  • Don't post images of code. Place the code in the question. – Sean Jan 07 '22 at 09:22
  • Does `IloNumVar` override `<=` operator? – VLL Jan 07 '22 at 09:39
  • Does IloNumVar override the "-" operator and "<=" operator? – cnp Jan 07 '22 at 09:42
  • The error occurs when I start typing "LP" not the entire expression "while(LP)". – PedBel Jan 07 '22 at 09:52
  • "The error occurs when I start typing" If by "error" you mean "the editor underlines my code with a red squiggle", ignore it until you have finished typing the statement. If the squiggle persists, hit the compile button. If you see compilation errors, *read them*. If you don't understand them, *post their full text here*. If you only see the squiggle and the compilation finishes successfully, there is a bug/misconfiguration in your editor, not in the code you write. – n. m. could be an AI Jan 07 '22 at 10:26
  • Actually, the error is C2451: conditional expression of type "IloRange" is illegal. – PedBel Jan 07 '22 at 10:54
  • @PedBel The code you posted does not contant `while(LP)`. Edit your question and include the actual code that causes the error. – VLL Jan 07 '22 at 11:24
  • "while" is at the end of the code (the last lines). – PedBel Jan 07 '22 at 11:44

1 Answers1

1

It is because LP is of type IloNumVar. It doesn't have any specific value until after CPLEX has solved the problem, and even then you will need to ask CPLEX for the selected value of that variable. You cannot use these CPLEX modelling variables as if they have a value like normal C++ variables. The same is true in the other APIs (C#, Java etc).

TimChippingtonDerrick
  • 2,042
  • 1
  • 12
  • 13
  • Thanks a lot for your answer. The values of LB and LP should be equal to Z_LR1 and Makespan respectively which are obtained by CPLEX. As both Z_LR1 and MAkespan are of type IloNumVar, How can I set the values of LB and LP? – PedBel Jan 07 '22 at 11:43
  • You don't. That is what CPLEX is for. – TimChippingtonDerrick Jan 07 '22 at 17:34