【问题标题】:Optimisation in GAMSGAMS 中的优化
【发布时间】:2018-03-09 17:14:09
【问题描述】:

我的 GAMS 代码和 GAMS 代码在 Ruby 中的实现存在问题。我知道 GAMS 不是最受欢迎的程序,但也许有人可以帮助我。 我有一个模型,我尝试将孩子最佳地分配到幼儿园。这是代码的基本示例:

Sets
          i       Child
          j       Kindergarten
          l       Links
          LI(l,i), LJ(l,j);

Parameters
          a(j)    accessibility
          C(j)    Capacity of Kindergartens
          ch      Capacity for handicapped children
          d(i,j)  distance
          da(i)   distance average
          h(i)    handicapped children
          p(i,j)  preferences
          s(i,j)  siblings;


Binary Variables
          x(i,j)  1 if child i is allocated to kindergarten j;

Free Variables
          ZFW     Zielfunktionswert;

*----- Iclude -----
$include ...



da(i) = sum(j, d(i,j)) / card(j);

*----- Deklaration -----
Equations
         ZF              objective function
         Zuordnung       every child is assigned to one kindergarten
         Kapa            the capacity of the kindergarten must be maintained
         Behin           handicapped children are only assigned to suitable kindergartens;


ZF..               ZFW =E= sum((i,j), x(i,j) * (p(i,j)/s(i,j)) *     (d(i,j)/da(i)));

Zuordnung(i)..     sum(j, x(i,j)) =E= 1;

Kapa(j)..          sum(i, x(i,j) * (1 - h(i))) + sum(i, x(i,j) * h(i) * ch) =L= C(j);

Behin(i,j)..       x(i,j) * h(i) =L= a(j);



Model KiGaOpt /all/;

Solve KiGaOpt using MIP minimizing ZFW;

display x.l;

我还有一个包含文件,我在其中定义参数。我的问题是,我想在 Ruby 中实现它,并且我想使用链接(l)代替循环来处理 i 和 j 之间的关系。 我知道,我必须根据 l 替换所有 i 和 j。但是每次我尝试这个时,我都会收到一条错误消息。我已经把它写成这个表格,我每次都用 l 替换参数依赖于 i 和 j 的地方。但我对其余部分有疑问。

Sets
          i       Child
          j       Kindergarten
          l       Links
          LI(l,i), LJ(l,j);

Parameters
          a(j)    accessibility
          C(j)    Capacity of Kindergartens
          ch      Capacity for handicapped children
          d(l)  distance
          da(i)   distance average
          h(i)    handicapped children
          p(l)  preferences
          s(l)  siblings;


Binary Variables
          x(l)  1 if child i is allocated to kindergarten j;

Free Variables
          ZFW     Zielfunktionswert;

*----- Iclude -----
$include ...



da(i) = sum(l$LI(l,i), d(l)) / card(j);

*----- Deklaration -----
Equations
         ZF              objective function
         Zuordnung       every child is assigned to one kindergarten
         Kapa            the capacity of the kindergarten must be maintained
         Behin           handicapped children are only assigned to suitable kindergartens;


ZF..               ZFW =E= sum(l, x(l) * (p(l)/s(l)) * (d(l)/da(i)));

Zuordnung(i)..     sum(l$LI(l,i), x(l)) =E= 1;

Kapa(j)..          sum(l$LJ(l,j), x(l) * (1 - h(i))) + sum(l$LJ(l,j), x(l) * h(i) * ch) =L= C(j);

Behin(i,j)..       x(l) * h(i) =L= a(j);



Model KiGaOpt /all/;

Solve KiGaOpt using MIP minimizing ZFW;

display x.l;

我的包含文件如下所示:

Sets
         i       /i1*i5/
         j       /j1*j2/
         l       /l1*l10/;




LI(l,i) = no;
LJ(l,j) = no;

LI( 'l1', 'i1') = yes;
LJ( 'l1', 'j1') = yes;

LI( 'l2', 'i1') = yes;
LJ( 'l2', 'j2') = yes;

LI( 'l3', 'i2') = yes;
LJ( 'l3', 'j1') = yes;

LI( 'l4', 'i2') = yes;
LJ( 'l4', 'j2') = yes;

LI( 'l5', 'i3') = yes;
LJ( 'l5', 'j1') = yes;

LI( 'l6', 'i3') = yes;
LJ( 'l6', 'j2') = yes;

LI( 'l7', 'i4') = yes;
LJ( 'l7', 'j1') = yes;

LI( 'l8', 'i4') = yes;
LJ( 'l8', 'j2') = yes;

LI( 'l9', 'i5') = yes;
LJ( 'l9', 'j1') = yes;

LI( 'l10', 'i5') = yes;
LJ( 'l10', 'j2') = yes;



Parameters


         a(j)
                /j1      1
                 j2      0/

         h(i)
                /i1      1
                 i2      0
                 i3      0
                 i4      0
                 i5      1/

         C(j)
                /j1      100
                 j2      100/

         ch      /2/;

Table p(i,j)
         j1      j2
i1       10      1
i2       10      1
i3       10      1
i4       10      1
i5       10      1       ;

Table d(i,j)
         j1      j2
i1       1       4
i2       2       1
i3       1       1
i4       1       2
i5       2       10.2    ;

Table s(i,j)
         j1      j2
i1       5       1
i2       1       1
i3       1       1
i4       1       1
i5       1       1       ;

有人可以帮我重新排列我的模型和包含数据吗?

谢谢!

【问题讨论】:

    标签: optimization gams-math


    【解决方案1】:

    我不完全明白,为什么你选择 LI/LJ 方法,但实际上你应该在方程中使用这些映射来控制 i 和 j(这些通常是不受控制的)。因此,通过以下方式更改方程可以消除所有编译错误并求解模型:

    ZF..               ZFW =E= sum(LI(l,i), x(l) * (p(l)/s(l)) * (d(l)/da(i)));
    
    Zuordnung(i)..     sum(l$LI(l,i), x(l)) =E= 1;
    
    Kapa(j)..          sum(LI(l,i)$LJ(l,j), x(l) * (1 - h(i))) + sum(LI(l,i)$LJ(l,j), x(l) * h(i) * ch) =L= C(j);
    
    Behin(l)..         x(l) *sum(LI(l,i),h(i)) =L= sum(LJ(l,j),a(j));
    

    最好的问候, 卢茨

    【讨论】:

    • 嗨 Lutz,你能帮我最后一次解决下面的问题吗?
    【解决方案2】:

    如果我运行你的模型,我得到的第一个错误是关于你的主模型和包含文件之间的命名/声明冲突。例如,在您的主模型中,您有

    Parameters
              ...
              d(l)  distance
              ...
              p(l)  preferences
              s(l)  siblings;
    

    然后在包含文件中我看到了

    Table p(i,j)
    ... ;
    
    Table d(i,j)
    ... ;
    
    Table s(i,j)
    ... ;
    

    你不能有相同的符号名称和不同的参数列表(错误信息中也提到:“**** 184 Domain list redefined”),所以这是你需要解决的第一件事。之后可以检查如何进行。

    祝你好运, 卢茨

    【讨论】:

    • 嗨卢茨,谢谢你的回答!我知道我必须以这种方式重写表,即没有表并且 p、d 和 s 依赖于 l。但如果我这样做,我会收到一条错误消息,因为方程依赖于 i 和 j 而不是 l。我需要知道,如何重写方程式,但我的想法不起作用
    • 您能提供您尝试过的更改代码吗?这样可以更轻松地帮助您解决遇到的错误
    • 我已经上传了上面的新包含文件。你知道,我怎样才能重新排列方程?
    【解决方案3】:

    这是我的新包含文件。我试图将它包含在上面的模型代码中。

    Sets
         i       /i1*i5/
         j       /j1*j2/
         l       /l1*l10/;
    
    
    
    LI(l,i) = no;
    LJ(l,j) = no;
    
    LI( 'l1', 'i1') = yes;
    LJ( 'l1', 'j1') = yes;
    
    LI( 'l2', 'i1') = yes;
    LJ( 'l2', 'j2') = yes;
    
    LI( 'l3', 'i2') = yes;
    LJ( 'l3', 'j1') = yes;
    
    LI( 'l4', 'i2') = yes;
    LJ( 'l4', 'j2') = yes;
    
    LI( 'l5', 'i3') = yes;
    LJ( 'l5', 'j1') = yes;
    
    LI( 'l6', 'i3') = yes;
    LJ( 'l6', 'j2') = yes;
    
    LI( 'l7', 'i4') = yes;
    LJ( 'l7', 'j1') = yes;
    
    LI( 'l8', 'i4') = yes;
    LJ( 'l8', 'j2') = yes;
    
    LI( 'l9', 'i5') = yes;
    LJ( 'l9', 'j1') = yes;
    
    LI( 'l10', 'i5') = yes;
    LJ( 'l10', 'j2') = yes;
    
    
    
    Parameters
    
    
         a(j)
                /j1      1
                 j2      0/
    
         h(i)
                /i1      1
                 i2      0
                 i3      0
                 i4      0
                 i5      1/
    
         C(j)
                /j1      100
                 j2      100/
    
         ch      /2/
    
          p(l)   /l1     10
                  l2     10
                  l3     10
                  l4     10
                  l5     10
                  l6     1
                  l7     1
                  l8     1
                  l9     1
                  l10    1/
    
          d(l)   /l1     1
                  l2     2
                  l3     1
                  l4     1
                  l5     2
                  l6     4
                  l7     1
                  l8     1
                  l9     2
                  l10    10.2/
    
          s(l)   /l1     5
                  l2     1
                  l3     1
                  l4     1
                  l5     1
                  l6     1
                  l7     1
                  l8     1
                  l9     1
                  l10    1/;
    

    【讨论】:

      【解决方案4】:

      这是我的模型:

       Sets
            i       Child
            j       Kindergarten
            l       Links
            LI(l,i), LJ(l,j);
      
       Parameters
            a(j)  accessibility
            C(j)  Capacity of Kindergartens
            ch    Capacity for handicapped children
            d(l)  distance
            da(i) distance average
            h(i)  handicapped children
            p(l)  preferences
            s(l)  siblings;
      
      
       Binary Variables
            x(l)  1 if child i is allocated to kindergarten j;
      
       Free Variables
            ZFW     objective function value;
      
       *----- Iclude -----
       $include  
      
      
      
       da(i) = sum(l$LI(l,i), d(l)) / card(j);
      
       *----- Deklaration -----
       Equations
           ZF              objective function
           Zuordnung       every child is assigned to one kindergarten
           Kapa            the capacity of the kindergarten must be maintained
           Behin           handicapped children are only assigned to suitable kindergartens
           Inklus          equal distribution of handicapped children;
      
      
       ZF..               ZFW =E= sum(LI(l,i), x(l) * (p(l)/s(l)) * (d(l)/da(i)));
      
       Zuordnung(i)..     sum(l$LI(l,i), x(l)) =E= 1;
      
       Kapa(j)..          sum(LI(l,i)$LJ(l,j), x(l) * (1 - h(i))) + sum(LI(l,i)$LJ(l,j), x(l) * h(i) * ch) =L= C(j);
      
       Behin(l)..         x(l) *sum(LI(l,i),h(i)) =L= sum(LJ(l,j),a(j))
      
       Inklus(j)..        sum(i, x(i,j)) =L= (card(h)/card(a) +1) * (1+TI) ;
      
      
      
       Model KiGaOpt /all/;
      
       Solve KiGaOpt using MIP minimizing ZFW;
      
       display x.l;
      

      这是我的包含文件:

       *Instanzen
      
       Sets
           i       /i1*i5/
           j       /j1*j2/
           l       /l1*l10/;
      
      
      
       LI(l,i) = no;
       LJ(l,j) = no;
      
       LI( 'l1', 'i1') = yes;
       LJ( 'l1', 'j1') = yes;
      
       LI( 'l2', 'i1') = yes;
       LJ( 'l2', 'j2') = yes;
      
       LI( 'l3', 'i2') = yes;
       LJ( 'l3', 'j1') = yes;
      
       LI( 'l4', 'i2') = yes;
       LJ( 'l4', 'j2') = yes;
      
       LI( 'l5', 'i3') = yes;
       LJ( 'l5', 'j1') = yes;
      
       LI( 'l6', 'i3') = yes;
       LJ( 'l6', 'j2') = yes;
      
       LI( 'l7', 'i4') = yes;
       LJ( 'l7', 'j1') = yes;
      
       LI( 'l8', 'i4') = yes;
       LJ( 'l8', 'j2') = yes;
      
       LI( 'l9', 'i5') = yes;
       LJ( 'l9', 'j1') = yes;
      
       LI( 'l10', 'i5') = yes;
       LJ( 'l10', 'j2') = yes;
      
      
      
       Parameters
      
      
            a(j)
                  /j1      1
                   j2      0/
      
            h(i)
                  /i1      1
                   i2      0
                   i3      0
                   i4      0
                   i5      1/
      
            C(j)
                  /j1      100
                   j2      100/
      
            ch      /2/
      
            p(l)   /l1     10
                    l2     10
                    l3     10
                    l4     10
                    l5     10
                    l6     1
                    l7     1
                    l8     1
                    l9     1
                    l10    1/
      
            d(l)   /l1     1
                    l2     2
                    l3     1
                    l4     1
                    l5     2
                    l6     4
                    l7     1
                    l8     1
                    l9     2
                    l10    10.2/
      
            s(l)   /l1     5
                    l2     1
                    l3     1
                    l4     1
                    l5     1
                    l6     1
                    l7     1
                    l8     1
                    l9     1
                    l10    1/
      
            TI     /0.5/;
      

      非常感谢您的帮助!

      最好的问候

      【讨论】:

      • 使用该模型,我得到了方程 Inklus 中变量 x 的域违规。在将方程式更改为“Inklus(j).. sum(LI(l,i)$LJ(l,j), x(l)) =L= (card(h)/card(a) +1) * (1+TI) ;"。希望有帮助,卢茨
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多