[Swift-commit] r6124 - SwiftApps/SciColSim

wilde at ci.uchicago.edu wilde at ci.uchicago.edu
Thu Jan 3 18:26:38 CST 2013


Author: wilde
Date: 2013-01-03 18:26:38 -0600 (Thu, 03 Jan 2013)
New Revision: 6124

Modified:
   SwiftApps/SciColSim/optimizer.cpp
Log:
Removed obsolete code, adjusted timing calculations, removed RNG priming loop, and minor code adjustments .

Modified: SwiftApps/SciColSim/optimizer.cpp
===================================================================
--- SwiftApps/SciColSim/optimizer.cpp	2013-01-04 00:24:00 UTC (rev 6123)
+++ SwiftApps/SciColSim/optimizer.cpp	2013-01-04 00:26:38 UTC (rev 6124)
@@ -11,7 +11,7 @@
 #define MAXNworkers 24
 int Nworkers=MAXNworkers;
 
-#define NUniverse 24
+#define NUniverse 240
 
 // Add operation code to enable existing code to be used at lower level from Swift scripts:
 
@@ -20,6 +20,8 @@
 
 unsigned initSeed = 0;
 
+int verbose_level = 2;
+
 #include <fstream>
 #include <iostream>
 #include <stdio.h>
@@ -66,6 +68,8 @@
 
 #define FIX_VARIABLES 1
 
+#define DB if(verbose_level > 3)
+
 using namespace boost;
 using namespace std;
 using namespace boost::numeric::ublas;
@@ -230,6 +234,26 @@
   return a;
 }
 
+timeval startTime, endTime;
+double elapsedTime;
+char timenow[100];
+
+char *now()
+{
+  timeval endTime;
+  double elapsedTime;
+
+  gettimeofday(&endTime, NULL);
+  elapsedTime = (endTime.tv_sec - startTime.tv_sec) * 1000.0;      // sec to ms
+  elapsedTime += (endTime.tv_usec - startTime.tv_usec) / 1000.0;   // us to ms
+  elapsedTime /= 1000.;
+  sprintf(timenow, "%10.3f", elapsedTime);
+  return(timenow);
+}
+
+
+
+
 //================================================
 class Universe {
 
@@ -260,7 +284,7 @@
   double current_novelty;
 
   int mode_identify_failed;
-  int verbose_level; // 0 is silent, higher is more
+  //int verbose_level; // 0 is silent, higher is more
 
   double k_max;
 
@@ -273,9 +297,9 @@
   double **EdgeIndex;
   double *Rank;
 
-  base_generator_type generator;
-  boost::uniform_real<> uni_dist;
-  boost::geometric_distribution<double> geo;
+    base_generator_type generator;
+//  boost::uniform_real<> uni_dist;
+//  boost::geometric_distribution<double> geo;
 
 public:
 
@@ -292,12 +316,13 @@
 
       //-------------------------------
 
-      base_generator_type gene(42u);
-      generator = gene;
+//      base_generator_type gene(42u);
+//      generator = gene;
 //        generator.seed(static_cast<unsigned int>(std::time(0)));
 
 
       if ( initSeed != 0.0 ) {
+
         generator.seed(static_cast<unsigned int>(initSeed));
       }
       else {
@@ -310,8 +335,8 @@
 
 
 
-      boost::uniform_real<> uni_d(0,1);
-      uni_dist = uni_d;
+//      boost::uniform_real<> uni_d(0,1);
+//      uni_dist = uni_d;
 
       //--------------------------------
 
@@ -336,7 +361,7 @@
 
       id = idd;
 
-      verbose_level = 1;
+      //verbose_level = 1;
 
       mode_identify_failed = identify_failed;
 
@@ -689,7 +714,7 @@
     std::vector<int> d(num_edges(Full_g));
     edge_descriptor s;
     boost::graph_traits<graph_t>::vertex_descriptor u, v;
-    cout << "reset_world: start id=" << id << endl;
+    DB cout << now() << " reset_world: start id=" << id << endl;
 
     for (int i=0; i<N_nodes-1; i++){
       for (int j=i+1; j<N_nodes; j++){
@@ -718,7 +743,7 @@
         Tried[i][j]=0.;
       }
     }
-    cout << "reset_world: end id=" << id << endl;
+    DB cout << now() << " reset_world: end id=" << id << endl;
   }
 
 
@@ -758,12 +783,13 @@
 
 
   //=================================================
+#ifdef notdef
   void set_verbose(int verbose){
 
     verbose_level = verbose;
   }
+#endif
 
-
   //=============================================================
   void update_probabilities(void){
 
@@ -998,7 +1024,7 @@
 
 
   //==================================================================================
-  void evolve_to_target_and_save(int istart, int iend, double* storage, int* counters){
+  void evolve_to_target_and_save(int rr, double* results, int* counters){
 
     //double ALOT=100000000000.;
     double ALOT= 3.0 * max_loss; // Per Andrey, 2012.12.29
@@ -1008,27 +1034,25 @@
 
     reset_world();
 
-    for (int k = istart; k < iend; k++){
-      cout << "evolve_ttas: k=" << k << endl;
+    DB cout << now() << " evolve_ttas: rr=" << rr << " current_novelty=" << current_novelty << " target_novelty=" << TargetNovelty << endl;
 
-      for(int i=0; i< N_epochs &&  current_novelty < TargetNovelty; i++){
+    for(int i=0; i< N_epochs &&  current_novelty < TargetNovelty; i++){
+      DB cout << now() << " rr=" << rr << " i=" << i << " current_novelty=" << current_novelty << " ratio=" << ratio << " max_loss=" << max_loss << endl;
 
-        if(ratio < max_loss){
-          update_world();
-        }
-        else{
-          cout << "k=" << k << " i=" << i << " ratio=" << ratio << " max_loss=" << max_loss << endl;
-          break;
-        }
-        ratio = current_loss/current_novelty;
+      if(ratio < max_loss){
+	update_world();
       }
-      if( ratio < max_loss )
-        storage[k]=current_loss/current_novelty;
-      else
-        storage[k]=ALOT;
-      counters[k]=1;
-      reset_world();
+      else{
+	break;
+      }
+      ratio = current_loss/current_novelty;
     }
+    if( ratio < max_loss )
+      results[rr]=current_loss/current_novelty;
+    else
+      results[rr]=ALOT;
+    counters[rr]=1;
+    reset_world();
   }
 
   //==============================================
@@ -1058,58 +1082,6 @@
   }
 
 
-  //==============================================
-  void evolve_to_target(){
-
-    reset_world();
-    if (beta < -1. || gamma < -1.){
-      CumulativeRelativeLoss = 100000000000.;
-      CRLsquare = 0.;
-      return;
-    }
-
-
-    for (int k=0; k< N_repeats; k++){
-
-
-      for(int i=0; i<N_epochs &&  current_novelty < TargetNovelty; i++){
-        update_world();
-      }
-
-      CumulativeRelativeLoss += current_loss/current_novelty;
-      CRLsquare += (current_loss/current_novelty)*(current_loss/current_novelty);
-      if(verbose_level==3){
-        std::cout <<  CumulativeRelativeLoss << " | " << CRLsquare << std::endl;
-      }
-
-      if(verbose_level==1){
-        std::cout <<  "." ;
-
-      }
-      else if(verbose_level==2){
-        std::cout <<  "**" << (k+1) <<  "**  curr loss " << current_loss << "; curr novelty " << current_novelty << std::endl;
-      }
-
-
-      reset_world();
-    }
-
-    CumulativeRelativeLoss /= double(N_repeats);
-    CRLsquare /= double(N_repeats);
-
-    if(verbose_level==1){
-      std::cout << std::endl;
-    }
-
-    if(verbose_level==2){
-      std::cout <<  CumulativeRelativeLoss << " || " << CRLsquare << std::endl;
-    }
-
-    CRLsquare = 2*sqrt((CRLsquare - CumulativeRelativeLoss*CumulativeRelativeLoss)/double(N_repeats));
-
-  }
-
-
   //================================================================
   int set_parameter(double value, int position){
 
@@ -1201,8 +1173,8 @@
 
   double Loss=0., LossSquare=0.;
 
-  timeval startTime, endTime;
-  double elapsedTime;
+//  timeval startTime, endTime;
+//  double elapsedTime;
   gettimeofday(&startTime, NULL);
 
   if ( N > NUniverse ) {
@@ -1222,7 +1194,7 @@
 
 #pragma omp parallel for private (i)
   for(i=0; i<N; i++){
-    un[i]->evolve_to_target_and_save(i, i+1, Results, Counters);
+    un[i]->evolve_to_target_and_save(i, Results, Counters);
   }
 
 /*
@@ -1266,220 +1238,41 @@
 
 //============================================================
 
-void multi_annealing(Universe* un[],
-                     double T_start, double T_end,
-                     double Target_rejection,
-                     int Annealing_repeats,
-                     double starting_jump,
+
+void no_annealing(Universe* un[],
+		  //double T_start, double T_end,
+		  // double Target_rejection,
+		  // int Annealing_repeats,
+		  // double starting_jump,
                      double* Results,
                      int*    Counters,
-                     double* params0,
-                     double annealing_cycles){
-
-  //.................................
-  // re-implement annealing
-
-  double dx[5]={0.,0.,0.,0.,0};
+                     double* params0 /*,
+				       double annealing_cycles*/ )
+{
   double x[5]={0.,0.,0.,0.,0};
-  double rejection[5]={0., 0., 0., 0., 0.};
-  double curr_x, curr_err, x_tmp;
-  double temperature;
-  double ratio, r;
-  int cycle=10;
-  //boost::variate_generator<base_generator_type&, boost::uniform_real<> > uni(generator, uni_dist);
 
-  // set up parameter for annealing
-
   x[0]=params0[0];
   x[1]=params0[1];
   x[2]=params0[2];
   x[3]=params0[3];
   x[4]=params0[4];
 
-  for(int i=0;i<5;i++){
-    dx[i] = starting_jump;
-  }
-
-  // establish the current value
   std::pair<double,double>Res;
 
-  Res = multi_loss(       un,               Results, Counters, x);
+  Res = multi_loss(un, Results, Counters, x);
 
   std::cout << "Returned from initial multi_loss:" << std::endl;
   std::cout << Res.first << " +- " << Res.second << std::endl;
 
-  if ( operation == 'm' ) {
-    FILE *f;
-    int N = un[0]->get_reruns();
-
-    f = fopen("multi_loss.data","w");
-    for(int i=0; i<N; i++) {
-      fprintf(f,"%.20e\n",Results[i]);
-    }
-    fclose(f);
-    exit(0);
+  FILE *f;
+  int N = un[0]->get_reruns();
+  
+  f = fopen("multi_loss.data","w");
+  for(int i=0; i<N; i++) {
+    fprintf(f,"%.20e\n",Results[i]);
   }
-
-  curr_x   = Res.first;
-  curr_err = Res.second;
-
-  // optimization cycle
-
-  for(int i=0; i<annealing_cycles; i++){
-
-    temperature = T_start*exp( i*(log(T_end)-log(T_start))/(double)annealing_cycles);
-    std::cout  << std::endl << "....T = " << wrap_double(temperature,3) << std::endl << std::endl;
-
-    if (i % cycle == 0 && i > 0){
-
-      for (int k=0; k<5; k++){
-        rejection[k]/=(double)cycle;
-
-        if (rejection[k] > 0){
-          dx[k] = dx[k]/(rejection[k]/Target_rejection);
-          rejection[k]=0.;
-        }
-        else{
-          dx[k]*=2.;
-        }
-        std::cout  << dx[k] << " ";
-      }
-      std::cout  << std::endl;
-    }
-
-
-    for (int j=0; j<5; j++){
-
-      ///////////////////////////////
-      if (FIX_VARIABLES==0 || var_fixed[j]==0){
-
-
-
-        // get new value of x[j]
-        double x_hold=x[j];
-        x_tmp = get_new_x(x[j],dx[j]);
-        x[j]=x_tmp;
-
-        std::cout << wrap_double(x_tmp,10) << " " << wrap_double(j,9) << "\n\n";
-        //=======================================
-        //.............................................
-        for(int w=0; w<Nworkers; w++){
-          un[w]->set_parameter(x_tmp, j);
-        }
-
-        Res = multi_loss(       un,               Results, Counters, x);
-
-        std::cout << Res.first << " +- " << Res.second << std::endl;
-
-        ratio = min(1.,exp(-(Res.first-curr_x)/temperature));
-        r = rand()/(double)(pow(2.,31)-1.);
-        std::cout << r << " vs " << ratio << std::endl;
-
-        double ALOT=100000000000.;
-
-        if (Res.first < ALOT)
-        {
-          ofstream filestr;
-
-          filestr.open ("best_opt_some.txt", ofstream::app);
-
-          // >> i/o operations here <<
-          filestr
-
-            << "N, " << i << ", " << j << ", " << dx[j] << ", " << rejection[j] << ", |, " // FIXME: MW-DEBUGGING
-
-            << un[0]->get_target() << ", "
-            << Res.first
-            << ", " << un[0]->get_parameter(0)
-            << ", " << un[0]->get_parameter(1)
-            << ", " << un[0]->get_parameter(2)
-            << ", " << un[0]->get_parameter(3)
-            << ", " << un[0]->get_parameter(4) << ", " << Res.second << ",\n";
-
-          filestr.close();
-
-
-          filestr.open ("max_dist.txt", ofstream::app);
-
-          // >> i/o operations here <<
-          filestr << max_dist << ",\n";
-
-          filestr.close();
-
-          FILE *bf;
-          bf = fopen("bestdb.txt","a");
-          fprintf(bf, "N %2d %2d %10.5f %5.2f | %5.2f %10.5f [ %5.2f %5.2f %10.5f %10.5f %10.5f ] %10.5f\n", i, j, dx[j], rejection[j],
-                  un[0]->get_target(),
-                  Res.first,
-                  un[0]->get_parameter(0),
-                  un[0]->get_parameter(1),
-                  un[0]->get_parameter(2),
-                  un[0]->get_parameter(3),
-                  un[0]->get_parameter(4),
-                  Res.second);
-          fclose(bf);
-        }
-
-
-        if (r > ratio){
-
-          std::cout << " "<< (i+1) << ","<< (j)
-                    <<" "<< (i+1) << " Did not accept "
-                    << x_tmp << "(" << j << ")" << std::endl;
-          std::cout << un[0]->get_parameter(0)
-                    << " " << un[0]->get_parameter(1)
-                    << " " << un[0]->get_parameter(2)
-                    << " " << un[0]->get_parameter(3)
-                    << " " << un[0]->get_parameter(4) << " " << std::endl;
-
-          x[j]=x_hold;
-          for(int w=0; w<Nworkers; w++){
-            un[w]->set_parameter(x[j], j);
-          }
-
-
-          //set_parameter(x[j], j);
-          rejection[j]+=1.;
-        }
-
-        else {
-
-          curr_x   = Res.first;
-          curr_err = Res.second;
-          x[j] = x_tmp;
-
-          for(int w=0; w<Nworkers; w++){
-            un[w]->set_parameter(x[j], j);
-          }
-
-          std::cout << (i+1) << string_wrap((string) " Rejection counts: ", 8)
-                    << wrap_double(rejection[0],2) << " "
-                    << wrap_double(rejection[1],7) << " "
-                    << wrap_double(rejection[2],5) << " "
-                    << wrap_double(rejection[3],9) << " "
-                    << wrap_double(rejection[4],6) << " "
-                    << std::endl << std::endl;
-
-          std::cout << " "<< (i+1) <<","<< (j)
-                    <<" "
-                    << string_wrap((string) "***** Did accept! ", 3)
-                    << wrap_double(un[0]->get_parameter(0),2) << " "
-                    << wrap_double(un[0]->get_parameter(1),7) << " "
-                    << wrap_double(un[0]->get_parameter(2),5) << " "
-                    << wrap_double(un[0]->get_parameter(3),9) << " "
-                    << wrap_double(un[0]->get_parameter(4),6) << " "
-                    << std::endl << std::endl;
-
-
-
-        }
-        //........................................................
-
-      }
-    }
-
-  }
-
+  fclose(f);
+  exit(0);
 }
 
 
@@ -1488,7 +1281,6 @@
 int
 main(int argc, char* argv[])
 {
-
   double params0[6] = {0., 0., 0., 0., 0., 0.2}, target=50., range;
   string par_names0[6] = {"alpha_i", "alpha_m", "beta", "gamma", "delta", "target"};
   string par_names1[4] = {"n_epochs", "n_steps", "n_reruns", "range"};
@@ -1502,26 +1294,22 @@
   //          temperature_start,  temperature_end,  annealing_steps target_rejection  Starting_jump
   double params2[5] = {1,             0.001,               100,              0.3,           1.5};
 
-  int verbose_level = 2;
+//  int verbose_level = 2;
   const std::string one="one", two="two";
+
   static Universe* un[NUniverse];
   static double* Results;
   static int*    Counters;
 
   timeval t1, t2;
   double elapsedTime;
-  // start timer
-  gettimeofday(&t1, NULL);
 
+  gettimeofday(&t1, NULL);  // start timer
 
   if (argc < 8) {
     std::cout << "Usage: super_optimizer alpha_i alpha_m beta gamma delta target_innov [n_epochs n_steps n_reruns] [range] [verbose_level]\n";
     std::cout << "         [T_start T_end Annealing_steps Target_rejection Starting_jump]\n";
     std::cout << "         [FREEZE_alpha_i FREEZE_alpha_m FREEZE_beta FREEZE_gamma FREEZE_delta]\n";
-
-    system("pwd");
-
-
     return(1);
   }
   else {
@@ -1560,14 +1348,10 @@
         initSeed = atoi(argv[nArg]);
         std::cout << par_names4[2] << ": " << initSeed <<  std::endl;
       }
-
-
     }
-
   }
 
   for (int j=0; j<5; j++){
-
     cout << j << " | " << var_fixed[j] << " (fixed) \n";
   }
 
@@ -1577,31 +1361,19 @@
   char* filename= (char *)"movie_graph.txt";
   int n_ep=params1[0], n_st=params1[1], n_rep=params1[2];
 
-  //...............................
-
   for(int i=0; i<NUniverse; i++){
     un[i] = new Universe((char *)filename,n_ep,n_st,
                          (int)n_rep,
                          identify_failed, target, i2string(i));
   }
 
-  //...............................
-  if(n_rep > 0){
-
+  if (n_rep > 0) {
     Results = new double[n_rep];
     Counters = new int[n_rep];
-
-  }else {
-
-    Results =  NULL;
-    Counters = NULL;
+  } else {
     std::cout << " Number of reruns should be positive! " <<  std::endl;
-    return 0;
-
+    return 1;
   }
-  //...............................
-  //srand(time(0));
-  //srandomdev();
 
   if ( initSeed != 0.0 ) {
     srand(initSeed);
@@ -1612,74 +1384,42 @@
     srand(t.tv_usec);
   }
 
+#ifdef notdef
   {
     double r=0;
     for (int j=0; j<100; j++){
-
-
-
       r = rand()/(double)(pow(2.,31)-1.);
       std::cout << r << " ";
     }
     std::cout << "\n";
   }
-  //random initiation of starting parameters
+#endif
 
-  if (range > 0.){
-
-    for (int i=0; i < 5; i++){
-
-      if (params0[i]==-100.){
-
-        double r1 = (rand()/(double)(pow(2.,31)-1.));
-        double r2 = (rand()/(double)(pow(2.,31)-1.));
-        double sign = 1.;
-
-        if(r1 > 0.5){
-          sign=-1.;
-        }
-
-        params0[i] = sign*r2*range;
-
-        std::cout << par_names0[i] << ": " << params0[i] <<  std::endl;
-      }
-    }
-
-  }
-
-
   double T_start=params2[0], T_end=params2[1], Target_rejection=params2[3], starting_jump=params2[4];
   int Annealing_repeats = (int) params2[2];
 
+#ifdef notdef
   multi_annealing(       un,                T_start, T_end, Target_rejection, Annealing_repeats,
                          starting_jump, Results, Counters, params0, Annealing_repeats);
+#endif
 
-  // stop timer
-  gettimeofday(&t2, NULL);
+  no_annealing(un, Results, Counters, params0);
 
+  gettimeofday(&t2, NULL);  // stop timer
+
   // compute and print the elapsed time in millisec
+
   elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
   elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
   elapsedTime /= 1000.;
   cout << "\n*** optimizer completed, elapsed time=" << elapsedTime << " seconds " << elapsedTime/60. << " minutes)\n\n";
 
-  //.....................
-
   for(int i=0; i<Nworkers; i++){
     delete un[i];
   }
-
-  //....................
   if(n_rep > 0){
-
     delete [] Results;
     delete [] Counters;
-
   }
-
   return 0;
-
-
-
 }
-




More information about the Swift-commit mailing list