0

I have written the following code below in GAMS and it may not be the best in the world but I don't see why it keeps returning the value of the parameter pp(ss,ii) as all zeros when it should be a matrix of exactly 2 ones in each row.

Could you please help me?

set

ii set of bays /1*10/
ss set of solutions /1*45/

Parameters

H Number of bays /10/
C Number of cranes /2/
r Safety Margin /1/
p(ii)
pp(ss,ii)
;

p(ii) = 0;

Scalar i; i=1;
Scalar j; j=i;
Scalar k; k=C;
Scalar qq;
Scalar q;
Scalar kk;
Scalar m;
Scalar n;
Scalar stop /0/;
Scalar stop1 /0/;
Scalar stop2 /0/;
Scalar F /1/;
Scalar k1 /0/;
Scalar k2 /0/;

while ((i <= H-(C-1)*r-1*(C-1)),

                 while((j<=H and stop=0),

                         if (sum(ii, p(ii)) = C,
                              stop = 1;
                            );
                   loop(ii$(ord(ii)=j),
                       p(ii) = 1;
                       );
                       j = j+r+1;
                   );
        if (sum(ii, p(ii)) = C,
         loop((ss,ii)$(ord(ss)=F),
              pp(ss,ii) = p(ii);
             );
           );

        F = F+1;
        qq=0;
        for (q = 1 to H-(C-1)*r-1*(C-1)-1,
             loop (ii$((ord(ii) = q) and (stop1 = 0)),
                    if ((p(ii) = 1) ,
                        qq=1;
                        stop1 = 1;
                       );
                   );
            );

*        if (qq=0,
*                if (p(ii)$(ord(ii)= H-(C-1)*r-1*(C-1))=1 and p(ii)$(ord(ii)=H) = 1,
*                     abort "finished";
*                   );
*
*            );

        k1 = 0;
        k2 = 0;
        if(qq=0,
                loop(ii$(ord(ii)= H-(C-1)*r-1*(C-1)),
                     if (p(ii) = 1,
                         k1 = 1;
                         );
                     );
                loop(ii$(ord(ii)=H),
                     if (p(ii) = 1,
                         k2 = 1;
                         );
                     );
                 abort$(k1 + k2 = 2) "Finished";

           );


        if (j-r-1 = H,
          loop(ii$(ord(ii)=H-(C-k)*r-(C-k)) ,
            if(k = C,
               k = k-1;
            elseif (p(ii)= 1),
                    k = k-1;


               );
              );
             if(k=1,
              loop(ii,
                p(ii)=0;
                   );
                i = i+1;
                j = i;
                k = C;
                else
                     loop(ii$(ord(ii)=H),
                         p(ii) = 0;
                         );
                         m=1;
                         n=0;
                         while((n<k),


                          loop(ii$(ord(ii)=m),
                                if(p(ii) = 1,
                                   n = n+1;
                                   );
                                 m = m+1;
                              );
                             );
                         loop(ii$(ord(ii) = m-1),
                              p(ii) = 0;
                              );
                         loop(ii$(ord(ii) = m),
                              p(ii) = 1;
                             );
                         j = m+r+1;
               );


            else
               loop(ii$(ord(ii) = j-r-1),
                 p(ii)=0;
                   );
               loop(ii$(ord(ii) = j-r),
                 p(ii)= 1;
                   );
                 j=j+1;
            );



        );

Display pp;

1 Answers1

0

On how to hunt errors in rather complex code:

  1. first look up where you assigne values to your variable. You set pp = p; one time.

  2. look up if p is right. Display says p is zero too.

  3. during the pre solve process you can add aditional "display" statements in the middle of you code - to get the values at this points.

  4. This gives you the hint that there are some values in p - but when you put the display statement after if (sum(ii, p(ii)) = C, you wont get any display calls -> sum(p) <> C ?

  5. A closer Look into the p before the if and you'll see that there are three values inside which are more than two.

  6. At the last step its simple to see the error in you code. It's the position of you stop = 1; criteria. Its placed at the beginning of the while loop. The loop will still continue in the current iteration and create a third value.

Solution: Change the criteria to if (sum(ii, p(ii)) = C-1, or put the criteria at the end of the while loop.

Paul G.
  • 632
  • 6
  • 16