defining a parameter in GAMS Topic is solved

Problems with syntax of GAMS
Post Reply
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

defining a parameter in GAMS

Post by Bahar_Mdi »

Dear all,
how can I define the following parameter in GAMS?

C(i) is the set of j that d(i,j)=1

I have defined parameter d but I do not know how can I define C(i) in GAMS?

I appreciate it if someone can help me.
Thanks and regards,
Bahar
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: defining a parameter in GAMS

Post by abhosekar »

your question is not clear to me.
Can you provide an example of what you want to do?

What do you want the set C(i) to be?

When you define a set C(i), it is a subset of set i. Therefore, it cannot be a set of j.
if you want C(i) such that d(i, j) is 1, you would need some information about j.

Please provide more clarification.

- Atharv
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: defining a parameter in GAMS

Post by Bahar_Mdi »

thanks for your reply. I will provide more clear information in the following:

let i is the set of customers and j is the set of some locations. I define parameter d(i,j) as a binary parameter which is 1 if the distance from the customer i to location j is less than a predefined parameter R, otherwise, d(i,j) would be 0.

I want to define C(i) for each customer such that shows all the locations (j) that d(i,j) is equal to 1.

for example for customer 1 we have d(1,1)=1, d(1,2)=0, d(1,3)=1
so C(1) would be {1,3}.
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: defining a parameter in GAMS

Post by abhosekar »

Okay, makes sense.
You will have to use a set C(i, j). Then you can assign elements to this set as per your requirement. This is same as what you want. Even though you write C(i, j), for a given i, the set will only include elements of j that satisfy your criteria.
GAMS allows you to define multidimensional sets.

I provide an example

Code: Select all

set
i /a, b, c/
j /d, e, f/

c(i,j)
;
table d(i, j)
   d   e   f 
a  1   0   0
b  1   1   0
c  0   1   1
;

c(i, j) = no;
* if d(i, j) is 1 include i, j element in set c
c(i, j)$(d(i, j) eq 1) = yes;


display
C;
You will see in the .lst file that set c has all elements for which d(i, j) is 1.

Hope this helps.
- Atharv
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: defining a parameter in GAMS

Post by Bahar_Mdi »

thanks a lot for your help. but I want an output such as the following example:
for example for customer 1 we have d(1,1)=1, d(1,2)=0, d(1,3)=1
so C(1) would be {1,3}.

because in my constraints I want to have sum p(j)>=1 for each customer i; in which p is a binary variable and the summation is on all j that belong to c(i).
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: defining a parameter in GAMS

Post by abhosekar »

You can certainly do that by using an 'if' condition. Your equation can be written as follows (let's say the equation name is customer(i)):

customer(i).. sum(j $c(i,j), p(j)) =g= 1;

The $ will check whether the element c(i, j) exists. This way, it will only include j that you have activated for a given i as shown in my previous code.

Hope this helps.

- Atharv
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: defining a parameter in GAMS

Post by Bahar_Mdi »

thanks a lot.
I have another problem related to this topic:
by the following statement c_afterProcess(i,j) will ba change,
c_afterProcess(i, j) $(c(i, j)$subV_T(j) eq 1) = no;

but I want to put c_afterProcess(i, j) for all i if c(i, j)$subV(j) eq 1. more precisely I want to change the value of all the row if a special element of that row is equal to 0. how can I do that ?
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: defining a parameter in GAMS

Post by abhosekar »

I am not sure if the command you wrote is correct according to the syntax.
You could write
c_afterProcess(i, j) $(c(i, j)$(subV_T(j) eq 1)) = no;
or
c_afterProcess(i, j)$(c(i, j) and subV_T(j) eq 1) = no;

In either case, the command checks for "every" i and j, if the element exists in set c(i, j) and if subV_T(j) eq 1.
As a result, it will naturally happen for all i.
You do not have to do anything special to do it for all i.
You can check the same example

Code: Select all

set
i /a, b, c/
j /d, e, f/

c(i,j)
cnew(i, j)
;

table d(i, j)
   d   e   f 
a  1   0   0
b  1   1   0
c  0   1   1
;

parameter p(j)
/
d 1
e 0
f 0

/
;
c(i, j) = no;
* if d(i, j) is 1 include i, j element in set c
c(i, j)$(d(i, j) eq 1) = yes;

*cnew(i, j)$(c(i, j) and p(j) eq 1) = yes;

cnew(i, j)$(c(i, j)$(p(j) eq 1)) = yes;

display
C
cnew;

You will see that only column d is displayed for set cnew.

Hope this answers your question.

In case you need to do it for a specific i, then your parameter should be subV_T(i, j).

- Atharv
Bahar_Mdi
User
User
Posts: 21
Joined: 4 years ago

Re: defining a parameter in GAMS

Post by Bahar_Mdi »

thanks a lot. sorry, I think subV_T(j) Which I have used is misleading. It refers to a subset on j. we can drop this.
consider the following example:
let c is:
1 0 1 1
0 1 1 1
subV_T(j) contains j=1 and j=2.
I want to check c(i,j) for all i and j that belongs to subV_T(j) and if I find that c(i,j) is 1, then drop all the elements in row i. with the command that I have written before just c(i,j) changes to 0, not all the elements in row i.
abhosekar
Moderator
Moderator
Posts: 295
Joined: 3 years ago

Re: defining a parameter in GAMS

Post by abhosekar »

Not sure if it is the best way, but you can easily do that with the help of a parameter. First define a parameter for rows and store the value of column-wise sum (conditioned over c(i, j) and subV_T(j)) into that parameter.
If any value is 1, then that parameter will have value greater than or equal to 1.
You can then use this parameter to define your new set.
First set all elements to yes.
cnew(i, j) = yes;

set selected elements to no.
cnew(i, j)$(row(i) >=1) = no;

Code: Select all

set
i /a, b, c/
j /d, e, f/

c(i,j)
cnew(i, j)
;

table d(i, j)
   d   e   f 
a  1   0   0
b  1   1   0
c  0   0   1
;

set
p(j) /d,e/;

c(i, j) = no;
* if d(i, j) is 1 include i, j element in set c
c(i, j)$(d(i, j) eq 1) = yes;


cnew(i,j) = yes;


parameter
row(i)
;

row(i) = sum(j$(c(i, j) and p(j)), 1);

display
row;

cnew(i, j)$(row(i) >= 1) =no;

display
C
cnew;


You can see this in the example.

- Atharv
Post Reply