Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Variable views, e.g., transposed matrix

    Posted 04/20/15 12:13 AM

    Originally posted by: glebB


    I have a matrix of variables and want to access it both by rows and by columns, e.g., for sum() of each row and each column.

    I tried to implement this like this:

    typedef IloArray<IloNumVarArray> IntVarMatrix;
    IntVarMatrix m(env, Nrows);
    IntVarMatrix mT(env, Ncols);     // transposed
    for (int i=0; i<Nrows; ++i)
             m[i] = IloNumVarArray(env, Ncols, 0, 1, ILOBOOL);
    // FAILS:
    for (int j=0; j<Ncols; ++j)
         for (int i=0; i<Nrows; ++i)
             mT[j].add( m[i][j] );
    

    ... which fails. Instead, now I solved th problem as follows:

    for (int j=0; j<Ncols; ++j)
          mT[j] = IloNumVarArray(env, Nrows, 0, 1, ILOBOOL);
    for (int j=0; j<Ncols; ++j)
          for (int i=0; i<Nrows; ++i)
                 model.add ( mT[j][i] == m[i][j] );
    
    Is there a more efficient way?
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Variable views, e.g., transposed matrix

    Posted 04/20/15 02:32 AM

    If I understand correctly then you want the matrix and its transpose to refer to the same variables? In that case your first attempt was almost correct. You only forgot to initialize mT[j] properly. This is the correct code:

    for (int j = 0; j < Ncols; ++j) {
       mT[j] = IloNumVarArray(env);
       for (int i = 0; i < Nrows; ++i)
          mT[j].add(m[i][j]);
    }

     


    #CPLEXOptimizers
    #DecisionOptimization