2 * Route searcher - command line parsing and result printing
5 * This is part of the YARRG website, a tool for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
33 double max_mass=-1, max_volu=-1, max_capi=-1;
34 double distance_loss_factor_per_league;
35 int max_dist=-1, min_trade_maxprofit=0;
40 #define tabdebugf printf
43 #define CTR(x) int ctr_##x;
44 #define CTRA(x,n) int ctr_##x[n];
49 static Bucket ****results[GRANUS];
50 /* results[GRANUS][start_isle_ix][finalisle][midisle]-> */
52 static pid_t debugoutpid;
54 int main(int argc, const char **argv) {
58 const char *database=0;
59 const char *concur_base=0, *concur_rhs=0;
63 debug_flags= ~( dbg_sql2 );
68 if (arg[0] != '-') break;
69 if (!strcmp(arg,"-d")) {
71 } else if (!strcmp(arg,"-C")) {
74 concur_lim= atoi(*++argv);
75 } else if (!strcmp(arg,"-g")) {
76 granus= atoi(*++argv);
77 assert(granus>=1 && granus<=GRANUS);
79 } else if (!strcmp(arg,"-DN")) {
81 } else if (!strcmp(arg,"-D1")) {
82 debug_flags= ~(dbg_sql2|dbg_lp|dbg_value2);
90 /* glpk insists on writing stuff to stdout, and it does buffering,
91 * so we route all our debug through it this too */
93 sysassert( (realstdout= dup(1)) > 2 );
94 sysassert( output= fdopen(realstdout,"w") );
97 sysassert(! pipe(pfd) );
98 sysassert( (debugoutpid= fork()) >=0 );
100 sysassert( dup2(pfd[0],0)==0 );
101 sysassert( dup2(2,1)==1 );
102 sysassert(! close(pfd[0]) );
103 sysassert(! close(pfd[1]) );
104 sysassert(! execlp("cat","cat",(char*)0) );
106 sysassert( dup2(pfd[1],1)==1 );
107 sysassert(! close(pfd[0]) );
108 sysassert(! close(pfd[1]) );
116 sysassert( !setvbuf(debug,0,_IOLBF,0) );
118 max_mass= atof(*argv++);
119 max_volu= atof(*argv++);
120 max_capi= atof(*argv++);
121 double loss_per_league= atof(*argv++);
122 distance_loss_factor_per_league= 1.0 - loss_per_league;
124 min_trade_maxprofit= atoi(*argv++);
127 for (i=0; i<concur_lim; i++) {
128 char *concfn= masprintf("%s%02d%s", concur_base, i, concur_rhs);
130 sysassert( (concfd= open(concfn, O_RDWR|O_CREAT|O_TRUNC, 0600)) >= 0);
132 memset(&fl,0,sizeof(fl));
134 r= fcntl(concfd, F_SETLK, &fl);
136 if (!r) goto concur_ok;
137 sysassert( errno == EWOULDBLOCK );
140 fprintf(output,"@@@ concurrency limit exceeded (%d)\n", concur_lim);
144 /* deliberately leak concfd */
145 fprintf(output,"concurrency slot %d\n", i);
152 for (i=0; i<narches; i++)
153 fprintf(output,"arch %d %s\n",i,archnames[i]);
154 fprintf(output,"setup complete, starting search\n");
157 if (!strcmp(arg,"specific")) {
159 while ((arg= *argv++))
162 double val= value_route(ni, ia, 0);
163 fprintf(output, "route value is %g\n", val);
164 } else if (!strcmp(arg,"search")) {
165 for (granui=0; granui<GRANUS; granui++)
166 MCALLOC(results[granui], argc);
168 max_dist= atoi(*argv++);
170 for (ap=0; ap<AP; ap++) {
171 int nhs= atoi(*argv++);
172 for (granui=0; granui<GRANUS; granui++) {
173 nhighscores[granui][ap]= nhs;
174 MCALLOC(highscores[granui][ap], nhs);
177 const char *final_isle_spec= *argv++;
180 while ((arg= argv[resultsix])) {
181 int init_isle= atoi(arg);
184 if (!strcmp(final_isle_spec,"circ")) final_isle= init_isle;
185 else if (!strcmp(final_isle_spec,"any")) final_isle= -1;
186 else final_isle= atoi(final_isle_spec);
189 Bucket ****buckets_base_io[GRANUS];
190 for (granui=0; granui<GRANUS; granui++)
191 buckets_base_io[granui]= &results[granui][resultsix];
193 search(init_isle, final_isle, buckets_base_io);
198 for (granui=0; granui<granus; granui++) {
199 fprintf(output,"\n");
200 for (i=0; i<resultsix; i++) {
201 tabdebugf("========== start #%d granui%d %s ==========\n",
203 Bucket ***buckets_resultsix= results[granui][i];
204 if (!buckets_resultsix) continue;
206 for (mid=0; mid<granusz_mid[granui]; mid++) {
207 tabdebugf("| m%-3d ",mid);
210 for (fin=0; fin<granusz_fin[granui]; fin++) {
211 Bucket **buckets_fin= buckets_resultsix[fin];
212 if (!buckets_fin) continue;
213 tabdebugf("f%-3d",fin);
214 for (mid=0; mid<granusz_mid[granui]; mid++) {
215 Bucket *result= buckets_fin[mid];
219 tabdebugf("|%5d",(int)(result->prs[A].value[A]));
221 tabdebugf("%4d",(int)(result->prs[P].value[P]));
228 for (ap=0; ap<AP; ap++) {
230 fprintf(output,"============== granui%d ap=%d ==============\n",
232 for (pos=nhighscores[granui][ap]-1; pos>=0; pos--) {
233 HighScoreEntry *hs= &highscores[granui][ap][pos];
234 Bucket *bucket= hs->bucket;
235 if (!bucket) continue;
236 OnePotentialResult *pr= &bucket->prs[ap];
237 const int *const ports= pr->ports;
239 for (nports=0; nports<MAX_ROUTELEN && ports[nports]>=0; nports++);
240 int finisle= ports[nports-1];
241 int finarch= isle2arch(finisle);
242 int midisle= ports[nports/2];
243 int midarch= route2midarch(ports,nports);
245 " @%2d %c#%2d | start%3d mid%d:%3d f%d:%3d"
246 " | %3dlg | %5d %5d %4d |",
247 pos, "ap"[ap], nhighscores[granui][ap] - pos,
248 ports[0], midarch,midisle, finarch,finisle, pr->length,
249 (int)hs->value, (int)pr->value[A], (int)pr->value[P]);
250 for (i=0; i<nports; i++) fprintf(output," %d",ports[i]);
251 fprintf(output,"\n");
255 fprintf(output,"\n");
261 #define CTR(x) fprintf(output," %-30s %10d\n",#x,ctr_##x);
262 #define CTRA(x,n) for (i=0;i<n;i++) \
263 fprintf(output," %-27s[%d] %10d\n",#x,i,ctr_##x[i]);
268 sysassert(! fclose(debug) );
269 waitpid_check_exitstatus(debugoutpid,"debug cat",1);
271 sysassert(! fclose(output) );