Decision Optimization

Decision Optimization

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


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

CPLEX syntax errors

  • 1.  CPLEX syntax errors

    Posted 07/18/18 01:55 PM

    Originally posted by: Arnautarrago


    I have some errors regarding a CPLEX code that I'm trying to write. The errors are on the constraints 2 and 3. Thank you in advance.

    Here is the code:

     

    using CP;
     
    // NETWORK+PARAMETERS
     
    int trucks=...; // set of trucks
    range truck= 1..trucks;
     
    int capacity [truck]=...; // capacity of a truck
     
    tuple nodeinfo {
    string name; // name of a node
    int starttime; // available start time 
    int endtime; // available end time
    float demand; // demand from a node
    }
     
    {nodeinfo} departurenode=...; //size=4
    {nodeinfo} arrivalnode=...; //size=4
    {nodeinfo} startingnode=...; //size=4
     
    // OPTION 2: vector of nodes
    //each node has tuple structure nodeinfo
    //length of the vector is length of dataset
    //read data from dataset
    //void* node = new {nodeinfo} [k];
     
    tuple arc {
    nodeinfo departurenode; //departure node of an arc
    nodeinfo arrivalnode; // arrival node of an arc
    nodeinfo startingnode; // starting node of an arc
    int traveltime; // travel time of an arc
    }
     
    {arc} arcs=...; //number of arcs (24)
     
    float cost [arcs][truck]=...; //cost of using an arc by a truck 
     
    // option2: int arc[i in departurenode,j in arrivalnode,k in startingnode]=...; //arcs (size=24)
    // how can i create a setof arcs taking into account the info from each arc??
     
    // VARIABLES
     
    dvar boolean x [arcs,truck]; // 1 if truck uses the arc, 0 otherwise (array of size 24x7)
    dvar int+ arrivaltime [arrivalnode,truck]; //arrival time of a truck at a node (array size of 4x7)
     
     
    // OBJECTIVE FUNCTION 
     
    dexpr float totalcost = 
    sum (a in arcs, t in truck) x [a,t] * cost [a,t];
     
    minimize totalcost;
     
    // CONSTRAINTS
     
    subject to {
     
    forall (n in arrivalnode,t in truck) n.starttime<=arrivaltime[n][t]<=n.endtime; //arrivaltime has to be inbetween the working time zone of a node
     
    forall (n in arrivalnode, a in arcs, t in truck) n.demand= sum (x [a,t] * capacity [t]); // the demand for each node has to be equal to the sum of the 
        //arcs chosen multiplied by the capacity of the truck (CONSTRAINT 2)
     
    forall (n in arrivalnode,t in truck, a in arcs) x [a,t] * (arrivaltime [n][t] + a.traveltime) <= arrivaltime [n][t]; // the arrival time of a truck to a node i + the travel time of this truck to a node j must be equal or less than the arrival time to a node j (CONSTRAINT 3)
    };
     
    execute {
      writeln(arcs);
    };
     
    1down votefa
    1down vote

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: CPLEX syntax errors

    Posted 07/18/18 02:44 PM

    Hi

    forall (n in arrivalnode)
    n.demand== sum (a in arcs,t in truck)(x [a,t] * capacity [t]);
    // the demand for each node has to be equal to the sum of the
        //arcs chosen multiplied by the capacity of the truck (CONSTRAINT 2)

    should work better

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: CPLEX syntax errors

    Posted 07/18/18 07:43 PM

    Originally posted by: Arnautarrago


    Hi Alex,

    so I understand from your message that the only error was on the constraint 2 right? When I write the constraint 3 in CPLEX apparently I don't get any problems on the syntax but I don't know if the way I wrote the constraint applies for what I really want to describe.

     

    CONSTRAINT 3

    forall (n in arrivalnode,t in truck, a in arcs) x [a,t] * (arrivaltime [n][t] + a.traveltime) <= arrivaltime [n][t]; // the arrival time of a truck to a node i + the travel time of this truck to a node j must be equal or less than the arrival time to a node j (that's what I want to describe)

     

    Thank you in advance.

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: CPLEX syntax errors

    Posted 07/19/18 03:32 AM

    Your third constraint is ok as far as syntax is concerned but is strange anyway.

    x [a,t] * (arrivaltime [n][t] + a.traveltime) <= arrivaltime [n][t];

    leads to x == 0 since arrivaltime [n][t] + a.traveltime is greater than arrivaltime [n][t] !

    Don t you want to write

    forall (n in arrivalnode,t in truck, a in arcs:a.arrivalnode==n)
    (x [a,t]==1) => ((arrivaltime [a.departurenode][t] + a.traveltime) <= arrivaltime [n][t]);

    ?

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: CPLEX syntax errors

    Posted 07/19/18 09:30 AM

    Originally posted by: Arnautarrago


    Thanks, that's exactly what i wanted to describe. By the way, I don't know why but when I'm trying to run the code i get this message (file attached).

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: CPLEX syntax errors



  • 7.  Re: CPLEX syntax errors

    Posted 07/19/18 12:06 PM

    Originally posted by: Arnautarrago


    Hi Alex, 

    Unfortunately when I run the code i don't get any information about conflicts nor relaxations, so that's why i have no idea where the problem comes from. 

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: CPLEX syntax errors

    Posted 07/19/18 12:10 PM

    Hi,

    because you need to label your constraints:

    For instance

    forall (n in arrivalnode,t in truck) ct1:n.starttime<=arrivaltime[n][t]<=n.endtime; //arrivaltime has to be inbetween the working time zone of a node
     
    forall (n in arrivalnode)
    ct2:n.demand== sum (a in arcs,t in truck)(x [a,t] * capacity [t]);
    // the demand for each node has to be equal to the sum of the
        //arcs chosen multiplied by the capacity of the truck (CONSTRAINT 2)

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: CPLEX syntax errors

    Posted 07/20/18 08:58 AM

    Originally posted by: Arnautarrago


    Thank you Alex!! I could finally get a solution. Nevertheless I think that i didn't describe the problem properly because only the node D meets the demand constraint, whereas node C doesn't. 

    Apart from that I'm having some problems trying to analyse the solution, so I want to know if there's a site with some examples where I can get used to read properly the engine's registry (i didn't find nothing on the CPLEX manual). 

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: CPLEX syntax errors



  • 11.  Re: CPLEX syntax errors

    Posted 07/20/18 09:50 AM

    Originally posted by: Arnautarrago


    Hi Alex! 

    For instance I've had a look at some examples but there are no explanations about the engine's registry. For instance, in my model I need to know the sequence of each trucks (that means: at what node do they start and the arcs they have chosen ordered along a time space). Unfortunately, I'm not able to read this sequence on the engine's registry so that's why I need some help for tackling this problem. 

    By the way, in one CPLEX example i got this message: 

    Exception from IBM ILOG CPLEX: CPLEX Error  1016: Promotional version. Problem size limits exceeded.-> 

    What does that mean?

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: CPLEX syntax errors

    Posted 07/20/18 09:56 AM

    Hi,

    that means you use the free Community edition that is limited

    You should either move to a free academic version if you re a student or a researcher

    https://twitter.com/AlexFleischer1/status/1017790355905884160

    https://twitter.com/AlexFleischer1/status/1017790355905884160 or to a non free commercial version

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 13.  Re: CPLEX syntax errors

    Posted 07/20/18 10:43 AM

    Originally posted by: Arnautarrago


    Thank you Alex!!

    Regarding the first doubt (truck sequences) and engine's registry do you know how can i get more in deep with it?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: CPLEX syntax errors

    Posted 07/20/18 01:34 PM

    Hi,

    after the subject to block, you could write

    execute {
      writeln("arrivaltime",arrivaltime);
    };

    and then you could get

    arrivaltime [[0 0]
             [0 0]
             [0 0]
             [0 0]]

    regards

     

    PS: At some point do not hesitate to read the documentation

    https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.0/ilog.odms.studio.help/pdf/opl_languser.pdf


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: CPLEX syntax errors

    Posted 07/22/18 01:27 PM

    Originally posted by: Arnautarrago


    Hi Alex! Actually before writing what you said me i already got the structure you mentioned. So for instance i have this:

    x = [[0 0]
                 [0 0]
                 [0 0]
                 [0 1]
                 [1 0]
                 [1 1]
                 [0 1]
                 [0 0]];

    arrivaltime = [[50 150]
                 [50 50]
                 [0 0]
                 [0 100]];

    Then that means that the second truck took first of all the 4th arc, then the 6th arc and finally the 7th? And that the first truck took the 5th arc and then the 6th arc?

    Thank you in advance!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: CPLEX syntax errors

    Posted 07/23/18 04:44 AM

    Hi,

    if you re not 100% sure you may write in the postprocess part:

    tuple arcResult
    {
    arc a;
    int truck;
    }

    {arcResult} arcresults={<a,t> | a in arcs,t in truck:x[a][t]==1};

    execute
    {
    writeln("arcresults = ",arcresults);
    }

     

    regards

     

    PS: You may have a look at the TSP example at CPLEX_Studio128\opl\examples\opl\models\TravelingSalesmanProblem


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: CPLEX syntax errors

    Posted 07/23/18 11:04 AM

    Originally posted by: Arnautarrago


    Hi Alex! Thank you for the tip it worked very well!!

    Regarding the constraint doubt any idea how to write it properly? I already had a look at the TSP example but the structure on that example is different than the one i'm using so i don't know how to extrapolate it. 

    Once again, thank you very much.

    Best regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: CPLEX syntax errors



  • 19.  Re: CPLEX syntax errors

    Posted 07/24/18 12:26 PM

    Originally posted by: Arnautarrago


    Hi Alex,

    Thanks for the info! I don't understand though what do you mean by removing the circuits.

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 20.  Re: CPLEX syntax errors

    Posted 07/24/18 12:30 PM

    Hi,

    in

    // Objective
    minimize sum (<i,j> in Edges) dist[<i,j>]*x[<i,j>];
    subject to {
       
       // Each city is linked with two other cities
       forall (j in Cities)
            sum (<i,j> in Edges) x[<i,j>] + sum (<j,k> in Edges) x[<j,k>] == 2;
            
       // Subtour elimination constraints.
     
       
    forall(k in r)  // all subsets but empty and all
        sum(e in Edges:(e.i in nodes2[k]) && (e.j in nodes2[k])) x[e]<=card(nodes2[k])-1;  

     }    

    I refered to the subtour elimination

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 21.  Re: CPLEX syntax errors

    Posted 07/24/18 12:43 PM

    Originally posted by: Arnautarrago


    Hi,

    Perfect now i understand what you referred to.

    Thanks 

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 22.  Re: CPLEX syntax errors

    Posted 07/22/18 05:34 PM

    Originally posted by: Arnautarrago


    By the way, I realised that when a truck arrives to an arrival node, this arrival node should be the starting node for the next arc but it isn't considered by the truck. Thus I wrote a new constraint in order to take that into account but i don't know how to compare the information of two different arcs (for instance i and j).

    CONSTRAINT 4:

    forall (n in arrivalnode, t in truck, a in arcs:a.arrivalnode==n)
    ct4:(x [a,t]==1) => (a.arrivalnode== a.startingnode); // the arrival node of the arc i should be the starting node of the arc j.

     

    Thank you very much and 

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 23.  Re: CPLEX syntax errors

    Posted 07/25/18 01:56 PM

    Originally posted by: Arnautarrago


    Hi Alex,

     

    I've been working on the code, but i still have some problems to describe it properly. Here they are:

    1) I need to get in the solution a sequence of the arcs chosen by a truck, but the problem is that if i'm using the boolean variable (called 'x' in my code) if the demand of a node is quite high the x matrix that i get in the solution is full up of 1 and the arcs chosen can't be repeated, so it's impossible to satisfy the demand. 

    2) I still have problems to write properly the constraint 4. I had a look at the TSP example but I can't adapt it to my code. As a reminder, this is the constraint 4:

    CONSTRAINT 4:

    forall (n in arrivalnode, t in truck, a in arcs:a.arrivalnode==n)
    ct4:(x [a,t]==1) => (a.arrivalnode== a.startingnode); // the arrival node of the arc i should be the starting node of the arc j.

    3) I also need to write down two other constraints regarding the fact that all the trucks must begin at node A  and return at node A. I've checked the TSP example but there's any info about that.

    Thanks in advance

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 24.  Re: CPLEX syntax errors

    Posted 07/25/18 02:07 PM

    Originally posted by: Arnautarrago


    By the way, I did a couple of trial codes in order to solve the problems i described before but in the first code i can't relate then cost with the sequence of nodes and in the second one i get a conflict on the line 78.

    Find please attached both codes with the data from each one.

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer