Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Matrix Definition

    Posted 04/18/12 11:25 AM

    Originally posted by: lriWANGCHEN


    Hello,everyone,

    I want to define a matrix like this Y[t][i][s][j], and the element in the matrix is float, I write the C++ codes with cplex like this:

    NumVarMatrix Y(env,n);
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    Y[i][j]=IloNumVarArray(env,n,0.0,1.0,ILOFLOAT);
    }
    }

    But it is not right, so could someone can help me? Thank you so much.
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Matrix Definition

    Posted 04/19/12 02:27 AM

    Originally posted by: SystemAdmin


    I see three different ways to implement that:
    1. Probably the easiest but not nicest: You the IloArray template:
    
    typedef IloArray<IloNumVarArray> NumVarArray2; 
    // 2D array typedef IloArray<NumVarArray2>   NumVarArray3; 
    // 3D array typedef IloArray<NumVarArray3>   NumVarArray4; 
    // 4D array   NumVarArray4 Y(env); 
    
    for (
    
    int t = 0; t < ...; ++t) 
    { NumVarArray a3(env); 
    
    for (
    
    int i = 0; i < ...; ++i) 
    { NumVarArray a2(env); 
    
    for (
    
    int s = 0; s < ...; ++s) 
    { a2.add(IloNumVarArray(env, n, 0.0, 1.0, ILOFLOAT)); 
    } a3.add(a2); 
    } Y.add(a3); 
    }
    

    2. Use a flat IloNumVarArray and a macro:
    
    #define Y(t,i,s,j) varY[(t) * maxI * maxS * maxJ + (i) * maxS * maxJ + (s) * maxJ + (j)] 
    
    int maxT = ..., maxI = ..., maxS = ..., maxJ = ...; IloNumVarArray varY(env); 
    
    for (
    
    int t = 0; t < maxT; ++t) 
    { NumVarArray a3(env); 
    
    for (
    
    int i = 0; i < maxI; ++i) 
    { NumVarArray a2(env); 
    
    for (
    
    int s = 0; s < maxS; ++s) 
    { 
    
    for (
    
    int j = 0; j < maxJ; ++j) 
    { varY.add(IloNumVar(env, 0.0, 1.0, ILOFLOAT)); 
    } 
    } a3.add(a2); 
    } 
    } 
    // Now access Y[0][1][2][3] like as Y(0, 1, 2, 3)
    

    3. Use recursive templates. This is IMO the most elegant approach but it requires some understanding about templates. I have posted example code for this on the Forum before.
    #CPLEXOptimizers
    #DecisionOptimization