chiark / gitweb /
WIP routesearch; before try not using interior point
[ypp-sc-tools.web-live.git] / yarrg / rsvalue.c
index a1cdf0f9549c79ebb206d90ffcab7d871fe05b72..135583ed1720ac2dd4556afedabf9c97d9df9df5 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
@@ -79,9 +79,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 +94,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);
     }
@@ -163,7 +163,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 +176,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 +234,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");
@@ -303,11 +305,13 @@ void value_route(int nislands, const int *islands) {
        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,8 +321,22 @@ void value_route(int nislands, const int *islands) {
   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) {
@@ -333,8 +351,8 @@ void setup_value(void) {
   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,31 +361,29 @@ 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