How do I implement a "lookup-table"?  FAQ

Frequently asked questions about GAMS

Moderator: aileen

Forum rules
Please ask questions in the other sub-forums
Locked
aileen
User
User
Posts: 136
Joined: 3 years ago

How do I implement a "lookup-table"?  FAQ

Post by aileen »

In my case the determinant is the summation of the variables from U1 to Ux that have been introduced as a binary variable to GAMS. In each iteration during the optimization these variables are set to 0 or 1 but the structure of these variables should be considered in the optimization process, for example:
  • if (u1=1, u2=0, u3=1, u4=1) then s=10
  • if (u1=0, u2=1, u3=0, u4=1) then s=100
  • if...
or:

Code: Select all

determinant  corresponding Value
  1-0-1-1          10
  0-1-0-1         100
...
The setting of variables in each iteration will be changed to reach the optimal point but each setting of variables has a cost (corresponding Value in the "lookup-table") that should be considered in the objective function.
aileen
User
User
Posts: 136
Joined: 3 years ago

Re: How do I implement a "lookup-table"?

Post by aileen »

Please have a look at the code below. The second model requires a decent GAMS/CPLEX license:

Code: Select all

set i / 1*4 /
    s / 1*5 /
table data(s,*)
  1 2 3 4 val
1 0 1 1 0  23
2 1 0 0 1  -5
3 1 0 1 0  16
4 0 1 0 1   4
5 1 1 1 0  111
;

parameter c(i) "cost of i when on"; 
c(i) = -uniformInt(1,10);
display c;
variable
    b(i) "binary selection",
    x(s) "indicator that s is active",
    z    "obj";
binary variables b,x;

equation
    defz    "obj",
    bigm_defx(s)  "identification of binary setting of b",
    indic_defx(s) "identification of binary setting of b"
;
defz..    z =e= sum(i, c(i)*b(i)) + sum(s, data(s,'val')*x(s));
bigm_defx(s)..  sum(i$(1=data(s,i)), b(i)) + sum(i$(0=data(s,i)), 1-b(i)) =g= card(i)*x(s);
indic_defx(s).. sum(i$(1=data(s,i)), b(i)) + sum(i$(0=data(s,i)), 1-b(i)) =e= card(i);

model bigm /defz, bigm_defx/;

option mip=cplex, optcr=0;
solve bigm using mip max z;

display z.l, b.l, x.l;

model indic /defz, indic_defx/;
$echo indic indic_defx(s)$x(s) 1 > cplex.opt
option mip=cplex, optcr=0; indic.optfile=1;
solve indic using mip max z;

display z.l, b.l, x.l;
Locked