chiark / gitweb /
WIP routesearch; fix route trades cache block linked list
[ypp-sc-tools.db-live.git] / yarrg / rsvalue.c
index 135583ed1720ac2dd4556afedabf9c97d9df9df5..70e1f65d96cbd683a08aab16867102a5e54de232 100644 (file)
@@ -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; leg<nislands-1; leg++) {
+  for (leg=0; leg<legs; leg++) {
     int row= leg+startrow;
     lpx_set_row_bnds(lp, row, LPX_UP, 0, max_thing);
     if (DEBUGP(value)) {
@@ -129,11 +130,11 @@ static void add_leg_c(int startrow, int leg, double value) {
 static IslandPair *ipair_get(int si, int di) {
   IslandPair *ip, **ipa;
 
-  assert(si < nislands);
-  assert(di < nislands);
+  assert(si < islandtablesz);
+  assert(di < islandtablesz);
 
   if (!(ipa= ipairs[si])) {
-    ipa= ipairs[si]= mcalloc(sizeof(*ipa) * nislands);
+    ipa= ipairs[si]= mcalloc(sizeof(*ipa) * islandtablesz);
   }
   if ((ip= ipa[di]))
     return ip;
@@ -142,7 +143,7 @@ static IslandPair *ipair_get(int si, int di) {
   ip->ntrades= 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; s<nislands; s++) {
-    IslandTradeEnds *ite;
-    int si= islands[s];
-    int i;
-    for (i=0, ite=ites; i<nites; i++, ite++)
-      if (ite->islandid==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; leg<d; leg++) {
@@ -299,11 +282,14 @@ double value_route(int nislands, const int *islands) {
          add_leg_c(capi_constraints, leg, t->src_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,21 @@ 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_interior(lp);
+    assert(ipr==LPX_E_OK);
 
-  if (DEBUGP(lp))
-    lpx_print_ips(lp, (char*)"/dev/stdout");
+    if (DEBUGP(lp))
+      lpx_print_ips(lp, (char*)DEBUG_DEV);
 
-  assert(lpx_ipt_status(lp) == LPX_T_OPT);
-  double profit= lpx_ipt_obj_val(lp);
+    assert(lpx_ipt_status(lp) == LPX_T_OPT);
+    profit= lpx_ipt_obj_val(lp);
+  }
 
   lpx_delete_prob(lp);
   lp= 0;
@@ -343,14 +333,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<commodstablesz; i++)
     commodstable[i].mass= commodstable[i].volu= -1;
 
+  itradeends= mcalloc(sizeof(*itradeends) * islandtablesz);
+
   SQL_PREPARE(sst,
              "SELECT commodid,unitmass,unitvolume FROM commods");
   while (SQL_STEP(sst)) {
@@ -388,5 +377,5 @@ void setup_value(void) {
   BS(sell)
 #undef BS
 
-  ipairs= mcalloc(sizeof(*ipairs) * nislands);
+  ipairs= mcalloc(sizeof(*ipairs) * islandtablesz);
 }