Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Problem with indexing in Cplex

  • 1.  Problem with indexing in Cplex

    Posted 07/05/12 02:55 PM

    Originally posted by: SystemAdmin


    Hi,

    I've been trying to implement a model in CPLEX for a while now. But it seems I just can't get the indices right...
    I get the error msg that "range is not available for scripting". Whatever that means...
    Basically what I want to model is this: There is a variable called "stages" It has the index j and j runs from 1 to the number of stages (in my case that's 3, I defined it as Nbstages). In one of the constraints I need to access j-1, so I only want to have this constraint for j in 2..Nbstages (as j is only defined from 1 to Nbstages and j-1 would be 0 for j=1).

    I would be very very grateful for some help on this topic. This is the code I came up with:

    int Nbstages = ...;
    range stages = 1..Nbstages;
    int LeadTime stages=...;
    int MaxServiceTime stages=...;

    float safetyfactor stages=...;
    float StandardDeviation stages=...;
    int MeanDmd stages=...;
    float StageCost stages=...;
    float HoldingCostRate stages=...;
    float HoldingCost stages=...;;

    dvar int+ OutboundServiceTime stages;
    dvar int+ InboundServiceTime stages;

    minimize
    sum (j in stages)
    (HoldingCost[j]*(MeanDmd[j]*(InboundServiceTime[j]+LeadTime[j]-OutboundServiceTime[j]))
    +(safetyfactor[j]*StandardDeviation[j]*/*sqrt*/(InboundServiceTime[j]+LeadTime[j]-OutboundServiceTime[j])));
    subject to {
    forall (j in stages)
    OutboundServiceTime [j]-InboundServiceTime[j] <= LeadTime[j];
    forall (j in stages)
    InboundServiceTime [j]-OutboundServiceTimej-1 >= 0;
    //forall (j in stages)
    OutboundServiceTime[0]==0;
    forall (j in stages)
    OutboundServiceTime [j]<= MaxServiceTime [j];
    forall (j in stages)
    InboundServiceTime [j]>=0;
    forall (j in stages)
    OutboundServiceTime [j]>=0;
    };

    Thanks a lot,
    Carolin
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Problem with indexing in Cplex

    Posted 07/06/12 08:09 AM
    Hi,

    could you attach your model and data file?

    regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Problem with indexing in Cplex

    Posted 07/06/12 12:38 PM

    Originally posted by: SystemAdmin


    Of course. Thanks so much for taking a look!

    Some of the data in the data file is not the real data I will work with later on. I still have to figure out a way how to aggregate some of my initial data to be able to use it. I just entered some placeholders to see if I could get the model to work.

    Best,
    carolin
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Problem with indexing in Cplex

    Posted 07/06/12 12:39 PM

    Originally posted by: SystemAdmin


    ...and here's the data file.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Problem with indexing in Cplex

    Posted 07/07/12 04:38 AM
    Hi

    I rewrote your constraints as

    subject to {
        forall (j in stages)
        OutboundServiceTime [j]-InboundServiceTime[j] <= LeadTime[j];
      forall (j in stages:j!=1)
        InboundServiceTime [j]-OutboundServiceTime[j-1] >= 0;
      //forall (j in stages)
        //OutboundServiceTime[0]==0;
      forall (j in stages)
        OutboundServiceTime [j]<= MaxServiceTime [j];
      forall (j in stages)
        InboundServiceTime [j]>=0;
      forall (j in stages)
        OutboundServiceTime [j]>=0;
        
      };
    


    regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Problem with indexing in Cplex

    Posted 07/07/12 04:41 AM
    PS:

    You can use range in script as in

    execute
      {
       for(var i in stages) writeln(i); 
      }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Problem with indexing in Cplex

    Posted 07/08/12 02:39 PM

    Originally posted by: SystemAdmin


    Thank you so much for your help!
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Problem with indexing in Cplex

    Posted 07/09/12 01:15 AM

    Originally posted by: SystemAdmin


    Sorry to bother you again, but I have another question. My objective function contains a square root of the sum of some variables. However, this obviously doesn't work that easily. In the beginning I thought it was because of the indexing issue. But it's not.

    This is the objective function:

    minimize
    sum (j in stages)
    (HoldingCost[j]*safetyfactor*StandardDeviation*sqrt(InboundServiceTime[j]+LeadTime[j]-OutboundServiceTime[j]));

    I tried to find out the confinements for using the sqrt function. The only thing I found was that Cplex can only extract roots from float values. I changed the integer variables to floats but that didn't help.

    Is there another constraint for the usage of square roots in an objective function?

    Thanks,
    Carolin
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: Problem with indexing in Cplex

    Posted 07/09/12 03:35 AM
    Hi,

    since your variables are integer, why don't use use Constraint Programming?

    In the code, you simply need to do using CP;

    using CP;
     
     
    //{string} stages=...;
     
    int Nbstages = ...;
    range stages = 1..Nbstages;
    


    Regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: Problem with indexing in Cplex

    Posted 07/11/12 09:34 PM

    Originally posted by: SystemAdmin


    Hi,

    thanks again. That was a lot easier than expected. :) The model works perfectly now.
    Just one more question. If I wanted to make a restriction on the possible values of a variable, how would I do that?

    For example I'd like to say that OutboundServiceTime [j] can either be InboundServiceTime [j]+LeadTime[j] or zero. I tried modelling it like this, but it's not working.

    forall (j in stages)
    OutboundServiceTime [j] == [InboundServiceTime[j]+LeadTimej] 0;

    The only information I could find in the documentation is that the domain of variables is denoted with square brackets...

    Thanks,
    Carolin
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: Problem with indexing in Cplex

    Posted 07/12/12 12:42 PM
    Hi

    why don't you do

    (OutboundServiceTime [j] == InboundServiceTime [j]+LeadTime[j] )
    ||
    (OutboundServiceTime [j] == 0)
    


    regards
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: Problem with indexing in Cplex

    Posted 07/21/12 02:22 AM

    Originally posted by: SystemAdmin


    Thanks so much. It worked. :)

    Unfortunately I have a new problem. I tried to implement a second model but again I have problems with the index ranges... This model is a bit more complicated as it contains two sets of indexes and I have to sum up over both of them.
    Also, I sometimes want to include variables in the index ranges. So for example I might want to sum up over all preiods starting with the current period and ending with the period that equals the transportation lead time of the stage. I don't really know how to do this in OPL...

    I hope you can help me again. I would be so grateful!
    Thanks,
    Carolin
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 13.  Re: Problem with indexing in Cplex

    Posted 07/21/12 02:23 AM

    Originally posted by: SystemAdmin


    data file
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: Problem with indexing in Cplex

    Posted 07/21/12 05:18 PM

    Originally posted by: SystemAdmin


    new question added
    #DecisionOptimization
    #OPLusingCPLEXOptimizer