If I have a set
A/a1,a2/;
How may I get the first or second element from set A?
Is there anyway to get element 'a1'?
how to get any element from a set Topic is solved

 User
 Posts: 84
 Joined: 2 years ago
 Location: Rosario  Argentina
Re: how to get any element from a set
Hi,
For example, if yours variable is H(A) the first element corresponds to H('a1')
Bye
For example, if yours variable is H(A) the first element corresponds to H('a1')
Bye
Re: how to get any element from a set
Hi
If you want a specific set element, you can also use the ord and card functions:
Cheers
Renger
If you want a specific set element, you can also use the ord and card functions:
Code: Select all
set
A/a1,a2/;
parameter h(a);
* first element
h("a1")
h(a)$(ord(a) = 1)
* second element
h("a2")
h(a)$(ord(a) = 2)
* last element
h(a)$(ord(a) eq card(a))
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist

 User
 Posts: 85
 Joined: 2 years ago
Re: how to get any element from a set
that works. Thanks!
Does GAMS not distinguish capital and lower letters?
Does GAMS not distinguish capital and lower letters?

 User
 Posts: 85
 Joined: 2 years ago
Re: how to get any element from a set
the way you showed is to grab elements from a parameter.
How about if we want to grab 'a1' from set A?
Is it A$(ord(A)=1) ?
How about if we want to grab 'a1' from set A?
Is it A$(ord(A)=1) ?
arnyeinstein wrote:Hi
If you want a specific set element, you can also use the ord and card functions:CheersCode: Select all
set A/a1,a2/; parameter h(a); * first element h("a1") h(a)$(ord(a) = 1) * second element h("a2") h(a)$(ord(a) = 2) * last element h(a)$(ord(a) eq card(a))
Renger

 User
 Posts: 108
 Joined: 2 years ago
Re: how to get any element from a set
True. It does not.Does GAMS not distinguish capital and lower letters?
To "populate" a set:
Code: Select all
set A /a1,a2/;
set B(A);
B(A)=YES$(ord(A)=2);
display B;
Re: how to get any element from a set
Hi Renger,
when I assign an element of a parameter to a scalar, error happens with "Uncontrolled set entered as constant" and "Set for 'ord' is not controlled"
Set A /a1,a2/;
parameter h(a) /a1*a2 2/;
scalar y /0/;
y=h(a)$(ord(a) = 1);
Could you please tell where the problem is? Thank you.
Best regards,
Yongning
when I assign an element of a parameter to a scalar, error happens with "Uncontrolled set entered as constant" and "Set for 'ord' is not controlled"
Set A /a1,a2/;
parameter h(a) /a1*a2 2/;
scalar y /0/;
y=h(a)$(ord(a) = 1);
Could you please tell where the problem is? Thank you.
Best regards,
Yongning
Renger wrote: ↑2 years agoHi
If you want a specific set element, you can also use the ord and card functions:CheersCode: Select all
set A/a1,a2/; parameter h(a); * first element h("a1") h(a)$(ord(a) = 1) * second element h("a2") h(a)$(ord(a) = 2) * last element h(a)$(ord(a) eq card(a))
Renger
Re: how to get any element from a set
Hi
y is defined as a scalar, so no index. If you write it like you did, Gams expects a set indicator also on the RHS. But this might not what you want, as you set the values for y for all other elements of a to zero:
You could keep y as a scalar and write the following
or
Hope this helps
Cheers
Renger
y is defined as a scalar, so no index. If you write it like you did, Gams expects a set indicator also on the RHS. But this might not what you want, as you set the values for y for all other elements of a to zero:
Code: Select all
parameter y(a);
y(a)$(ord(a) =1) = h(a);
display y(a)
Code: Select all
loop(a,
y = h(a)$(ord(a) = 1);
);
Code: Select all
y = h("a1")
Cheers
Renger
____________________________________
Enjoy modeling even more: Read my blog on modeling at The lazy economist
Enjoy modeling even more: Read my blog on modeling at The lazy economist