chiark / gitweb /
routesearch: make absolute/perleague into 2-element arrays rather than macros
authorIan Jackson <ian@liberator.(none)>
Sat, 10 Oct 2009 12:40:08 +0000 (13:40 +0100)
committerIan Jackson <ian@liberator.(none)>
Sat, 10 Oct 2009 12:40:08 +0000 (13:40 +0100)
yarrg/rscommon.h
yarrg/rsmain.c
yarrg/rssearch.c

index 5da0721..ab92fa5 100644 (file)
@@ -33,14 +33,15 @@ extern FILE *debug_file;
    CTR(routes_stratelim)                       \
    CTR(routes_valued)                          \
    CTR(routes_wrongfinal)                      \
-   CTR(newbests_strat_absolute)                        \
-   CTR(newbests_strat_perleague)               \
+   CTRA(newbests_strat,2)                      \
    CTR(subroute_tails_valued)                  \
    CTR(subroutes_valued)                       \
    CTR(subroutes_nonempty)
-#define CTR(x) extern int ctr_##x;
+#define CTR(x)    extern int ctr_##x;
+#define CTRA(x,n) extern int ctr_##x[n];
   COUNTER_LIST
 #undef CTR
+#undef CTRA
 
 #define SQL_MUST( call ) ({                                             \
     /* `call' is an expression returning result, using  const char *sqe; \
@@ -111,9 +112,13 @@ IslandPair *ipair_get_maybe(int si, int di);
 double value_route(int nislands, const int *islands, int exclude_arbitrage);
 void setup_value(void);
 
+#define AP 2 /* 0=absolute, 1=perleague */
+#define A 0
+#define P 1
+
 typedef struct {
-  double absolute, perleague;
-  int absolute_ports[MAX_ROUTELEN], perleague_ports[MAX_ROUTELEN];
+  double value[AP];
+  int ports[AP][MAX_ROUTELEN];
 } PotentialResult;
 
 void setup_search(void);
@@ -156,9 +161,8 @@ typedef struct {
   PotentialResult *pr;
 } HighScoreEntry;
 
-extern int nhighscores_absolute, nhighscores_perleague;
-extern HighScoreEntry *highscores_absolute;
-extern HighScoreEntry *highscores_perleague;
+extern int nhighscores[AP];
+extern HighScoreEntry *highscores[AP];
 
 
 #define ONDEMAND(pointer_lvalue, calloc_size_count)                         \
index fa52f81..524cbec 100644 (file)
@@ -15,9 +15,11 @@ FILE *output;
 DEBUG_DEFINE_SOME_DEBUGF(tableau,tabdebugf);
 
 
-#define CTR(x) int ctr_##x;
+#define CTR(x)    int ctr_##x;
+#define CTRA(x,n) int ctr_##x[n];
   COUNTER_LIST
 #undef CTR
+#undef CTRA
 
 static PotentialResult ****results;
   /* results[start_isle_ix][finalisle][midisle]-> */
@@ -26,12 +28,12 @@ static pid_t debugoutpid;
 
 int main(int argc, const char **argv) {
   const char *arg;
-  int i;
+  int i, ap;
 
 #ifndef debug_flags
   debug_flags= ~( dbg_sql2 );
 #endif
-  
+
   for (;;) {
     arg= *++argv;
     if (arg[0] != '-') break;
@@ -89,7 +91,7 @@ int main(int argc, const char **argv) {
   for (i=0; i<narches; i++)
     fprintf(output,"arch %d %s\n",i,archnames[i]);
   fprintf(output,"setup complete, starting search\n");
-  
+
   arg= *argv++;
   if (!strcmp(arg,"specific")) {
     int ia[argc], ni=0;
@@ -102,17 +104,16 @@ int main(int argc, const char **argv) {
     MCALLOC(results, argc);
 
     max_dist= atoi(*argv++);
-    nhighscores_absolute= atoi(*argv++);
-    nhighscores_perleague= atoi(*argv++);
+    for (ap=0; ap<AP; ap++) {
+      nhighscores[ap]= atoi(*argv++);
+      MCALLOC(highscores[ap], nhighscores[ap]);
+    }
     const char *final_isle_spec= *argv++;
 
-    MCALLOC(highscores_absolute, nhighscores_absolute);
-    MCALLOC(highscores_perleague, nhighscores_perleague);
-
     int resultsix= 0;
     while ((arg= argv[resultsix])) {
       int init_isle= atoi(arg);
-      
+
       int final_isle;
       if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
       else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
@@ -143,44 +144,45 @@ int main(int argc, const char **argv) {
          if (!result) {
            tabdebugf("|          ");
          } else {
-           tabdebugf("|%5d",(int)(result->absolute));
+           tabdebugf("|%5d",(int)(result->value[A]));
            tabdebugf(" ");
-           tabdebugf("%4d",(int)(result->perleague));
+           tabdebugf("%4d",(int)(result->value[P]));
          }
        }
        tabdebugf("\n");
       }
     }
 
-    int pos;
-#define OUT(absperl)                                                         \
-    fprintf(output,"\n================== " #absperl " ==================\n"); \
-    for (pos=0; pos<nhighscores_##absperl; pos++) {                          \
-      HighScoreEntry *hs= &highscores_##absperl[pos];                        \
-      PotentialResult *pr= hs->pr;                                           \
-      if (!pr) continue;                                                     \
-      const int *const ports= pr->absperl##_ports;                           \
-      int nports;                                                            \
-      for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);      \
-      int finisle= ports[nports-1]; int finarch= isle2arch(finisle);         \
-      int midarch= route2midarch(ports,nports);                                      \
-      fprintf(output,                                                        \
-             " @%2d #%2d | start%3d mid%d f%d:%3d | %5d %5d %4d |",     \
-             pos, nhighscores_##absperl - 1 - pos,                           \
-             ports[0], midarch, finarch,finisle,                     \
-             (int)hs->value, (int)pr->absolute, (int)pr->perleague);         \
-      for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);               \
-      fprintf(output,"\n");                                                  \
+    for (ap=0; ap<AP; ap++) {
+      int pos;
+      fprintf(output,"\n================== ap=%d ==================\n", ap);
+      for (pos=0; pos<nhighscores[ap]; pos++) {
+       HighScoreEntry *hs= &highscores[ap][pos];
+       PotentialResult *pr= hs->pr;
+       if (!pr) continue;
+       const int *const ports= pr->ports[ap];
+       int nports;
+       for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);
+       int finisle= ports[nports-1]; int finarch= isle2arch(finisle);
+       int midarch= route2midarch(ports,nports);
+       fprintf(output,
+               " @%2d #%2d | start%3d mid%d f%d:%3d | %5d %5d %4d |",     \
+               pos, nhighscores[ap] - 1 - pos,
+               ports[0], midarch, finarch,finisle,                   \
+               (int)hs->value, (int)pr->value[A], (int)pr->value[P]);
+       for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);
+       fprintf(output,"\n");
+      }
     }
-    OUT(absolute)
-    OUT(perleague)
     fprintf(output,"\n");
-    
+
   } else {
     abort();
   }
 
 #define CTR(x) fprintf(output,"  %-30s %10d\n",#x,ctr_##x);
+#define CTRA(x,n) for (i=0;i<n;i++) \
+  fprintf(output,"  %-27s[%d] %10d\n",#x,i,ctr_##x[i]);
   COUNTER_LIST
 #undef CTR
 
index fd2e66a..646c796 100644 (file)
@@ -43,7 +43,7 @@ static PotentialResult ***strat_base;
 
 static double process_route(int nports, int totaldist,
                            double overestimate_excepting_tail) {
-  int i;
+  int i, ap;
   int leagues_divisor= totaldist + nports;
 
   ctr_routes_considered++;
@@ -55,12 +55,12 @@ static double process_route(int nports, int totaldist,
     debugf(" %d",ports[i]);
   debugf("\n");
 
-  double guess_absolute=0, guess_perleague=0;
+  double guess[AP]={0,0};
   if (nports>=2) {
     int pair[2], i;
     pair[1]= ports[nports-1];
-    guess_absolute= overestimate_excepting_tail;
-    
+    guess[A]= overestimate_excepting_tail;
+
     for (i=0; i<nports; i++) {
       pair[0]= ports[i];
       IslandPair *ip= ipair_get_maybe(pair[0], pair[1]);
@@ -69,21 +69,21 @@ static double process_route(int nports, int totaldist,
        ctr_subroute_tails_valued++;
        ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
       }
-      guess_absolute += ip->route_tail_value;
+      guess[A] += ip->route_tail_value;
     }
-    guess_perleague= guess_absolute / leagues_divisor;
+    guess[P]= guess[A] / leagues_divisor;
 
     if (wrong_final) {
       ctr_routes_wrongfinalelim++;
       debugf(" WFELIM\n");
-      return guess_absolute;
+      return guess[A];
     }
 
-    if (guess_absolute <= highscores_absolute[0].value &&
-       guess_perleague <= highscores_perleague[0].value) {
+    if (guess[A] <= highscores[A][0].value &&
+       guess[P] <= highscores[P][0].value) {
       ctr_routes_quickelim++;
-      debugf(" QELIM %f %f\n", guess_absolute, guess_perleague);
-      return guess_absolute;
+      debugf(" QELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
     }
   }
 
@@ -94,74 +94,73 @@ static double process_route(int nports, int totaldist,
   PotentialResult *strat= ONDEMAND(strat_fin[midarch], 1);
 
   if (nports>=2) {
-    if (guess_absolute <= strat->absolute &&
-       guess_perleague <= strat->perleague) {
+    if (guess[A] <= strat->value[A] &&
+       guess[P] <= strat->value[P]) {
       ctr_routes_stratelim++;
-      debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
-      return guess_absolute;
+      debugf(" ELIM %f %f\n", guess[A], guess[P]);
+      return guess[A];
     }
-    debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
+    debugf(" COMPUTE %f %f\n", guess[A], guess[P]);
   }
 
   ctr_routes_valued++;
 
-  double absolute= value_route(nports, ports, 0);
-  double perleague= absolute / leagues_divisor;
+  double value[AP];
+  value[A]= value_route(nports, ports, 0);
+  value[P]= value[A] / leagues_divisor;
 
   if (wrong_final) {
     ctr_routes_wrongfinal++;
-    return absolute;
+    return value[0];
   }
 
-  if (absolute <= strat->absolute &&
-      perleague <= strat->perleague)
-    return absolute;
+  if (value[A] <= strat->value[A] &&
+      value[P] <= strat->value[P])
+    return value[A];
 
   debugf(" SOMEHOW BEST\n");
 
   fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
 
-#define CHK(absperl)                                                         \
-  fildebugf(#absperl " %15f", absperl);                                              \
-  if (absperl < strat->absperl) {                                            \
-    debugf("      ");                                                        \
-  } else {                                                                   \
-    int pos;                                                                 \
-    ctr_newbests_strat_##absperl++;                                          \
-    strat->absperl= absperl;                                                 \
-    memcpy(strat->absperl##_ports, ports, sizeof(*ports) * nports);          \
-    if (nports < MAX_ROUTELEN-1) strat->absperl##_ports[nports]= -1;         \
-    fildebugf("** ");                                                        \
-    for (pos=0; pos < nhighscores_##absperl; pos++)                          \
-      if (highscores_##absperl[pos].pr == strat) goto found_##absperl;       \
-    /* not found */                                                          \
-    pos= -1;                                                                 \
-   found_##absperl:                                                          \
-    for (;;) {                                                               \
-      pos++;                                                                 \
-      if (pos >= nhighscores_##absperl-1) break; /* new top */               \
-      if (highscores_##absperl[pos].value >= absperl) break; /* found spot */ \
-      if (pos>0)                                                             \
-       highscores_##absperl[pos-1]= highscores_##absperl[pos];               \
-    }                                                                        \
-    pos--;                                                                   \
-    if (pos>0) {                                                             \
-      highscores_##absperl[pos].value= absperl;                                      \
-      highscores_##absperl[pos].pr= strat;                                   \
-    }                                                                        \
-    fildebugf("@%2d", pos);                                                  \
+  for (ap=0; ap<AP; ap++) {
+    fildebugf("ap=%d %15f", ap, value[ap]);
+    if (value[ap] < strat->value[ap]) {
+      debugf("      ");
+    } else {
+      int pos;
+      ctr_newbests_strat[ap]++;
+      strat->value[ap]= value[ap];
+      memcpy(strat->ports[ap], ports, sizeof(*ports) * nports);
+      if (nports < MAX_ROUTELEN-1) strat->ports[ap][nports]= -1;
+      fildebugf("** ");
+      for (pos=0; pos < nhighscores[ap]; pos++)
+       if (highscores[ap][pos].pr == strat) goto found;
+      /* not found */
+      pos= -1;
+    found:
+      for (;;) {
+       pos++;
+       if (pos >= nhighscores[ap]-1) break; /* new top */
+       if (highscores[ap][pos].value >= value[ap]) break; /* found spot */
+       if (pos>0)
+         highscores[ap][pos-1]= highscores[ap][pos];
+      }
+      pos--;
+      if (pos>0) {
+       highscores[ap][pos].value= value[ap];
+       highscores[ap][pos].pr= strat;
+      }
+      fildebugf("@%2d", pos);
+    }
   }
 
-  CHK(absolute)
-  CHK(perleague)
-
   fildebugf(" route");
 
   for (i=0; i<nports; i++)
     fildebugf(" %d",ports[i]);
   fildebugf("\n");
 
-  return absolute;
+  return value[0];
 }
 
 static void recurse(int last_isle,
@@ -188,9 +187,8 @@ void search(int start_isle, int final_isle_spec,
   recurse(start_isle,0,0,1e6);
 }
 
-int nhighscores_absolute, nhighscores_perleague;
-HighScoreEntry *highscores_absolute;
-HighScoreEntry *highscores_perleague;
+int nhighscores[AP];
+HighScoreEntry *highscores[AP];
 
 int narches;
 char **archnames;