Browse Source

Add genetic search 1,1

Olivier Marty 8 years ago
parent
commit
74a5a463ef
7 changed files with 79 additions and 14 deletions
  1. 1 0
      .gitignore
  2. 1 1
      src/Makefile
  3. 8 1
      src/halton.cpp
  4. 1 0
      src/halton.h
  5. 3 2
      src/main.cpp
  6. 55 7
      src/search.cpp
  7. 10 3
      src/search.h

+ 1 - 0
.gitignore

@@ -27,4 +27,5 @@ main.pdf
 /src/main
 /src/dump
 /src/random_search
+/src/genetic_search_*
 /src/discr_calc/*_discr

+ 1 - 1
src/Makefile

@@ -1,4 +1,4 @@
-CPP=g++ -Wall -O2
+CPP=g++ -Wall -O2 -std=c++11
 CC=gcc
 
 main: discr_calc/dem_discr pointset.o permutation.o halton.o search.o main.o

+ 8 - 1
src/halton.cpp

@@ -15,8 +15,10 @@ 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++)
+  for(int i = 0; i < dim; i++) {
+    assert(p[i] > 1);
     pi.push_back(permutation(p[i]));
+  }
 }
 
 permutation& halton::get_pi(int i) {
@@ -29,3 +31,8 @@ void halton::compute(int i, double* res) {
     res[d] = phi(p[d], pi[d], i);
   }
 }
+
+int halton::get_p(int i) {
+  assert(0 <= i && i < dim);
+  return p[i];
+}

+ 1 - 0
src/halton.h

@@ -10,6 +10,7 @@ class halton {
   public:
     halton(int dim, int *p); // p is not deep-copied
     permutation& get_pi(int i);
+    int get_p(int i);
     void compute(int i, double* res);
   protected:
     int dim;

+ 3 - 2
src/main.cpp

@@ -18,8 +18,9 @@ int main(int argc, char **argv) {
     printf("%d%s", p[i], (i==dim-1) ? "\n" : ", ");
   printf("iterations = %d\n\n", iterations);
 
-  random_search rs(dim, npoints, p);
-  rs.run(iterations);
+  //random_search s(dim, npoints, p);
+  genetic_search s(dim, npoints, p, 1, 1);
+  s.run(iterations);
 
   return EXIT_SUCCESS;
 }

+ 55 - 7
src/search.cpp

@@ -1,6 +1,10 @@
 #include "search.h"
 #include <cassert>
 
+
+
+/**********************    search    ***********************/
+
 search::search(int dim, int npoints, int *p) : dim(dim), npoints(npoints),
     best(1.), ha(dim, p), ps(dim, npoints) {
   assert(dim > 0);
@@ -8,10 +12,9 @@ search::search(int dim, int npoints, int *p) : dim(dim), npoints(npoints),
 }
 
 void search::check() {
-  // TODO adapter l'API pour ne pas recalculer la discrepancy
-  double discr = ps.discrepancy();
-  if(discr < best) {
-    best = discr;
+  current = ps.discrepancy();
+  if(current < best) {
+    best = current;
     FILE *f = fopen(filename.c_str(), "w");
     ps.dump(f);
     fclose(f);
@@ -24,6 +27,15 @@ double search::run(int iterations) {
   return best;
 }
 
+// compute points
+void search::compute() {
+  for(int i = 0; i < npoints; i++)
+    ha.compute(i+1, ps.point(i));
+}
+
+
+/**********************    random_search    ***********************/
+
 random_search::random_search(int dim, int npoints, int *p) : search(dim, npoints, p) {
   filename = std::string("random_search");
 }
@@ -35,10 +47,46 @@ void random_search::_run(int iterations) {
       ha.get_pi(i).random();
 
     // compute points
-    for(int i = 0; i < npoints; i++)
-      ha.compute(i+1, ps.point(i));
-
+    compute();
     // compute discrepancy and check if it is good
     check();
   }
 }
+
+
+
+/**********************    genetic_search    ***********************/
+
+genetic_search::genetic_search(int dim, int npoints, int *p, int lambda, int mu)
+    :search(dim, npoints, p) {
+  filename = std::string("genetic_search_") + std::to_string(lambda) + "_" + std::to_string(mu);
+  assert(lambda == 1 && mu == 1);
+
+  // initial random config
+  for(int i = 0; i < dim; i++)
+    ha.get_pi(i).random();
+  compute();
+  check();
+}
+
+void genetic_search::_run(int iterations) {
+  for(int t = 0; t < iterations; t++) {
+    double previous = current;
+    // modification
+    int i = rand()%dim;
+    int p = ha.get_p(i);
+    permutation pi = ha.get_pi(i);
+    int t1 = rand()%p, t2 = rand()%(p-1);
+    // ensure t1 != t2
+    if(t2 >= t1)
+      t2++;
+    pi.transpose(t1, t2);
+
+    compute();
+    check();
+    if(current > previous) { // undo modification
+      pi.transpose(t1, t2);
+      current = previous;
+    }
+  }
+}

+ 10 - 3
src/search.h

@@ -7,17 +7,18 @@
 
 class search {
   public:
-    search(int npoints, int dim, int *ps);
+    search(int npoints, int dim, int *p);
 
     // return best discrepancy, write result in file filename
     double run(int iterations);
     virtual void _run(int iterations) = 0;
 
   protected:
-    void check();
+    void compute(); // compute points set
+    void check(); // compute discrepancy, fill current, if current < best write solution in filename
 
     int dim, npoints;
-    double best;
+    double current, best;
     std::string filename;
     halton ha;
     pointset ps;
@@ -29,4 +30,10 @@ class random_search: public search {
     virtual void _run(int iterations);
 };
 
+class genetic_search: public search {
+  public:
+    genetic_search(int dim, int npoints, int *p, int lambda, int mu);
+    virtual void _run(int iterations);
+};
+
 #endif // SEARCH_H