Domain Restriction of a sum

Problems with syntax of GAMS
Post Reply
adramalama
User
User
Posts: 5
Joined: 2 years ago

Domain Restriction of a sum

Post by adramalama »

I've some model which i would like to implement in GAMS.
Unfortunately there are some issues regarding the domain restriction of a sum.
This is the Equation, which i would like to implement:

Image

I've tried: sum((j$(ord(j) ne ord(i))),x1(j,i,g)) but i get Error 8,10,119,149.

If anyone could help me with this one i would really appreciate it :)
Attachments
Bildschirmfoto 2021-12-10 um 23.07.34 (2).png
Bildschirmfoto 2021-12-10 um 23.07.34 (2).png (8.34 KiB) Viewed 3371 times
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain Restriction of a sum

Post by abhosekar »

Please attach a minimal code that can reproduce this issue. Error 8 looks like missing parenthesis which must be coming from some other statement in the code. There is nothing too suspicious about the summation you mention (what you have tried). But it would be good to know how you define j (is a subset of i, alias for i?) How you define your equation (you should define it over i). If the sets are defined correctly, the summation looks fine to me.

The errors seem to be coming from some other part of the code (you can click on the error and it will take the cursor to the statement where the error occurs). It will also be helpful if you mention error message instead of codes as users don't remember the error codes.

Please also look at the documentation where common compilation errors and ways to fix those are mentioned: https://www.gams.com/33/docs/UG_FixingE ... ors_ErrorH
- Atharv
adramalama
User
User
Posts: 5
Joined: 2 years ago

Re: Domain Restriction of a sum

Post by adramalama »

Thank you for your prompt reply!
Here is my code, i've attached also a screenshot of the whole model:

set i 'inbound trucks ' / 1*10 /;
Alias (i,j);

Set
i0 'i=0 in res2' / 0 /
n1 'n+1 inbound for res3' / 1*11 /
o 'outbound' / 1*5 /
g 'inbound doors available for processing inbound' / 1*2 /
;

Parameter
p(i) 'processing time for unloading inbound truck i'
d(o) 'departure time of outbound truck o'
t(g,o) 'transshipment time from inbound dock g to the dock where outbound truck o is processed'
w(i,o) 'weight, e.g., the number of products, of a shipment delivered by inbound truck i dedicated to outbound truck o'
sum_i '# of i'
sum_g '# of g';

sum_i = card (i);
sum_g = card (g);

p(i) = normal(30,6);
d(o) = (sum(i,p(i))/sum_g)*uniform(0.5,0.9);
w(i,o) = uniform (1,10);
t(g,o) = uniform (1,10);

Variable
Z 'ZFW';

Positive Variable
C(i) 'completion time of inbound i';

Binary Variable
x1(j,i,g) 'res1'

x2(i0,j,g) 'binary variable: 1, if inbound i is processed first at gate g; 0, otherwise'

x3(i,j,g) 'res3'

y(i,o) 'ZF';

Scalar BIGM;
BIGM = sum(i, p(i))+ smax((g,o),t(g,o));

Equation
ZF 'ZF'
res1 (i,g) 'every truck is scheduled'
res2 (g) 'one truck per gate'
res3 (i,g) ''
res4 (i,j,g) ''
res5 (i,o) ''
;

ZF .. Z =e= sum((i,o), w(i,o)*y(i,o));

res1(i,g) .. sum(j$(ord(j) ne ord(i)) ,x1(j,i,g)) =e= 1 ;

res2(g) .. sum((j,i0),x2(i0,j,g)) =l= 1 ;

res3(i,g) .. sum(j$(ord(j) ne ord(i)), x3(i,j,g)) =e= sum(j$(ord(j) ne ord(i)),x1(j,i,g)) ;

res4(i,j,g) .. C(i) =g= C(j) +p(i) - BIGM * (1-x1(j,i,g)) ;

res5(i,o) .. y(i,o) * BIGM =g= C(i)-d(o)+sum(g,t(g,o))*sum((j$(ord(j) ne ord(i))),x1(j,i,g)) ;

Greetings
Lukas
Image
Attachments
Bildschirmfoto 2021-12-11 um 08.34.32 (2).png
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain Restriction of a sum

Post by abhosekar »

As I suspected, the error was coming from a different equation res5 where some unnecessary parenthesis were present. If you replace res5 with the following equation, you will not see any compilation errors:

Code: Select all

res5(i,o) .. y(i,o) * BIGM =g= C(i)-d(o)+sum(g,t(g,o)*sum(j$(ord(j) ne ord(i)),x1(j,i,g))) ;
Please note that the inner sum over j is inside the outer sum over g. If you don't do that, x1^g has no meaning.

Please use code blocks to provide codes for future communication as I have used.

- Atharv
adramalama
User
User
Posts: 5
Joined: 2 years ago

Re: Domain Restriction of a sum

Post by adramalama »

Again, thank you very much for your help!

But if i do it like that, i get the errors that 'set is under control already':

Code: Select all

res5(i,o) .. y(i,o) * BIGM =g= C(i)-d(o)+sum(g,t(g,o)*sum((g,j$(ord(j) ne ord(i))),x1(j,i,g))) ;

- Lukas
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain Restriction of a sum

Post by abhosekar »

The equation I showed and the one you show are different. I don’t sum over g twice.

- Atharv
adramalama
User
User
Posts: 5
Joined: 2 years ago

Re: Domain Restriction of a sum

Post by adramalama »

I'm sorry, i got an infeasible solution and thought naively that would help.
In restriction1 there is a sum first over g and then over j but when i code it in that way below i get an error message: 'expected ',' and ')' and '=l=# or similar. I thought when you've multiple sums that would be the right syntax?

Code: Select all

res1(i) .. sum((g,j$ord(j) ne ord(i)),x1(j,i,g)) =e= 1 ;
-Lukas
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain Restriction of a sum

Post by abhosekar »

Please follow GAMS tutorials and read about these compilation errors. Again in this equation you have incorrect parentheses.
Besides, while declaring equation, you have res1(i,g) but while defining you have res1(i).

In this case, you are having trouble defining a sum over two sets. You can check it here: https://www.gams.com/latest/docs/UG_Par ... ons_22_sum

Second, there is some confusion about how $ conditions are written, what is the correct syntax etc. You can check it here: https://www.gams.com/latest/docs/UG_Con ... rCondition

Here is your fixed equation:

Code: Select all

res1(i) .. sum((g,j)$(ord(j) ne ord(i)),x1(j,i,g)) =e= 1 ;
- Atharv
adramalama
User
User
Posts: 5
Joined: 2 years ago

Re: Domain Restriction of a sum

Post by adramalama »

Thank you for your help, i really appreciate it!
I've corrected my mistakes and i get no compilation error, thank you!
Unfortunately, the solution is still infeasible. I've tried to set the variable C to zero as described in the model but that didn't worked either.
I've asked you very much, but again, do you probably know why i still can't get a solution?

Code: Select all

$title TSFD

$onText
Beschreibung 
$offText

set i   'inbound trucks '                                           / 1*5 /;
Alias (i,j);

Set
    i0  'i=0 in res2'                                               / 0 /
    n1  'n+1 inbound trucks; für res3 benötigt'                     / 1*6 /
    o   'outbound trucks'                                           / 1*2  /
    g   'inbound doors available for processing inbound trucks'     / 1*2  /
    ;

    display i0;
Parameter
   p(i)     'processing time for unloading inbound truck i'
   d(o)     'departure time of outbound truck o'
   t(g,o)   'transshipment time from inbound dock g to the dock where outbound truck o is processed'
   w(i,o)   'weight, e.g., the number of products, of a shipment delivered by inbound truck i dedicated to outbound truck o'
   sum_i    'Anzahl der Elemente in der Indexmenge i'
   sum_g    'Anzahl der Elemente in der Indexmenge g'
  ;

    sum_i = card (i);
    sum_g = card (g);
    
    
    
$onText
generate pseudorandom numbers for p(i), d(o), t(g,o), w(i,o)
execseed = 1 + gmillised(jnow);
$offText
   
    p(i) = normal(30,6);
    d(o) = (sum(i,p(i))/sum_g)*uniform(0.5,0.9);
    w(i,o) = uniform (1,10);
    t(g,o) = uniform (1,10);
    
    display p,d,w,t;
    
Variable
    Z       'ZFW'
    C(i0)   'starting C';
  
Positive Variable
    C(i)    'completion time of inbound i';
    

Binary Variable

    x1(j,i,g)    'res1'

    x2(i0,j,g)   'binary variable: 1, if inbound i is processed first at gate g; 0, otherwise'

    x3(i,j,g)    'res3'
 
    y(i,o)      'ZF';     
 


Scalar BIGM;
BIGM = sum(i, p(i))+ smax((g,o),t(g,o));

display BIGM;


Equation
    ZF              'Zielfunktion'
    res1 (i)        'every truck is schedulded'
    res2 (g)        'one truck per gate at the same time'
    res3 (i,g)      ''
    res4 (i,j,g)    ''
    res5 (i,o)      ''
    res6 (i0)
;

ZF .. Z =e= sum((i,o), w(i,o)*y(i,o));

res1(i) .. sum((g,j)$(ord(j) ne ord(i)),x1(j,i,g)) =e= 1 ;

res2(g) .. sum((j,i0),x2(i0,j,g)) =l= 1 ;

res3(i,g) .. sum(j$(ord(j) ne ord(i)), x1(j,i,g)) =e= sum(j$(ord(j) ne ord(i)),x3(i,j,g)) ;

res4(i,j,g) .. C(i) =g= C(j) +p(i) - BIGM * (1-x1(j,i,g)) ;

res5(i,o) .. y(i,o) * BIGM =g= C(i)-d(o)+sum(g,t(g,o)*sum(j$(ord(j) ne ord(i)),x1(j,i,g))) ;

res6(i0).. C(i0) =e= 0;

$onText
option optCr = 0.0, optCa = 0.0;
$offText

Model TSFD / all /;


solve TSFD using mip minimizing Z;

display z.l;
-Lukas
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: Domain Restriction of a sum

Post by abhosekar »

Sorry but your model doesn't compile without errors. Besides the model you show in the picture and the one you attach are two different models.

If you see summation over j in the model, you will see that it is I U {0} or I U {n + 1}. You are not doing this anywhere in the model.
You are defining C over i0 as well as i. When you are writing a mathematical model, all small details matter and any change can make the model infeasible.
There is n in the model you attach but there is no n in your GAMS code. So why is this difference? You can try to make sense of your model only when you have modelled "exactly" what you wanted to model.

In this particular case, you need to read about subsets. https://www.gams.com/latest/docs/UG_Set ... on_Subsets
Because you alias j with i but if in the model, j is I U {0} or I U anything, it is "more than" i. So you really need to create a set that is a superset of i.

I can't suggest everything but this should be a good starting point.

- Atharv
Post Reply