Hi everyone.
I’m trying to create some sets with Python, but I lack any knowledge about Python and am running into some problems (right now, Python says mapping_com_rest would not be defined - but I’m sure there are more errors. I also noticed that some code of mine already worked with “$onembeddedCode Python”, but with my dynamic sets, I need to run Python in execution time).
I’ve tried to simplify my problem and would be very happy if somebody could take a look at it.
Here is what I’d like to do:
I have sectors i (beef, pork, dairy and vegetables) that include certain commodities com. They are mapped via mapping_com_sec.
Some commodities com_disagg are supposed to appear in a disaggregated form. For this I want to create a new set tl, in which the disaggregated sectors com_disagg appear, as well as the “rest” of the sectors.
So in the end, I would like to have a set tl that looks something like
beef1, beef3, beef4, rest_beef, pork2, pork3, rest_pork, dairy1, dairy2, dairy3, rest_dairy
and a mapping tl2com like
beef1.beef1
beef3.beef3
beef4.beef4
rest_beef.(beef2, beef5)
pork2.pork2
pork3.pork3
rest_pork.(pork1, pork4, pork5)
dairy1.dairy1
dairy2.dairy2
dairy3.dairy3
rest_dairy.()
This is my code:
Set sec /beef, pork, dairy, vegetables/
sec_disagg(sec)
com /beef1*beef5, pork1*pork5, dairy1*dairy3, vegetables1*vegetables3/
com_disagg(com) /beef1, beef3, beef4, pork2, pork3, dairy1, dairy2, dairy3/
mapping_com_sec(sec,com) /beef.(beef1*beef5)
pork.(pork1*pork5)
dairy.(dairy1*dairy3)
vegetables.(vegetables1*vegetables3)/
tl(*)
tl2com(*,com)
sec_disagg(sec)
mapping_com_rest(sec,com);
sec_disagg(sec) = Yes$sum(com_disagg, 1$mapping_com_sec(sec,com_disagg));
mapping_com_rest(sec_disagg,com) = Yes$mapping_com_sec(sec_disagg,com);
mapping_com_rest(sec_disagg,com_disagg) = No;
Display mapping_com_rest, sec_disagg;
embeddedCode Python:
tl = []
for com_disagg in gams.get("com_disagg"):
tl.append(com_disagg)
for sec_disagg in gams.get("sec_disagg"):
tl.append("rest_" + sec_disagg)
gams.set("tl",tl)
tl2com = []
for com_disagg in gams.get("com_disagg"):
tl2com.append((com_disagg,com_disagg))
for sec_disagg in gams.get("sec_disagg"):
for com in gams.get("com"):
if mapping_com_rest in gams.get("mapping_com_rest"):
tl2com.append(("rest_" + sec_disagg,com))
gams.set("tl2com",tl2com)
endEmbeddedCode tl tl2com
Display tl, tl2com;
Many thanks in advance!