Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to use a variable as index?

    Posted 07/10/12 11:45 AM

    Originally posted by: Dmartinc


    Hi,

    I am trying to use a variable as an index in a coeffiecient matrix,
    What should I do in order to make it work?

    /* //Objective function
    IloObjective Obj_Func_1(env);
    for (j = 0; j < NTasks; j++)
    {
    Obj_Func_1 += c[Tasksj][j];
    }
    model.add(IloMinimize(env, Obj_Func_1));

    //Tasks all different
    model.add(IloAllDiff(env, Tasks)); */

    David
    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: How to use a variable as index?

    Posted 07/11/12 05:28 PM

    Originally posted by: mslusky


    If c and Tasks are one-dimensional arrays, you could use IloElement:
    
    Obj_Func_1 += IloElement(c, Tasks[j]);
    


    Marla
    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: How to use a variable as index?

    Posted 07/12/12 08:46 AM

    Originally posted by: Dmartinc


    Thank you Marla,

    but in the case that I have a 2 dimension array, does anyone know how to make it work?

    David
    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: How to use a variable as index?

    Posted 07/12/12 12:07 PM

    Originally posted by: ChrisBr


    Hello David,

    I think some character have been eaten by the writer, so I guess you wanted to write
    
    c[ Tasks[j][j] ] 
    //case 1
    

    or
    
    c[ Tasks[j] ][j] 
    //case 2
    

    In case 1
    I suppose that Task is an IloArray<IloIntVarArray> (so 2 dimensions) and c is either an IloIntArray or a IloNumArray (so 1 dimension)

    Note that in this case you cannot write IloAllDiff(env, Tasks), you have to flatten the array Tasks before.

    Here is an sample of code which looks like what you want:
    
    
    //model.add(IloAllDiff(env, Tasks)); // cannot compile! IloIntVarArray Tasks2(env, NTasks*NTasks); 
    
    for (i=0; i<NTasks; ++i) 
    
    for (j=0; j<NTasks; ++j) Tasks2[i*NTasks+j] = Tasks[i][j]; model.add(IloAllDiff(env, Tasks2));   IloExpr Obj_Func_1(env, 0); 
    
    for (j = 0; j < NTasks; j++) 
    { Obj_Func_1 += c[Tasks[j][j]]; 
    } IloObjective objective = IloMinimize(env, Obj_Func_1); model.add(objective);
    


    In case 2
    I suppose that Task is an IloIntVarArray (so 1 dimension) and c is either an IloArray<IloIntArray> or a IloArray<IloNumArray> (so 2 dimensions)
    You have to flatten the array c

    Here is an sample of code which looks like what you want:
    
    model.add(IloAllDiff(env, Tasks)); IloIntArray c2(env, NTasks*NTasks); 
    
    for (i=0; i<NTasks; ++i) 
    
    for (j=0; j<NTasks; ++j) c2[i*NTasks+j] = c[i][j];   IloExpr Obj_Func_1(env, 0); 
    
    for (j = 0; j < NTasks; j++) 
    { 
    //Obj_Func_1 += c[Tasks[j]][j]; // cannot compile! Obj_Func_1 += c2[Tasks[j]*NTasks +j]; 
    } IloObjective objective = IloMinimize(env, Obj_Func_1); model.add(objective);
    


    I hope this helps,

    Chris
    #CPOptimizer
    #DecisionOptimization


  • 5.  Re: How to use a variable as index?

    Posted 07/12/12 12:57 PM

    Originally posted by: Dmartinc


    Thanks Chris,

    it was the second case:
    
    c[Tasks[j]][j]
    


    Now, following the same approach, I have M Employees who can have or not the enough skills, in order to carry out a tasks. Thus,
    I have an elegibility matrix, with domain http://0..1, being 1 if the employee i is elgibiled to carry out task j, otherwise cannot.

    Would be the following contraint properly defined according to the previous statement?

    
    
    //Elegibility constraint IloIntArray e2(env, MEmployees*NTasks); 
    
    for (i=0; i<MEmployees; ++i) 
    
    for (j=0; j<NTasks; ++j) e2[i*NTasks+j] = e[i][j];   
    
    for(i=0; i< MEmployees; ++i) 
    { 
    
    for(j=0; j< NTasks; ++j) 
    { model.add(e2[Tasks[j]*NTasks+j]==1); 
    } 
    }
    


    David
    #CPOptimizer
    #DecisionOptimization


  • 6.  Re: How to use a variable as index?

    Posted 07/12/12 01:02 PM

    Originally posted by: Dmartinc


    Sorry, it should be like this:

    
    
    
    for(j=0; j< NTasks; ++j) 
    { model.add(e2[Tasks[j]*NTasks+j]==1); 
    }
    


    I am not quite sure if the approach it´s the correct one.

    David
    #CPOptimizer
    #DecisionOptimization


  • 7.  Re: How to use a variable as index?

    Posted 07/12/12 02:00 PM

    Originally posted by: ChrisBr


    Hello David,

    Please, note that if the variable is used as index in last dimension, you don't need to flatten the array, so you can write:
    
    
    
    for(i=0; i< MEmployees; ++i) 
    { 
    
    for(j=0; j< NTasks; ++j) 
    { model.add(e[i][Tasks[j]]==1); 
    } 
    }
    


    Regards,

    Chris
    #CPOptimizer
    #DecisionOptimization


  • 8.  Re: How to use a variable as index?

    Posted 07/12/12 03:11 PM

    Originally posted by: Dmartinc


    Thanks Chris,

    That it´s good to know, because my current approach is to assign Employees to Tasks,
    but it could be the other way around.

    Thanks again.

    David
    #CPOptimizer
    #DecisionOptimization