chiark / gitweb /
routesearch: rework some things, bugfixes
authorIan Jackson <ian@liberator.(none)>
Thu, 15 Oct 2009 18:55:18 +0000 (19:55 +0100)
committerIan Jackson <ian@liberator.(none)>
Thu, 15 Oct 2009 18:55:18 +0000 (19:55 +0100)
Debugging: New value2 debug flag, new option -D1 for medium debug output, etc.
Rename PotentialResult to a Bucket of two OnePotentialResults (no intended
  change in behaviour for this)
Fix granularity full test to work correctly

yarrg/rscommon.h
yarrg/rsmain.c
yarrg/rssearch.c
yarrg/rssql.c
yarrg/rsvalue.c
yarrg/x.gdb

index 84fad6fdb4e788d3cb2a3962734de063a71a4619..66e36d905e7b8431acba4bce9b3f50075dd17bcb 100644 (file)
@@ -7,6 +7,7 @@
    DF(sql)                                     \
    DF(sql2)                                    \
    DF(value)                                   \
+   DF(value2)                                  \
    DF(search)                                  \
    DF(filter)                                  \
    DF(check)                                   \
@@ -120,12 +121,16 @@ void setup_value(void);
 
 typedef struct {
   double value[AP];
-  int ports[AP][MAX_ROUTELEN];
-} PotentialResult;
+  int ports[MAX_ROUTELEN];
+} OnePotentialResult;
+
+typedef struct {
+  OnePotentialResult prs[AP];
+} Bucket;
 
 void setup_search(void);
 void search(int start_isle, int final_isle /* -1 means any */,
-           PotentialResult ****buckets_base_io[GRANUS]
+           Bucket ****buckets_base_io[GRANUS]
                /* bucket_base[granui][finalthing][midthing]-> */);
 
 extern double max_mass, max_volu, max_capi;
@@ -162,7 +167,7 @@ extern FILE *output;
 
 typedef struct {
   double value;
-  PotentialResult *pr;
+  Bucket *bucket;
 } HighScoreEntry;
 
 extern int granus;
index eaa16c54834032a521e887e8c712a88488485745..a784ae383ef3fa521bc86f7c1d1ce929914f2eaf 100644 (file)
@@ -21,7 +21,7 @@ FILE *output;
 #undef CTR
 #undef CTRA
 
-static PotentialResult ****results[GRANUS];
+static Bucket ****results[GRANUS];
   /* results[GRANUS][start_isle_ix][finalisle][midisle]-> */
 
 static pid_t debugoutpid;
@@ -41,6 +41,8 @@ int main(int argc, const char **argv) {
 #ifndef debug_flags
     if (!strcmp(arg,"-DN")) {
       debug_flags= 0;
+    } else if (!strcmp(arg,"-D1")) {
+      debug_flags= ~(dbg_sql2|dbg_lp|dbg_value2);
     } else
 #endif
     {
@@ -126,7 +128,7 @@ int main(int argc, const char **argv) {
       else final_isle= atoi(final_isle_spec);
       assert(final_isle);
 
-      PotentialResult ****buckets_base_io[GRANUS];
+      Bucket ****buckets_base_io[GRANUS];
       for (granui=0; granui<GRANUS; granui++)
        buckets_base_io[granui]= &results[granui][resultsix];
        
@@ -138,9 +140,9 @@ int main(int argc, const char **argv) {
     for (granui=0; granui<granus; granui++) {
       fprintf(output,"\n");
       for (i=0; i<resultsix; i++) {
-       tabdebugf("========== start #%d granui%d %s [PARTIAL] ==========\n",
+       tabdebugf("========== start #%d granui%d %s ==========\n",
                  i, granui, argv[i]);
-       PotentialResult ***buckets_resultsix= results[granui][i];
+       Bucket ***buckets_resultsix= results[granui][i];
        if (!buckets_resultsix) continue;
        tabdebugf("    ");
        for (mid=0; mid<granusz_mid[granui]; mid++) {
@@ -148,17 +150,17 @@ int main(int argc, const char **argv) {
        }
        tabdebugf("\n");
        for (fin=0; fin<granusz_fin[granui]; fin++) {
-         PotentialResult **buckets_fin= buckets_resultsix[fin];
+         Bucket **buckets_fin= buckets_resultsix[fin];
          if (!buckets_fin) continue;
          tabdebugf("f%-3d",fin);
          for (mid=0; mid<granusz_mid[granui]; mid++) {
-           PotentialResult *result= buckets_fin[mid];
+           Bucket *result= buckets_fin[mid];
            if (!result) {
              tabdebugf("|          ");
            } else {
-             tabdebugf("|%5d",(int)(result->value[A]));
+             tabdebugf("|%5d",(int)(result->prs[A].value[A]));
              tabdebugf(" ");
-             tabdebugf("%4d",(int)(result->value[P]));
+             tabdebugf("%4d",(int)(result->prs[P].value[P]));
            }
          }
          tabdebugf("\n");
@@ -171,9 +173,10 @@ int main(int argc, const char **argv) {
                granui, ap);
        for (pos=nhighscores[granui][ap]-1; pos>=0; pos--) {
          HighScoreEntry *hs= &highscores[granui][ap][pos];
-         PotentialResult *pr= hs->pr;
-         if (!pr) continue;
-         const int *const ports= pr->ports[ap];
+         Bucket *bucket= hs->bucket;
+         if (!bucket) continue;
+         OnePotentialResult *pr= &bucket->prs[ap];
+         const int *const ports= pr->ports;
          int nports;
          for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);
          int finisle= ports[nports-1];
index 31ec3d76b7ab12b842f15cf73a309331d87c8290..23d2ece5eb8541ccc2616fd8ae749d0966759b3b 100644 (file)
@@ -38,7 +38,7 @@ static Neighbour *get_neighbours(int isle) {
 }
 
 
-static PotentialResult ***buckets_base[GRANUS];
+static Bucket ***buckets_base[GRANUS];
 
 
 static double process_route(int nports, int totaldist,
@@ -79,8 +79,8 @@ static double process_route(int nports, int totaldist,
       return guess[A];
     }
 
-    if (guess[A] <= highscores[GRANUS-1][A][0].value &&
-       guess[P] <= highscores[GRANUS-1][P][0].value) {
+    if (guess[A] <= highscores[granus-1][A][0].value &&
+       guess[P] <= highscores[granus-1][P][0].value) {
       ctr_routes_quickelim++;
       debugf(" QELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
@@ -93,10 +93,10 @@ static double process_route(int nports, int totaldist,
   int midisle= ports[nports/2];
   int midarch= route2midarch(ports,nports);
 
-  PotentialResult *buckets[GRANUS];
+  Bucket *buckets[GRANUS];
   int granui;
   for (granui=0; granui<granus; granui++) {
-    PotentialResult **buckets_fin;
+    Bucket **buckets_fin;
     int mid, fin;
     switch (granui) {
     case 0: fin=finarch; mid=midarch; break;
@@ -109,8 +109,8 @@ static double process_route(int nports, int totaldist,
   }
 
   if (nports>=2) {
-    if (guess[A] <= buckets[0]->value[A] &&
-       guess[P] <= buckets[0]->value[P]) {
+    if (guess[A] <= buckets[0]->prs[A].value[A] &&
+       guess[P] <= buckets[0]->prs[P].value[P]) {
       ctr_routes_bucketelim++;
       debugf(" ELIM %f %f\n", guess[A], guess[P]);
       return guess[A];
@@ -130,39 +130,39 @@ static double process_route(int nports, int totaldist,
   }
 
   for (granui=granus-1; granui>=0; granui--) {
-    PotentialResult *bucket= buckets[granui];
+    Bucket *bucket= buckets[granui];
 
-    if (value[A] <= bucket->value[A] &&
-       value[P] <= bucket->value[P])
+    if (value[A] <= bucket->prs[A].value[A] &&
+       value[P] <= bucket->prs[P].value[P])
       continue;
 
     debugf(" SOMEHOW %d BEST\n",granui);
 
-    fildebugf("final %d:%3d mid %d ",finarch,finisle,midarch);
-    int relevant=0;
+    fildebugf("granu %d f%d:%3d mid%d:%3d ",granui,
+             finarch,finisle,midarch,midisle);
 
     for (ap=0; ap<AP; ap++) {
       HighScoreEntry *scores= highscores[granui][ap];
-      int *nscores= &nhighscores[granui][ap];
+      int nscores= nhighscores[granui][ap];
 
       fildebugf("ap=%d %15f", ap, value[ap]);
-      if (value[ap] < bucket->value[ap]) {
+      if (value[ap] < bucket->prs[ap].value[ap]) {
        debugf("      ");
       } else {
        int pos;
        ctr_newbests_granu[granui*AP+ap]++;
-       bucket->value[ap]= value[ap];
-       memcpy(bucket->ports[ap], ports, sizeof(*ports) * nports);
-       if (nports < MAX_ROUTELEN-1) bucket->ports[ap][nports]= -1;
+       memcpy(bucket->prs[ap].value, value, sizeof(value));
+       memcpy(bucket->prs[ap].ports, ports, sizeof(*ports) * nports);
+       if (nports < MAX_ROUTELEN-1) bucket->prs[ap].ports[nports]= -1;
        fildebugf("** ");
-       for (pos=0; pos < *nscores; pos++)
-         if (scores[pos].pr == bucket) goto found;
+       for (pos=0; pos < nscores; pos++)
+         if (scores[pos].bucket == bucket) goto found;
        /* not found */
        pos= -1;
       found:
        for (;;) {
          pos++;
-         if (pos >= *nscores-1) break; /* new top */
+         if (pos >= nscores-1) break; /* new top */
          if (scores[pos].value >= value[ap]) break; /* found spot */
          if (pos>0)
            scores[pos-1]= scores[pos];
@@ -170,16 +170,19 @@ static double process_route(int nports, int totaldist,
        pos--;
        if (pos>0) {
          scores[pos].value= value[ap];
-         scores[pos].pr= bucket;
-         relevant=1;
+         scores[pos].bucket= bucket;
+         if (granui < granus-1 &&
+             highscores[granui][A][0].bucket &&
+             highscores[granui][P][0].bucket) {
+           /* both absolute and perleague are full at this granularity,
+            * so we don't care about anything more granular */
+           fildebugf("\n                STOP-GRANU            ");
+           granus= granui+1;
+         }
        }
-       fildebugf("@%2d", pos);
+       fildebugf("@%2d/%2d ", pos, nscores);
       } /* new best */
     } /* ap */
-    if (!relevant)
-      /* both absolute and perleague are full at this granularity,
-       * so we don't care about anything more granular */
-      granus= granui+1;
   } /* granui */
 
   fildebugf(" route");
@@ -209,7 +212,7 @@ static void recurse(int last_isle,
 }
 
 void search(int start_isle, int final_isle_spec,
-           PotentialResult ****buckets_base_io[GRANUS]) {
+           Bucket ****buckets_base_io[GRANUS]) {
   int granui;
   for (granui=0; granui<GRANUS; granui++)
     buckets_base[granui]=
index 26a77bb7811dd0be8fc504037a86b8a06f44a20d..23b3bd04af68ff56033d754a1dca60349797fe08 100644 (file)
@@ -7,6 +7,7 @@ sqlite3_stmt *ss_ipair;
 int islandtablesz;
 
 DEBUG_DEFINE_DEBUGF(sql);
+DEBUG_DEFINE_SOME_DEBUGF(sql,debug2f);
 
 static int busy_handler(void *u, int previous) {
   debugf("[[DB BUSY %d]]",previous);
@@ -44,7 +45,7 @@ void sql_fatal(const char *stmt_what, int sqr, const char *act_what) {
 
 void sql_bind(sqlite3_stmt *ss, int index, int value,
              const char *ss_what, const char *val_what) {
-  debugf("SQL BIND %s #%d = %d = %s\n", ss_what, index, value, val_what);
+  debug2f("SQL BIND %s #%d = %d = %s\n", ss_what, index, value, val_what);
   int sqr= sqlite3_bind_int(ss, index, value);
   if (sqr) sql_fatal(ss_what, sqr,
                     masprintf("bind #%d (%s)", index, val_what));
@@ -84,10 +85,10 @@ int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
     sqr= sqlite3_step((ssh));
     switch (sqr) {
     case SQLITE_DONE:
-      debugf("SQL %s DONE\n",ssh_string);
+      debug2f("SQL %s DONE\n",ssh_string);
       return 0;
     case SQLITE_ROW:
-      if (DEBUGP(sql)) {
+      if (DEBUGP(sql2)) {
        int i;
        fprintf(debug,"SQL %s R",ssh_string);
        for (i=0; i<sqlite3_column_count(ssh); i++) {
index 8fe5f229b04b00355817b7225475a9068e8d9621..22b3d2988f89c857196509f83b14678df629073d 100644 (file)
@@ -5,6 +5,7 @@
 #include "rscommon.h"
 
 DEBUG_DEFINE_DEBUGF(value);
+DEBUG_DEFINE_SOME_DEBUGF(value2,debug2f);
 
 typedef struct { int mass, volu; } CommodInfo;
 static int commodstabsz;
@@ -148,7 +149,7 @@ static IslandPair *ipair_get_create(int si, int di) {
   if (si==di) ctr_islands_arbitrage++;
   else ctr_ipairs_relevant++;
 
-  debugf("VALUE ipair_get(i%d,i%d) running...\n", si,di);
+  debug2f("VALUE ipair_get(i%d,i%d) running...\n", si,di);
   SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 1, si) );
   SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 2, di) );
   assert(SQL_STEP(ss_ipair_dist));
@@ -314,6 +315,9 @@ double value_route(int nislands, const int *islands, int exclude_arbitrage) {
   lpx_delete_prob(lp);
   lp= 0;
 
+  debugf("    %s %f\n",
+        exclude_arbitrage ? "base value" : "route value",
+        profit);
   return profit;
 }
 
index 87f1260d63d9e11df3181fc4653dc51ca79eb092..32daa5faa5b868fff0b8a111b71682d4c8a68a01 100644 (file)
@@ -1,3 +1,4 @@
 file ./routesearch
-set args -DN OCEAN-Midnight.db 13460 20210 -1 0.0005 search 10 20 20 circ  4 19 134 32 13 24 36
+set args OCEAN-Midnight.db 13470 20220 -1 0.0005 search 10 10 30 any 34 7 5 10 35 24 2 2>u
+#break rssearch.c:179 if ports[0]==24 && ports[1]==21
 run