Subsets consisting of odd/even elements + compressing sets

Problems with syntax of GAMS
l_n_80
User
User
Posts: 4
Joined: 3 months ago

Subsets consisting of odd/even elements + compressing sets

Postby l_n_80 » 3 months ago

Hi all,

I have a vector U(rr), where rr = /1*40/. I also have a set i(rr) = /1*20/, and would like to populate two vectors, U1(i) and U2(i), where U1(i) should contain the odd elements of U(rr) and U2(i) should contain the even elements of U(rr).

So far I have attempted using dynamic sets, i.e.

rreven(rr)$(mod(rr,2) = 0) = yes,

but I cannot correctly define vectors the U1 and U2 over set i = /1*20/. Is there any simple solution to this problem in GAMS?

Thanks in advance.

User avatar
bussieck
Moderator
Moderator
Posts: 54
Joined: 1 year ago

Re: Subsets consisting of odd/even elements + compressing sets

Postby bussieck » 3 months ago

Hi,

Do you just want easy access to the U elements with odd and even index, or do you want to remap the even/odd indexes from 1 to 40 to 1 to 20? The following example shows you solutions for both.

Code: Select all

set rr /1*40/, i(rr) /1*20/, even(rr), odd(rr);
Variable U(rr);
even(rr) = mod(rr.val,2)=0;
odd(rr) = not even(rr);

* In order to get the even rr or odd rr just use the dynamnic set
U.fx(even) = 0;
U.fx(odd) = 1;

* If you want to remap U1('1) = U('1), U1('2') = U('3'), U1('3') = U('5')
* and the same with U2: U2('1) = U('2), U2('2') = U('4'), U2('3') = U('6')
* then do the following:

set m_even(rr,i), m_odd(rr,i);
m_even(even,i)$(even.pos=i.pos) = yes;
m_odd(odd,i)$(odd.pos=i.pos) = yes;
* One can do this set matching also with the "matching option" which
* is short and faster but probably not so easy to understand without reading
* the docs: https://www.gams.com/24.9/docs/UG_OptionStatement.html#UG_OptionStatement_IndexMatching
option m_even(even:i), m_odd(odd:i);

display m_even, m_odd;

Variable U1(i), U2(i);
equation e_even(rr,i), e_odd(rr,i);
e_odd(m_odd(odd,i)).. U1(i) =e= U(odd);
e_even(m_even(even,i)).. U2(i) =e= U(even);
-Michael

l_n_80
User
User
Posts: 4
Joined: 3 months ago

Re: Subsets consisting of odd/even elements + compressing sets

Postby l_n_80 » 3 months ago

Thanks Michael! This really helped me with the task of mapping the sets to do what I needed.


Who is online

Users browsing this forum: No registered users and 1 guest