How do I create a function in GAMS?

Problems with modeling
Post Reply
Saku45
User
User
Posts: 4
Joined: 5 years ago

How do I create a function in GAMS?

Post 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?
cladelpino
User
User
Posts: 108
Joined: 7 years ago

Re: How do I create a function in GAMS?

Post 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
Post Reply