searching string in set Topic is solved

Problems with syntax of GAMS
ferrib
User
User
Posts: 14
Joined: 1 year ago

searching string in set

Post by ferrib » 9 months ago

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!

User avatar
bussieck
Moderator
Moderator
Posts: 340
Joined: 2 years ago

Re: searching string in set

Post by bussieck » 9 months ago

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

ferrib
User
User
Posts: 14
Joined: 1 year ago

Re: searching string in set

Post by ferrib » 9 months ago

Thanks Michael, this was very helpful!

Jarenka
User
User
Posts: 50
Joined: 1 year ago

Re: searching string in set

Post by Jarenka » 8 months ago

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.

Manassaldi
User
User
Posts: 83
Joined: 2 years ago
Location: Rosario - Argentina

Re: searching string in set

Post by Manassaldi » 8 months 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!

Jarenka
User
User
Posts: 50
Joined: 1 year ago

Re: searching string in set

Post by Jarenka » 8 months ago

Manassaldi wrote:
8 months 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";
);

Manassaldi
User
User
Posts: 83
Joined: 2 years ago
Location: Rosario - Argentina

Re: searching string in set

Post by Manassaldi » 8 months ago

Hi, I think that order doesn't matter when you defining an equation.

set tid /2000*2014/;

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

Jarenka
User
User
Posts: 50
Joined: 1 year ago

Re: searching string in set

Post by Jarenka » 8 months ago

I would like to make a loop from 2014 to 2000 - in ascending order, and not from 2000 to 2014.

Manassaldi
User
User
Posts: 83
Joined: 2 years ago
Location: Rosario - Argentina

Re: searching string in set

Post by Manassaldi » 8 months ago

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;
);

Manassaldi
User
User
Posts: 83
Joined: 2 years ago
Location: Rosario - Argentina

Re: searching string in set

Post by Manassaldi » 8 months ago

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;
););

Post Reply