|
@@ -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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|