chiark / gitweb /
WIP routesearch; route tail guesswork pruning optimisation
authorIan Jackson <ian@liberator.(none)>
Sat, 3 Oct 2009 23:04:54 +0000 (00:04 +0100)
committerIan Jackson <ian@liberator.(none)>
Sat, 3 Oct 2009 23:04:54 +0000 (00:04 +0100)
yarrg/rscommon.h
yarrg/rsmain.c
yarrg/rssearch.c
yarrg/rsvalue.c

index a9bca8c1fdadc87dfab844bcde2f28d9f4fe8350..f62fd99d9ed15cdf4d9cd873a6406c3d7a60d38b 100644 (file)
@@ -48,8 +48,20 @@ void sql_bind(sqlite3_stmt *ss, int index, int value,
 extern sqlite3 *db;
 
 void setup_sql(void);
 extern sqlite3 *db;
 
 void setup_sql(void);
-double value_route(int nislands, const int *islands);
+
+
+typedef struct {
+  double distance_loss_factor;
+  int ntrades;
+  struct TradesBlock *trades;
+  double route_tail_value;
+} IslandPair;
+
+IslandPair *ipair_get(int si, int di);
+
+double value_route(int nislands, const int *islands, int exclude_arbitrage);
 void setup_value(void);
 void setup_value(void);
+
 void setup_search(void);
 void search(int start_isle);
 
 void setup_search(void);
 void search(int start_isle);
 
index 868d790411d4db5afd9457349f0f1a9b2d684103..0982889c8b3f8fc34d2c25e9fb4fe314f3d79a84 100644 (file)
@@ -46,7 +46,7 @@ int main(int argc, const char **argv) {
     while ((arg= *argv++))
       ia[ni++]= atoi(arg);
 
     while ((arg= *argv++))
       ia[ni++]= atoi(arg);
 
-    double val= value_route(ni, ia);
+    double val= value_route(ni, ia, 0);
     printf("route value is %g\n", val);
   } else if (!strcmp(arg,"search")) {
     max_dist= atoi(*argv++);
     printf("route value is %g\n", val);
   } else if (!strcmp(arg,"search")) {
     max_dist= atoi(*argv++);
index f14b0cd7a87670e6785d6a34bcc2d0adca4e3a22..eb40a47fbdeba5cb144118e53fc40b9de099ad82 100644 (file)
@@ -36,18 +36,44 @@ static Neighbour *get_neighbours(int isle) {
 
 static double best_absolute, best_perleague;
 
 
 static double best_absolute, best_perleague;
 
-static void process_route(int nports, int totaldist) {
+static double process_route(int nports, int totaldist,
+                           double overestimate_excepting_tail) {
   int i;
   int i;
+  int leagues_divisor= totaldist + nports;
 
   debugf("========== ROUTE");
   for (i=0; i<nports; i++)
     debugf(" %d",ports[i]);
   debugf("\n");
 
 
   debugf("========== ROUTE");
   for (i=0; i<nports; i++)
     debugf(" %d",ports[i]);
   debugf("\n");
 
-  double absolute= value_route(nports, ports);
-  double perleague= absolute / (totaldist + nports);
+  if (nports>=2) {
+    int pair[2], i;
+    pair[1]= ports[nports-1];
+    double guess_absolute= overestimate_excepting_tail;
+    
+    for (i=0; i<nports; i++) {
+      pair[0]= ports[i];
+      IslandPair *ip= ipair_get(ports[nports-2], ports[nports-1]);
+      if (ip->route_tail_value < 0)
+       ip->route_tail_value= value_route(2, pair, pair[0]!=pair[1]);
+      guess_absolute += ip->route_tail_value;
+    }
+    double guess_perleague= guess_absolute / leagues_divisor;
+
+    if (guess_absolute <= best_absolute && guess_perleague <= best_perleague) {
+      debugf(" ELIM %f %f\n", guess_absolute, guess_perleague);
+      return guess_absolute;
+    }
+    debugf(" COMPUTE %f %f\n", guess_absolute, guess_perleague);
+  }
+
+  double absolute= value_route(nports, ports, 0);
+  double perleague= absolute / leagues_divisor;
 
 
-  if (absolute < best_absolute && perleague < best_perleague) return;
+  if (absolute <= best_absolute && perleague <= best_perleague)
+    return absolute;
+
+  debugf(" SOMEHOW BEST\n");
 
 #define CHK(absperl)                                   \
   fprintf(stderr,#absperl " %15f", absperl);           \
 
 #define CHK(absperl)                                   \
   fprintf(stderr,#absperl " %15f", absperl);           \
@@ -62,13 +88,16 @@ static void process_route(int nports, int totaldist) {
   for (i=0; i<nports; i++)
     fprintf(stderr," %d",ports[i]);
   putc('\n',stderr);
   for (i=0; i<nports; i++)
     fprintf(stderr," %d",ports[i]);
   putc('\n',stderr);
+
+  return absolute;
 }
 
 static void recurse(int last_isle,
                    int nports, /* excluding last_isle */
 }
 
 static void recurse(int last_isle,
                    int nports, /* excluding last_isle */
-                   int totaldist /* including last_isle */) {
+                   int totaldist /* including last_isle */,
+                   double last_estimate) {
   ports[nports++]= last_isle;
   ports[nports++]= last_isle;
-  process_route(nports, totaldist);
+  double estimate= process_route(nports, totaldist, last_estimate);
   if (nports >= MAX_ROUTELEN) return;
 
   Neighbour *add;
   if (nports >= MAX_ROUTELEN) return;
 
   Neighbour *add;
@@ -76,12 +105,12 @@ static void recurse(int last_isle,
     int newdist= totaldist + add->dist;
     if (newdist > max_dist) continue;
 
     int newdist= totaldist + add->dist;
     if (newdist > max_dist) continue;
 
-    recurse(add->islandid, nports, newdist);
+    recurse(add->islandid, nports, newdist, estimate);
   }
 }
 
 void search(int start_isle) {
   }
 }
 
 void search(int start_isle) {
-  recurse(start_isle,0,0);
+  recurse(start_isle,0,0,1e6);
 }
 
 void setup_search(void) {
 }
 
 void setup_search(void) {
index b4e23227d2b42ca598612ac21cb8248150cee9a4..eb4d3f053480f82ef5b04ab710488054d1568c2c 100644 (file)
@@ -21,18 +21,12 @@ typedef struct {
 
 #define TRADES_PER_BLOCK 10
 
 
 #define TRADES_PER_BLOCK 10
 
-typedef struct TradesBlock{
+typedef struct TradesBlock {
   struct TradesBlock *next;
   Trade t[TRADES_PER_BLOCK];
 } TradesBlock;
 
   struct TradesBlock *next;
   Trade t[TRADES_PER_BLOCK];
 } TradesBlock;
 
-typedef struct {
-  double distance_loss_factor;
-  int ntrades;
-  TradesBlock *trades;
-} IslandPair;
-
-IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
+static IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
 
 typedef struct IslandTradeEnd {
   struct IslandTradeEnd *next;
 
 typedef struct IslandTradeEnd {
   struct IslandTradeEnd *next;
@@ -136,7 +130,7 @@ static void add_leg_c(int startrow, int leg, double value) {
   add_constraint(startrow+leg, value);
 }
 
   add_constraint(startrow+leg, value);
 }
 
-static IslandPair *ipair_get(int si, int di) {
+IslandPair *ipair_get(int si, int di) {
   IslandPair *ip, **ipa;
 
   assert(si < islandtablesz);
   IslandPair *ip, **ipa;
 
   assert(si < islandtablesz);
@@ -151,6 +145,7 @@ static IslandPair *ipair_get(int si, int di) {
   ipa[di]= ip= mmalloc(sizeof(*ip));
   ip->ntrades= 0;
   ip->trades= 0;
   ipa[di]= ip= mmalloc(sizeof(*ip));
   ip->ntrades= 0;
   ip->trades= 0;
+  ip->route_tail_value= -1;
   int inblock= TRADES_PER_BLOCK;
   TradesBlock *block=0, **tail=&ip->trades;
 
   int inblock= TRADES_PER_BLOCK;
   TradesBlock *block=0, **tail=&ip->trades;
 
@@ -187,7 +182,7 @@ static IslandPair *ipair_get(int si, int di) {
   return ip;
 }
 
   return ip;
 }
 
-double value_route(int nislands, const int *islands) {
+double value_route(int nislands, const int *islands, int exclude_arbitrage) {
   int s,d;
 
   /* We need to construct the LP problem.  GLPK talks
   int s,d;
 
   /* We need to construct the LP problem.  GLPK talks
@@ -244,7 +239,9 @@ double value_route(int nislands, const int *islands) {
        s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) {
     int si= islands[s];
     
        s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) {
     int si= islands[s];
     
-    for (d=s; d<nislands; d++) {
+    for (d= s + exclude_arbitrage;
+        d < nislands;
+        d++) {
       int di= islands[d];
       int already_d;
       for (already_d=s+1; already_d<d; already_d++)
       int di= islands[d];
       int already_d;
       for (already_d=s+1; already_d<d; already_d++)