Browse Source

Add local_search and sa_local_search

Olivier Marty 8 years ago
parent
commit
58f0f652e7
5 changed files with 95 additions and 131 deletions
  1. 2 2
      .gitignore
  2. 3 3
      src/main.cpp
  3. 0 100
      src/random_search
  4. 61 23
      src/search.cpp
  5. 29 3
      src/search.h

+ 2 - 2
.gitignore

@@ -26,6 +26,6 @@ main.pdf
 *.o
 /src/main
 /src/dump
-/src/random_search
-/src/genetic_search_*
+/src/random_local_search
+/src/sa_local_search_*
 /src/discr_calc/*_discr

+ 3 - 3
src/main.cpp

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

+ 0 - 100
src/random_search

@@ -1,100 +0,0 @@
-0.571429	0.384615	0.586207	0.000000	0.000000	0.806452
-0.857143	0.000000	0.413793	0.333333	0.210526	0.548387
-0.142857	0.769231	0.448276	0.666667	0.578947	0.387097
-0.285714	0.076923	0.275862	0.000000	0.105263	0.580645
-0.714286	0.923077	0.103448	0.333333	0.631579	0.419355
-0.428571	0.153846	0.965517	0.777778	0.684211	0.000000
-0.081633	0.846154	0.344828	0.111111	0.947368	0.903226
-0.653061	0.692308	0.310345	0.444444	0.263158	0.709677
-0.938776	0.230769	0.000000	0.888889	0.526316	0.516129
-0.224490	0.615385	0.827586	0.222222	0.157895	0.838710
-0.367347	0.538462	0.931034	0.555556	0.421053	0.967742
-0.795918	0.307692	0.655172	0.666667	0.368421	0.677419
-0.510204	0.491124	0.551724	0.000000	0.315789	0.354839
-0.122449	0.414201	0.034483	0.333333	0.894737	0.483871
-0.693878	0.029586	0.758621	0.777778	0.842105	0.096774
-0.979592	0.798817	0.137931	0.111111	0.052632	0.741935
-0.265306	0.106509	0.482759	0.444444	0.789474	0.612903
-0.408163	0.952663	0.620690	0.925926	0.736842	0.645161
-0.836735	0.183432	0.724138	0.259259	0.473684	0.870968
-0.551020	0.875740	0.206897	0.592593	0.000000	0.451613
-0.020408	0.721893	0.896552	0.703704	0.210526	0.774194
-0.591837	0.260355	0.379310	0.037037	0.578947	0.161290
-0.877551	0.644970	0.862069	0.370370	0.105263	0.290323
-0.163265	0.568047	0.689655	0.814815	0.631579	0.193548
-0.306122	0.337278	0.241379	0.148148	0.684211	0.935484
-0.734694	0.461538	0.068966	0.481481	0.947368	0.032258
-0.448980	0.384615	0.793103	0.962963	0.263158	0.258065
-0.040816	0.000000	0.172414	0.296296	0.526316	0.322581
-0.612245	0.769231	0.537455	0.629630	0.157895	0.129032
-0.897959	0.076923	0.606421	0.740741	0.421053	0.225806
-0.183673	0.923077	0.434007	0.074074	0.368421	0.090531
-0.326531	0.153846	0.468490	0.407407	0.315789	0.832466
-0.755102	0.846154	0.296076	0.851852	0.894737	0.574402
-0.469388	0.692308	0.123662	0.185185	0.842105	0.413111
-0.102041	0.230769	0.985731	0.518519	0.052632	0.606660
-0.673469	0.615385	0.365042	0.888889	0.789474	0.445369
-0.959184	0.538462	0.330559	0.222222	0.736842	0.026015
-0.244898	0.307692	0.020214	0.555556	0.484765	0.929240
-0.387755	0.520710	0.847800	0.666667	0.011080	0.735692
-0.816327	0.443787	0.951249	0.000000	0.221607	0.542144
-0.530612	0.059172	0.675386	0.333333	0.590028	0.864724
-0.061224	0.828402	0.571938	0.777778	0.116343	0.993757
-0.632653	0.136095	0.054697	0.111111	0.642659	0.703434
-0.918367	0.982249	0.778835	0.444444	0.695291	0.380853
-0.204082	0.213018	0.158145	0.925926	0.958449	0.509886
-0.346939	0.905325	0.502973	0.259259	0.274238	0.122789
-0.775510	0.751479	0.640904	0.592593	0.537396	0.767950
-0.489796	0.289941	0.744352	0.703704	0.168975	0.638918
-0.011662	0.674556	0.227111	0.037037	0.432133	0.671176
-0.583090	0.597633	0.916766	0.370370	0.379501	0.896982
-0.868805	0.366864	0.399524	0.814815	0.326870	0.477627
-0.154519	0.467456	0.882283	0.148148	0.905817	0.800208
-0.297376	0.390533	0.709869	0.481481	0.853186	0.187305
-0.725948	0.005917	0.261593	0.975309	0.063712	0.316337
-0.440233	0.775148	0.089180	0.308642	0.800554	0.219563
-0.093294	0.082840	0.813317	0.641975	0.747922	0.961498
-0.664723	0.928994	0.192628	0.753086	0.504155	0.058273
-0.950437	0.159763	0.531510	0.086420	0.030471	0.284079
-0.236152	0.852071	0.600476	0.419753	0.240997	0.348595
-0.379009	0.698225	0.428062	0.864198	0.609418	0.155047
-0.807580	0.236686	0.462545	0.197531	0.135734	0.251821
-0.521866	0.621302	0.290131	0.530864	0.662050	0.082206
-0.134111	0.544379	0.117717	0.901235	0.714681	0.824142
-0.705539	0.313609	0.979786	0.234568	0.977839	0.566077
-0.991254	0.532544	0.359096	0.567901	0.293629	0.404787
-0.276968	0.455621	0.324614	0.679012	0.556787	0.598335
-0.419825	0.071006	0.014269	0.012346	0.188366	0.437045
-0.848397	0.840237	0.841855	0.345679	0.451524	0.017690
-0.562682	0.147929	0.945303	0.790123	0.398892	0.920916
-0.032070	0.994083	0.669441	0.123457	0.346260	0.727367
-0.603499	0.224852	0.565993	0.456790	0.925208	0.533819
-0.889213	0.917160	0.048751	0.938272	0.872576	0.856400
-0.174927	0.763314	0.772889	0.271605	0.083102	0.985432
-0.317784	0.301775	0.152200	0.604938	0.819945	0.695109
-0.746356	0.686391	0.497027	0.716049	0.767313	0.372529
-0.460641	0.609467	0.634958	0.049383	0.479224	0.501561
-0.052478	0.378698	0.738407	0.382716	0.005540	0.114464
-0.623907	0.473373	0.221165	0.827160	0.216066	0.759625
-0.909621	0.396450	0.910820	0.160494	0.584488	0.630593
-0.195335	0.011834	0.393579	0.493827	0.110803	0.662851
-0.338192	0.781065	0.876338	0.987654	0.637119	0.888658
-0.766764	0.088757	0.703924	0.320988	0.689751	0.469303
-0.481050	0.934911	0.255648	0.654321	0.952909	0.791883
-0.113703	0.165680	0.083234	0.765432	0.268698	0.178980
-0.685131	0.857988	0.807372	0.098765	0.531856	0.308012
-0.970845	0.704142	0.186683	0.432099	0.163435	0.211238
-0.256560	0.242604	0.532699	0.876543	0.426593	0.953174
-0.399417	0.627219	0.601665	0.209877	0.373961	0.049948
-0.827988	0.550296	0.429251	0.543210	0.321330	0.275754
-0.542274	0.319527	0.463734	0.913580	0.900277	0.340271
-0.072886	0.526627	0.291320	0.246914	0.847645	0.146722
-0.644315	0.449704	0.118906	0.580247	0.058172	0.243496
-0.930029	0.065089	0.980975	0.691358	0.795014	0.077003
-0.215743	0.834320	0.360285	0.024691	0.742382	0.818939
-0.358601	0.142012	0.325803	0.358025	0.506925	0.560874
-0.787172	0.988166	0.015458	0.802469	0.033241	0.399584
-0.501458	0.218935	0.843044	0.135802	0.243767	0.593132
-0.017493	0.911243	0.946492	0.469136	0.612188	0.431842
-0.588921	0.757396	0.670630	0.950617	0.138504	0.012487
-0.874636	0.295858	0.567182	0.283951	0.664820	0.915713

+ 61 - 23
src/search.cpp

@@ -1,8 +1,8 @@
 #include "search.h"
+#include <cmath>
 #include <cassert>
 
 
-
 /**********************    search    ***********************/
 
 search::search(int dim, int npoints, int *p) : dim(dim), npoints(npoints),
@@ -55,40 +55,78 @@ void random_search::_run(int iterations) {
 }
 
 
+/**********************    local_search    ***********************/
 
-/**********************    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);
+local_search::local_search(int dim, int npoints, int *p) : search(dim, npoints, p),
+    undoable(false) {
+}
 
-  // initial random config
+void local_search::init() {
+  // random initial configuration
   for(int i = 0; i < dim; i++)
     ha.get_pi(i).random();
   compute();
   check();
 }
 
-void genetic_search::_run(int iterations) {
-  // TODO en fait c'est randomized local search
-  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 = 1+rand()%(p-1), t2 = 1+rand()%(p-2);
-    // ensure t1 != t2
-    if(t2 >= t1)
-      t2++;
-    pi.transpose(t1, t2);
 
+void local_search::random_neighbour() {
+  undoable = true;
+  i = rand()%dim;
+  int p = ha.get_p(i);
+  t1 = 1+rand()%(p-1);
+  t2 = 1+rand()%(p-2);
+  // ensure t1 != t2
+  if(t2 >= t1)
+    t2++;
+  ha.get_pi(i).transpose(t1, t2);
+}
+
+void local_search::undo() {
+  assert(undoable);
+  undoable = false;
+  ha.get_pi(i).transpose(t1, t2);
+}
+
+void local_search::_run(int iterations) {
+  for(int t = 0; t < iterations; t++) {
+    previous = current;
+    random_neighbour();
     compute();
     check();
-    if(current > previous) { // undo modification
-      pi.transpose(t1, t2);
+    if(!accept(previous, current)) {
       current = previous;
+      undo();
     }
   }
 }
+
+
+/**********************    random_local_search    ***********************/
+
+random_local_search::random_local_search(int dim, int npoints, int *p)
+    :local_search(dim, npoints, p) {
+  filename = std::string("random_local_search");
+  init();
+}
+
+bool random_local_search::accept(double previous, double current) {
+  return current < previous;
+}
+
+
+/**********************    sa_search    ***********************/
+
+sa_local_search::sa_local_search(int dim, int npoints, int *p, double lambda)
+    :local_search(dim, npoints, p), lambda(lambda), temp(1.) {
+  assert(0 <= lambda && lambda < 1);
+  filename = std::string("sa_local_search_") + std::to_string(lambda);
+  init();
+}
+
+bool sa_local_search::accept(double previous, double current) {
+  if(current < previous)
+    temp *= lambda;
+  //printf("%lf\t%lf\n", temp, current);
+  return current < previous || (double)rand()/RAND_MAX < exp((previous - current)/temp);
+}

+ 29 - 3
src/search.h

@@ -30,10 +30,36 @@ class random_search: public search {
     virtual void _run(int iterations);
 };
 
-class genetic_search: public search {
+class local_search: public search {
   public:
-    genetic_search(int dim, int npoints, int *p, int lambda, int mu);
-    virtual void _run(int iterations);
+    local_search(int dim, int npoints, int *p);
+    virtual void _run(int iteartions);
+    virtual bool accept(double previous, double current) = 0; // return true if we must accept this solution
+  protected:
+    void init(); // random initial configuration
+    virtual void random_neighbour();
+    virtual void undo(); // (no history : only one modification is remembered)
+  private:
+    double previous;
+    // for undo :
+    bool undoable;
+    int i, t1, t2;
+};
+
+class random_local_search: public local_search {
+  public:
+    random_local_search(int dim, int npoints, int *p);
+    virtual bool accept(double previous, double current);
+};
+
+// simulated annealing
+class sa_local_search: public local_search {
+  public:
+    sa_local_search(int dim, int npoints, int *p, double lambda);
+    virtual bool accept(double previous, double current);
+  protected:
+    double lambda;
+    double temp;
 };
 
 #endif // SEARCH_H