0

我需要定义一组具有自然递归关系的参数。

这是一个 MWE,我尝试在一组(九个)参数 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”是一个关系,包含按排序顺序排列的元素对,相差 1。从逻辑上讲,f 的定义与阶乘函数的定义相匹配(对于输入 1 到 9),但 GAMS 似乎不喜欢 f 是递归定义。GAMS 编译的输出如下所示:

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.

问题:

是否可以在 GAMS 中递归定义参数?如果没有,什么是解决方法?

(PS 有足够代表的人应该创建一个标签“GAMS”并将其添加到这个问题中。)

4

1 回答 1

0

有人使用 while 循环向我展示了我的示例的解决方案。但是,此解决方案特定于阶乘,并不能推广到任意递归函数。

$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;
于 2011-09-29T18:36:09.550 回答