chiark / gitweb /
Properly quote various error messages
[ypp-sc-tools.main.git] / yarrg / rsvalue.c
1 /*
2  * Route searcher - route evaluation
3  */
4 /*
5  *  This is part of the YARRG website, a tool for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  *
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU Affero General Public License as
12  *  published by the Free Software Foundation, either version 3 of the
13  *  License, or (at your option) any later version.
14  *  
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Affero General Public License for more details.
19  *  
20  *  You should have received a copy of the GNU Affero General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *  
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 #include <glpk.h>
29
30 #include "rscommon.h"
31
32 DEBUG_DEFINE_DEBUGF(value);
33 DEBUG_DEFINE_SOME_DEBUGF(value2,debug2f);
34
35 typedef struct { int mass, volu; } CommodInfo;
36 static int commodstabsz;
37 static CommodInfo *commodstab;
38
39 static sqlite3_stmt *ss_ipair_dist;
40 static sqlite3_stmt *ss_ite_buy, *ss_ite_sell;
41
42
43 #define MAX_LEGS (MAX_ROUTELEN-1)
44
45 typedef struct {
46   int commodid, src_price, dst_price;
47 } Trade;
48
49 #define TRADES_PER_BLOCK 10
50
51 typedef struct TradesBlock {
52   struct TradesBlock *next;
53   int ntrades;
54   Trade t[TRADES_PER_BLOCK];
55 } TradesBlock;
56
57 static IslandPair ***ipairs; /* ipairs[sislandid][dislandid] */
58
59 typedef struct IslandTradeEnd {
60   struct IslandTradeEnd *next;
61   /* key: */
62   int commodid, price;
63   /* values: */
64   int qty;
65   unsigned long generation;
66   int rownum;
67 } IslandTradeEnd;
68
69 typedef struct {
70   IslandTradeEnd *src, *dst;
71 } IslandTradeEndHeads;
72
73 IslandTradeEndHeads *itradeends;
74   /* itradeends[islandid].{src,dst}->commodid etc. */
75
76 static LPX *lp;
77 static unsigned long generation;
78
79 static int nconstraint_rows;
80 static int constraint_rows[1+2+3*MAX_LEGS];
81 static double constraint_coeffs[1+2+3*MAX_LEGS];
82       /* dummy0, src, dst, for_each_leg( [mass], [volume], [capital] ) */
83
84 static void add_constraint(int row, double coefficient) {
85   nconstraint_rows++; /* glpk indices start from 1 !!! */
86   constraint_rows  [nconstraint_rows]= row;
87   constraint_coeffs[nconstraint_rows]= coefficient;
88 }
89
90 static IslandTradeEnd *get_ite(const Trade *t, IslandTradeEnd **trades,
91                                int price) {
92   IslandTradeEnd *search;
93   
94   for (search= *trades; search; search=search->next)
95     if (search->commodid==t->commodid && search->price==price)
96       return search;
97   abort();
98 }
99
100 static void avail_c(const Trade *t, IslandTradeEnd *ite,
101                     int price, const char *srcdst,
102                     int islandid, sqlite3_stmt *ss_ite) {
103   /* find row number of trade availability constraint */
104   IslandTradeEnd *search= ite;
105
106   if (search->generation != generation) {
107     search->rownum= lpx_add_rows(lp, 1);
108     lpx_set_row_bnds(lp, search->rownum, LPX_UP, 0, search->qty);
109
110     if (DEBUGP(value) || DEBUGP(check)) {
111       char *name= masprintf("%s_i%d_c%d_%d_all",
112                             srcdst, islandid, t->commodid, price);
113       lpx_set_row_name(lp,search->rownum,name);
114
115       if (DEBUGP(check)) {
116         int nrows= lpx_get_num_rows(lp);
117         assert(search->rownum == nrows);
118         int i;
119         for (i=1; i<nrows; i++)
120           assert(strcmp(name, lpx_get_row_name(lp,i)));
121       }
122       free(name);
123     }
124     search->generation= generation;
125   }
126
127   add_constraint(search->rownum, 1.0);
128 }
129
130 static int setup_leg_constraints(double max_thing, int legs, const char *wh) {
131   int leg, startrow;
132   if (max_thing < 0 || !legs) return -1;
133   startrow= lpx_add_rows(lp, legs);
134   for (leg=0; leg<legs; leg++) {
135     int row= leg+startrow;
136     lpx_set_row_bnds(lp, row, LPX_UP, 0, max_thing);
137     if (DEBUGP(value)) {
138       char *name= masprintf("%s_%d",wh,leg);
139       lpx_set_row_name(lp,row,name);
140       free(name);
141     }
142   }
143   return startrow;
144 }
145
146 static void add_leg_c(int startrow, int leg, double value) {
147   if (startrow<=0) return;
148   assert(value > 0);
149   add_constraint(startrow+leg, value);
150 }
151
152 IslandPair *ipair_get_maybe(int si, int di) {
153   IslandPair **ipa;
154
155   assert(si < islandtablesz);
156   assert(di < islandtablesz);
157
158   if (!(ipa= ipairs[si])) return 0;
159   return ipa[di];
160 }
161
162 static IslandPair *ipair_get_create(int si, int di) {
163   IslandPair *ip, **ipa;
164
165   assert(si < islandtablesz);
166   assert(di < islandtablesz);
167
168   if (!(ipa= ipairs[si])) {
169     ipairs[si]= MCALLOC(ipa, islandtablesz);
170   }
171   if ((ip= ipa[di]))
172     return ip;
173
174   ipa[di]= NEW(ip);
175   ip->trades= 0;
176   ip->route_tail_value= -1;
177
178   if (si==di) ctr_islands_arbitrage++;
179   else ctr_ipairs_relevant++;
180
181   debug2f("VALUE ipair_get(i%d,i%d) running...\n", si,di);
182   SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 1, si) );
183   SQL_MUST( sqlite3_bind_int(ss_ipair_dist, 2, di) );
184   assert(SQL_STEP(ss_ipair_dist));
185   int dist= sqlite3_column_int(ss_ipair_dist, 0);
186   ip->distance_loss_factor= pow(distance_loss_factor_per_league, dist);
187   sqlite3_reset(ss_ipair_dist);
188   
189   return ip;
190 }
191
192 double value_route(int nislands, const int *islands, int exclude_arbitrage) {
193   int s,d;
194
195   ctr_subroutes_valued++;
196
197   /* We need to construct the LP problem.  GLPK talks
198    * about rows and columns, which are numbered from 1.
199    *
200    * Each column is a `structural variable' ie one of the entries in
201    * the objective function.  In our case the set of structural
202    * variable is, for each port, the set of Trades which collect at
203    * that island.  (We use `port' to mean `specific visit to an
204    * island' so if an island appears more than once so do its trades.)
205    * We don't need to worry about crossing with all the possible
206    * delivery locations as we always deliver on the first port.
207    * We will call such a structural variable a Flow, for brevity.
208    *
209    * We iterate over the possible Flows adding them as columns as we
210    * go, and also adding their entries to the various constraints.
211    *
212    * Each row is an `auxiliary variable' ie one of the constraints.
213    * We have two kinds of constraint:
214    *   - mass/volume/capital: one constraint for each sailed leg
215    *       (unless relevant constraint is not satisfied)
216    *   - quantity of commodity available for collection
217    *       or delivery at particular price and island
218    * The former are numbered predictably: we have first all the mass
219    * limits, then all the volume limits, then all the capital limits
220    * (as applicable) - one for each leg, ie one for each entry
221    * in islands except the first.
222    *
223    * The latter are added as needed and the row numbers are stored in
224    * a data structure for later reuse.
225    */
226
227   assert(nislands >= 1);
228   assert(++generation);
229
230   assert(!lp);
231   lp= lpx_create_prob();
232   lpx_set_obj_dir(lp, LPX_MAX);
233   lpx_set_int_parm(lp, LPX_K_MSGLEV, DEBUGP(lp) ? 3 : 1);
234   lpx_set_int_parm(lp, LPX_K_PRESOL, 1);
235
236   if (DEBUGP(value)) {
237     lpx_set_prob_name(lp,(char*)"value_route");
238     lpx_set_obj_name(lp,(char*)"profit");
239   }
240
241   int legs= nislands-1;
242   int mass_constraints= setup_leg_constraints(max_mass, legs, "mass");
243   int volu_constraints= setup_leg_constraints(max_volu, legs, "volu");
244   int capi_constraints= setup_leg_constraints(max_capi, legs, "capi");
245
246   double delay_slot_loss_factor= 1.0;
247   for (s=0;
248        s<nislands;
249        s++, delay_slot_loss_factor *= LOSS_FACTOR_PER_DELAY_SLOT) {
250     int si= islands[s];
251     
252     for (d= s + exclude_arbitrage;
253          d < nislands;
254          d++) {
255       int di= islands[d];
256       int already_d;
257       for (already_d=s+1; already_d<d; already_d++)
258         if (islands[already_d] == di)
259           /* visited this island already since we left s, uninteresting */
260           goto next_d;
261
262       if (d>s && di==si)
263         /* route has returned to si, no need to think more about s */
264         goto next_s;
265
266       /*----- actually add these trades to the LP problem -----*/
267       
268       IslandPair *ip= ipair_get_maybe(islands[s], islands[d]);
269
270       if (!ip || !ip->trades)
271         goto next_d;
272
273       double loss_factor= delay_slot_loss_factor * ip->distance_loss_factor;
274       debugf(" SOME   i%d#%d..i%d#%d  dslf=%g dlf=%g  lf=%g\n",
275              si,s, di,d,
276              delay_slot_loss_factor, ip->distance_loss_factor, loss_factor);
277
278       TradesBlock *block;
279       for (block=ip->trades; block; block=block->next) {
280         int inblock;
281         for (inblock=0; inblock<block->ntrades; inblock++) {
282           Trade *t= &block->t[inblock];
283
284           debugf("  TRADE i%d#%d..i%d#%d c%d %d-%d  ",
285                  si,s, di,d, t->commodid, t->src_price, t->dst_price);
286
287           IslandTradeEnd
288             *src_ite= get_ite(t, &itradeends[si].src, t->src_price),
289             *dst_ite= get_ite(t, &itradeends[di].dst, t->dst_price);
290
291           int qty= src_ite->qty < dst_ite->qty ? src_ite->qty : dst_ite->qty;
292           int maxprofit= qty * (t->dst_price - t->src_price);
293           debugf("maxprofit=%d ",maxprofit);
294           if (maxprofit < min_trade_maxprofit) {
295             debugf("trivial\n");
296             continue;
297           }
298
299           nconstraint_rows=0;
300
301           avail_c(t, src_ite, t->src_price, "src", si,ss_ite_sell);
302           avail_c(t, dst_ite, t->dst_price, "dst", di,ss_ite_buy);
303
304           int leg;
305           for (leg=s; leg<d; leg++) {
306             add_leg_c(mass_constraints,leg, commodstab[t->commodid].mass*1e-3);
307             add_leg_c(volu_constraints,leg, commodstab[t->commodid].volu*1e-3);
308             add_leg_c(capi_constraints,leg, t->src_price);
309           }
310
311           double unit_profit= t->dst_price * loss_factor - t->src_price;
312           debugf("    unit profit %f\n", unit_profit);
313           if (unit_profit <= 0) continue;
314
315           int col= lpx_add_cols(lp,1);
316           lpx_set_col_bnds(lp, col, LPX_LO, 0, 0);
317           lpx_set_obj_coef(lp, col, unit_profit);
318           lpx_set_mat_col(lp, col, nconstraint_rows,
319                           constraint_rows, constraint_coeffs);
320
321           if (DEBUGP(value)) {
322             char *name= masprintf("c%d_p%d_%d_p%d_%d", t->commodid,
323                                   s, t->src_price, d, t->dst_price);
324             lpx_set_col_name(lp, col, name);
325             free(name);
326           }
327         } /* inblock */
328       } /* block */
329       
330       /*----- that's done adding these trades to the LP problem -----*/
331       
332     next_d:;
333     } /* for (d) */
334   next_s:;
335   } /* for (s) */
336
337   double profit= 0;
338
339   if (lpx_get_num_cols(lp)) {
340     ctr_subroutes_nonempty++;
341     
342     if (DEBUGP(lp))
343       lpx_write_cpxlp(lp, (char*)DEBUG_DEV);
344
345     int ipr= lpx_simplex(lp);
346     assert(ipr==LPX_E_OK);
347
348     if (DEBUGP(lp))
349       lpx_print_sol(lp, (char*)DEBUG_DEV);
350
351     int lpst= lpx_get_status(lp);
352     assert(lpst == LPX_OPT);
353     profit= lpx_get_obj_val(lp);
354   }
355
356   lpx_delete_prob(lp);
357   lp= 0;
358
359   debugf("    %s %f\n",
360          exclude_arbitrage ? "base value" : "route value",
361          profit);
362   return profit;
363 }
364
365 #define TRADE_FROM                                                      \
366     "  FROM sell, buy\n"                                                \
367     "  WHERE sell.commodid=buy.commodid AND sell.price < buy.price\n"
368               
369 static void read_trades(void) {
370   /* We would like to use DISTINCT but sqlite3 is too stupid
371    * to notice that it could use the index to do the DISTINCT
372    * which makes it rather slow. */
373   sqlite3_stmt *ss_trades;
374
375 #define TRADE_COLS \
376     "sell.commodid, sell.islandid, sell.price, buy.islandid, buy.price"
377   SQL_PREPARE(ss_trades,
378               " SELECT " TRADE_COLS "\n"
379               TRADE_FROM
380               "  ORDER BY " TRADE_COLS);
381
382   SQL_DISTINCT_DECL(cols,5);
383   while (SQL_DISTINCT_STEP(ss_trades,cols,5)) {    
384     ctr_trades_loaded++;
385     IslandPair *ip= ipair_get_create(cols[1], cols[3]);
386     TradesBlock *block= ip->trades;
387     if (!block || ip->trades->ntrades >= TRADES_PER_BLOCK) {
388       NEW(block);
389       block->next= ip->trades;
390       ip->trades= block;
391       block->ntrades= 0;
392     }
393     Trade *trade= &block->t[block->ntrades];
394     trade->commodid=  cols[0];
395     trade->src_price= cols[2];
396     trade->dst_price= cols[4];
397     block->ntrades++;
398   }
399   sqlite3_finalize(ss_trades);
400 }
401
402 static void read_islandtradeends(const char *bs, int srcdstoff) {
403
404 #define TRADEEND_KEYCOLS "%s.commodid, %s.islandid, %s.stallid"
405   char *stmt= masprintf(" SELECT " TRADEEND_KEYCOLS ", %s.price, %s.qty\n"
406                         TRADE_FROM
407                         "  ORDER BY "  TRADEEND_KEYCOLS,
408                         bs,bs,bs,bs,bs, bs,bs,bs);
409   char *stmt_id= masprintf("qtys (%s)",bs);
410   sqlite3_stmt *ss= sql_prepare(stmt, stmt_id);
411   free(stmt); free(stmt_id);
412
413   SQL_DISTINCT_DECL(cols,5);
414   while (SQL_DISTINCT_STEP(ss,cols,3)) {
415     ctr_quantities_loaded++;
416     IslandTradeEnd *search;
417
418     int commodid= cols[0];
419     int islandid= cols[1];
420     int price= cols[3];
421     int qty= cols[4];
422
423     IslandTradeEnd **trades= (void*)((char*)&itradeends[islandid] + srcdstoff);
424
425     for (search= *trades; search; search=search->next)
426       if (search->commodid==commodid && search->price==price)
427         goto found;
428     /* not found, add new end */
429
430     NEW(search);
431     search->commodid= commodid;
432     search->price= price;
433     search->next= *trades;
434     search->generation= 0;
435     search->qty= 0;
436     *trades= search;
437
438   found:
439     search->qty += qty;
440   }
441   sqlite3_finalize(ss);
442 }
443
444 void setup_value(void) {
445   sqlite3_stmt *sst;
446
447   commodstabsz= sql_single_int("SELECT max(commodid) FROM commods") + 1;
448   MCALLOC_INITEACH(commodstab, commodstabsz,
449                    this->mass= this->volu= -1
450                    );
451
452   SQL_PREPARE(sst,
453               "SELECT commodid,unitmass,unitvolume FROM commods");
454   while (SQL_STEP(sst)) {
455     ctr_commodities_loaded++;
456     int id= sqlite3_column_int(sst,0);
457     assert(id>=0 && id<commodstabsz);
458     commodstab[id].mass= sqlite3_column_int(sst,1);
459     commodstab[id].volu= sqlite3_column_int(sst,2);
460   }
461   sqlite3_finalize(sst);
462
463   MCALLOC(ipairs, islandtablesz);
464   MCALLOC(itradeends, islandtablesz);
465
466   SQL_PREPARE(ss_ipair_dist,
467               " SELECT dist FROM dists\n"
468               "  WHERE aiid=? and biid=?");
469
470   read_trades();
471   read_islandtradeends("sell", offsetof(IslandTradeEndHeads, src));
472   read_islandtradeends("buy",  offsetof(IslandTradeEndHeads, dst));
473 }