Decision Optimization

Decision Optimization

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

 View Only
  • 1.  EndOf and StartOf using java in cplex

    Posted Wed May 02, 2018 04:36 AM

    Originally posted by: Ahlem


    I have this code of the flexible job shop scheduling problem : 

    /*Problem Description
    -------------------
    
    This problem is an extension of the classical Job-Shop Scheduling
    problem (see SchedJobShop.java) which allows an operation to be
    processed by any machine from a given set. The operation processing
    time depends on the allocated machine. The problem is to assign each
    operation to a machine and to order the operations on the machines
    such that the maximal completion time (makespan) of all operations is
    minimized.
    
    ------------------------------------------------------------ */
    
    import ilog.concert.*;
    import ilog.cp.*;
    
    import java.io.*;
    import java.util.List;
    import java.util.ArrayList;
    
    public class FlexibleJobShop {
    
        static class DataReader {
    
            private StreamTokenizer st;
    
            public DataReader(String filename) throws IOException {
                FileInputStream fstream = new FileInputStream(filename);
                Reader r = new BufferedReader(new InputStreamReader(fstream));
                st = new StreamTokenizer(r);
            }
    
            public int next() throws IOException {
                st.nextToken();
                return (int) st.nval;
            }
        }
    
        static IloIntExpr[] arrayFromList(List<IloIntExpr> list) {
            return (IloIntExpr[])list.toArray(new IloIntExpr[list.size()]);
        }
    
        static class IntervalVarList extends ArrayList<IloIntervalVar> {
            public IloIntervalVar[] toArray() {
                return (IloIntervalVar[]) this.toArray(new IloIntervalVar[this.size()]);
            }
        }
    
        public static void main(String[] args) throws IOException {
    
            String filename = "../../../examples/data/jobshopflex_default.data";
            int failLimit = 10000;
    
            if (args.length > 0)
                filename = args[0];
            if (args.length > 1)
                failLimit = Integer.parseInt(args[1]);
    
            IloCP cp = new IloCP();
            DataReader data = new DataReader(filename);
    
            try {
                int nbJobs = data.next();
                int nbMachines = data.next();
    
                IntervalVarList[] machines = new IntervalVarList[nbMachines];
                for (int j = 0; j < nbMachines; j++)
                    machines[j] = new IntervalVarList();
                List<IloIntExpr> ends = new ArrayList<IloIntExpr>();
    
                for (int i = 0; i < nbJobs; i++) {
                    int nbOperations = data.next();
                    IloIntervalVar prec = cp.intervalVar();
                    for (int j = 0; j < nbOperations; j++) {
                        int nbOpMachines = data.next();
                        IloIntervalVar master = cp.intervalVar();
                        IntervalVarList members = new IntervalVarList();
                        for (int k = 0; k < nbOpMachines; k++) {
                            int m = data.next();
                            int d = data.next();
                            IloIntervalVar member = cp.intervalVar(d);
                            member.setOptional();
                            members.add(member);
                            machines[m - 1].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));
                }
    
                for (int j = 0; j < nbMachines; j++) {
                    cp.add(cp.noOverlap(machines[j].toArray()));
                }
    
                IloObjective objective = cp.minimize(cp.max(arrayFromList(ends)));
                cp.add(objective);
    
                cp.setParameter(IloCP.IntParam.FailLimit, failLimit);
                System.out.println("Instance \t: " + filename);
                if (cp.solve()) {
                    System.out.println("Makespan \t: " + cp.getObjValue());
                } else {
                    System.out.println("No solution found.");
                }
            } catch (IloException e) {
                System.err.println("Error: " + e);
            }
        }
    }
    

    And i have attached the file data below. 

    The code is correct and show me the makespan (completition time) but i would like to know how can i extract the end and start date of each operation and  the run time of each machine.

    How can i do this? 

    Please help me.

    Regards. 

     


    #ConstraintProgramming-General
    #DecisionOptimization


  • 2.  Re: EndOf and StartOf using java in cplex

    Posted Thu May 03, 2018 01:00 PM

    Originally posted by: ChrisBr


    Hello,

     

    Below a way to retrieve the requested information.

            try {
                int nbJobs = data.next();
                int nbMachines = data.next();
    
                IntervalVarList[] machines = new IntervalVarList[nbMachines];
                for (int j = 0; j < nbMachines; j++)
                    machines[j] = new IntervalVarList();
                List<IloIntExpr> ends = new ArrayList<IloIntExpr>();
    
                
    IntervalVarList[] jobs = new IntervalVarList[nbJobs];
    
                for (int i = 0; i < nbJobs; i++) {
                    
    jobs[i] = new IntervalVarList();
                    int nbOperations = data.next();
                    IloIntervalVar prec = cp.intervalVar();
                    for (int j = 0; j < nbOperations; j++) {
                        int nbOpMachines = data.next();
                        IloIntervalVar master = cp.intervalVar();
                        
    jobs[i].add(master);
                        IntervalVarList members = new IntervalVarList();
                        for (int k = 0; k < nbOpMachines; k++) {
                            int m = data.next();
                            int d = data.next();
                            IloIntervalVar member = cp.intervalVar(d);
                            member.setOptional();
                            members.add(member);
                            machines[m - 1].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));
                }
    
                for (int j = 0; j < nbMachines; j++) {
                    cp.add(cp.noOverlap(machines[j].toArray()));
                }
    
                IloObjective objective = cp.minimize(cp.max(arrayFromList(ends)));
                cp.add(objective);
    
                cp.setParameter(IloCP.IntParam.FailLimit, failLimit);
                System.out.println("Instance \t: " + filename);
                if (cp.solve()) {
                    System.out.println("Makespan \t: " + cp.getObjValue());
                   
    for (int i = 0; i < nbJobs; i++) {
                      IloIntervalVar[] jobOps = jobs[i].toArray();
                      for (int j = 0; j < jobOps.length; j++) {
                        IloIntervalVar it = jobOps[j];
                        int s = cp.getStart(it);
                        int e = cp.getEnd(it);
                        System.out.println("Job " + i + " Operation " + j + " start at " + s + "  end at " + e);
                      }
                    }
                    for (int i = 0; i < nbMachines; 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.");
                }
    

    I hope this helps,

    Chris.


    #ConstraintProgramming-General
    #DecisionOptimization


  • 3.  Re: EndOf and StartOf using java in cplex

    Posted Fri May 04, 2018 05:39 AM

    Originally posted by: Ahlem


    Hi, 

    Thanx you so much Smile .

    But, i have one more question please. In my mathematical formulation that you find below, I have other constraints. When I use ILOG.CP.jar without cplex.jar (and I think it's possible) the program displays 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) 

    I think that my problem is too big for the Community Edition. 

    This is my piece of 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]; //hedhi mtaa l'affichage f solve()
                                                    
                            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 take into consideration all my other constraints in this case?

    I have attached my mathematical formulation to more understand me.

    Help me please !

    Thanxs.

     


    #ConstraintProgramming-General
    #DecisionOptimization


  • 4.  Re: EndOf and StartOf using java in cplex

    Posted Fri May 04, 2018 11:43 AM

    Originally posted by: ChrisBr


    Hello,

    Indeed if your problem is too big, it is possible that it exceeds the authorized limit for the Community Edition.

    About your mathematical formulation, it strongly looks like the JobShopFlex problem.
    Could you precise what constraint you would like add which is not in JobShopFlex?

    Best Regards,

    Chris.

     


    #ConstraintProgramming-General
    #DecisionOptimization


  • 5.  Re: EndOf and StartOf using java in cplex

    Posted Fri May 04, 2018 12:07 PM

    Originally posted by: Ahlem


    Hi, 

    Yes, i am trying to solve the flexible job shop problem. I want to add the first 4 constraints in my formulation which I have already attached in my last message. 

    I wrote them in my code but I don't know how can i do to applied their results in the output of the program (makespan in our case) ?. Because the result of the maksepan is based on just a few constraints i mean this code :  (but I find that the other constraints are ignored). 

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

    Best Regards.


    #ConstraintProgramming-General
    #DecisionOptimization


  • 6.  Re: EndOf and StartOf using java in cplex

    Posted Fri May 04, 2018 01:14 PM

    Originally posted by: ChrisBr


    OK, I think I understand better.

    You need to use the scheduding features instead of auxiliary numerical variables, and the most of your constraints are indeed already set by the JobShopFlex part (maybe all actually, I did not checked in details).
    For example, the constraint (5) which "is used to make sure that one machine cannot execute two different operations simultaneously (5.1) and an operation cannot be executed on two distinct machines at the same time (5.2)" is stated by cp.noOverlap(machines[j]) (5.1) and cp.alternative(master, members) *5.2).
    Constraint 6 is cp.endBeforeStart(prec, master).
    I let you check the other constraints.

    I hope this clarifies,

    Chris.

     


    #ConstraintProgramming-General
    #DecisionOptimization