Topic
  • 1 reply
  • Latest Post - ‏2011-05-27T08:16:43Z by SystemAdmin
SystemAdmin
SystemAdmin
7929 Posts

Pinned topic how to create an array of 5 dimension.

‏2011-05-27T07:48:53Z |
I want to know how to create an array of 5 dimension.
I tried with this solution but it didn't work:
"typedef IloArray<IloBoolVarArray> IloBoolVarArrayArrayArrayArrayArray;
typedef IloArray<IloBoolVarArray> IloBoolVarArrayArrayArrayArray;
typedef IloArray<IloBoolVarArray> IloBoolVarArrayArrayArray;
typedef IloArray<IloBoolVarArray> IloBoolVarArrayArray;
typedef IloArray<IloBoolVarArray> IloBoolVarArray;
.......
IloNumVarArrayArrayArrayArrayArray x(env,nb_tache);
for ( i = 0; i < nb_tache+1 ; i++)
{
x[i] = IloNumVarArrayArrayArrayArrayArray(env, nb_tache);
for ( j = 1; j < nb_avion+1 ; j++)
{
x[i][j] = IloNumVarArrayArrayArrayArray(env, nb_avion);
for ( k = 0; k < nb_tache+1 ; k++)
{
x[i][j][k] = IloNumVarArrayArrayArray(env, nb_tache);
for ( l = 1; l < nb_avion+1 ; l++)
{
x[i][j][k][l] = IloNumVarArrayArray(env, nb_avion);
for ( m = 1; m < nb_vehicule+1 ; m++)
{
x[i][j][k][l][m] = IloNumVarArray(env, nb_vehicule, 0, 1, ILOINT);

}
}
}
}
}
Updated on 2011-05-27T08:16:43Z at 2011-05-27T08:16:43Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    7929 Posts

    Re: how to create an array of 5 dimension.

    ‏2011-05-27T08:16:43Z  
    OK, your code has several flaws:
    • You are defining types called IloBoolVarArray... but are using types called IloNumVarArray...
    • All your IloBoolVarArray... types are the same.
    • You are in fact trying to create a 6-dimensional array here: You have 5 nested loops and in the innermost loop you create an array.
    I changed your code so that it at least compiles:
    #include <ilcplex/ilocplex.h>
     
    int
    main(void)
    {
       IloEnv env;
       int i, j, k, l, m;
       int nb_tache = 2, nb_avion = 2, nb_vehicule = 2;
       typedef IloArray<IloNumVarArray> IloNumVarArrayArray;
       typedef IloArray<IloNumVarArrayArray> IloNumVarArrayArrayArray;
       typedef IloArray<IloNumVarArrayArrayArray> IloNumVarArrayArrayArrayArray;
       typedef IloArray<IloNumVarArrayArrayArrayArray> IloNumVarArrayArrayArrayArrayArray;
       typedef IloArray<IloNumVarArrayArrayArrayArrayArray> IloNumVarArrayArrayArrayArrayArrayArray;
     
       IloNumVarArrayArrayArrayArrayArrayArray x(env,nb_tache);  
     
       for ( i = 0; i < nb_tache+1 ; i++) 
       {
          x[i] = IloNumVarArrayArrayArrayArrayArray(env, nb_tache);
          for ( j = 1; j < nb_avion+1 ; j++) 
          {
             x[i][j] = IloNumVarArrayArrayArrayArray(env, nb_avion);
             for ( k = 0; k < nb_tache+1 ; k++)
             {
                x[i][j][k] = IloNumVarArrayArrayArray(env, nb_tache);
                for ( l = 1; l < nb_avion+1 ; l++) 
                {
                   x[i][j][k][l]  = IloNumVarArrayArray(env, nb_avion);
                   for ( m = 1; m < nb_vehicule+1 ; m++)
                   {
                      x[i][j][k][l][m] = IloNumVarArray(env, nb_vehicule, 0, 1, ILOINT);
                                   
                   }
                }
             }
          }
       }
     
       return 0;
    }
    

    It is up to you to decide whether you want 5 or 6 dimensions.

    Depending on how familiar you are with C++ and templates it may be an option to use recursive templates instead of the typedefs you are doing. I explained that here.

    Be careful: You are creating dense arrays here. This may result in large memory requirements. You may want to consider using a sparse implementation (which can also be done nicely by recursive templates, I think) that does not allocate space for variables that will not be used in the model.
    Updated on 2014-03-25T01:10:39Z at 2014-03-25T01:10:39Z by iron-man