chiark / gitweb /
routesearch: sort out debugging output
[ypp-sc-tools.web-live.git] / yarrg / rscommon.h
1 #ifndef RSCOMMON_H
2 #define RSCOMMON_H
3
4 #include <sqlite3.h>
5
6 #define DEBUG_FLAG_LIST                         \
7    DF(sql)                                      \
8    DF(sql2)                                     \
9    DF(value)                                    \
10    DF(search)                                   \
11    DF(filter)                                   \
12    DF(check)                                    \
13    DF(lp)
14
15 #define debug debug_file
16
17 #include "common.h"
18
19 extern FILE *debug_file;
20 #define DEBUG_DEV "/dev/stdout" /* just for glpk */
21
22
23 #define COUNTER_LIST                            \
24    CTR(commodities_loaded)                      \
25    CTR(trades_loaded)                           \
26    CTR(islands_arbitrage)                       \
27    CTR(ipairs_relevant)                         \
28    CTR(quantities_loaded)                       \
29    CTR(routes_considered)                       \
30    CTR(routes_wrongfinalelim)                   \
31    CTR(routes_quickelim)                        \
32    CTR(routes_stratelim)                        \
33    CTR(routes_valued)                           \
34    CTR(routes_wrongfinal)                       \
35    CTR(newbests_strat_absolute)                 \
36    CTR(newbests_strat_perleague)                \
37    CTR(subroute_tails_valued)                   \
38    CTR(subroutes_valued)                        \
39    CTR(subroutes_nonempty)
40 #define CTR(x) extern int ctr_##x;
41   COUNTER_LIST
42 #undef CTR
43
44 #define SQL_MUST( call ) ({                                              \
45     /* `call' is an expression returning result, using  const char *sqe; \
46      * chk1 and chk2 are blocks using sqe and  int sqr; */               \
47     const char *sql_must_call_string= #call;                             \
48     int sqr;                                                             \
49     if (DEBUGP(sql2)) fprintf(debug,"SQL %s", sql_must_call_string);     \
50     sqr= (call);                                                         \
51     if (DEBUGP(sql2)) fprintf(debug," = %d\n", sqr);                     \
52     if (sqr) sql_fatal("(unknown)", sqr, sql_must_call_string);          \
53   })                                                                     \
54
55 void sql_fatal(const char *stmt_what, int sqr, const char *act_what) NORET;
56
57 #define SQL_STEP(ssh) (sql_step((ssh), #ssh, __FILE__, __LINE__))
58 int sql_step(sqlite3_stmt *ssh, const char *ssh_string,
59              const char *file, int line);
60
61 #define SQL_DISTINCT_DECL(cols, nintcols)       \
62   int cols[nintcols];                           \
63   cols[0]= -1;
64 #define SQL_DISTINCT_STEP(ssh, cols, nkeycols)                           \
65   (sql_step_distinct((ssh), #ssh, __FILE__, __LINE__,                    \
66                      (cols), sizeof((cols))/sizeof((cols)[0]), nkeycols))
67 int sql_step_distinct(sqlite3_stmt *ssh, const char *ssh_string,
68                       const char *file, int line,
69                       int *cols, int ncols, int nkeycols);
70    /* These work if we're making a query whose columns consist of:
71     *  - keys: integer column(s) on which the results are sorted by the query
72     *  - consequences: zero or more integer cols strictly dependent on the keys
73     *  - extra: zero or more further (possibly non-integer) columns
74     *
75     * Call SQL_DISTINCT_DECL, passing intcols = the total number of keys and
76     * consequences; it will declare  int cols[intfields];
77     *
78     * Then each SQL_DISTINCT_STEP is like SQL_STEP only you have to
79     * pass the number of key columns and it only returns rows with
80     * distinct keys.  Rows with all-identical keys are asserted to
81     * have identical consequences.  After each call to
82     * SQL_DISTINCT_STEP the keys and consequences will be stored in
83     * cols.
84     */
85
86 int sql_single_int(const char *stmt);
87
88 #define SQL_PREPARE(ss,stmt) ((ss)= sql_prepare((stmt),#ss))
89 sqlite3_stmt *sql_prepare(const char *stmt, const char *what);
90
91 #define SQL_BIND(ss,index,value) (sql_bind((ss),(index),(value),#ss,#value))
92 void sql_bind(sqlite3_stmt *ss, int index, int value,
93               const char *ss_what, const char *val_what);
94
95 #define MAX_ROUTELEN 20
96
97 extern sqlite3 *db;
98
99 void setup_sql(void);
100
101
102 typedef struct {
103   double distance_loss_factor;
104   struct TradesBlock *trades;
105   double route_tail_value;
106 } IslandPair;
107
108 IslandPair *ipair_get_maybe(int si, int di);
109
110 double value_route(int nislands, const int *islands, int exclude_arbitrage);
111 void setup_value(void);
112
113 typedef struct {
114   double absolute, perleague;
115   int absolute_ports[MAX_ROUTELEN], perleague_ports[MAX_ROUTELEN];
116 } PotentialResult;
117
118 void setup_search(void);
119 void search(int start_isle, int final_isle /* -1 means any */,
120             PotentialResult ****strat_base_io
121                 /* strat_base[finalarch][midarch]-> */);
122
123 extern double max_mass, max_volu, max_capi;
124 extern double distance_loss_factor_per_league;
125 extern int max_dist;
126
127 #define LOSS_FACTOR_PER_DELAY_SLOT (1-1e-8)
128
129 extern int islandtablesz;
130
131 extern int narches;
132 extern char **archnames;
133 extern int *islandid2arch;
134
135
136 extern FILE *output;
137
138
139 #define NEW(ptr) ((ptr)= mmalloc(sizeof(*ptr)))
140
141 #define MCALLOC(array, count) ((array)= mcalloc(sizeof(*(array)) * (count)))
142
143 #define MCALLOC_INITEACH(array, count, init_this) ({                   \
144     MCALLOC((array), (count));                                         \
145     int initi;                                                         \
146     typeof(&(array)[0]) this;                                          \
147     for (initi=0, this=(array); initi<(count); initi++, this++) {      \
148       init_this;                                                       \
149     }                                                                  \
150   })
151
152
153 typedef struct {
154   double value;
155   PotentialResult *pr;
156 } HighScoreEntry;
157
158 extern int nhighscores_absolute, nhighscores_perleague;
159 extern HighScoreEntry *highscores_absolute;
160 extern HighScoreEntry *highscores_perleague;
161
162
163 #define ONDEMAND(pointer_lvalue, calloc_size_count)                          \
164   ((pointer_lvalue) ? :                                                      \
165    ((pointer_lvalue) = mcalloc(sizeof(*(pointer_lvalue)) * calloc_size_count)))
166
167
168 static inline int isle2arch(int isle) {
169   int arch= islandid2arch[isle];
170   assert(arch>=0);
171   return arch;
172 }
173
174 static inline int route2midarch(const int *ports, int nports) {
175   int archs[nports], last_arch=-1, narchs=0, i;
176   for (i=0; i<nports; i++) {
177     int arch= isle2arch(ports[i]);
178     if (arch==last_arch) continue;
179     archs[narchs++]= last_arch= arch;
180   }
181   return archs[narchs/2];
182 }
183
184
185 #endif /*RSCOMMON_H*/