Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Difference between IloCP and IloCplex in java using cplex

    Posted 05/03/18 07:10 AM

    Originally posted by: Ahlem


    Hi,

    What is the difference between the use of  IloCP cp = new IloCP() and  IloCplex cplex = new IloCplex() ? 

    I am trying to solve a scheduling problem, can i use just the ILOG.CP.jar without cplex.jar ? 

    When i try to use just the  ILOG.CP.jar , the program shows me this error: 

    ilog.concert.IloException: CP Optimizer Community Edition solves problems with a search space of up to 2^1000
    at ilog.cp.cppimpl.cp_wrapJNI.IloCP_solve(Native Method)
    at ilog.cp.cppimpl.IloCP.solve(IloCP.java:149)

    Thanxs.


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Difference between IloCP and IloCplex in java using cplex

    Posted 05/03/18 06:47 PM

    Yes, you can use ILOG.CP.jar without cplex.jar, provide you are using only CP Optimizer and not CPLEX. (If you declare an instance of IloCplex, for example, you will need cplex.jar.)

    The exception suggests that your problem is too big for the Community Edition. By "too big" I mean some combination of too many variables and too large a domain for each.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Difference between IloCP and IloCplex in java using cplex

    Posted 05/04/18 05:47 AM

    Originally posted by: Ahlem


    Hi,

    How can I fix this problem? do I have to limit the domain of my variables for example?

    This is my code : 

    import java.util.List;
    import java.util.ArrayList;
    
    import ilog.concert.IloException;
    import ilog.concert.IloIntExpr;
    import ilog.concert.IloIntVar;
    import ilog.concert.IloIntervalVar;
    import ilog.concert.IloLinearNumExpr;
    import ilog.concert.IloModel;
    import ilog.concert.IloNumVar;
    import ilog.concert.IloNumVarType;
    import ilog.concert.cppimpl.IloObjective;
    import ilog.cplex.IloCplex;
    import ilog.cp.*;
    
    public class evaluationCplex {
    
    
            public evaluationCplex() {
                    super();
            }
            
            static class IntervalVarList extends ArrayList<IloIntervalVar> {
            public IloIntervalVar[] toArray() {
                return (IloIntervalVar[]) this.toArray(new IloIntervalVar[this.size()]);
            }
        }
    
            static IloIntExpr[] arrayFromList(List<IloIntExpr> list) {
            return (IloIntExpr[]) list.toArray(new IloIntExpr[list.size()]);
        }
            public static void modell() {
                                    
                    Information f = new Information();
                    MatriceGlobal benchmrak = f.getM();
                    int job = f.getNbjob();
                    int ressource = f.getNbressource();
                    int operation = f.getNboperationparjob();
                    Assignment a = new Assignment();
                    ArrayList<MatriceGlobal> MG = a.MatrixRandom();
                     
                    /*
                     * for(int i=0; i<MG.size();i++)
                     * 
                     * { for (int j=0 ; j < job ; j++) 
                     * { MG.get(i).get(j).afficher(); }
                     * System.out.println("mara okhraa"); }
                     */
                    int L = 1000;
                    int failLimit = 10000;
    
                    try {
    
                            
                            IloCP cp = new IloCP();
                            //IloCplex cplex = new IloCplex(); // define new model
                            
                            IntervalVarList[] machines = new IntervalVarList[ressource];
                for (int j = 0; j < ressource; j++)
                    machines[j] = new IntervalVarList();
                List<IloIntExpr> ends = new ArrayList<IloIntExpr>();
    
                            // variables
    
                            Matrice M = new Matrice(ressource, operation);
                            int [][][] x = new int[job][][];
                            IloNumVar [][][] c = new IloNumVar[job][][];
                            IloNumVar [][][] s = new IloNumVar[job][][];                    
                            IloNumVar[][][][][] y = new IloNumVar[job][][][][];
                            IloNumVar [] CTime = new IloNumVar[job];
                            IloIntervalVar[][] information = new IloIntervalVar[job][operation];
                            
                            IntervalVarList[] jobs = new IntervalVarList[job]; 
                                                    
                            for (int m = 0; m < MG.size(); m++) //to extract my information from bechmarks
    
                            {                            
                                    for (int i = 0; i < job; i++) {
    
                                            c[i] = new IloNumVar[MG.get(m).get(i).getX()][];
                                            s[i] = new IloNumVar[MG.get(m).get(i).getX()][];
    
                                            for (int j = 0; j < MG.get(m).get(i).getX(); j++) {
                                                    c[i][j] = cp.numVarArray(ressource, 0, Double.MAX_VALUE);
                                                    s[i][j] = cp.numVarArray(ressource, 0, Double.MAX_VALUE);
                                            }
                                    }
    
                                    for (int i1 = 0; i1 < job; i1++) {
    
                                            y[i1] = new IloNumVar[MG.get(m).get(i1).getX()][][][];
                                                                                    
                                            for (int j1 = 0; j1 < MG.get(m).get(i1).getX(); j1++) {
                                    
                                                    y[i1][j1] = new IloNumVar[job][][];
    
                                                    for (int i2 = 0; i2 < job; i2++) {
                                                            
                                                            y[i1][j1][i2] = new IloNumVar[MG.get(m).get(i2).getX()][];
                                                            
                                                            for (int j2 = 0; j2 < MG.get(m).get(i2).getX(); j2++) {
    
                                                                    y[i1][j1][i2][j2] = cp.boolVarArray(ressource);
                                                            }
                                                    }
                                            }
                                    }
    
                                    for (int j = 0; j < job; j++) {
                                            CTime[j] = cp.numVar(0, Double.MAX_VALUE);
                                    }
                                                            
                                    for (int i1 = 0; i1 < job; i1++) { //Initialize p[i][j][k]
    
                                            int[][] MatBinaire = MG.get(m).get(i1).getMatrice();
                                            int[][] mat = benchmrak.get(i1).getMatrice();
                                            int[][][] p = new int[job][MG.get(m).get(i1).getX()][MG.get(m).get(i1).getY()];
                                            
                                            for (int j1 = 0; j1 < MG.get(m).get(i1).getX(); j1++) {
                                                    
                                                    for (int i2 = 0; i2 < job; i2++) {
    
                                                      for (int j2 = 0; j2 < MG.get(m).get(i2).getX(); j2++) {
                                                    
                                                             for (int k = 0; k < ressource; k++) {
    
                                                            if (MatBinaire[j1][k] ==1) {
    
                                                                    int duree = mat[j1][k];
                                                                    p[i1][j1][k] = duree;
                                                            }
                                                            
                                                            cp.addGe(c[i1][j1][k],
                                                                            cp.sum(c[i2][j2][k], cp.prod(y[i1][j1][i2][j2][k], p[i1][j1][k]),
                                                                                            cp.sum(-L, cp.prod(L, y[i2][j2][i1][j1][k])))); // constraint5                                          
                                                            
                                                    }
                                                    
                                            }
                                    }
                            }
                                            
                                            for (int j1 = 0; j1 < MG.get(m).get(i1).getX(); j1++) {
    
                                                    for (int i2 = 0; i2 < job; i2++) {
    
                                                            for (int j2 = 1; j2 < MG.get(m).get(i2).getX(); j2++) {
    
                                                                    for (int k = 0; k < ressource; k++) {
                                                                            
                                                                            cp.addGe(c[i1][j1][k], cp.sum(c[i2][j2 - 1][k],
                                                                                            cp.prod(y[i1][j1][i2][j2][k], p[i1][j1][k])));// constraint 6 fame(-1)
    
                                                                    }
                                                            }
                                                    }
                                            }                                                    
                               }
                                    
                                    //expressions
                                    
                                    for (int i2 = 0; i2 < job; i2++) {
                                            
                                            IloLinearNumExpr[][][] expr = new IloLinearNumExpr[job][MG.get(m).get(i2).getX()][ressource];
                                            IloLinearNumExpr[][][] expr1 = new IloLinearNumExpr[job][MG.get(m).get(i2).getX()][ressource];
                                            for (int j2 = 0; j2 < MG.get(m).get(i2).getX(); j2++) {
                                                                                                    
                                                    for (int k = 0; k < ressource; k++) {
    
                                                            expr[i2][j2][k] = cp.linearNumExpr();
                                                            expr1[i2][j2][k] = cp.linearNumExpr();
                                                            
                                                            for (int i1 = 0; i1 < job; i1++) {
    
                                                                    for (int j1 = 0; j1 < MG.get(m).get(i1).getX(); j1++) {
                                                                    
                                                                            expr[i2][j2][k].addTerm(1.0, y[i1][j1][i2][j2][k]);     
                                                                            expr1[i2][j2][k].addTerm(1.0, y[i2][j2][i1][j1][k]);
                                                                    
                                                                    }
                                                                    
                                                            }
                                                            
                                                            cp.addLe(expr[i2][j2][k], 1); //Constraint3
                                                            cp.addLe(expr[i2][j2][k], expr1[i2][j2][k]); //Constaint4 there is a problem anywhere                                                   
                                                            
                                                    }
                                            }
                                    }                                                    
                                                                                                                                                                                                                    
                                    //Precedence constraints 
                                    
                                                    
                                    for (int i = 0; i < job; i++) 
                                    {
                                            jobs[i] = new IntervalVarList();
                                            IloIntervalVar prec = cp.intervalVar();
                                            int[][] MatBinaire = MG.get(m).get(i).getMatrice();
                                            int[][] mat = benchmrak.get(i).getMatrice();
                                            for (int j = 0; j < MG.get(m).get(i).getX(); j++) //operation by job
                                            {
                                                    IloIntervalVar master = cp.intervalVar();
                                                    jobs[i].add(master);
                                                    IntervalVarList members = new IntervalVarList();
                                for (int k = 0; k < ressource; k++) 
                                            {
                                    if (MatBinaire[j][k] ==1) 
                                    {
                                                                    int duree = mat[j][k];
                                                                    IloIntervalVar member = cp.intervalVar(duree);
                                            member.setOptional();
                                            members.add(member);
                                            machines[k].add(member);
                                    
                                    }
                                            }
                                cp.add(cp.alternative(master, members.toArray()));
                                if (j > 0)
                                    cp.add(cp.endBeforeStart(prec, master));
                                prec = master;
                                            }
                                            
                                            ends.add(cp.endOf(prec)); //définir l'objectif de minimisation.                                      
    
                                    }
                                    
                                    for (int j = 0; j < ressource; j++) 
                                     {
                                    cp.add(cp.noOverlap(machines[j].toArray()));
                             }
                            }
                            
                                IloObjective objective = (IloObjective) cp.minimize(cp.max(arrayFromList(ends)));
                        cp.add(objective);
                        cp.setParameter(IloCP.IntParam.FailLimit, failLimit);
    
                        if (cp.solve()) {
                            System.out.println("Makespan \t: " + cp.getObjValue());       
                            
                            for (int i = 0; i < job; i++) {
                                IloIntervalVar[] jobOps = jobs[i].toArray();
                                for (int j = 0; j < jobOps.length; j++) {
                                  IloIntervalVar it = jobOps[j];
                                  int m = cp.getStart(it);
                                  int e = cp.getEnd(it);
                                  System.out.println("Job " + i + " Operation " + j + " start at " + m + "  end at " + e);
                                }
                              }
                             
                             for (int i = 0; i < ressource; i++) {
                                IloIntervalVar[] mchOps = machines[i].toArray();
                                int rt = 0;
                                for (int j = 0; j < mchOps.length; j++) {
                                  IloIntervalVar it = mchOps[j];
                                  if (cp.isPresent(it)) {
                                    rt += cp.getSize(it);
                                  }
                                }
                                System.out.println("Machine" + i + " run time " + rt);
                              }
                             
                        } else {
                            System.out.println("No solution found.");
                        }
    
    
                    } catch (IloException exc) {
                            exc.printStackTrace();
                    }
    
            }
    }
    

    How can I correct it in your opinion, please?

    Thanxs.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Difference between IloCP and IloCplex in java using cplex

    Posted 05/04/18 03:45 PM

    I'm not an expert on CP Optimizer, and I don't have time (or inclination) to read your code carefully, so I'll just offer a few quick observations.

    1. If you are not going to use CPLEX (as indicated by commenting out the line that creates an IloCplex instance), then you should also get rid of the import. Even if the import is unused, I suspect it will trigger a compile error unless cplex.jar is included among the libraries.
    2. Any class that is listed in the Java API manual for CP Optimizer (in either the ilog.concert or the ilog.cp package) is fair to use. If you are using anything that is listed only in the ilog.cplex package, it will probably cause an error.
    3. In mathematical programming (where you would use CPLEX), solvers generally have a comparatively easy time with continuous variables and have to work harder when variables are binary or general integer. In constraint programming (where you would use CP Optimizer), the opposite tends to be the case: solvers "like" discrete variables and have to work harder (I think) when there are continuous variables. That's not to say that you cannot use continuous variables, but if any of your IloNumVars are actually integer-valued, I think you're better off declaring them as IloIntVar.
    4. I'm not sure limiting the domains of all the variables is absolutely necessary, but the more you can limit the domains of discrete variables (without excluding any optimal solutions, of course), the faster CP Optimizer is likely to be when solving the model (and the less memory you are likely to need to use).

    #CPLEXOptimizers
    #DecisionOptimization