chiark / gitweb /
Can compute route value
[ypp-sc-tools.db-live.git] / yarrg / rsvalue.c
index 8bdddaeb9dbb1a2bc221cc6e8453226887efae90..f1a8d224cab316a116012ff74b41c45d09704560 100644 (file)
@@ -1,11 +1,22 @@
 /**/
 
+#include <glpk.h>
+
 #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;
+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
@@ -16,6 +27,7 @@ typedef struct TradesBlock{
 } TradesBlock;
 
 typedef struct {
+  double distance_loss_factor;
   int ntrades;
   TradesBlock *trades;
 } IslandPair;
@@ -23,6 +35,97 @@ typedef struct {
 int nislands;
 IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
 
+typedef struct IslandTradeEnd {
+  struct IslandTradeEnd *next;
+  int commodid, price;
+  int qty;
+  unsigned long generation;
+  int rownum;
+} IslandTradeEnd, *IslandDirnTradeEnds;
+
+typedef struct {
+  int islandid;
+  IslandDirnTradeEnds collect, deliver;
+} IslandTradeEnds;
+
+static LPX *lp;
+static unsigned long generation;
+
+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_c(const Trade *t, IslandDirnTradeEnds *trades,
+                   int price, const char *srcdst,
+                   int islandid, sqlite3_stmt *ss_ite) {
+  /* find row number of trade availability constraint */
+  IslandTradeEnd *search;
+
+  for (search= *trades; search; search=search->next)
+    if (search->commodid==t->commodid && search->price==price)
+      goto found;
+  /* not found, add new row */
+
+  search= mmalloc(sizeof(*search));
+  search->commodid= t->commodid;
+  search->price= price;
+  search->next= *trades;
+  search->generation= 0;
+
+  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) );
+  
+  *trades= search;
+  
+ found:;
+  if (search->generation != generation) {
+    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_c%d_%d",srcdst,t->commodid,price);
+      lpx_set_row_name(lp,search->rownum,name);
+      free(name);
+    }
+    search->generation= generation;
+  }
+
+  add_constraint(search->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; leg<nislands-1; leg++) {
+    int row= leg+startrow;
+    lpx_set_row_bnds(lp, row, LPX_UP, 0, max_thing);
+    if (DEBUGP(value)) {
+      char *name= masprintf("max_leg%d_%s",leg,wh);
+      lpx_set_row_name(lp,row,name);
+      free(name);
+    }
+  }
+  return startrow;
+}
+
+static void add_leg_c(int startrow, int leg, double value) {
+  if (startrow<=0) return;
+  assert(value > 0);
+  add_constraint(startrow+leg, value);
+}
+
 static IslandPair *ipair_get(int si, int di) {
   IslandPair *ip, **ipa;
 
@@ -34,17 +137,25 @@ static IslandPair *ipair_get(int si, int di) {
   }
   if ((ip= ipa[di]))
     return ip;
-  
+
   ipa[di]= ip= mmalloc(sizeof(*ip));
   ip->ntrades= 0;
   ip->trades= 0;
   int inblock= TRADES_PER_BLOCK;
   TradesBlock *block= 0;
 
-  SQL_MUST( sqlite3_bind_int(ss_ipair, 1, si) );
-  SQL_MUST( sqlite3_bind_int(ss_ipair, 2, di) );
+  debugf("VALUE ipair_get(%d,%d) running...\n", si,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);
+
+  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)) {
+  while (SQL_STEP(ss_ipair_trades)) {
     if (inblock == TRADES_PER_BLOCK) {
       block= mmalloc(sizeof(*block));
       block->next= ip->trades;
@@ -52,58 +163,234 @@ static IslandPair *ipair_get(int si, int di) {
       inblock= 0;
     }
     int *irp, i;
-    for (i=0, irp=&block->t[inblock].commodid; i<5; i++, irp++)
-      *irp= sqlite3_column_int(ss_ipair, i);
+    for (i=0, irp=&block->t[inblock].commodid; i<3; i++, irp++)
+      *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;
 }
 
-void value_route(int nislands, const int *islands) {
+double value_route(int nislands, const int *islands) {
   int s,d;
-  
+
+  /* We need to construct the LP problem.  GLPK talks
+   * about rows and columns, which are numbered from 1.
+   *
+   * 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 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 port.
+   * We will call such a structural variable a Flow, for brevity.
+   *
+   * We iterate over the possible Flows adding them as columns as we
+   * go, and also adding their entries to the various constraints.
+   *
+   * Each row is an `auxiliary variable' ie one of the constraints.
+   * We have two kinds of constraint:
+   *   - mass/volume/capital: one constraint for each sailed leg
+   *       (unless relevant constraint is not satisfied)
+   *   - quantity of commodity available for collection
+   *       or delivery at particular price and island
+   * The former are numbered predictably: we have first all the mass
+   * limits, then all the volume limits, then all the capital limits
+   * (as applicable) - one for each leg, ie one for each entry
+   * in islands except the first.
+   *
+   * The latter are added as needed and the row numbers are stored in
+   * a data structure for later reuse.
+   */
+
+  assert(nislands >= 1);
+  assert(++generation);
+
+  int nites=0;
+  IslandTradeEnds ites[nislands], *iteps[nislands];
+
   for (s=0; s<nislands; s++) {
-    for (d=s; d<nislands; d++) {
-      ipair_get(islands[s], islands[d]);
-    }
+    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;
   }
 
-  //char *tail;
-  //struct sqlite_vm *sth;
-  //r= sqlite_compile(db, stmt, &tail, &sth, 
+  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");
+  }
+
+  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;
+       s<nislands;
+       s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) {
+    int si= islands[s];
+    
+    for (d=s; d<nislands; d++) {
+      int di= islands[d];
+      int already_d;
+      for (already_d=s+1; already_d<d; already_d++)
+       if (islands[already_d] == di)
+         /* visited this island already since we left s, uninteresting */
+         goto next_d;
+
+      if (d>s && di==si)
+       /* 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 tradestodo= ip->ntrades;
+      if (!tradestodo)
+       goto next_d;
+
+      int inblock= 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;
+       }
+       Trade *t= &block->t[inblock++];
+
+       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_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);
+
+       int leg;
+       for (leg=s; leg<d; leg++) {
+         add_leg_c(mass_constraints, leg, commodstable[t->commodid].mass);
+         add_leg_c(volu_constraints, leg, commodstable[t->commodid].volu);
+         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);
+assert(unit_profit < 1e6);
+
+       lpx_set_col_bnds(lp, col, LPX_LO, 0, 0);
+       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);
+         lpx_set_col_name(lp, col, name);
+         free(name);
+       }
+
+       col++;
+      } /* while (tradestodo-- >0) */
+      
+      /*----- that's done adding these trades to the LP problem -----*/
+      
+    next_d:;
+    } /* for (d) */
+  next_s:;
+  } /* for (s) */
+
+  if (DEBUGP(lp))
+    lpx_write_cpxlp(lp, (char*)"/dev/stdout");
+
+  int ipr= lpx_interior(lp);
+  assert(ipr==LPX_E_OK);
+
+  if (DEBUGP(lp))
+    lpx_print_ips(lp, (char*)"/dev/stdout");
+
+  assert(lpx_ipt_status(lp) == LPX_T_OPT);
+  double profit= lpx_ipt_obj_val(lp);
+
+  lpx_delete_prob(lp);
+  lp= 0;
+
+  return profit;
 }
 
 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);
+  nislands= sql_single_int("SELECT max(islandid) FROM islands") + 1;
   debugf("VALUE nislands=%d\n",nislands);
 
-  SQL_MUST( sqlite3_prepare(db,
-     "SELECT\n"
-     " sell.commodid           commodid,\n"
-     " sell.price              src_price,\n"
-     " sum(sell.qty)           src_qty,\n"
-     " buy.price               dst_price,\n"
-     " sum(buy.qty)            dst_qty\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=?\n"
-     " GROUP BY sell.commodid, sell.price, buy.price",
-                           -1, &ss_ipair, 0) );
+  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;
+
+  SQL_PREPARE(sst,
+             "SELECT commodid,unitmass,unitvolume FROM commods");
+  while (SQL_STEP(sst)) {
+    int id= sqlite3_column_int(sst,0);
+    assert(id>=0 && id<commodstablesz);
+    commodstable[id].mass= sqlite3_column_int(sst,1);
+    commodstable[id].volu= sqlite3_column_int(sst,2);
+  }
+  sqlite3_finalize(sst);
+
+  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);
 }