Page 1 of 1

Can GAMS define my own function?

Posted: Mon Jul 16, 2018 11:39 pm
by GabrielYin
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

Re: Can GAMS define my own function?

Posted: Tue Jul 17, 2018 12:50 am
by dirkse
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

Re: Can GAMS define my own function?

Posted: Wed Jul 18, 2018 12:06 am
by GabrielYin
dirkse wrote: 5 years 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