0

I need to define a set of parameters that have a natural recursive relation.

Here is a MWE where I try to define the factorial function over a set of (nine) parameters S:

$title TitleOfProblem

set S / s1*s9 /;

alias(S, S1, S2);

set delta1(S1,S2);
delta1(S1,S2) = yes$(ord(S1) + 1 = ord(S2));

parameter f(S);

f(S) = 1$(ord(S) = 1) + (ord(S) * sum(S1$(delta1(S1, S)), f(S1)))$(ord(S) > 1);

display f;

"delta1" is a relation containing pairs of elements in sorted order that differ by 1. Logically, the definition of f matches the definition of the factorial function (for inputs 1 to 9), but GAMS doesn't seem to like that f is defined recursively. The output of GAMS compilation looks something like this:

f(S) = 1$(ord(S) = 1) + (ord(S) * sum(S1$(delta1(S1, S)), f(S1)))$(ord(S) > 1);
                                                          $141

141  Symbol neither initialized nor assigned
    A wild shot: You may have spurious commas in the explanatory
    text of a declaration. Check symbol reference list.

Question:

Is it possible to recursively define a parameter in GAMS? If not, what is a work-around?

(P.S. Someone with enough rep should create a tag "GAMS" and add it to this question.)

Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
Tyson Williams
  • 1,630
  • 15
  • 35

1 Answers1

0

Someone showed me a solution for my example using a while loop. However, this solution is specific to factorial and does not generalize to an arbitrary recursive function.

$title factorial

set S / s1*s9 /;

parameter f(S);
parameter temp;

Loop(S,
  temp=ord(s);
  f(S)=ord(s);
    While(temp > 1,
      f(S) = f(S) * (temp-1);
      temp = temp - 1;
    );
);

display f;
Tyson Williams
  • 1,630
  • 15
  • 35