Pyrmon55
Pyrmon55

Reputation: 203

Optimisation in GAMS

I have a problem with my GAMS-code and the implementation of the GAMS-code in Ruby. I know that GAMS is not the most popular program, but maybe someone can help me. I have a model, where I try to allocate children optimally to kindergartens. This is a basic example of the code:

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;

I have also a include file, where I define the parameters. My problem is, that I want to implement this in Ruby and I wanna use links (l) instaed of loops for the relation between i and j. I know, that I have to replace all i's and j's in dependence of l. But everytime I try this, I receive an error message. I have written it to this form, where I have replaced each time, when a parameter is dependent on i and j, with a l. But I have problems with the rest.

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;

My include file looks as follow:

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       ;

Can someone help me to rearrange my model and my include data?

Thank you!

Upvotes: 0

Views: 390

Answers (4)

Pyrmon55
Pyrmon55

Reputation: 203

this is my model:

 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;

and this is my include file:

 *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/;

Thank you very much for your help!

Best regards

Upvotes: 0

Lutz
Lutz

Reputation: 2292

I do not completely understand, why you went for the LI/LJ approach, but you actually should to use these maps in your equations to control i and j (these are often uncontrolled). So changing the equations in the following way gets rid of all compilation errors and solves the model:

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));

Best regards, Lutz

Upvotes: 0

Pyrmon55
Pyrmon55

Reputation: 203

This is my new include file. I tried to include it in the model code above.

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/;

Upvotes: 0

Lutz
Lutz

Reputation: 2292

If I run your model, the first errors I get are about naming/declaration conflicts between your main model and the include file. For example, in you main model you have

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

And then in the include file I see

Table p(i,j)
... ;

Table d(i,j)
... ;

Table s(i,j)
... ;

You cannot have the same symbol name with different argument lists (which is also mentioned in the error message: "**** 184 Domain list redefined"), so this is the first thing you need to resolve. Afterwards one could check how to proceed.

Good luck, Lutz

Upvotes: 0

Related Questions