Browse Source

Add random choice in disp in permutation_crossover

Olivier Marty 8 years ago
parent
commit
b18d08505a
1 changed files with 8 additions and 5 deletions
  1. 8 5
      src/permutation.cpp

+ 8 - 5
src/permutation.cpp

@@ -1,5 +1,5 @@
 #include "permutation.h"
-#include <algorithm> // swap
+#include <algorithm> // swap, advance
 #include <cstdlib> // rand
 #include <cassert>
 #include <set>
@@ -44,7 +44,8 @@ bool permutation::check() {
 }
 
 // Algorithm:
-// for each i (starting with 0, then randomly : we use a permutation :P)
+// for each i (randomly : we use a permutation :P (it always starts with 0 but
+// it's OK since a[0]=b[0]=0 and also we want ret[0]=0))
 // we choose either a[i] or b[i]
 // if one value was already choosen, we choose the other
 // if both were, we choose a value we have seen but not choose (in the set disp)
@@ -59,9 +60,11 @@ permutation permutation_crossover(permutation &a, permutation &b) {
     int j = ord[i];
     bool seen_a = seen[a[j]], seen_b = seen[b[j]];
     if(seen_a && seen_b) {
-      // take value in disp
-      // TODO randomly
-      ret.sigma[i] = *disp.begin();
+      // take a random value in disp
+      assert(disp.size()>0);
+      auto it = disp.begin();
+      advance(it, rand()%disp.size());
+      ret.sigma[i] = *it;
     }
     else if(seen_a) {
       // take value in b