Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

CP permutation constraint

  • 1.  CP permutation constraint

    Posted Sun October 07, 2018 06:59 AM

    Originally posted by: imanopl


    Hello everybody,

     

    I am working on a permutation flowshop code. I have an issue when I want to add some intervalsequencevararray variables, the model.end does not work and I get unhandelled exception error.

     int Machines = 1, Jobs = 1, rr = 1; //*counter variables
            int W, M;
            IloEnv env;
            IloModel model(env);
            IloNumVar cmax(env, 0, +IloInfinity, ILOFLOAT);
    
            IloInt minInterShiftRest = 480;
    
            IloIntervalVarArray2 start2;
            IloRangeArray constraints;
    
            Machines = 2;
            Jobs = 3;
    
            TwoDimension Start(env, Machines); 
    
            for (int i = 0; i < Machines; i++)
            {
    
                    Start[i] = IloNumVarArray(env, Jobs, 0, +IloInfinity, ILOFLOAT);
            }
     
            IloIntervalVarArray2 Startvar(env, Jobs);
            for (int j = 0; j < Jobs; j++)
                    Startvar[j] = IloIntervalVarArray(env, Machines);
    
            for (int j = 0; j < Jobs; j++)
                    for (int m = 0; m < Machines; m++)
                            Startvar[j][m] = IloIntervalVar(env);
    
            IloIntervalVarArray sizeofoperation(env, Jobs);
            IloIntervalVarArray sizeofoperation2(env, Jobs);
     
    
            for (int i = 0; i < Jobs; i++)
            model.add(cmax >= IloEndOf(Startvar[i][Machines - 1]));
    
            IloIntervalSequenceVarArray seqs(env, Machines);
            for (int i = 0; i < Machines; i++) {
                    seqs[i] = IloIntervalSequenceVar(env, Startvar[i]);
                    model.add(IloNoOverlap(env, seqs[i]));
                    if (0 < i) {
                            model.add(IloSameSequence(env, seqs[0], seqs[i]));
                    }
            }
    
    
    
            for (int i = 0; i < Jobs; i++)
            model.add(cmax >= IloEndOf(Startvar[i][Machines - 1]));
    
    
    
            for (IloInt k = 0; k < Jobs; k++)
            {
                    sizeofoperation[k] = Startvar[k][0];
    
            }
    
            for (IloInt k = 0; k < Jobs; k++)
            {
    
                    model.add(IloNoOverlap(env, sizeofoperation));
            }
    
    
            for (IloInt k = 0; k < Jobs; k++)
            {
                    sizeofoperation2[k] = Startvar[k][1];
            }
    
    
            for (IloInt k = 0; k < Jobs; k++)
            {
    
                    model.add(IloNoOverlap(env, sizeofoperation2));
            }
             
    
            TwoDimensionNum t1(env, Machines - 1);
            for (int i = 0; i < Machines - 1; i++)
            {
                    t1[i] = IloNumArray(env, Jobs, 0, +IloInfinity, ILOFLOAT);
            }
    
     
            TwoDimensionNum Processing(env, Machines);//*   x is
            for (int i = 0; i < Machines; i++)
            {
                    Processing[i] = IloNumArray(env, Jobs, 0, +IloInfinity, ILOFLOAT);
            }
    
    
            for (int j = 0; j < Machines - 1; j++)
                    for (int i = 0; i < Jobs; i++)
                    {
                            model.add(IloEndBeforeStart(env, Startvar[i][j], Startvar[i][j + 1], t1[j][i]));
                    }
    
    
            for (int i = 0; i < Jobs; i++)
                    for (int j = 0; j < Machines; j++)
                    {
                            model.add(IloSizeOf(Startvar[i][j]) == Processing[j][i]);
                    }
    
    
            readparameters(t1, Machines, Jobs, Processing, M, W);//reading parameters data
    
            IloObjective obj(env, cmax, IloObjective::Minimize);
    
            model.add(obj);
    
             
            IloCP cp(model); ;
            cp.solve();
            model.end();
            env.end();
    

    I tried to add error handler but I could not. The error occurs after adding

    IloIntervalSequenceVarArray seqs(env, Machines);
            for (int i = 0; i < Machines; i++) {
                    seqs[i] = IloIntervalSequenceVar(env, Startvar[i]);
                    model.add(IloNoOverlap(env, seqs[i]));
                    if (0 < i) {
                            model.add(IloSameSequence(env, seqs[0], seqs[i]));
                    }
            }
    

    Despite the model is solved, it cannot end the model. I add this to make the model permuation flowshop for all machines. Does anyone can help me our regarding this issue?

    Regards


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: CP permutation constraint

    Posted Mon October 08, 2018 03:37 AM

    Originally posted by: Petr Vilím


    Hello,

    unfortunately I'm not able to compile your sample, e.g. TwoDimension or readparameters are undefined.

    I suggest to catch the exception an print it, hopefully the description will help you. I.e:

    try {
      your_code here;
    } catch (IloException & ex) {
      std::cout << "Caught: " << ex << std::endl;
    }
    

    Best regards, Petr


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: CP permutation constraint

    Posted Mon October 08, 2018 04:33 AM

    Originally posted by: Petr Vilím


    Philippe noticed one more thing: 

     

    In array startVar the first index seem to be the index of the jobs:
     for (int i = 0; i < Jobs; i++)
            model.add(cmax >= IloEndOf(Startvar[i][Machines - 1]));

    But in the loop on machines for the sameSequence it seems to use the index of the jobs:

    IloIntervalSequenceVarArray seqs(env, Machines);
    for (int i = 0; i < Machines; i++) {
       seqs[i] = IloIntervalSequenceVar(env, Startvar[i]);
       model.add(IloNoOverlap(env, seqs[i]));
     

    If it is really the source of the issue (number of machines is different from number of jobs) then errors like this should be detected by asserts that are present in our header files. Just compile in debug mode (without NDEBUG defined) and the code should crash on an assert whenever an array is indexed by a wrong value. Otherwise such kind of errors may lead to an undefined behavior like the one you're probably experiencing.

    Best regards, Petr


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: CP permutation constraint

    Posted Tue October 09, 2018 01:30 AM

    Originally posted by: imanopl


    Thanks Petr,

     

    The error I got is:

    Exception thrown at 0x00007FF787423537 in Template - CPlex 12.6.2 in Visual Studio.exe: 0xC0000005: Access violation reading location 0x0000000000000020.

    I have attached a little shorter version of my code and the input file. It is in visual studio and a simple permutation flowshop.

     

    Regards,

    Iman


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: CP permutation constraint

    Posted Tue October 09, 2018 10:19 AM

    Originally posted by: PhilippeLaborie


    Hello,

     

    First, concerning the error, it indeed is an issue in CP Optimizer on models with sameSequence constraints that are defined without the arrays of interval variables: in this case model.end() may crash. It will be fixed in future releases.

    There are several possible work-arounds:

    1- Do not use model.end() but let the memory be released when you end the environment (env.end()). If you only want to release the memory of the IloCP engine, you can also call cp.end()

    2- If you really need to destroy only the model, then you should use the constructor of sameSequence that, beside the two sequence variables, also uses two arrays of interval variables. 

     

    Looking at the model you described above,  I'm really wondering why it is so complex:

     

    1- If you have precedence constraints (endBeforeStart) between consecutive operations of the same job, then, by construction, they do not overlap, so you do not need to add a noOverlap constraint between these operations (startVar[i] is an array that represents all the operations of job i):

      seqs[i] = IloIntervalSequenceVar(env, Startvar[i]);

      model.add(IloNoOverlap(env, seqs[i]));

     

    2- Given that your problem is like a flow shop and all jobs go through the same machines in the same order, you do not need to have sameSequence constraints between these sequences

     

    3- You post too many noOverlap constraints for the machines. You do not need to post one (and the same) noOverlap per job!

     

      for (IloInt k = 0; k < Jobs; k++) {

        sizeofoperation[k] = Startvar[k][0];

      }

      for (IloInt k = 0; k < Jobs; k++) {

        model.add(IloNoOverlap(env, sizeofoperation));

      }

     

    4- And if your problem is a permutation flow shop problem, then it is between these noOverlap (the noOverlap on the operations of a machine, not the fixed sequence of operation on a job) that you need to post a sameSequence constraint

     

    5- If you want to minimize the makespan, you do not need to introduce a decision variable for that. You can just minimize the "max" of the last operations end times.

     

    In short, if your problem is similar to the classical permutation flow-shop problem, you should look at the distributed example 'cpoptimizer/examples/src/cpp/sched_pflowshop.cpp'. It is exactly about this problem.

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: CP permutation constraint

    Posted Tue October 09, 2018 03:53 PM

    Originally posted by: imanopl


    Hi Phillipe,

     

    Thanks for your response. If we want to run the model only once, it is good to just ignore model.end(), however if we want to run model multiple times, such as what we do in benders or some other optimization, then we can not proceed using the current model and each time we need to run a new model. I tried this and I tried to run for different values of processing time using a loop and ignored model.end but I received an error:
    Unhandled exception at 0x00007FFEBBD5A1C8 in Template - CPlex 12.6.2 in Visual Studio.exe: Microsoft C++ exception: IloCP::MultipleObjException at memory location 0x000000566557F428

    Even though I used env.end()

    I attached the text files including the code and the input files that I used.

    Is the second workaround you told me is not what I am doing now?

    About the points you made:

    1. Actually I am adding this for having permutation flowshop but it seems it is not working.

    2. My problem is permutation flowshop

    3. When I use this one, the answer changes and reduced. I do not know why.

        IloIntervalVarArray sizeofoperation(env, Jobs);

         for (IloInt k = 0; k < Jobs; k++) {

             model.add(IloNoOverlap(env, sizeofoperation));

         }

     

    4. OK, this is what I am trying to do but what can I do with the issue of model.end

    5. Not sure how I can manage to write this constraint.

     

    Regards,

    Iman

     

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 7.  Re: CP permutation constraint

    Posted Wed October 10, 2018 02:39 AM

    Originally posted by: PhilippeLaborie


    For the use of model.end(), I think that you can easily, at each run, create a new IloEnv and call IloEnv::end() . So something like:

    iteration(...) {
      IloEnv env;
      IloModel model(env);
      // Create model
      //...
      IloCP cp(model);
      if (cp.solve()) {
        //...
      }
      env.end();
    }
    

    1. I think that beside the problem of model.end(), it is not working because your model is not correct. You are at least missing the sameSequence constraint between the sequences *on the machines*.

    As I already said, there is a mistake in your model. For instance here, you iterate on the machines ( for (int i = 0; i < Machines; i++) ), but within the loop, in Startvar[i] you access the array Startvar whose first index i is a Job:

    IloIntervalSequenceVarArray seqs(env, Machines);
    for (int i = 0; i < Machines; i++) {
      seqs[i] = IloIntervalSequenceVar(env, Startvar[i]);
      model.add(IloNoOverlap(env, seqs[i]));
      if (0 < i) {
        model.add(IloSameSequence(env, seqs[0], seqs[i]));
      }
    }
    
    

    And the array Startvar is defined as:

    IloIntervalVarArray2 Startvar(env, Jobs);
    for (int j = 0; j < Jobs; j++)
      Startvar[j] = IloIntervalVarArray(env, Machines);
    

    Try with an instance such that Resources > Jobs and I'm pretty sure it will crash!

    4. No, your model is different from the delivered permutation flowshop problem, as I explained

    5. Look at the delivered permutation flowshop problem. In this problem, the makespan objective function is formulated as an integer expression (IloMax), it is not a decision variable.

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 8.  Re: CP permutation constraint

    Posted Mon October 15, 2018 08:29 PM

    Originally posted by: imanopl


    Thanks, I used the array and it worked after I stopped using model.end and redefining startvar.

     


    #DecisionOptimization
    #OPLusingCPOptimizer