X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Frsvalue.c;h=f86731923f873524a80e7446600f9d4a0f1f9694;hb=051cadee0203526bac95936a28d8fa35901cc925;hp=135583ed1720ac2dd4556afedabf9c97d9df9df5;hpb=23f08e0abdbd5212e4a867c80f7833e4ebcbb1cf;p=ypp-sc-tools.main.git diff --git a/yarrg/rsvalue.c b/yarrg/rsvalue.c index 135583e..f867319 100644 --- a/yarrg/rsvalue.c +++ b/yarrg/rsvalue.c @@ -32,7 +32,6 @@ typedef struct { TradesBlock *trades; } IslandPair; -int nislands; IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */ typedef struct IslandTradeEnd { @@ -41,12 +40,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 +63,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 */ @@ -108,7 +109,7 @@ 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; 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,8 +159,9 @@ 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; @@ -212,25 +214,6 @@ double 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); @@ -289,8 +272,8 @@ double value_route(int nislands, const int *islands) { 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[s].src, t->src_price, "src", si, ss_ite_sell); + avail_c(t, &itradeends[d].dst, t->dst_price, "dst", di, ss_ite_buy); int leg; for (leg=s; legsrc_price); } + double unit_profit= (t->dst_price - t->src_price) * loss_factor; + 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("c%d_p%d_%d_p%d_%d", t->commodid, s, t->src_price, d, t->dst_price); @@ -321,17 +307,22 @@ double value_route(int nislands, const int *islands) { next_s:; } /* for (s) */ - if (DEBUGP(lp)) - lpx_write_cpxlp(lp, (char*)"/dev/stdout"); + double profit= 0; + + if (lpx_get_num_cols(lp)) { + if (DEBUGP(lp)) + lpx_write_cpxlp(lp, (char*)DEBUG_DEV); - int ipr= lpx_interior(lp); - assert(ipr==LPX_E_OK); + int ipr= lpx_simplex(lp); + assert(ipr==LPX_E_OK); - if (DEBUGP(lp)) - lpx_print_ips(lp, (char*)"/dev/stdout"); + if (DEBUGP(lp)) + lpx_print_sol(lp, (char*)DEBUG_DEV); - assert(lpx_ipt_status(lp) == LPX_T_OPT); - double profit= lpx_ipt_obj_val(lp); + int lpst= lpx_get_status(lp); + assert(lpst == LPX_OPT); + profit= lpx_get_obj_val(lp); + } lpx_delete_prob(lp); lp= 0; @@ -343,14 +334,13 @@ 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