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