【问题标题】:How to write a while loop in GAMS如何在 GAMS 中编写 while 循环
【发布时间】:2015-11-01 21:01:05
【问题描述】:

谁能帮我解决这个问题。我想一个一个地加到 i 上,并在每一步中让 x(i) 的数量等于 1,所以我写如下但它不起作用

loop(i,
    x('0')=1;
    t('0')=1;
while(t>m,
      ord(i)=ord(i)+1;
      display i;
      x(i)=1;
      display x;
      t(i)=t(i-1) +1;
   );
 );

顺便说一句,m 是一个在此之前计算的变量。

【问题讨论】:

    标签: gams-math


    【解决方案1】:

    如果m 是以前求解的模型中的变量,则应使用.l 属性检查其级别:

    ...
    while(t>m.l,
    ...
    

    【讨论】:

      【解决方案2】:

      查看此page 以更好地了解如何在 GAMS 中创建 while 循环。另请查看此代码,因为它可能对您有所帮助:

      root=minroot;
      *find a sign switch
      while(signswitch=0 and root le maxroot,
         root=root+inc;
         function_value2= a-b*root+c*sqr(root);
         if((sign(function_value1) ne sign(function_value2)
            and abs(function_value1) gt 0
            and abs(function_value2) gt tolerance),
               maxroot=root;
               signswitch=1
         else
            if(abs(function_value2) gt tolerance,
               function_value1=function_value2;
               minroot=root;);
            );
      *  display 'inside',minroot,maxroot,function_value1,function_value2;
      );
      

      【讨论】:

      • 谢谢,但我的问题是 M 是一个变量
      • 难道不能根据变量写while循环吗?
      猜你喜欢
      • 2020-08-14
      • 2015-06-26
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2010-09-07
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      相关资源
      最近更新 更多