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