chiark / gitweb /
routesearch: make absolute/perleague into 2-element arrays rather than macros
[ypp-sc-tools.db-live.git] / yarrg / rsvalue.c
index c3fac1a01f9260c0812ea58c0f0bffe23f395fe0..8fe5f229b04b00355817b7225475a9068e8d9621 100644 (file)
@@ -13,6 +13,7 @@ static CommodInfo *commodstab;
 static sqlite3_stmt *ss_ipair_dist;
 static sqlite3_stmt *ss_ite_buy, *ss_ite_sell;
 
+
 #define MAX_LEGS (MAX_ROUTELEN-1)
 
 typedef struct {
@@ -118,22 +119,35 @@ static void add_leg_c(int startrow, int leg, double value) {
   add_constraint(startrow+leg, value);
 }
 
-IslandPair *ipair_get(int si, int di) {
+IslandPair *ipair_get_maybe(int si, int di) {
+  IslandPair **ipa;
+
+  assert(si < islandtablesz);
+  assert(di < islandtablesz);
+
+  if (!(ipa= ipairs[si])) return 0;
+  return ipa[di];
+}
+
+static IslandPair *ipair_get_create(int si, int di) {
   IslandPair *ip, **ipa;
 
   assert(si < islandtablesz);
   assert(di < islandtablesz);
 
   if (!(ipa= ipairs[si])) {
-    ipa= ipairs[si]= mcalloc(sizeof(*ipa) * islandtablesz);
+    ipairs[si]= MCALLOC(ipa, islandtablesz);
   }
   if ((ip= ipa[di]))
     return ip;
 
-  ipa[di]= ip= mmalloc(sizeof(*ip));
+  ipa[di]= NEW(ip);
   ip->trades= 0;
   ip->route_tail_value= -1;
 
+  if (si==di) ctr_islands_arbitrage++;
+  else ctr_ipairs_relevant++;
+
   debugf("VALUE ipair_get(i%d,i%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) );
@@ -148,6 +162,8 @@ IslandPair *ipair_get(int si, int di) {
 double value_route(int nislands, const int *islands, int exclude_arbitrage) {
   int s,d;
 
+  ctr_subroutes_valued++;
+
   /* We need to construct the LP problem.  GLPK talks
    * about rows and columns, which are numbered from 1.
    *
@@ -185,6 +201,7 @@ double value_route(int nislands, const int *islands, int exclude_arbitrage) {
   lp= lpx_create_prob();
   lpx_set_obj_dir(lp, LPX_MAX);
   lpx_set_int_parm(lp, LPX_K_MSGLEV, DEBUGP(lp) ? 3 : 1);
+  lpx_set_int_parm(lp, LPX_K_PRESOL, 1);
 
   if (DEBUGP(value)) {
     lpx_set_prob_name(lp,(char*)"value_route");
@@ -218,9 +235,9 @@ double value_route(int nislands, const int *islands, int exclude_arbitrage) {
 
       /*----- actually add these trades to the LP problem -----*/
       
-      IslandPair *ip= ipair_get(islands[s], islands[d]);
+      IslandPair *ip= ipair_get_maybe(islands[s], islands[d]);
 
-      if (!ip->trades)
+      if (!ip || !ip->trades)
        goto next_d;
 
       double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor;
@@ -278,6 +295,8 @@ double value_route(int nislands, const int *islands, int exclude_arbitrage) {
   double profit= 0;
 
   if (lpx_get_num_cols(lp)) {
+    ctr_subroutes_nonempty++;
+    
     if (DEBUGP(lp))
       lpx_write_cpxlp(lp, (char*)DEBUG_DEV);
 
@@ -317,10 +336,11 @@ static void read_trades(void) {
 
   SQL_DISTINCT_DECL(cols,5);
   while (SQL_DISTINCT_STEP(ss_trades,cols,5)) {    
-    IslandPair *ip= ipair_get(cols[1], cols[3]);
+    ctr_trades_loaded++;
+    IslandPair *ip= ipair_get_create(cols[1], cols[3]);
     TradesBlock *block= ip->trades;
     if (!block || ip->trades->ntrades >= TRADES_PER_BLOCK) {
-      block= mmalloc(sizeof(*block));
+      NEW(block);
       block->next= ip->trades;
       ip->trades= block;
       block->ntrades= 0;
@@ -347,6 +367,7 @@ static void read_islandtradeends(const char *bs, int srcdstoff) {
 
   SQL_DISTINCT_DECL(cols,5);
   while (SQL_DISTINCT_STEP(ss,cols,3)) {
+    ctr_quantities_loaded++;
     IslandTradeEnd *search;
 
     int commodid= cols[0];
@@ -361,7 +382,7 @@ static void read_islandtradeends(const char *bs, int srcdstoff) {
        goto found;
     /* not found, add new end */
 
-    search= mmalloc(sizeof(*search));
+    NEW(search);
     search->commodid= commodid;
     search->price= price;
     search->next= *trades;
@@ -377,16 +398,16 @@ static void read_islandtradeends(const char *bs, int srcdstoff) {
 
 void setup_value(void) {
   sqlite3_stmt *sst;
-  int i;
 
   commodstabsz= sql_single_int("SELECT max(commodid) FROM commods") + 1;
-  commodstab= mmalloc(sizeof(*commodstab)*commodstabsz);
-  for (i=0; i<commodstabsz; i++)
-    commodstab[i].mass= commodstab[i].volu= -1;
+  MCALLOC_INITEACH(commodstab, commodstabsz,
+                  this->mass= this->volu= -1
+                  );
 
   SQL_PREPARE(sst,
              "SELECT commodid,unitmass,unitvolume FROM commods");
   while (SQL_STEP(sst)) {
+    ctr_commodities_loaded++;
     int id= sqlite3_column_int(sst,0);
     assert(id>=0 && id<commodstabsz);
     commodstab[id].mass= sqlite3_column_int(sst,1);
@@ -394,8 +415,8 @@ void setup_value(void) {
   }
   sqlite3_finalize(sst);
 
-  ipairs= mcalloc(sizeof(*ipairs) * islandtablesz);
-  itradeends= mcalloc(sizeof(*itradeends) * islandtablesz);
+  MCALLOC(ipairs, islandtablesz);
+  MCALLOC(itradeends, islandtablesz);
 
   SQL_PREPARE(ss_ipair_dist,
              " SELECT dist FROM dists\n"