From fe2cd54600f099689170bc2d9c47a48559c1d692 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 4 Oct 2009 00:04:54 +0100 Subject: [PATCH] WIP routesearch; route tail guesswork pruning optimisation --- yarrg/rscommon.h | 14 +++++++++++++- yarrg/rsmain.c | 2 +- yarrg/rssearch.c | 45 +++++++++++++++++++++++++++++++++++++-------- yarrg/rsvalue.c | 19 ++++++++----------- 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/yarrg/rscommon.h b/yarrg/rscommon.h index a9bca8c..f62fd99 100644 --- a/yarrg/rscommon.h +++ b/yarrg/rscommon.h @@ -48,8 +48,20 @@ void sql_bind(sqlite3_stmt *ss, int index, int value, 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_search(void); void search(int start_isle); diff --git a/yarrg/rsmain.c b/yarrg/rsmain.c index 868d790..0982889 100644 --- a/yarrg/rsmain.c +++ b/yarrg/rsmain.c @@ -46,7 +46,7 @@ int main(int argc, const char **argv) { 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++); diff --git a/yarrg/rssearch.c b/yarrg/rssearch.c index f14b0cd..eb40a47 100644 --- a/yarrg/rssearch.c +++ b/yarrg/rssearch.c @@ -36,18 +36,44 @@ static Neighbour *get_neighbours(int isle) { 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 leagues_divisor= totaldist + nports; debugf("========== ROUTE"); for (i=0; i=2) { + int pair[2], i; + pair[1]= ports[nports-1]; + double guess_absolute= overestimate_excepting_tail; + + for (i=0; iroute_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); \ @@ -62,13 +88,16 @@ static void process_route(int nports, int totaldist) { for (i=0; i= 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; - recurse(add->islandid, nports, newdist); + recurse(add->islandid, nports, newdist, estimate); } } void search(int start_isle) { - recurse(start_isle,0,0); + recurse(start_isle,0,0,1e6); } void setup_search(void) { diff --git a/yarrg/rsvalue.c b/yarrg/rsvalue.c index b4e2322..eb4d3f0 100644 --- a/yarrg/rsvalue.c +++ b/yarrg/rsvalue.c @@ -21,18 +21,12 @@ typedef struct { #define TRADES_PER_BLOCK 10 -typedef struct TradesBlock{ +typedef struct 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; @@ -136,7 +130,7 @@ static void add_leg_c(int startrow, int leg, double 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); @@ -151,6 +145,7 @@ static IslandPair *ipair_get(int si, int di) { 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; @@ -187,7 +182,7 @@ static IslandPair *ipair_get(int si, int di) { 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 @@ -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]; - for (d=s; d