GAMS does not read my code

Problems with modeling
Post Reply
happiniar
User
User
Posts: 5
Joined: 3 months ago

GAMS does not read my code

Post by happiniar » 3 months ago

Dear friends,

I have a problem. Here is my code, but GAMS ignore my second Sum function in "objective" line.

PS: I want to take into consideration the values which are not equal to zero in the Table aa(i,j,i,j) for objective function.

sets i /A,B,C,D/
j /1,2,3,4/;

table d(i,j)
1 2 3 4
A 0.020833 0.083333 0.010417 0.041667
B 0.041667 0.062500 0.020833 0.031250
C 0.062500 0.041667 0.031250 0.020833
D 0.083333 0.020833 0.041667 0.010417 ;

table aa(i,j,i,j)

A.1 A.2 A.3 A.4 B.1 B.2 B.3 B.4 C.1 C.2 C.3 C.4 D.1 D.2 D.3 D.4
A.1 0 0 0 0 0 0.006 0.013 0.020 0 0.014 0.028 0.042 0 0.0216 0.043 0.064
A.2 0 0 0 0 0 0 0.006 0.013 0 0 0.014 0.028 0 0 0.0216 0.0432
A.3 0 0 0 0 0 0 0 0.006 0 0 0 0.014 0 0 0 0.021
A.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B.1 0 0.006 0.013 0.020 0 0 0 0 0 0.010 0.020 0.031 0 0.0308 0.0617 0.092
B.2 0 0 0.006 0.013 0 0 0 0 0 0 0.010 0.020 0 0 0.0308 0.061
B.3 0 0 0 0.006 0 0 0 0 0 0 0 0.010 0 0 0 0.0308
B.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
C.1 0 0.014 0.028 0.042 0.010 0.0209 0.0314 0 0 0 0 0 0 0 0 0
C.2 0 0 0.014 0.028 0 0.0104 0.020 0 0 0 0 0 0 0 0 0
C.3 0 0 0 0.014 0 0 0.020 0 0 0 0 0 0 0 0 0
C.4 0 0 0 0.014 0 0 0.0209 0 0 0 0 0 0 0 0 0
D.1 0 0.021 0.043 0.014 0.030 0.061 0.0209 0 0 0 0 0 0 0 0 0
D.2 0 0 0.021 0.014 0 0.030 0.0209 0 0 0 0 0 0 0 0 0
D.3 0 0 0 0.014 0 0 0.0209 0 0 0 0 0 0 0 0 0
D.4 0 0 0 0.014 0 0 0.0209 0 0 0 0 0 0 0 0 0

;

display aa ;

binary variables x(i,j);

variable z;

equations objective,
con1,
con2;
objective..z=e= Sum((i,j), x(i,j)*d(i,j))+ Sum((i,j), x(i,j)*aa(i,j,i,j));
con1(j)..Sum(i,x(i,j))=e=1;
con2(i)..Sum(j, x(i,j))=e=1

model trial /all/;
solve trial using miqcp minimizing z;

If you give me advices about solving this problem, I would be pretty happy!
Thank you in advance,

Best wishes.

User avatar
dirkse
Moderator
Moderator
Posts: 36
Joined: 2 years ago
Location: Fairfax, VA

Re: GAMS does not read my code

Post by dirkse » 3 months ago

Your problem is similar to that of accessing all elements of a parameter A(i,i). This link explains how an alias (or in your case, two aliases: one for i, another for j) will solve the problem:

User avatar
dirkse
Moderator
Moderator
Posts: 36
Joined: 2 years ago
Location: Fairfax, VA

Re: GAMS does not read my code

Post by dirkse » 3 months ago


happiniar
User
User
Posts: 5
Joined: 3 months ago

Re: GAMS does not read my code

Post by happiniar » 3 months ago

Dear Steve,

Thank you very much for your reply. I will check the link and let you know if I solve it or not.
Thanks again!

All the best

happiniar
User
User
Posts: 5
Joined: 3 months ago

Re: GAMS does not read my code

Post by happiniar » 1 month ago

Dear friends,

I tried the information in this link: https://www.gams.com/latest/docs/UG_Par ... 20diagonal

But still my problem remains..My second Sum function in goal equation is still not read.. I check it from lst file's execution tab.

Here is my revised code:

sets i /A,B,C,D/
j /1,2,3,4/;

alias (i,s)
alias (s,i)
alias (j,t)
alias (t,j) ;

table d(i,j)
1 2 3 4
A 25 100 13 50
B 50 75 25 38
C 75 50 38 25
D 100 25 50 13 ;

table aa(i,j,i,j)

A.1 A.2 A.3 A.4 B.1 B.2 B.3 B.4 C.1 C.2 C.3 C.4 D.1 D.2 D.3 D.4
A.1 0 0 0 0 0 12 24 37 0 26 51 77 0 39 78 117
A.2 0 0 0 0 0 0 12 24 0 0 26 51 0 0 39 78
A.3 0 0 0 0 0 0 0 12 0 0 0 26 0 0 0 39
A.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B.1 0 12 24 37 0 0 0 0 0 19 38 57 0 56 111 167
B.2 0 0 12 24 0 0 0 0 0 0 19 38 0 0 56 111
B.3 0 0 0 12 0 0 0 0 0 0 0 19 0 0 0 56
B.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
C.1 0 26 51 77 0 19 38 57 0 0 0 0 0 0 0 0
C.2 0 0 26 51 0 0 19 38 0 0 0 0 0 0 0 0
C.3 0 0 0 26 0 0 0 19 0 0 0 0 0 0 0 0
C.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
D.1 0 39 78 117 0 56 111 167 0 0 0 0 0 0 0 0
D.2 0 0 39 78 0 0 56 111 0 0 0 0 0 0 0 0
D.3 0 0 0 39 0 0 0 56 0 0 0 0 0 0 0 0
D.4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;

display aa ;

binary variables x(i,j);
binary variables x(s,t);

variable z;

equations goal,
kisit1,
kisit2;

goal..z=e= Sum((i,j), x(i,j)*d(i,j))+ Sum((s,t), x(s,t)*aa(s,t,s,t));
kisit1(t)..Sum(i,x(i,t))=e=1;
kisit2(s)..Sum(j, x(s,j))=e=1

model trial /all/;
solve trial using miqcp minimizing z;


execute_unload "bpe.gdx" z.L x.L
execute 'gdxxrw.exe bpe.gdx var=x.L'
execute 'gdxxrw.exe bpe.gdx var=z.L'

If you help me i will be pretty pretty happy..
thank you in advance,
All the best.

User avatar
bussieck
Moderator
Moderator
Posts: 163
Joined: 2 years ago

Re: GAMS does not read my code

Post by bussieck » 1 month ago

Your program compiles and runs and solves, so if it is not "working" than you must have different expectations for the results. You say "GAMS ignore my second Sum function", not clear what this means. You should look at the equation listing and see if the objective looks as you expect. The sum looks suspicious in your objective: Sum((s,t), x(s,t)*aa(s,t,s,t)) : you only sum over the diagonal of aa, i.e. aa(a,1,a,1)*x(a,1)+aa(a,2,a,2)*x(a,2)+...+aa(d,4,d,4)*x(d,4). You do have off-diagonal entries in aa and aa is only used in this equation, so you probably want to do something different. Also the fact that you declare x twice: binary variables x(i,j);binary variables x(s,t); makes we wonder if you have grasped the concept of GAMS aliases. GAMS resembles very closely the mathematical notation of a problem. If you have this it should be pretty easy to give you advice where you go wrong.

-Michael

happiniar
User
User
Posts: 5
Joined: 3 months ago

Re: GAMS does not read my code

Post by happiniar » 1 month ago

Dear Michael,

Thank you very much for your response. And let me thank you again, you enlightened me deeply. Yes, you are right, my second sum function only sums over the diagonals at table aa. But it is not my aim.
Let me explain my problem a bit more detailed:

Set i (A,B,C,D) represents facilities and set j (1,2,3,4) represents locations. I want to decide the location of facilities. For example A to 1,2,3 or 4? B to 1,2,3 or 4? And so on. So I thought decision variables as binary.
My goal is determining these locations with minimum cost; which is sum of fixed costs and neighbourhood costs.
The first table demonstrates the fixed costs of locating facilities to locations.
The second table is kind a neighborhood cost table. About my second sum function Let me explain: If I locate A to 1 and B to 2 at the same time, then I have to put up with (A,1,B,2) cost demonstrated as 12 at table aa. Here, x(A,1) and x(B,2) must be 1 simultaneously. And it turns into: x(A,1)*x(B,2)*aa(A,1,B,2) and then 1*1*12 in second sum equation. This means it will add 12 to my goal function of cost .So, I only want to be taken into consideration the values which are not equal to zero in Table aa; for my second sum function.
About writing decision variable twice, it was a train and error for me. Since I am new at quadratic programming of GAMS, I am trying something, and then another thing.
My problem is in my mind clearly, as I wrote above but I am not sure about writing it in GAMS.
Could you please be so kind to give me advices about the GAMS formulation? I would be pretty pretty happy..

Thank you in advance,
All the best,
Pınar.

User avatar
bussieck
Moderator
Moderator
Posts: 163
Joined: 2 years ago

Re: GAMS does not read my code

Post by bussieck » 1 month ago

I don't understand where the problem is:

Code: Select all

goal..z=e= Sum((i,j), x(i,j)*d(i,j))+ Sum((s,i,t,j), x(s,i)*aa(s,i,t,j)*x(t,j));
There is even an example in the GAMS model library: https://www.gams.com/latest/gamslib_ml/ ... wkqap.html. Usually, the matrix aa (as in the awkqap example) is often compiled from two matrices: distance between locations (d) and transportation demand between facilities (f).

-Michael

happiniar
User
User
Posts: 5
Joined: 3 months ago

Re: GAMS does not read my code

Post by happiniar » 1 month ago

Dear Michael,

I solved it! Thank you very very much for all your advices*

All the best,
Pınar.

Post Reply