Page 1 of 2

searching string in set

Posted: Tue Nov 20, 2018 7:17 pm
by ferrib
Hi all,

Let's suppose i have a set X = \y1 y2 y3 ... z1 z2\ and a variable w(X) = \y1 10, y2 20, y3 30, ... , z1 40, z2 50\
Is there an easy way of summing the values for all the elements of w(X) that have the string "y" in their name?

Thanks in advance!

Re: searching string in set

Posted: Wed Nov 21, 2018 6:44 am
by bussieck
Doing set member ship based on the name of a label is not what you should do in GAMS. We make this difficult on purpose, because that is a fragile thing. Better be explicit about it:

Code: Select all

set X /y1*y3, z1* z2/, Y(X) / y1*y3 /;
Parameter w(X)  /y1 10, y2 20, y3 30, z1 40, z2 50/;
Scalar ysubsum; ysubsum = sum(Y, w(Y)); display ysubsum;
The ord function (https://www.gams.com/latest/docs/UG_Par ... gFunctions) allows to access the ASCII code of a string for particular positions, so the following code calculates the set Y based on the first character being a 'y' or a 'Y':

Code: Select all

set X /y1*y3, z1* z2/, Y(X);
Parameter w(X)  /y1 10, y2 20, y3 30, z1 40, z2 50/;
loop(X, Y(X) = (ord(X.tl,1)=ord('y',1)) or (ord(X.tl,1)=ord('Y',1)));
Scalar ysubsum; ysubsum = sum(Y, w(Y)); display Y,ysubsum;
-Michael

Re: searching string in set

Posted: Wed Nov 21, 2018 11:34 am
by ferrib
Thanks Michael, this was very helpful!

Re: searching string in set

Posted: Mon Dec 03, 2018 11:18 am
by Jarenka
I have a different problem.

I have the following code:

Code: Select all

set Tid /2000*2020/;
singleton set FinalYear /2017/;
singleton set StatBank /2017/;

if (FinalYear < StatBank,
	loop( ...);
);
I have an error when starting "if" condition.
I would like to "compare" the years (elements) of these two singleton sets, and if they are the same I want to do the loop statement.

Re: searching string in set

Posted: Tue Dec 04, 2018 12:27 pm
by Manassaldi
Hi, to be able to compare them I think they should be defined as parameters.
Another possibility is to use auxiliary parameters.
To search for a string in a set, I usually use the "sameas" command.
Bye!

Re: searching string in set

Posted: Tue Dec 11, 2018 10:28 am
by Jarenka
Manassaldi wrote: 5 years ago Hi, to be able to compare them I think they should be defined as parameters.
Another possibility is to use auxiliary parameters.
To search for a string in a set, I usually use the "sameas" command.
Bye!
Yes, thank you ! I used "sameas" command :)

Another think is that I want to loop over elements in a set in descending order. For example:

Code: Select all

set tid /2000*2014/;
loop(tid$(2014, 2013, ..., 2000),
	"an equation";
);

Re: searching string in set

Posted: Tue Dec 11, 2018 1:19 pm
by Manassaldi
Hi, I think that order doesn't matter when you defining an equation.

set tid /2000*2014/;

equationx(tid)$condition(tid).. "an equation"

Re: searching string in set

Posted: Tue Dec 11, 2018 1:27 pm
by Jarenka
I would like to make a loop from 2014 to 2000 - in ascending order, and not from 2000 to 2014.

Re: searching string in set

Posted: Tue Dec 11, 2018 1:57 pm
by Manassaldi
I quickly think that it can be something like this:

set tid /2000*2014/;
parameter
p
;
loop(tid,
p=2014-(ord(tid) -1)
display p;
);

Re: searching string in set

Posted: Tue Dec 11, 2018 1:59 pm
by Manassaldi
Or, if you need to use the set tid:

Code: Select all

set tid /2000*2014/;
alias(tid,tidp)
parameter
p
;
loop(tidp,
loop(tid$(ord(tid) eq card(tid) - (ord(tidp)-1)),
p=ord(tid);
display p;
););