0
                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();
                        }
                    }
                }

                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);

                Restricciones(Map, cplex, x);

                cplex.setParam(IloCplex.Param.MIP.Display, 0); 

                if (cplex.solve()) {

                    valorFuncionObjetivo = cplex.getValue(funcion_objetivo);

                    System.out.println("Valor de la funcion objetivo: " + valorFuncionObjetivo);

                } else {
                    System.out.println("No tiene solución");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

public static void Restricciones(Mapa Map, IloCplex cplex, IloNumVar[][][] x) {

            try {
            for (int i = 0; i < Map.NumPersM; i++) {

                    IloLinearNumExpr expr1 = cplex.linearNumExpr();

                    for (int j = 0; j < Map.NumHrsM; j++) {
                        for (int k=0; k < Map.NumTrab; k++) {

                           expr1.addTerm(1, x[i][j][k]);
                        }
                    }
                    if(Map.Personas[i].Tipo_contrato == 1) { 

                        cplex.addEq(expr1, Map.Turnos[1].Horas_turno-1);
                    }
                    else {
                        cplex.addEq(expr1, Map.Turnos[3].Horas_turno);
                    }

                }... //there are more constraints

I'm using cplex in java. I just implemented a program that I have proved first in cplex and runs ok. But I'm trying to implement it in java and it doesn't work well,it says the objetive value is zero and LP Presolve eliminated 96 rows and 0 columns: this is de console answer:

Established Conexion
Version identifier: 12.10.0.0 | 2019-11-26 | 843d4de2ae
CPXPARAM_MIP_Display                             0
Tried aggregator 1 time.
LP Presolve eliminated 96 rows and 0 columns.
All rows and columns eliminated.
Presolve time = 0.00 sec. (0.01 ticks)
Valor de la funcion objetivo: 0.0   
halfelf
  • 9,737
  • 13
  • 54
  • 63
conroran
  • 15
  • 1
  • 3
  • Welcome to SO! Please clarify what you are trying to achieve here. – Erich Apr 17 '20 at 21:30
  • minimize sum(j in horas2, k in trabajos) ((1/prioridad[j][k]) * (sum(i in personas2) x[i][j][k] - min_demanda[j][k])); this is the objetive function I'm trying to introduce in java, but I dont know if it is well written – conroran Apr 21 '20 at 09:36

1 Answers1

1

You mentioned that you, "proved first in cplex". I think this means that you have run a model file using the CPLEX interactive. Or, perhaps you have used the OPL IDE. Either way, I find that the easiest way to fix issues like this is to export both models to LP format and compare them. In your Java program you can do this by adding the following line of code just before calling the solve method:

cplex.exportModel("test.lp");

See the documentation here.

rkersh
  • 4,447
  • 2
  • 22
  • 31
  • I tried it but my opl proyect is .mod and I tried to change it but it needs to take information from another file, .dat from excel so I can't export it. Anyway my main question is if the objetive function is ok or there is any bug. I'll read every constraint again, thanks! – conroran Apr 21 '20 at 09:31