Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  Implicit int to ilog type casting?

    Posted Mon June 19, 2017 02:00 AM

    Originally posted by: Laser.Cho


    Hi. I made a source file to solve a problem with my latest CPLEX with Visual C++, and I did it. 

    but my original code is too ugly to let it be. so, I'm trying to make it better code. But I cannot accumulate variable array value with loop sentence.

    Funny thing is I can add all elements by hard coding.

     

    here is the summarized code.

     

     

    ===================original code(it works. just ugly)==================================

            typedef IloArray<IloIntVarArray> intvar2array;
            intvar2array route(env, 9);
            for (int i = 0; i < 9; i++)
                    route[i] = IloIntVarArray(env, 10, 0, 1);
    // bla bla ~~~
    // constraints
            IloExpr tmp(env,0), tmp2(env,0);
     
          for (int x = 1; x <= 8; x++) {
     
             tmp = route[x][0] + route[x][1] + route[x][2] + route[x][3] + route[x][4] + route[x][5] + route[x][6] + route[x][7] + route[x][8] + route[x][9];
                    tmp2 = route[0][x] + route[1][x] + route[2][x] + route[3][x] + route[4][x] + route[5][x] + route[6][x] + route[7][x] + route[8][x];
             model.add(tmp==tmp2);
            }
    

    ==========================================

     

    ================but it doesnt work.===================

            for (int x = 1; x <= 8; x++) {
     
          
     for (int j = 0; j < 10; j++)
                            tmp += route[x][j];
    //              tmp = route[x][0] + route[x][1] + route[x][2] + route[x][3] + route[x][4] + route[x][5] + route[x][6] + route[x][7] + route[x][8] + route[x][9];
                    tmp2 = route[0][x] + route[1][x] + route[2][x] + route[3][x] + route[4][x] + route[5][x] + route[6][x] + route[7][x] + route[8][x];
                    model.add(tmp == tmp2);
            }
    

    ==================================================

     

    sometimes it show the message "+= operand does not type INT and ILOINTVARARRAY blabla~" sorry i forgot the message. 

     

    but curious thing is why the "hard coding part" code works? and then why it doesnt work in "loop coding" code? 

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Implicit int to ilog type casting?

    Posted Mon June 19, 2017 02:16 AM

    The following works for me:

       for (int x = 1; x <= 8; ++x) {
          IloExpr tmp(env, 0);
          tmp = route[x][0] + route[x][1] + route[x][2] + route[x][3] + route[x][4] + route[x][5] + route[x][6] + route[x][7] + route[x][8] + route[x][9];
       }
    
       for (int x = 1; x <= 8; ++x) {
          IloExpr tmp(env, 0);
          for (int j = 0; j < 10; ++j)
             tmp += route[x][j];
       }
    

    I guess you posted the wrong code?


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Implicit int to ilog type casting?

    Posted Mon June 19, 2017 03:00 AM

    Originally posted by: Laser.Cho


    Now i Konw what i did wrong. (But only know just a piece of whole part)

     

    my previous code is this.

     

     for (int j = 0; j < 10; j++)
            tmp += route[x][j];

     

    problem is. "Not reset tmp".

    I have to clear the accumulate variable.

     

    your code works because you made clean object every loop.

     

    I'm sincerey appreciate you to giving an idea to me. thanx


    #CPLEXOptimizers
    #DecisionOptimization