Page 1 of 1

How do I create a function in GAMS?

Posted: Tue May 15, 2018 2:41 pm
by Saku45
In my equation definitions, I need to use certain expressions involving the variables over and over again. How can I define a function that I can reuse in place of writing the expressions?

Here is what I thought of, but I am unsure if it is advisable.

Basically, I introduce a new variable "F" ("F" for function) and a new equation. In that equation, I set F equal to whatever formula I want it to equal. Then I can reuse this expression elsewhere.

Is this the way to do it?

---------------------

Here's an example. If I have a variable x(i,j) and I want to calculate the sum over i for fixed j, then I would write

someequation(j) .. sum(i, x(i,j)) =e= 1 ;

However, I could introduce a new variable F(j), and then I could write

someequation(j) .. F(j) =e= 1;
anotherequation(j) .. F(j) =e= sum(i, x(i,j)) ;


----------------------------------------------------

Is this advisable or is there a better way? Note that if I needed to create another function involving summing x(i,j), I could just write F(j) in place of sum(i, x(i,j)). But is there not a built-in way to do this?

Re: How do I create a function in GAMS?

Posted: Tue May 15, 2018 11:48 pm
by cladelpino
Yes, this is usual practice.

If it is "advisable" or not really cannot be answered in general, AFAIK. Some solvers (ie CONOPT in NLP) favor "sparseness" in models and would recommend this approach. In the context of MILP, different formulations could lead to different LP relaxations and therefore the impact is uncertain (there may be some general rule of which I'm not aware).

Best