Originally posted by: JuanCarlosPego
Hello Olivier, sorry for not explaining well but my English is not good.
copy the code again:
int EjemploPregunta()
{
typedef IloArray<IloIntVarArray> IloIntVarArray2;
// FILE LOG
ofstream salida("Log1.txt");
IloEnv env;
try
{
IloModel model(env);
IloInt i, j, t, f, g, k;
IloInt NEmployees=20; // Total number of employees
IloInt NActivities=10; // Total number of activities to cover
IloInt NCantEmpPerAct=2; // Number of employees by activity
IloIntArray ArrayBoss(env,10,0,1,2,3,4,5,6,7,8,9);
IloIntArray ArrayEmployee(env,10,10,11,12,13,14,15,16,17,18,19);
IloIntArray ArrayBeginAct(env,10,18,21,31,31,33,35,36,39,45,46);
IloIntArray ArrayEndAct(env,10,37,40,68,68,53,40,68,47,83,83);
// Array for specialties (0=BOSS,1=EMPLOYEE)
IloIntArray ArrayEmp_B_E(env, NEmployees);
for (IloInt i=0;i<ArrayBoss.getSize();i++){
ArrayEmp_B_E[i]=0;
}
for (IloInt i=0;i<ArrayEmployee.getSize();i++){
ArrayEmp_B_E[i]=1;
}
// Variable to cover activities
IloIntVarArray2 VarActivities(env, NActivities);
for(i = 0; i < NActivities; i++)
{
VarActivities[i] = IloIntVarArray(env, NCantEmpPerAct,0,NEmployees);
}
// Variable activities per employee
IloIntVarArray2 EmpActivity(env, NEmployees);
for(i = 0; i < NEmployees; i++)
{
EmpActivity[i] = IloIntVarArray(env, NActivities,-1,NActivities);
}
for(i = 0; i < NEmployees; i++)
{
for(j = 0; j < NActivities; j++)
{
for(k = 0; k < NCantEmpPerAct; k++){
// I need to get the index of activity
model.add(IloIfThen(env, VarActivities[j][k] == i, EmpActivity[i][VarActivities[j][k]]==j));
}
}
}
// *** If no solution is uncommented ***
// each activity will exist as much a boss and an employee
/*for(i = 0; i < NActivities; i++){
IloIntVarArray TrpPM_COVar(env, 2, 0, NEmployees - 1);
for (j = 0; j < 2; j++){
model.add(TrpPM_COVar[j] == ArrayEmp_B_E[VarActivities[i][j]]);
}
for (j = 0; j < 2; j++){
model.add(IloCount(TrpPM_COVar, j) <= 1);
}
}*/
IloExpr Obj_Func_2(env);
for (i = 0; i < NActivities; ++i)
{
for (j = 0; j < 2; ++j)
{
Obj_Func_2 += VarActivities[i][j];
}
}
model.add( IloMaximize(env,Obj_Func_2) );
/* ---------------------------------------EXTRACTING THE MODEL AND SOLVING-----------------------------------------. */
//Call to Ilog Optimizer
IloCP cp(model);
cp.setParameter(IloCP::TimeLimit, 5);
cp.setParameter(IloCP::LogPeriod, 10000);
if (cp.solve())
{
cp.out() <<std::endl;
cp.out() <<std::endl;
cp.out() << "SOLUTION.... " << std::endl;
salida << "Solution : " << std::endl;
salida <<std::endl;
salida << " ";
salida<< "Activities and employees assigned :"<<std::endl;
salida<< std::endl;
for(j = 0; j < NCantEmpPerAct; j++){
salida <<" IdEmp"<<j+1<<" ";
}
salida<< std::endl;
for(i = 0; i < NActivities; i++)
{
salida << " " << i+1 << ": ";
for(j = 0; j < NCantEmpPerAct; j++){
salida <<" ("<<cp.getValue(VarActivities[i][j])<<")"<<" ";
}
salida<< std::endl;
}
salida<< std::endl;
salida<< std::endl;
salida<< "Employees and assigned activities :"<<std::endl;
int id_actividad=0;
for(i = 0; i < NEmployees; i++)
{
salida << " " << i << ": ";
for(j = 0; j < NActivities; j++)
{
id_actividad=cp.getValue(EmpActivity[i][j]);
if (id_actividad>=0){
salida <<id_actividad+1<< " ";
}
}
salida<< std::endl;
}
}
else
{
cp.out() << "No solution found. " << std::endl;
}
}catch (IloException& ex)
{
env.out() << "Error: " << ex << std::endl;
}
env.end();
return 0;
}
probably you want the second loop to iterate over the indices ArrayBoss.getSize() to ArrayBoss.getSize() + ArrayEmployee.getSize() -1, don't you?
the answer is yes.
I need to get the ID of all activities where each employee is assigned. For example, the solution is now this
Solution :
Activities and employees assigned :
IdEmp1 IdEmp2
1: (4) (4)
2: (3) (3)
3: (5) (5)
4: (7) (7)
5: (1) (1)
6: (6) (6)
7: (8) (8)
8: (2) (2)
9: (9) (9)
10: (0) (0)
Employees and assigned activities :
0: 10
1: 5
2: 8
3: 2
4: 1
5: 3
6: 6
7: 4
8: 7
9: 9
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
********************
and try to find the solution that is:
Solution :
Activities and employees assigned :
IdEmp1 IdEmp2
1: (1) (10)
2: (2) (11)
3: (3) (12)
4: (4) (13)
5: (5) (14)
6: (6) (15)
7: (7) (16)
8: (1) (17)
9: (2) (10)
10: (3) (11)
Employees and assigned activities :
0:
1: 1,8
2: 2,9
3: 3,10
4: 4
5: 5
6: 6
7: 7
8:
9:
10: 1,9
11: 2,10
12: 3
13: 4
14: 5
15: 6
16: 7
17:
18:
19:
Thank you very much and sorry
Juan Carlos
#CPOptimizer#DecisionOptimization