Thursday, February 9, 2012

xor

It is not often that I can use a XOR in a GAMS model. But here we are..

I am working on some tools for a randomized algorithm for a multi-criteria 0-1 problem. As the algorithm can produce duplicate points we want a simple way to filter these out.

loop(pmax,

* generate new solution (dummy solution for now)

* assume rounded to 0 or 1
    x.l(i) = 1$(uniform(0,1)<0.25);

* is this a new solution or a duplicate
    dup(p) =
not sum(i, x.l(i) xor solpool(p,i));
   
abort$(card(dup)>1) "Error: solpool contains duplicates"
;

* if new: add to list

* if old: update counter
   
if(card(dup),
        solpool(dup,
'count') = solpool(dup,'count'
) + 1;
   
else

        solpool(pmax,
'count') = 1;
        solpool(pmax,i) = x.l(i);
        p(pmax) =
yes
;
    );
);

The results look like:

----     54 PARAMETER solpool  solution pool of unique solutions 

             i1          i2          i3          i4          i5       count
p1            1                                                           4
p2            1                                   1                       2
p3                                                            1           2
p4                        1                                               2
p5                                    1           1                       1
p7            1                       1                                   1
p8                                                                        2
p9                        1                       1                       2
p13           1           1                                   1           2
p15           1           1           1                                   1
p16                                   1                       1           1

Of course instead of the XOR we could have written:

dup(p) = not sum(i$(x.l(i)<>solpool(p,i)),1);