Topic
  • 4 replies
  • Latest Post - ‏2013-07-05T05:42:02Z by babyvera
babyvera
babyvera
4 Posts

Pinned topic c++ cplex IloNumVarArray problem

‏2013-07-04T13:41:16Z |
Hi all. I am the beginner of the c++ cplex. I have a qusetion about the IloNumVarArray. I spend 5 days on this question,but failed.
First  I use  'IloNumVarArray' to set up a array with 5 elements, named f. Then use 'add' to make 5 arrays become a 5*5 matrix named fij. Third I want to row sum. Except use fij[1]+ fij[2]+ fij[3]+ fij[4]+ fij[5] , Are there any other way to solve this problem? Because the model have 1047*1047 matrix,  I will write down  fij[1]+ fij[2]...........fij[1047]. That maybe a hard work.
Thank you!!!
  • Philippe_Refalo
    Philippe_Refalo
    9 Posts

    Re: c++ cplex IloNumVarArray problem

    ‏2013-07-04T15:33:24Z  
    You need to sum up the variables in an expression using a for loop:
     
    for(IloInt r = 0; r < fij.getSize(); r++){
      IloExpr sr = IloSum(fij[r]); // sr the sum expression of variables in array fij[r]
    }
     
    If you want to create the sum of a column, you need to loop on all the elements:
     
    IloInt c = // column to sum
    IloExpr sc(env)
    for(IloInt r = 0; r < fij.getSize(); r++){
      sc += fij[r][c];
    }
     
    // Here sc is the sum of variables in column c
     
    Cheers, 
     
    Philippe
  • babyvera
    babyvera
    4 Posts

    Re: c++ cplex IloNumVarArray problem

    ‏2013-07-05T01:24:18Z  
    You need to sum up the variables in an expression using a for loop:
     
    for(IloInt r = 0; r < fij.getSize(); r++){
      IloExpr sr = IloSum(fij[r]); // sr the sum expression of variables in array fij[r]
    }
     
    If you want to create the sum of a column, you need to loop on all the elements:
     
    IloInt c = // column to sum
    IloExpr sc(env)
    for(IloInt r = 0; r < fij.getSize(); r++){
      sc += fij[r][c];
    }
     
    // Here sc is the sum of variables in column c
     
    Cheers, 
     
    Philippe

    thank you for your help.thank you very much.

      for (j = 0; j < nodenum; j++)
      {
        for(v=0;v<nodenum;v++)
        {
         sr +=  fij[j*nodenum+v]; // sr the sum expression of variables in array fij[r]
         cout<<fij[j*nodenum+v]<<endl;
        }
        for(v=0;v<nodenum;v++)
        {
         sl += fij[v*nodenum+j]; // sr the sum expression of variables in array fij[c]
         cout<<fij[v*nodenum+j]<<endl;
        }

       //model.add(fij[j]+fij[1*5+j]+fij[2*5+j]+fij[3*5+j]+fij[4*5+j]-fij[j*5]-fij[j*5+1]-fij[j*5+2]-fij[j*5+3]-fij[j*5+4]+G[j]==Load[j]);
       model.add(sl-sr+G[j]==Load[j]);
      }

    but when I use model.add(fij[j]+fij[1*5+j]+fij[2*5+j]+fij[3*5+j]+fij[4*5+j]-fij[j*5]-fij[j*5+1]-fij[j*5+2]-fij[j*5+3]-fij[j*5+4]+G[j]==Load[j]);
    there has Solution;when I use model.add(sl-sr+G[j]==Load[j]); there is no Solution; 
     

  • babyvera
    babyvera
    4 Posts

    Re: c++ cplex IloNumVarArray problem

    ‏2013-07-05T04:29:08Z  
    You need to sum up the variables in an expression using a for loop:
     
    for(IloInt r = 0; r < fij.getSize(); r++){
      IloExpr sr = IloSum(fij[r]); // sr the sum expression of variables in array fij[r]
    }
     
    If you want to create the sum of a column, you need to loop on all the elements:
     
    IloInt c = // column to sum
    IloExpr sc(env)
    for(IloInt r = 0; r < fij.getSize(); r++){
      sc += fij[r][c];
    }
     
    // Here sc is the sum of variables in column c
     
    Cheers, 
     
    Philippe

    I realise that

     
    for(IloInt r = 0; r < 5;r++){
      IloExpr sr += fij[r];
    }

    do not equal to 'sr= fij[1]+ fij[2]+ fij[3]+ fij[4]+ fij[5]';

    so there is no solution;How can I make it equal to each other;

    thank you!

  • babyvera
    babyvera
    4 Posts

    Re: c++ cplex IloNumVarArray problem

    ‏2013-07-05T05:42:02Z  
    You need to sum up the variables in an expression using a for loop:
     
    for(IloInt r = 0; r < fij.getSize(); r++){
      IloExpr sr = IloSum(fij[r]); // sr the sum expression of variables in array fij[r]
    }
     
    If you want to create the sum of a column, you need to loop on all the elements:
     
    IloInt c = // column to sum
    IloExpr sc(env)
    for(IloInt r = 0; r < fij.getSize(); r++){
      sc += fij[r][c];
    }
     
    // Here sc is the sum of variables in column c
     
    Cheers, 
     
    Philippe

    thank you Philippe,I have already solve this problem.thank you very much~