remove element from a set

Problems with syntax of GAMS
yanzhiping
User
User
Posts: 85
Joined: 8 months ago

remove element from a set

Postby yanzhiping » 4 months ago

I have a set G, and two subsets G1, G2. I want to remove 'A' from G1, and G2, since in my project, there are many subsets that are related to G. it is impossible to remove 'A' from each subset.
Is there any way I can just remove 'A' from G so ultimately 'A' will be gone from all the subsets G1, G2, ....?

I tried the following code, but it throws error as below.

Code: Select all

Set G /'A','B','C'/
G1(G) /'A','B'/
G2(G) /'A','C'/;
display G,G1,G2;
G('A')=no;
display G,G1,G2;
assigning to this set is not allowed. the set may have been used in a domain definition or is a predefined/readonly set.

cladelpino
User
User
Posts: 55
Joined: 9 months ago

Re: remove element from a set

Postby cladelpino » 4 months ago

Hi,

I don't know if it fits your workflow, but, using $include of different set definitions in different files is a possibility that comes to mind. Then you just comment/uncomment the ones you are currently using.

This has the advantage that you can make this kind of edits easily with your text editor of choice, or even build them on the fly with your favourite scripting tool (which of course includes using put from gams).

Best regards!
Claudio

yanzhiping
User
User
Posts: 85
Joined: 8 months ago

Re: remove element from a set

Postby yanzhiping » 4 months ago

Do you have any examples how to do this?

cladelpino
User
User
Posts: 55
Joined: 9 months ago

Re: remove element from a set

Postby cladelpino » 4 months ago

Hi, in main file:

Code: Select all

*You just comment or uncomment according to your needs

*$include SetDefWithA.gms
$include SetDefWithoutA.gms
*$include SomeOtherSetDef.gms
And you can have:

SetDefWithA.gms

Code: Select all

Set G /'A','B','C'/
G1(G) /'A','B'/
G2(G) /'A','C'/;
SetDefWithoutA.gms

Code: Select all

Set G /'B','C'/
G1(G) /'B'/
G2(G) /'C'/;
This approach does not rely in GAMS set manipulation, which as you have seen and as far as I know, is somehow hard to work around in some cases, like the one you bring up.

The gain in "expressivity" comes at the "cost" of having to rely on separate files (which I actually find more of a gain on itself, but this appreciation depends on your taste). If this definitions are static, nothing changes. If you are removing elements in a dynamic way (sometimes you remove A, sometimes B, and so on) it shouldn't be hard to script away the set definition files generation.

Of course, there MAY be a way to do this using only GAMS dynamic sets. I don't know it.

Best regards!
Claudio


Who is online

Users browsing this forum: No registered users and 1 guest