Browse Source

Add fixpoint pi[0]=0 in permutations

Olivier Marty 8 years ago
parent
commit
41bb90340a
4 changed files with 8 additions and 8 deletions
  1. 1 1
      src/halton.cpp
  2. 5 6
      src/permutation.cpp
  3. 1 0
      src/permutation.h
  4. 1 1
      src/search.cpp

+ 1 - 1
src/halton.cpp

@@ -16,7 +16,7 @@ double phi(int p, permutation pi, int i) {
 halton::halton(int dim, int *p) : dim(dim), p(p) {
   assert(dim > 0);
   for(int i = 0; i < dim; i++) {
-    assert(p[i] > 1);
+    assert(p[i] > 2); // otherwise there is only one permutation : no optimisations are possible
     pi.push_back(permutation(p[i]));
   }
 }

+ 5 - 6
src/permutation.cpp

@@ -3,7 +3,6 @@
 #include <cstdlib> // rand
 #include <cassert>
 
-// TODO faut-il pi[0] = 0 et pourquoi ???
 permutation::permutation(int size) : size(size) {
   assert(size > 0);
   sigma = new int[size];
@@ -12,17 +11,17 @@ permutation::permutation(int size) : size(size) {
 }
 
 void permutation::transpose(int i, int j) {
-  assert(0 <= i && i < size);
-  assert(0 <= j && j < size);
+  assert(0 < i && i < size);
+  assert(0 < j && j < size);
   std::swap(sigma[i], sigma[j]);
 }
 
+// TODO rapport : dire qu'on garde l'invariant pi[0] = 0
 void permutation::random() {
   // Knuth shuffle
-  // TODO algo plus efficace ?
   // there is a bias due to the use of rand()%_
-  for(int i = 0; i < size; i++)
-    transpose(i, rand()%(i+1));
+  for(int i = 1; i < size; i++)
+    transpose(i, 1+rand()%i);
 }
 
 int permutation::operator[](int i) {

+ 1 - 0
src/permutation.h

@@ -1,6 +1,7 @@
 #ifndef PERMUTATION_H
 #define PERMUTATION_H
 
+// permutation with fixpoint pi[0]=0
 class permutation {
   public:
     permutation(int size); // initialize to identity

+ 1 - 1
src/search.cpp

@@ -76,7 +76,7 @@ void genetic_search::_run(int iterations) {
     int i = rand()%dim;
     int p = ha.get_p(i);
     permutation pi = ha.get_pi(i);
-    int t1 = rand()%p, t2 = rand()%(p-1);
+    int t1 = 1+rand()%(p-1), t2 = 1+rand()%(p-2);
     // ensure t1 != t2
     if(t2 >= t1)
       t2++;