Can GAMS define my own function? Topic is solved

Problems with syntax of GAMS
Post Reply
GabrielYin
User
User
Posts: 53
Joined: 7 months ago
Location: Dallas, TX, USA

Can GAMS define my own function?

Post by GabrielYin » 3 months ago

Hi All,

I am considering if GAMS can store codes to one callable function like Matlab and C then use the function in another gms. file. Like the sample use of:

Code: Select all

Loop(iter,
	if(ord(iter)<=1,
		s(i) = m.l(i);
		call:function.gms;
		...
	);
	if(ord(iter)>1,
		call:function.gms;
		...
	);
);
the "function.gms" contains a large amounts of codes including assignment and model solving, say 500 lines. So having them in the two ifs will be exceedingly tedious. That's the motivation of this question.

Thanks!
Gabriel

User avatar
dirkse
Moderator
Moderator
Posts: 36
Joined: 2 years ago
Location: Fairfax, VA

Re: Can GAMS define my own function?

Post by dirkse » 3 months ago

Gabriel,

GAMS does not support functions of the sort you describe. Instead, you can use the include facility to include a file containing the GAMS source at both spots. A little reminder though: you cannot declare things inside an if statement. It is fine to make assignments and solve models inside an if statement, though, as you suggest.

There is also a $batinclude facility that builds on the $include facility: it takes text strings as arguments so you can parameterize the behavior of the included file.

https://www.gams.com/latest/docs/UG_Dat ... batinclude

-Steve

GabrielYin
User
User
Posts: 53
Joined: 7 months ago
Location: Dallas, TX, USA

Re: Can GAMS define my own function?

Post by GabrielYin » 3 months ago

dirkse wrote:
3 months ago
Gabriel,

GAMS does not support functions of the sort you describe. Instead, you can use the include facility to include a file containing the GAMS source at both spots. A little reminder though: you cannot declare things inside an if statement. It is fine to make assignments and solve models inside an if statement, though, as you suggest.

There is also a $batinclude facility that builds on the $include facility: it takes text strings as arguments so you can parameterize the behavior of the included file.

https://www.gams.com/latest/docs/UG_Dat ... batinclude

-Steve
Thanks Steve! Then you mean that GAMS does not allow the automatic data transfer between different GAMS programs right? OK I will try to reduce my codes.

Gabriel

Post Reply