Issue
This is my objetive function in cplex:
minimize sum(j in horas, k in trabajos) ((1/prioridad[j][k]) * (sum(i in personas) x[i][j][k] - min_demanda[j][k]));
and this is how I tried to do it in java but it doesn't works well ( the objetive value is zero and all the var x[][][] too so its must be negative in that case because there is the term -min_demanda[j] (not null)):
double valorFuncionObjetivo = 0;
IloCplex cplex = new IloCplex();
IloNumVar[][][] x = new IloNumVar[Map.NumPersM][Map.NumHrsM][Map.NumTrab];
for (int i = 0; i < Map.NumPersM; i++) {
for (int j = 0; j < Map.NumHrsM; j++) {
for (int k = 0; k < Map.NumTrab; k++) {
x[i][j][k] = cplex.boolVar();
}
}
}
//cplex.setParam(IloCplex.Param.Preprocessing.Presolve, false);
// Model
IloLinearNumExpr funcion_objetivo = cplex.linearNumExpr();
for (int j = 0; j < Map.NumHrsM; j++) {
for (int k = 0; k < Map.NumTrab; k++) {
for (int i = 0; i < Map.NumPersM; i++) {
funcion_objetivo.addTerm(x[i][j][k],Map.Demandas[j][k].Prioridad);
}
funcion_objetivo.setConstant(-(Map.Demandas[j][k].Prioridad)*Map.Demandas[j][k].Min_personas);
}
}
cplex.addMinimize( funcion_objetivo);
Solution
There is a problem with the way you set the constant term in your objective. You code is
for (int j = 0; j < Map.NumHrsM; j++) {
for (int k = 0; k < Map.NumTrab; k++) {
for (int i = 0; i < Map.NumPersM; i++) {
funcion_objetivo.addTerm(x[i][j][k],Map.Demandas[j][k].Prioridad);
}
funcion_objetivo.setConstant(-(Map.Demandas[j][k].Prioridad)*Map.Demandas[j][k].Min_personas);
}
}
So in each iteration of the j/k loop you overwrite the constant term. If the last term happens to be 0 then the constant term in the objective will be 0.
You probably want to sum the constant like this (I added funcion_objectivo.getConstant()
):
for (int j = 0; j < Map.NumHrsM; j++) {
for (int k = 0; k < Map.NumTrab; k++) {
for (int i = 0; i < Map.NumPersM; i++) {
funcion_objetivo.addTerm(x[i][j][k],Map.Demandas[j][k].Prioridad);
}
funcion_objetivo.setConstant(funcion_objectivo.getConstant() - (Map.Demandas[j][k].Prioridad)*Map.Demandas[j][k].Min_personas);
}
}
Also, you can System.out.println(funcion_objective.getConstant())
to double check that the constant term is indeed non-zero.
I am not sure whether the fact that all variables are 0 is unexpected or not. If that is unexpected then you are probably missing some constraints that require the variables to be non-zero. A good way to debug this is to assign names to your variables and constraints (use the setName()
function), export the model to LP file format using cplex.exportModel("model.lp")
and then check the created model.lp
file in a text editor to make sure all constraints look as expected.
Answered By - Daniel Junglas