Topic
12 replies Latest Post - ‏2014-04-03T02:48:00Z by saeed62s
saeed62s
saeed62s
21 Posts
ACCEPTED ANSWER

Pinned topic Array in CP

‏2014-03-26T20:36:10Z |

When I am running my code i got error in the following line. OPL problem. Does anybody know how we can address the wanted point inside of the array? in MATLAB it is easy but in OPL I have problem with it 

dexpr float sh3[x in routes][n in stops]=sum(i in (1..V[x+3][(m[x]-n)]+1): V[x+3][(m[x]-n)+1]>1 && x<=3)(S[x+3][Q[x+3][i]]*d[x+3][Q[x+3][i]]-W[x+3][i]);

  • GGR
    GGR
    56 Posts
    ACCEPTED ANSWER

    Re: Array in CP

    ‏2014-03-27T11:18:56Z  in response to saeed62s

    Hi Saeed

    Can you be more precise about the problem. In particular what is the error message if any.

    Cheers

     

    • saeed62s
      saeed62s
      21 Posts
      ACCEPTED ANSWER

      Re: Array in CP

      ‏2014-03-28T00:21:14Z  in response to GGR

      Hi GGR

      Thanks for reply. My project is a transportation project, so I have 3 routes in one direction (go) and 3 routes for their opposite direction (return). So I considered 6 routes data as an input data for the code. I have (n) stops for all of my routes.  Now based on my model , I need the following action from my code:  when one stop in one route has been selected then it should read the match stop in the opposite route. So if we consider the route as "x" and "V" as list of stops for that route. We will have(just example):

      dexpr float sh2[x in routes][n in stops] = sum(i in V[x][n]+1..m[x] : V[x][n]>0)(e[x][Q[x][i]]

      for the current route (which is OK), but I need to make  action for the opposite direction, so because of 3 routes of current direction we can have ( the same as above but for opposite direction):

      dexpr float sh3[x in routes][n in stops] = sum(i in V[x+3][(m[x]-n)+1]..m[x] : V[x+3][(m[x]-n)+1]>0 && x<=3)(e[x+3][Q[x+3][i]]

      Where m[x] is maximum number of stops for each route, the reason for adding "3" is since I have 6 routes, the opposite route for each route by adding "3" would be reachable.  For example if my current route is "1" the opposite direction route is "4".

      The problem is OPL cannot read the above code and the objective function as well, I have got this error" CP cannot extract the above (sh3) expression"

      I think I am wrong for writing the code for"sh3" since OPL cannot accept to do any action inside of its array.

      Hope it can help, I also attached my code and its input data maybe it can be clearer.

      If there is any question please let me know.

      Best,

      Attachments

      • GGR
        GGR
        56 Posts
        ACCEPTED ANSWER

        Re: Array in CP

        ‏2014-03-28T11:16:25Z  in response to saeed62s

        Hi saeed62s

        There is clearly an issue at between the interpretation of the expression and passing it to the solver. The message seems to indicate that is is badly formed.

         

        A first thing is there a chance an overflow of the array of variable mat happen. For this I would build the array before the constraints declarations scope  and display the resulting array to check out if everything goes well.

        Anyway, could you send us your .mod file with a .dat data source (not a xls) in order we can have a look.

         

        Cheers

        • saeed62s
          saeed62s
          21 Posts
          ACCEPTED ANSWER

          Re: Array in CP

          ‏2014-03-28T12:21:27Z  in response to GGR

           

        • saeed62s
          saeed62s
          21 Posts
          ACCEPTED ANSWER

          Re: Array in CP

          ‏2014-03-28T12:26:54Z  in response to GGR

          Hi there,

          Yes, Please find the attached file. 

          And the input data is xls file which I have sent it before.

          Thanks 

          Attachments

          • ChrisBr
            ChrisBr
            57 Posts
            ACCEPTED ANSWER

            Re: Array in CP

            ‏2014-03-28T18:54:26Z  in response to saeed62s

            Hello Mahmood,

            Indeed, the problem comes from the expression used to access the array "V".
            Because V is defined using "routes" and "stops" as indexers (in the current case their values are 1..6 and 1..266), you should check that the expressions used to access the array is matching the right ranges.
            (m[x]-n) gives a negative value in the most of cases, so the expression V[x+3][(m[x]-n)] is wrong.

            Regards,

            Chris.
             

            • saeed62s
              saeed62s
              21 Posts
              ACCEPTED ANSWER

              Re: Array in CP

              ‏2014-03-29T21:09:45Z  in response to ChrisBr

               

              Thanks Chris

              You are right, but the problem is how I can define upper limit for "X" since I have 6 routes but I need to use first  3 of them to show the next 3 of them, so  based on the expression of V[x+3] when I use the condition of"X<=3" still it is out of boundary. For (m[x]-n)  , "n" is from stop but when we use V[x][n], the value of" n" is less than m[x] (I mean it should be) since for each route(x) the maximum number of stop is m[x]. You can also see the xls file.

              Cheers

              • ChrisBr
                ChrisBr
                57 Posts
                ACCEPTED ANSWER

                Re: Array in CP

                ‏2014-03-31T12:07:52Z  in response to saeed62s

                Hello Mahmood,

                I think there is a mistake in the declaration of "shi".
                If I replace
                   dexpr float shi [n in stops][x in routes]
                by
                   dexpr float shi [x in routes][n in stops]
                the extraction is OK.

                I hope this helps,

                Chris
                 

                • saeed62s
                  saeed62s
                  21 Posts
                  ACCEPTED ANSWER

                  Re: Array in CP

                  ‏2014-04-01T03:18:46Z  in response to ChrisBr

                   

                  Hi Chris

                  Thanks good spotting! The code is running now, but I have got this answer for the "shi" 'IloMapOutOfBoundException: Out of bounds when accessing element in the map indexed with [1,266] with the following mapIndex:0' 

                    I know this will come from [n in stops] but based on my knowledge if we define [x in routes][n in stops]  first it should go to route and then on that route it should select the stop. For each route the maximum value of stop is m[x], so the value of V[x+3][ (m[x]-n)]+1] should not be negative am I right?

                   

                  and still I am not sure that this expression is correct or not: dexpr float shi [x in routes][n in stops]= sum(i in V[x+3][ (m[x]-n)]+1]..m[x] : V[x+3][ (m[x]-n)]+1]>0&& x<=3) specially the highlighted part

                   

                  One more thing the code is Mixed integer programming am I right? If my constraint is nonlinear such as :

                  forall ( x in routes , n in stops )

                   S[x][n]*W[x][n] == 0;

                  Where S[x][n] as decision variable is binary , either 0,1

                  And W[x][n] as another decision variable is an integer variable

                  Still my model is linear or not?

                  Thanks, I appreciate for the time.

                   

                  • ChrisBr
                    ChrisBr
                    57 Posts
                    ACCEPTED ANSWER

                    Re: Array in CP

                    ‏2014-04-01T16:46:07Z  in response to saeed62s

                    Hello Mahmood,

                    • I'm not sure I understand your first question.

                    You wrote
                        "V[x+3][(m[x]-n)]+1] should not be negative"
                    That's true because there isn't any negative value in V.
                    I think you meant "(m[x]-n)+1 should not be negative", am I right?

                    Actually, if we generate all the possible values of "(m[x]-n)+1" for all the possible pairs <x,n>, we can see that only few of these generated values are valid. But I think that you are sure that only the valid <x,n> are actually used, are you? In this case, this is OK.
                     

                    • About your question:

                    "I am not sure that this expression is correct or not:
                    dexpr float shi [x in routes][n in stops]= sum(i in V[x+3][ (m[x]-n)]+1]..m[x] : V[x+3][ (m[x]-n)]+1]>0&& x<=3)"

                    It's true that despite the fact that the wrong indices' values are avoided by the filter "x<=3", these wrong values appear also in the part in V[x+3].
                    So it would be safer to change the order of the tests in the filter (x<=3 && V[x+3][ (m[x]-n)+1]>0) or better:
                    dexpr float shi [x in routes][n in stops]=
                          ((x<=3) ? sum(i in V[x+3][ (m[x]-n)+1]..m[x] : V[x+3][ (m[x]-n)+1]>0) : 0);

                    • And, no, your model is not linear, but this isn't really a problem for CP.


                    Regards,

                    Chris
                     

                    • saeed62s
                      saeed62s
                      21 Posts
                      ACCEPTED ANSWER

                      Re: Array in CP

                      ‏2014-04-03T02:48:00Z  in response to ChrisBr

                       

                      Hi Chris

                      Thanks for your valuable helps. Now it is much better, but still I should work on it.

                      May I ask that do you have any idea how can I understand Complexity of this optimization model?

                      Best

    • saeed62s
      saeed62s
      21 Posts
      ACCEPTED ANSWER

      Re: Array in CP

      ‏2014-03-28T00:56:07Z  in response to GGR

      I thought maybe this figure could be helpful.