chiark / gitweb /
routesearch: three different bucket sizes for better result sets
[ypp-sc-tools.main.git] / yarrg / rsmain.c
index 2ed1e0e7dae61cc16aa1a454e1c4f4904e9147e2..50b73485e02685f293b616d9c890c5f85d69fd4f 100644 (file)
 
 #include "rscommon.h"
 
+#include <ctype.h>
+
 int o_quiet= 0;
+double max_mass=-1, max_volu=-1, max_capi=-1;
+double distance_loss_factor_per_league;
+int max_dist= -1;
 
-int main(int argc, const char **argv) {
-  int ia[argc], ni=0;
+FILE *debug_file;
+FILE *output;
+
+#define tabdebugf printf
+
+
+#define CTR(x)    int ctr_##x;
+#define CTRA(x,n) int ctr_##x[n];
+  COUNTER_LIST
+#undef CTR
+#undef CTRA
 
-  debug_flags= ~0UL;
-  
+static PotentialResult ****results[STRATS];
+  /* results[STRATS][start_isle_ix][finalisle][midisle]-> */
+
+static pid_t debugoutpid;
+
+int main(int argc, const char **argv) {
   const char *arg;
-  while ((arg= *++argv)) {
-    ia[ni++]= atoi(arg);
+  int i, ap;
+  int strati;
+
+#ifndef debug_flags
+  debug_flags= ~( dbg_sql2 );
+#endif
+
+  for (;;) {
+    arg= *++argv;
+    if (arg[0] != '-') break;
+#ifndef debug_flags
+    if (!strcmp(arg,"-DN")) {
+      debug_flags= 0;
+    } else
+#endif
+    {
+      abort();
+    }
+  }
+
+  if (debug_flags) {
+    /* glpk insists on writing stuff to stdout, and it does buffering,
+     * so we route all our debug through it this too */
+    int realstdout;
+    sysassert( (realstdout= dup(1)) > 2 );
+    sysassert( output= fdopen(realstdout,"w") );
+
+    int pfd[2];
+    sysassert(! pipe(pfd) );
+    sysassert( (debugoutpid= fork()) >=0 );
+    if (!debugoutpid) {
+      sysassert( dup2(pfd[0],0)==0 );
+      sysassert( dup2(2,1)==1 );
+      sysassert(! close(pfd[0]) );
+      sysassert(! close(pfd[1]) );
+      sysassert(! execlp("cat","cat",(char*)0) );
+    }
+    sysassert( dup2(pfd[1],1)==1 );
+    sysassert(! close(pfd[0]) );
+    sysassert(! close(pfd[1]) );
+
+    debug_file= stdout;
+  } else {
+    output= stdout;
+    debug_file= stderr;
+  }
+
+  const char *database= *argv++;
+
+  sysassert( !setvbuf(debug,0,_IOLBF,0) );
+
+  max_mass= atof(*argv++);
+  max_volu= atof(*argv++);
+  max_capi= atof(*argv++);
+  double loss_per_league= atof(*argv++);
+  distance_loss_factor_per_league= 1.0 - loss_per_league;
+
+  setup_sql(database);
+  setup_value();
+  setup_search();
+
+  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;
+    while ((arg= *argv++))
+      ia[ni++]= atoi(arg);
+
+    double val= value_route(ni, ia, 0);
+    fprintf(output, "route value is %g\n", val);
+  } else if (!strcmp(arg,"search")) {
+    for (strati=0; strati<STRATS; strati++)
+      MCALLOC(results[strati], argc);
+
+    max_dist= atoi(*argv++);
+
+    for (ap=0; ap<AP; ap++) {
+      int nhs= atoi(*argv++);
+      for (strati=0; strati<STRATS; strati++) {
+       nhighscores[strati][ap]= nhs;
+       MCALLOC(highscores[strati][ap], nhs);
+      }
+    }
+    const char *final_isle_spec= *argv++;
+
+    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;
+      else final_isle= atoi(final_isle_spec);
+      assert(final_isle);
+
+      PotentialResult ****strat_base_io[STRATS];
+      for (strati=0; strati<STRATS; strati++)
+       strat_base_io[strati]= &results[strati][resultsix];
+       
+      search(init_isle, final_isle, strat_base_io);
+      resultsix++;
+    }
+
+    int mid, fin;
+    for (strati=minstrat; strati<STRATS; strati++) {
+      fprintf(output,"\n");
+      for (i=0; i<resultsix; i++) {
+       tabdebugf("========== start #%d strati%d %s [PARTIAL] ==========\n",
+                 i, strati, argv[i]);
+       PotentialResult ***strat_resultsix= results[strati][i];
+       if (!strat_resultsix) continue;
+       tabdebugf("    ");
+       for (mid=0; mid<stratsz_mid[strati]; mid++) {
+         tabdebugf("|   m%-3d   ",mid);
+       }
+       tabdebugf("\n");
+       for (fin=0; fin<stratsz_fin[strati]; fin++) {
+         PotentialResult **strat_fin= strat_resultsix[fin];
+         if (!strat_fin) continue;
+         tabdebugf("f%-3d",fin);
+         for (mid=0; mid<stratsz_mid[strati]; mid++) {
+           PotentialResult *result= strat_fin[mid];
+           if (!result) {
+             tabdebugf("|          ");
+           } else {
+             tabdebugf("|%5d",(int)(result->value[A]));
+             tabdebugf(" ");
+             tabdebugf("%4d",(int)(result->value[P]));
+           }
+         }
+         tabdebugf("\n");
+       }
+      } /* i */
+
+      for (ap=0; ap<AP; ap++) {
+       int pos;
+       fprintf(output,"============== strati%d ap=%d ==============\n",
+               strati, ap);
+       for (pos=0; pos<nhighscores[strati][ap]; pos++) {
+         HighScoreEntry *hs= &highscores[strati][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 midisle= ports[nports/2];
+         int midarch= route2midarch(ports,nports);
+         fprintf(output,
+                 " @%2d #%2d | start%3d mid%d:%3d f%d:%3d | %5d %5d %4d |",
+                 pos, nhighscores[strati][ap] - 1 - pos,
+                 ports[0], midarch,midisle, 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");
+       } /* pos */
+      } /* ap */
+    } /* strati */
+    fprintf(output,"\n");
+
+  } else {
+    abort();
   }
-  value_route(ni, ia);
+
+#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
+
+  if (debug_flags) {
+    sysassert(! fclose(debug) );
+    waitpid_check_exitstatus(debugoutpid,"debug cat",1);
+  }
+  sysassert(! fclose(output) );
+
   return 0;
 }