From 8aaade51e5904619daf638d496ce1162138d940f Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 2 Oct 2009 20:52:31 +0100 Subject: [PATCH] WIP route value calculation - compiles, although doesn't run the LP --- yarrg/Makefile | 2 +- yarrg/rscommon.h | 11 ++- yarrg/rsmain.c | 10 ++ yarrg/rssetup.c | 9 ++ yarrg/rsvalue.c | 242 ++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 247 insertions(+), 27 deletions(-) diff --git a/yarrg/Makefile b/yarrg/Makefile index 9019bd2..ca05ff5 100644 --- a/yarrg/Makefile +++ b/yarrg/Makefile @@ -45,7 +45,7 @@ yarrg: $(CONVERT_OBJS) $(COMMON_OBJS) -lnetpbm -lXtst -lX11 -lpcre -lm $(CONVERT_OBJS): common.h ocr.h convert.h structure.h -routesearch: $(ROUTESEARCH_OBJS) $(COMMON_OBJS) -lsqlite3 +routesearch: $(ROUTESEARCH_OBJS) $(COMMON_OBJS) -lsqlite3 -lglpk -lm $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(ROUTESEARCH_OBJS): common.h rscommon.h diff --git a/yarrg/rscommon.h b/yarrg/rscommon.h index 8bfe3e1..b1003bf 100644 --- a/yarrg/rscommon.h +++ b/yarrg/rscommon.h @@ -24,13 +24,22 @@ int sql_step_wrap(sqlite3_stmt *ssh, const char *ssh_string, const char *file, int line); +int sql_single_int(const char *stmt); + +#define MAX_ROUTELEN 20 + #include "common.h" extern sqlite3 *db; -extern sqlite3_stmt *ss_ipair; void setup(void); void value_route(int nislands, const int *islands); void setup_value(void); +void setup_commods(void); + +extern double max_mass, max_volu, max_capi; +extern double distance_loss_factor_per_league; + +#define LOSS_FACTOR_PER_DELAY_SLOT (1-1e8) #endif /*RSCOMMON_H*/ diff --git a/yarrg/rsmain.c b/yarrg/rsmain.c index 85f271b..ed17635 100644 --- a/yarrg/rsmain.c +++ b/yarrg/rsmain.c @@ -3,12 +3,22 @@ #include "rscommon.h" int o_quiet= 0; +double max_mass=-1, max_volu=-1, max_capi=-1; +double distance_loss_factor_per_league; int main(int argc, const char **argv) { int ia[argc], ni=0; debug_flags= ~0UL; setup(); + + max_mass= atof(*++argv); + max_volu= atof(*++argv); + max_capi= atof(*++argv); + double loss_per_league= atof(*++argv); + + if (!loss_per_league) loss_per_league= 1e-7; + distance_loss_factor_per_league= 1.0 - loss_per_league; const char *arg; while ((arg= *++argv)) { diff --git a/yarrg/rssetup.c b/yarrg/rssetup.c index e169dd5..88b6fc6 100644 --- a/yarrg/rssetup.c +++ b/yarrg/rssetup.c @@ -23,6 +23,15 @@ void setup(void) { setup_value(); } +int sql_single_int(const char *stmt) { + sqlite3_stmt *sst; + SQL_MUST( sqlite3_prepare(db, stmt, -1,&sst,0) ); + assert( SQL_STEP(sst) ); + int rv= sqlite3_column_int(sst,0); + sqlite3_finalize(sst); + return rv; +} + int sql_step_wrap(sqlite3_stmt *ssh, const char *ssh_string, const char *file, int line) { for (;;) { diff --git a/yarrg/rsvalue.c b/yarrg/rsvalue.c index c2cab43..9cc9ec7 100644 --- a/yarrg/rsvalue.c +++ b/yarrg/rsvalue.c @@ -1,9 +1,20 @@ /**/ +#include +#include + #include "rscommon.h" DEBUG_DEFINE_DEBUGF(value); +typedef struct { int mass, volu; } CommodInfo; +static int commodstablesz; +static CommodInfo *commodstable; + +static sqlite3_stmt *ss_ipair_dist, *ss_ipair_trades; + +#define MAX_LEGS (MAX_ROUTELEN-1) + typedef struct { int commodid, src_price, src_qty, dst_price, dst_qty; } Trade; @@ -16,6 +27,7 @@ typedef struct TradesBlock{ } TradesBlock; typedef struct { + double distance_loss_factor; int ntrades; TradesBlock *trades; } IslandPair; @@ -23,6 +35,82 @@ typedef struct { int nislands; IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */ +typedef struct IslandTradeEnd { + struct IslandTradeEnd *next; + int commodid, price; + int qty, rownum; +} IslandTradeEnd, *IslandDirnTradeEnds; + +typedef struct { + int islandid; + IslandDirnTradeEnds collect, deliver; +} IslandTradeEnds; + +static struct obstack ites_obstack; +static LPX *lp; + +static int nconstraint_rows; +static int constraint_rows[1+2+3*MAX_LEGS]; +static double constraint_coeffs[1+2+3*MAX_LEGS]; + /* dummy0, src, dst, for_each_leg( [mass], [volume], [capital] ) */ + +static void add_constraint(int row, double coefficient) { + nconstraint_rows++; /* glpk indices start from 1 !!! */ + constraint_rows [nconstraint_rows]= row; + constraint_coeffs[nconstraint_rows]= coefficient; +} + +static void avail_constraint(const Trade *t, IslandDirnTradeEnds *trades, + int price, int qty, const char *srcdst) { + /* find row number of trade availability constraint */ + IslandTradeEnd *search; + + for (search= *trades; search; search=search->next) + if (search->commodid==t->commodid && search->price==price) { + assert(search->qty == qty); + goto found; + } + /* not found, add new row */ + search= obstack_alloc(&ites_obstack, sizeof(*search)); + search->commodid= t->commodid; + search->price= price; + search->qty= qty; + search->rownum= lpx_add_rows(lp, 1); + search->next= *trades; + if (DEBUGP(value)) { + char *name= masprintf("%s_commod%d_price%d",srcdst,t->commodid,price); + lpx_set_row_name(lp,search->rownum,name); + free(name); + } + *trades= search; + found:; + int rownum= search->rownum; + + lpx_set_row_bnds(lp, rownum, LPX_UP, 0, qty); + add_constraint(rownum, 1.0); +} + +static int setup_leg_constraints(double max_thing, int legs, const char *wh) { + int leg, startrow; + if (max_thing < 0 || !legs) return -1; + startrow= lpx_add_rows(lp, legs); + for (leg=0; legntrades= 0; ip->trades= 0; @@ -42,10 +130,17 @@ static IslandPair *ipair_get(int si, int di) { TradesBlock *block= 0; debugf("VALUE ipair_get(%d,%d) running...\n", si,di); - SQL_MUST( sqlite3_bind_int(ss_ipair, 1, si) ); - SQL_MUST( sqlite3_bind_int(ss_ipair, 2, 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)); + int dist= sqlite3_column_int(ss_ipair_dist, 0); + ip->distance_loss_factor= pow(distance_loss_factor_per_league, dist); + sqlite3_reset(ss_ipair_dist); - while (SQL_STEP(ss_ipair)) { + SQL_MUST( sqlite3_bind_int(ss_ipair_trades, 1, si) ); + SQL_MUST( sqlite3_bind_int(ss_ipair_trades, 2, di) ); + + while (SQL_STEP(ss_ipair_trades)) { if (inblock == TRADES_PER_BLOCK) { block= mmalloc(sizeof(*block)); block->next= ip->trades; @@ -54,14 +149,14 @@ static IslandPair *ipair_get(int si, int di) { } int *irp, i; for (i=0, irp=&block->t[inblock].commodid; i<5; i++, irp++) - *irp= sqlite3_column_int(ss_ipair, i); + *irp= sqlite3_column_int(ss_ipair_trades, i); ip->ntrades++; inblock++; } if (inblock < TRADES_PER_BLOCK) block->t[inblock].commodid= -1; - sqlite3_reset(ss_ipair); + sqlite3_reset(ss_ipair_trades); return ip; } @@ -74,11 +169,11 @@ void value_route(int nislands, const int *islands) { * * Each column is a `structural variable' ie one of the entries in * the objective function. In our case the set of structural - * variable is, for each visit, the set of Trades which collect at - * that island. (We use `visit' to mean `specific visit to an + * variable is, for each port, the set of Trades which collect at + * that island. (We use `port' to mean `specific visit to an * island' so if an island appears more than once so do its trades.) * We don't need to worry about crossing with all the possible - * delivery locations as we always deliver on the first visit. + * delivery locations as we always deliver on the first port. * We will call such a structural variable a Flow, for brevity. * * We iterate over the possible Flows adding them as columns as we @@ -99,24 +194,70 @@ void value_route(int nislands, const int *islands) { * a data structure for later reuse. */ + assert(nislands >= 1); + + char *free_to_reset_obstack= obstack_alloc(&ites_obstack, 1); + + int nites=0; + IslandTradeEnds ites[nislands], *iteps[nislands]; + for (s=0; sislandid==si) + goto found; + /* not found, add new */ + assert(ite == &ites[nites]); + ite->islandid= si; + ite->collect= ite->deliver= 0; + nites++; + found: + iteps[s]= ite; + } + + assert(!lp); + lp= lpx_create_prob(); + lpx_set_obj_dir(lp, LPX_MAX); + if (DEBUGP(value)) { + lpx_set_prob_name(lp,(char*)"value_route"); + lpx_set_obj_name(lp,(char*)"profit"); + } + + int legs= nislands-1; + int mass_constraints= setup_leg_constraints(max_mass, legs, "mass"); + int volu_constraints= setup_leg_constraints(max_volu, legs, "volu"); + int capi_constraints= setup_leg_constraints(max_capi, legs, "capi"); + + double delay_slot_loss_factor= 1.0; + for (s=0; + ss && di==si) - /* route has returned to s, no need to think more about this */ - goto next_d; + /* route has returned to si, no need to think more about s */ + goto next_s; + + /*----- actually add these trades to the LP problem -----*/ IslandPair *ip= ipair_get(islands[s], islands[d]); TradesBlock *block= ip->trades; - int ntrades= ip->ntrades; + int tradestodo= ip->ntrades; int inblock= 0; - while (ntrades-- >0) { + int col= lpx_add_cols(lp,ip->ntrades); + + double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor; + + while (tradestodo-- >0) { if (inblock >= TRADES_PER_BLOCK) { block= block->next; inblock= 0; @@ -125,24 +266,71 @@ void value_route(int nislands, const int *islands) { debugf(" TRADE %d#%d..%d#%d %d %d-%d\n", si,s, di,d, t->commodid, t->src_price, t->dst_price); - - } - } + + nconstraint_rows=0; + + avail_constraint(t, &iteps[s]->collect, t->src_price,t->src_qty,"src"); + avail_constraint(t, &iteps[d]->deliver, t->dst_price,t->dst_qty,"dst"); + + int leg; + for (leg=s; legcommodid].mass); + add_leg_c(volu_constraints, leg, commodstable[t->commodid].volu); + add_leg_c(capi_constraints, leg, t->src_price); + } + + lpx_set_col_bnds(lp, col, LPX_LO, 0, 0); + lpx_set_obj_coef(lp, col, + (t->dst_price - t->src_price) * loss_factor); + lpx_set_mat_col(lp, col, nconstraint_rows, + constraint_rows, constraint_coeffs); + if (DEBUGP(value)) { + char *name= masprintf("trade_commod%d_port%d_at%d_port%d_at%d", + t->commodid, s, t->src_price, d, t->dst_price); + lpx_set_col_name(lp, col, name); + free(name); + } + } /* while (tradestodo-- >0) */ + + /*----- that's done adding these trades to the LP problem -----*/ + next_d:; - } + } /* for (d) */ + next_s:; + } /* for (s) */ + + lpx_delete_prob(lp); + lp= 0; + + obstack_free(&ites_obstack, free_to_reset_obstack); } void setup_value(void) { sqlite3_stmt *sst; + int i; - SQL_MUST( sqlite3_prepare(db, "SELECT max(islandid) FROM islands", - -1,&sst,0) ); - assert( SQL_STEP(sst) ); - nislands= sqlite3_column_int(sst,0); - nislands++; - sqlite3_finalize(sst); +#define obstack_chunk_alloc mmalloc +#define obstack_chunk_free free + obstack_init(&ites_obstack); + + nislands= sql_single_int("SELECT max(islandid) FROM islands") + 1; debugf("VALUE nislands=%d\n",nislands); + commodstablesz= sql_single_int("SELECT max(commodid) FROM commods") + 1; + commodstable= mmalloc(sizeof(*commodstable)*commodstablesz); + for (i=0; i=0 && id