chiark / gitweb /
WIP routesearch; searcher
[ypp-sc-tools.db-live.git] / yarrg / rsvalue.c
index a1cdf0f9549c79ebb206d90ffcab7d871fe05b72..f046c99a896c56ee753a9430144ebd99b6fe7857 100644 (file)
@@ -16,7 +16,7 @@ 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
@@ -32,7 +32,6 @@ typedef struct {
   TradesBlock *trades;
 } IslandPair;
 
-int nislands;
 IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
 
 typedef struct IslandTradeEnd {
@@ -79,9 +78,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) );
@@ -94,7 +93,7 @@ static void avail_c(const Trade *t, IslandDirnTradeEnds *trades,
     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);
+      char *name= masprintf("%s_c%d_%d",srcdst,t->commodid,price);
       lpx_set_row_name(lp,search->rownum,name);
       free(name);
     }
@@ -108,7 +107,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 +128,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;
@@ -163,7 +162,7 @@ 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++)
+    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 +175,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 s,d;
 
   /* We need to construct the LP problem.  GLPK talks
@@ -234,6 +233,8 @@ void value_route(int nislands, const int *islands) {
   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");
@@ -297,17 +298,22 @@ void 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("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 +323,39 @@ 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_interior(lp);
+    assert(ipr==LPX_E_OK);
+
+    if (DEBUGP(lp))
+      lpx_print_ips(lp, (char*)DEBUG_DEV);
+
+    assert(lpx_ipt_status(lp) == LPX_T_OPT);
+    profit= lpx_ipt_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<commodstablesz; i++)
     commodstable[i].mass= commodstable[i].volu= -1;
 
-  SQL_MUST( sqlite3_prepare(db,
-     "SELECT commodid,unitmass,unitvolume FROM commods", -1,&sst,0) );
+  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);
@@ -343,34 +364,32 @@ void setup_value(void) {
   }
   sqlite3_finalize(sst);
 
-  SQL_MUST( sqlite3_prepare(db,
-     " SELECT dist from dists where aiid=? and biid=?",
-                           -1, &ss_ipair_dist, 0) );
-
-  SQL_MUST( sqlite3_prepare(db,
-     "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=?",
-                           -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);
 }