Access strings in Sets

Archive of Gamsworld Google Group
Post Reply
Archiver
User
User
Posts: 7876
Joined: 2 years ago

Access strings in Sets

Post by Archiver » 2 years ago


Hi folks,

GAMS offers the possibility to read Sets from e.g. an Excel file using the Values=String option. Then instead of just having Y or N as data for each element, whatever string is in the column is read into GAMS as value. Now my question is: is there any way to actually access the strings read in that way? I would imagine a suffix such as ".text" or similar to do that.

Say you have:

Sets
a /a0*a5/
b(a)
;

Then the set b(a) can usually have values such as:

b("a0") = Y
b("a1") = Y
b("a2") = N

But using gdxxrw you have the option to read in whatever string you have instead of the Y / N. So the resulting GDX would have values such as:

b("a0") = some
b("a1") = random
b("a2") = string

In itself this is not very useful (it just looks ugly and the string has no meaning in itself), but I would like to "misuse" that feature for my data upload, where I need to do things with strings (which are categorical values). However, there seems no way of actually accessing that string. Whenever I try to access it, I just get 1 as the output.

cheers
Ingmar

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 2 years ago

Re: Access strings in Sets

Post by Archiver » 2 years ago


Hi Ingmar,
using the put facility you can access the string that is associated to the set element.
I have never used it but an example can be found on page 5 of https://www.wageningenur.nl/web/file?uu ... dd743a71d7

You can use some identifiers of sets in put statements:set element label: .tl (useful in loops)set element associated text: .te(index)
set associated text: .ts
Example:
file out /"results.out"/; put out;
set i associated text of set /i1 first element, i2 second element, i3/;
put i.ts /;
loop(i, put i.tl, i.te(i) /);
The resulting file results.out will look like:
associated text of set
i1 first element
i2 second element
i3 I3
{Contributed by Rob Dellink}



Hope that helps,
Cheers,
Clemens



On Tue, Apr 12, 2016 at 2:29 PM, Ingmar Schlecht wrote:

Hi folks,

GAMS offers the possibility to read Sets from e.g. an Excel file using the Values=String option. Then instead of just having Y or N as data for each element, whatever string is in the column is read into GAMS as value. Now my question is: is there any way to actually access the strings read in that way? I would imagine a suffix such as ".text" or similar to do that.

Say you have:

Sets
a /a0*a5/
b(a)
;

Then the set b(a) can usually have values such as:

b("a0") = Y
b("a1") = Y
b("a2") = N

But using gdxxrw you have the option to read in whatever string you have instead of the Y / N. So the resulting GDX would have values such as:

b("a0") = some
b("a1") = random
b("a2") = string

In itself this is not very useful (it just looks ugly and the string has no meaning in itself), but I would like to "misuse" that feature for my data upload, where I need to do things with strings (which are categorical values). However, there seems no way of actually accessing that string. Whenever I try to access it, I just get 1 as the output.

cheers
Ingmar

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.




--
Clemens Gerbaulet
Mobile: +49 160 4909568

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 2 years ago

Re: Access strings in Sets

Post by Archiver » 2 years ago


Thanks Clemens, that looks interesting.

The only additional problem I have is that I would want to use it in an if statement.

Along the lines of:

loop(i,
if(i.te == "some text",
do something
);
);

Does anybody know if something like that is possible at all?

Cheers
Ingmar


On Wednesday, April 13, 2016 at 8:28:35 AM UTC+2, Clemens Gerbaulet wrote:

Hi Ingmar,
using the put facility you can access the string that is associated to the set element.
I have never used it but an example can be found on page 5 of https://www.wageningenur.nl/web/file?uu ... dd743a71d7

You can use some identifiers of sets in put statements:set element label: .tl (useful in loops)set element associated text: .te(index)
set associated text: .ts
Example:
file out /"results.out"/; put out;
set i associated text of set /i1 first element, i2 second element, i3/;
put i.ts /;
loop(i, put i.tl, i.te(i) /);
The resulting file results.out will look like:
associated text of set
i1 first element
i2 second element
i3 I3
{Contributed by Rob Dellink}



Hope that helps,
Cheers,
Clemens



On Tue, Apr 12, 2016 at 2:29 PM, Ingmar Schlecht wrote:

Hi folks,

GAMS offers the possibility to read Sets from e.g. an Excel file using the Values=String option. Then instead of just having Y or N as data for each element, whatever string is in the column is read into GAMS as value. Now my question is: is there any way to actually access the strings read in that way? I would imagine a suffix such as ".text" or similar to do that.

Say you have:

Sets
a /a0*a5/
b(a)
;

Then the set b(a) can usually have values such as:

b("a0") = Y
b("a1") = Y
b("a2") = N

But using gdxxrw you have the option to read in whatever string you have instead of the Y / N. So the resulting GDX would have values such as:

b("a0") = some
b("a1") = random
b("a2") = string

In itself this is not very useful (it just looks ugly and the string has no meaning in itself), but I would like to "misuse" that feature for my data upload, where I need to do things with strings (which are categorical values). However, there seems no way of actually accessing that string. Whenever I try to access it, I just get 1 as the output.

cheers
Ingmar

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.




--
Clemens Gerbaulet
Mobile: +49 160 4909568

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Archiver
User
User
Posts: 7876
Joined: 2 years ago

Re: Access strings in Sets

Post by Archiver » 2 years ago


Ingmar,

The following macros do what you want. They are somewhat expensive, but if you use them infrequently you should be okay. The set indx is required, because we need to "iterate" through the characters of the string.

$macro upperchar(x) ifthen(x>=97 and x.tl (useful in loops)set element associated text: .te(index)
set associated text: .ts
Example:
file out /"results.out"/; put out;
set i associated text of set /i1 first element, i2 second element, i3/;
put i.ts /;
loop(i, put i.tl, i.te(i) /);
The resulting file results.out will look like:
associated text of set
i1 first element
i2 second element
i3 I3
{Contributed by Rob Dellink}



Hope that helps,
Cheers,
Clemens



On Tue, Apr 12, 2016 at 2:29 PM, Ingmar Schlecht wrote:

Hi folks,

GAMS offers the possibility to read Sets from e.g. an Excel file using the Values=String option. Then instead of just having Y or N as data for each element, whatever string is in the column is read into GAMS as value. Now my question is: is there any way to actually access the strings read in that way? I would imagine a suffix such as ".text" or similar to do that.

Say you have:

Sets
a /a0*a5/
b(a)
;

Then the set b(a) can usually have values such as:

b("a0") = Y
b("a1") = Y
b("a2") = N

But using gdxxrw you have the option to read in whatever string you have instead of the Y / N. So the resulting GDX would have values such as:

b("a0") = some
b("a1") = random
b("a2") = string

In itself this is not very useful (it just looks ugly and the string has no meaning in itself), but I would like to "misuse" that feature for my data upload, where I need to do things with strings (which are categorical values). However, there seems no way of actually accessing that string. Whenever I try to access it, I just get 1 as the output.

cheers
Ingmar

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+...@googlegroups.com.
To post to this group, send email to gams...@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.




--
Clemens Gerbaulet
Mobile: +49 160 4909568

--
To unsubscribe from this group and stop receiving emails from it, send an email to gamsworld+unsubscribe@googlegroups.com.
To post to this group, send email to gamsworld@googlegroups.com.
Visit this group at https://groups.google.com/group/gamsworld.
For more options, visit https://groups.google.com/d/optout.

Post Reply