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