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 interval (maybe)

  • 1.  Problem with interval (maybe)

    Posted 06/21/10 06:04 PM

    Originally posted by: rkresnadi


    Hi,
    New to CP. I tried to enforce the precedence on my intervals and I got the following error: "Cannot use type string for <Ordid:string,TaskName:string,Worker:string,Duration:int>."

    Here is my code:
    {string} Ordid = ...; /* this is a query from a database from a table name called tblOrder*/

    /*a tuple from a database from a table name: tbltask*/
    tuple MyTask {
    string Ordid;
    string TaskName;
    string Worker;
    int Duration;
    }

    {MyTask} TaskData = ...;
    int Duration d in TaskData = ...;

    /*define precence*/
    tuple Precedence {
    string pre;
    string post;
    };

    {Precedence} Precedences = ...;

    /*define interval*/
    dvar interval ordid o in Ordid in OrdTime[o]..(maxint) size 10;

    dvar interval itvs o in Ordidt in TaskData size Duration[t];
    /*CONSTRAINTS*/
    subject to
    {
    forall(o in Ordid)
    forall(p in Precedences)
    endBeforeStart(itvs[o]http://p.pre, itvs[o]http://p.post);

    This last line is where it gets the error from. Any idea? Thanks in advance.
    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Problem with interval (maybe)

    Posted 06/22/10 06:06 AM

    Originally posted by: Didier Vidal


    Hello,

    I think that the issue is about the OPL syntax. Here is a trick that would solve it I guess:

    1) Declare a key to the tuple My Task (note: that should be done only if the key uniquely defines a tuple)

    
    tuple MyTask 
    { key string Ordid; string TaskName; string Worker; 
    
    int Duration; 
    }
    


    Once this is done, it is possible to retreive an element of a set of MyTask, just by an order ID.
    Because the second indexer of itvs is a set of MyTask, this feature can be used.

    2) retrieve a element of the TaskData set for second indexer of itvs, using the
    
    <key>
    
    syntax
    
    endBeforeStart(itvs[o][<p.pre>], itvs[o][<p.post>]);
    


    Hope this helps.

    Didier.
    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Problem with interval (maybe)

    Posted 06/22/10 09:20 AM

    Originally posted by: rkresnadi


    Didier,
    Thank you for your help but the error is still there.

    Do I need to modify the itvs declaration statement?
    also, in my code I have int Duration d in TaskData = ...;. Do i still need this statement? If I don't declare it my itvs code couldn't find the "duration".

    Thanks.
    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: Problem with interval (maybe)

    Posted 06/24/10 05:48 AM

    Originally posted by: Didier Vidal


    Hi,

    Do you mean that you still have the same error message ?
    
    Cannot use type string 
    
    for <Ordid:string,TaskName:string,Worker:string,Duration:int>
    
    ?

    That would be strange because the type of the indexer is no longer a string in the example I gave you (it is a tuple of type MyTask).

    I copied your code example in my environment and it worked well - I didn't have to change the definition of itvs.

    Maybe there is an other issue I didn't see in your code - Can you copy your code statements between the code markups (see plain text help for all markups) - otherwise some characters of your code, such are brackets are transformed into something else (a link in this case) when your message is displayed.

    Regards,

    Didier.
    #CPOptimizer
    #DecisionOptimization


  • 5.  Re: Problem with interval (maybe)

    Posted 06/25/10 05:37 PM

    Originally posted by: rkresnadi


    Didier, I got that problem working now. I must have typed it incorrectly.

    But I got another problem now. There are two key fields in that MyTask tupple: Ordid and TaskName and Cplex didn't like it because it has duplicate records under the Ordid field.

    The data set looks like this:
    Ordid TaskName
    1 Task1
    2 Task2
    etc.

    I may have to restructure the data. I just don't know how to do it yet.
    #CPOptimizer
    #DecisionOptimization


  • 6.  Re: Problem with interval (maybe)

    Posted 06/25/10 05:39 PM

    Originally posted by: rkresnadi


    Sorry, what I meant was:

    The data set looks like this:
    Ordid TaskName
    1 Task1
    1 Task2
    etc.
    #CPOptimizer
    #DecisionOptimization


  • 7.  Re: Problem with interval (maybe)

    Posted 06/28/10 01:37 PM

    Originally posted by: Didier Vidal


    Then, if you need two fields to make a key and have only one in the precedence, it depends how you need to create the precedence constraints. For instance, if the preceddence constraints apply only for the tasks of a given order, it could look like (I assume that the string in a precedence is a taskname):

    
    forall(o in ordid) forall (p in precedences) endBeforeStart(itvs[o][<o, p.pre>], itvs[o][<o, p.post>]);
    


    But since taskData carries the orderId, it seems redundant to create the first indexer for the itvs intervals.

    Didier.
    #CPOptimizer
    #DecisionOptimization


  • 8.  Re: Problem with interval (maybe)

    Posted 06/30/10 04:32 PM

    Originally posted by: rkresnadi


    Thanks Didier. It works fine without error, but as soon as I run the model I got another error: data element "Duration" not defined.

    By the way, I created another tuple called OrderData. It is like this (since I thought this is the proper way):

    /*From tblOrder*/
    tuple MyOrder {
    key string Ordid;
    int OrdTime;
    int EarlyArr2;
    int LateArr2;
    }
    {MyOrder} OrderData = ...;

    Now OPL does not like these syntax:
    dvar interval ordid OrderData in (o in OrderData)http://<o.OrdTime>..(maxint) size 10;

    dvar interval itvs TaskData size (t in TaskData)http://<t.Duration>;

    by the way, how do you make the editor not to automatically create a URL?
    #CPOptimizer
    #DecisionOptimization


  • 9.  Re: Problem with interval (maybe)

    Posted 07/01/10 04:48 AM

    Originally posted by: Didier Vidal


    I cannot read your code - you didn't use the code markups and your text has been transformed.

    I guess that the error is that you didn't declare the iteration variable (o or t) at the right place. It should be

    
    dvar interval ordid [o in OrderData] in <o.OrdTime>..(maxint) size 10;
    

    Didier,
    #CPOptimizer
    #DecisionOptimization


  • 10.  Re: Problem with interval (maybe)

    Posted 07/01/10 03:51 PM

    Originally posted by: rkresnadi


    
    it works, and now how to declare the itvs variable i tried to use similar method and didn
    't work:   dvar interval itvs [t in TaskData] size [<t.Duration>];   now both interval ordid and itvs got errors: Formal parameter 
    "t" not supported in 
    
    this context, can not have iteration expression.
    

    #CPOptimizer
    #DecisionOptimization


  • 11.  Re: Problem with interval (maybe)

    Posted 07/02/10 08:19 AM

    Originally posted by: Didier Vidal


    I believe this is because the keyword size expects an integer (that will be evaluated for each interval). Not a tuple.

    What about that syntax ?

    
    dvar interval itvs [t in TaskData] size t.Duration;
    


    Didier.
    #CPOptimizer
    #DecisionOptimization


  • 12.  Re: Problem with interval (maybe)

    Posted 07/02/10 10:23 AM

    Originally posted by: rkresnadi


    
    OK, I changed those two inverval variables like you said:   dvar interval ordid [o in OrderData] in o.OrdTime..(maxint) size 10; dvar interval itvs [t in TaskData] size t.Duration;   Error is gone but I got another error (Error: not an array type) 
    
    for my constraint:   forall(t in TaskData) forall(p in Precedences) endBeforeStart(itvs[t][<p.pre>], itvs[t][<p.post>]);
    

    #CPOptimizer
    #DecisionOptimization


  • 13.  Re: Problem with interval (maybe)

    Posted 07/05/10 08:42 AM

    Originally posted by: Didier Vidal


    This is probably because in your constraint you consider itvs as a 2 dimension array while you declared it as a one dimension array.

    Didier.
    #CPOptimizer
    #DecisionOptimization


  • 14.  Re: Problem with interval (maybe)

    Posted 07/09/10 09:15 AM

    Originally posted by: rkresnadi


    
    yes but it did not work when i typed like 
    
    this:   forall(t in TaskData) forall(p in Precedences) endBeforeStart(itvs[<p.pre>], itvs[<p.post>]);   the error says: 
    "Cannot use type string as index in {MyTask}"
    

    #CPOptimizer
    #DecisionOptimization


  • 15.  Re: Problem with interval (maybe)

    Posted 07/09/10 12:37 PM

    Originally posted by: Didier Vidal


    This is strange... The code below, that I took from your original example (I added dummy inline data) works for me with OPL 6.3

    
    using CP;   
    {string
    } Ordid = 
    {
    "o1"
    }; 
    /* this is a query from a database from a table name called tblOrder*/   
    /*a tuple from a database from a table name: tbltask*/ tuple MyTask 
    { key string Ordid; key string TaskName; string Worker; 
    
    int Duration; 
    }   
    {MyTask
    } TaskData = 
    { < 
    /*comment*/ 
    "o1",
    "t1",
    "w1",10>, < 
    /*comment*/ 
    "o1",
    "t2",
    "w1",20> 
    }; 
    
    int Duration [d in TaskData] = d.Duration;   
    /*define precence*/ tuple Precedence 
    { string pre; string post; 
    };   
    {Precedence
    } Precedences = 
    { < 
    /*comment*/ 
    "t1",
    "t2">  
    };   
    
    int OrdTime[o in Ordid] = (o==
    "o1") ? 3 : 1;   
    /*define interval*/ dvar interval ordid [o in Ordid] in OrdTime[o]..(maxint) size 10;   dvar interval itvs [t in TaskData] size Duration[t];   
    /*CONSTRAINTS*/ subject to 
    { forall(o in Ordid) forall(p in Precedences) endBeforeStart(itvs[<o,p.pre>], itvs[<o,p.post>]); 
    }
    

    #CPOptimizer
    #DecisionOptimization


  • 16.  Re: Problem with interval (maybe)

    Posted 07/13/10 11:08 AM

    Originally posted by: rkresnadi


    Thank you Didier, I think the syntax is fine now. But now when I tried to solve I got "CP cannot extract expression" error. I think the error comes from the objective function. See all my updated code below:
    
    using CP; 
    /*From tblOrder*/ tuple MyOrder 
    { key string Ordid; 
    
    int OrdTime; 
    
    int EarlyArr2; 
    
    int LateArr2; 
    } 
    {MyOrder
    } OrderData = ...;   
    /*From tblTask*/ tuple MyTask 
    { key string Ordid; key string TaskName; string Worker; 
    
    int Duration; 
    }   
    {MyTask
    } TaskData = ...;   tuple Precedence 
    { string pre; string post; 
    };   
    {Precedence
    } Precedences = ...;   dvar interval ordid [o in OrderData] in o.OrdTime..(maxint) size 10; dvar interval itvs [t in TaskData] size t.Duration;     
    /*OBJECTIVE*/ minimize sum(o in OrderData) (maxl(0, endOf(ordid[o]) - o.LateArr2)) + sum(o in OrderData) (maxl(0, o.EarlyArr2 - endOf(ordid[o])));   
    /*CONSTRAINTS*/ subject to 
    { forall(o in OrderData) forall(p in Precedences) endBeforeStart(itvs[<o.Ordid,p.pre>], itvs[<o.Ordid,p.post>]); 
    }
    

    #CPOptimizer
    #DecisionOptimization


  • 17.  Re: Problem with interval (maybe)

    Posted 07/13/10 01:26 PM

    Originally posted by: ChrisBr


    Hello,

    You should try to define your interval vars like this:

    
    
    
    int maxinterval = (maxint div 2)-1; dvar interval ordid [o in OrderData] in o.OrdTime..(maxinterval) size 10;
    

    following the OPL documentation:

    
    dvar interval a [optional[(IsOptional)]] [in StartMin..EndMax] [size SZ | in SZMin .. SZMax] [intensity F]
    

    Where:
    
    
    
    int IsOptional, StartMin, EndMax, SZ, SZMin, SZMax; stepFunction F; -maxint/2 + 1 <= StartMin <= maxint/2 - 1 -maxint/2 + 1 <= EndMax <= maxint/2 - 1 0 <= SZ <= maxint/2 - 1 0 <= SZMin <= maxint/2 - 1 0 <= SZMax <= maxint/2 - 1
    


    Hope this helps,

    Chris
    #CPOptimizer
    #DecisionOptimization


  • 18.  Re: Problem with interval (maybe)

    Posted 07/13/10 02:49 PM

    Originally posted by: rkresnadi


    Thank you. It works now.
    #CPOptimizer
    #DecisionOptimization