X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Frsvalue.c;h=eb4d3f053480f82ef5b04ab710488054d1568c2c;hb=fe2cd54600f099689170bc2d9c47a48559c1d692;hp=a1cdf0f9549c79ebb206d90ffcab7d871fe05b72;hpb=a040d95f6eaf2e43a35e13f50660d9605c638a37;p=ypp-sc-tools.db-live.git diff --git a/yarrg/rsvalue.c b/yarrg/rsvalue.c index a1cdf0f..eb4d3f0 100644 --- a/yarrg/rsvalue.c +++ b/yarrg/rsvalue.c @@ -16,24 +16,17 @@ static sqlite3_stmt *ss_ite_buy, *ss_ite_sell; #define MAX_LEGS (MAX_ROUTELEN-1) typedef struct { - int commodid, src_price, src_qty, dst_price, dst_qty; + int commodid, src_price, dst_price; } Trade; #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; - -int nislands; -IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */ +static IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */ typedef struct IslandTradeEnd { struct IslandTradeEnd *next; @@ -41,12 +34,14 @@ typedef struct IslandTradeEnd { int qty; unsigned long generation; int rownum; -} IslandTradeEnd, *IslandDirnTradeEnds; +} IslandTradeEnd; typedef struct { - int islandid; - IslandDirnTradeEnds collect, deliver; -} IslandTradeEnds; + IslandTradeEnd *src, *dst; +} IslandTradeEndHeads; + +IslandTradeEndHeads *itradeends; + /* itradeends[islandid].{src,dst}->commodid etc. */ static LPX *lp; static unsigned long generation; @@ -62,7 +57,7 @@ static void add_constraint(int row, double coefficient) { constraint_coeffs[nconstraint_rows]= coefficient; } -static void avail_c(const Trade *t, IslandDirnTradeEnds *trades, +static void avail_c(const Trade *t, IslandTradeEnd **trades, int price, const char *srcdst, int islandid, sqlite3_stmt *ss_ite) { /* find row number of trade availability constraint */ @@ -79,9 +74,9 @@ static void avail_c(const Trade *t, IslandDirnTradeEnds *trades, search->next= *trades; search->generation= 0; - SQL_MUST( sqlite3_bind_int(ss_ite, 1, islandid) ); - SQL_MUST( sqlite3_bind_int(ss_ite, 2, t->commodid) ); - SQL_MUST( sqlite3_bind_int(ss_ite, 3, price) ); + SQL_BIND(ss_ite, 1, islandid); + SQL_BIND(ss_ite, 2, t->commodid); + SQL_BIND(ss_ite, 3, price); assert(SQL_STEP(ss_ite)); search->qty= sqlite3_column_int(ss_ite, 0); SQL_MUST( sqlite3_reset(ss_ite) ); @@ -93,9 +88,18 @@ static void avail_c(const Trade *t, IslandDirnTradeEnds *trades, search->rownum= lpx_add_rows(lp, 1); lpx_set_row_bnds(lp, search->rownum, LPX_UP, 0, search->qty); - if (DEBUGP(value)) { - char *name= masprintf("%s_commod%d_price%d",srcdst,t->commodid,price); + if (DEBUGP(value) || DEBUGP(check)) { + char *name= masprintf("%s_i%d_c%d_%d_all", + srcdst, islandid, t->commodid, price); lpx_set_row_name(lp,search->rownum,name); + + if (DEBUGP(check)) { + int nrows= lpx_get_num_rows(lp); + assert(search->rownum == nrows); + int i; + for (i=1; igeneration= generation; @@ -108,11 +112,11 @@ 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; + ip->route_tail_value= -1; int inblock= TRADES_PER_BLOCK; - TradesBlock *block= 0; + TradesBlock *block=0, **tail=&ip->trades; debugf("VALUE ipair_get(%d,%d) running...\n", si,di); SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 1, si) ); @@ -158,12 +163,13 @@ static IslandPair *ipair_get(int si, int di) { while (SQL_STEP(ss_ipair_trades)) { if (inblock == TRADES_PER_BLOCK) { block= mmalloc(sizeof(*block)); - block->next= ip->trades; - ip->trades= block; + block->next= 0; + *tail= block; + tail= &block->next; inblock= 0; } int *irp, i; - for (i=0, irp=&block->t[inblock].commodid; i<5; i++, irp++) + for (i=0, irp=&block->t[inblock].commodid; i<3; i++, irp++) *irp= sqlite3_column_int(ss_ipair_trades, i); ip->ntrades++; inblock++; @@ -176,7 +182,7 @@ static IslandPair *ipair_get(int si, int di) { return ip; } -void 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 @@ -212,28 +218,11 @@ void value_route(int nislands, const int *islands) { assert(nislands >= 1); assert(++generation); - 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); + lpx_set_int_parm(lp, LPX_K_MSGLEV, DEBUGP(lp) ? 3 : 1); + if (DEBUGP(value)) { lpx_set_prob_name(lp,(char*)"value_route"); lpx_set_obj_name(lp,(char*)"profit"); @@ -250,7 +239,9 @@ void value_route(int nislands, const int *islands) { s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) { int si= islands[s]; - for (d=s; dntrades); double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor; + debugf(" SOME i%d#%d..i%d#%d dslf=%g dlf=%g lf=%g\n", + si,s, di,d, + delay_slot_loss_factor, ip->distance_loss_factor, loss_factor); while (tradestodo-- >0) { if (inblock >= TRADES_PER_BLOCK) { @@ -282,32 +276,37 @@ void value_route(int nislands, const int *islands) { } Trade *t= &block->t[inblock++]; - debugf(" TRADE %d#%d..%d#%d %d %d-%d\n", + debugf(" TRADE i%d#%d..i%d#%d c%d %d-%d ", si,s, di,d, t->commodid, t->src_price, t->dst_price); nconstraint_rows=0; - avail_c(t, &iteps[s]->collect, t->src_price,"src", si, ss_ite_sell); - avail_c(t, &iteps[d]->deliver, t->dst_price,"dst", di, ss_ite_buy); + avail_c(t, &itradeends[si].src, t->src_price, "src", si, ss_ite_sell); + avail_c(t, &itradeends[di].dst, t->dst_price, "dst", di, ss_ite_buy); 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); + add_leg_c(mass_constraints,leg, commodstable[t->commodid].mass*1e-3); + add_leg_c(volu_constraints,leg, commodstable[t->commodid].volu*1e-3); + add_leg_c(capi_constraints,leg, t->src_price); } + double unit_profit= t->dst_price * loss_factor - t->src_price; + debugf(" unit profit %f\n", unit_profit); + 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_obj_coef(lp, col, unit_profit); 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", + char *name= masprintf("c%d_p%d_%d_p%d_%d", t->commodid, s, t->src_price, d, t->dst_price); lpx_set_col_name(lp, col, name); free(name); } + + col++; } /* while (tradestodo-- >0) */ /*----- that's done adding these trades to the LP problem -----*/ @@ -317,24 +316,42 @@ void value_route(int nislands, const int *islands) { next_s:; } /* for (s) */ + double profit= 0; + + if (lpx_get_num_cols(lp)) { + if (DEBUGP(lp)) + lpx_write_cpxlp(lp, (char*)DEBUG_DEV); + + int ipr= lpx_simplex(lp); + assert(ipr==LPX_E_OK); + + if (DEBUGP(lp)) + lpx_print_sol(lp, (char*)DEBUG_DEV); + + int lpst= lpx_get_status(lp); + assert(lpst == LPX_OPT); + profit= lpx_get_obj_val(lp); + } + lpx_delete_prob(lp); lp= 0; + + return profit; } void setup_value(void) { sqlite3_stmt *sst; int i; - 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 sell.price\n" - " WHERE sell.islandid=?\n" - " AND buy.islandid=?", - -1, &ss_ipair_trades, 0) ); - -#define BS(bs) \ - SQL_MUST( sqlite3_prepare(db, \ - "SELECT\n" \ - " sum(qty)\n" \ - " FROM " #bs "\n" \ - " WHERE islandid=?\n" \ - " AND commodid=?\n" \ - " AND price=?", \ - -1, &ss_ite_##bs, 0) ); + SQL_PREPARE(ss_ipair_dist, + " SELECT dist FROM dists\n" + " WHERE aiid=? and biid=?"); + + SQL_PREPARE(ss_ipair_trades, + "SELECT DISTINCT\n" + " sell.commodid commodid,\n" + " sell.price src_price,\n" + " buy.price dst_price\n" + " FROM sell JOIN buy\n" + " ON sell.commodid = buy.commodid\n" + " AND buy.price > sell.price\n" + " WHERE sell.islandid=?\n" + " AND buy.islandid=?"); + +#define BS(bs) \ + SQL_PREPARE(ss_ite_##bs, \ + "SELECT\n" \ + " sum(qty)\n" \ + " FROM " #bs "\n" \ + " WHERE islandid=?\n" \ + " AND commodid=?\n" \ + " AND price=?"); BS(buy) BS(sell) #undef BS - ipairs= mcalloc(sizeof(*ipairs) * nislands); + ipairs= mcalloc(sizeof(*ipairs) * islandtablesz); }