How do I create a function in GAMS?

Problems with modeling
Post Reply
Posts: 4
Joined: 8 months ago

How do I create a function in GAMS?

Post by Saku45 » 8 months ago

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?

Posts: 108
Joined: 1 year ago

Re: How do I create a function in GAMS?

Post by cladelpino » 8 months ago

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


Post Reply