996
|
1 /*
|
|
2 * Copyright (c) 2009, 2011 Gregor Richards
|
|
3 *
|
|
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 * of this software and associated documentation files (the "Software"), to deal
|
|
6 * in the Software without restriction, including without limitation the rights
|
|
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 * copies of the Software, and to permit persons to whom the Software is
|
|
9 * furnished to do so, subject to the following conditions:
|
|
10 *
|
|
11 * The above copyright notice and this permission notice shall be included in
|
|
12 * all copies or substantial portions of the Software.
|
|
13 *
|
|
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20 * SOFTWARE.
|
|
21 */
|
|
22
|
|
23 /* NOTE: I am aware of how ugly this code is. It was quick hack. */
|
|
24
|
|
25 #ifndef _BSD_SOURCE
|
|
26 #define _BSD_SOURCE
|
|
27 #endif
|
|
28
|
|
29 #include <pthread.h>
|
|
30 #include <fcntl.h>
|
|
31 #include <signal.h>
|
|
32 #include <stdio.h>
|
|
33 #include <stdlib.h>
|
|
34 #include <string.h>
|
|
35 #include <sys/types.h>
|
|
36 #include <sys/wait.h>
|
|
37 #include <unistd.h>
|
|
38
|
|
39 #include "helpers.h"
|
|
40
|
|
41 #define MINTAPELEN 10
|
|
42 #define MAXTAPELEN 30
|
|
43 #define POLARITIES 2
|
|
44 #define RUNCOUNT (((MAXTAPELEN-MINTAPELEN) + 1) * POLARITIES)
|
|
45
|
|
46 #define KEEP_PROGRAMS 47
|
|
47
|
|
48 char *fyb, *cachedir;
|
|
49 int programCount;
|
|
50 char **programs;
|
|
51 signed char *scores;
|
|
52 int breakdown;
|
|
53
|
|
54 struct ProgramScore {
|
|
55 int id;
|
|
56 double pts;
|
|
57 double prc;
|
|
58 };
|
|
59
|
|
60 void *runnerThread(void *pvp)
|
|
61 {
|
|
62 int p = (int) (size_t) pvp;
|
|
63 int row = p * programCount;
|
|
64 pid_t pid;
|
|
65 int i, tmpi;
|
|
66 int st;
|
|
67 int scorepipe[2];
|
|
68 char scorepipes[128];
|
|
69 char *cacheFilename;
|
|
70 FILE *cacheF;
|
|
71
|
|
72 /* go through each program ... */
|
|
73 for (i = p + 1; i < programCount; i++) {
|
|
74 /* check the cache */
|
|
75 SF(cacheFilename, malloc, NULL, (strlen(cachedir) + strlen(programs[p]) + strlen(programs[i]) + 5));
|
|
76 sprintf(cacheFilename, "%s/:%s:%s:", cachedir, programs[p], programs[i]);
|
|
77 cacheF = fopen(cacheFilename, "r");
|
|
78
|
|
79 st = -1;
|
|
80 if (cacheF != NULL) {
|
|
81 /* get the result out of the cache */
|
|
82 st = (signed char) getc(cacheF);
|
|
83 fclose(cacheF);
|
|
84 if (st == EOF)
|
|
85 st = -1;
|
|
86 scores[row + i] = st;
|
|
87
|
|
88 }
|
|
89
|
|
90 if (st == -1) {
|
|
91 /* and run p vs i */
|
|
92 SF(tmpi, pipe, -1, (scorepipe));
|
|
93 sprintf(scorepipes, "%d", scorepipe[1]);
|
|
94 SF(pid, fork, -1, ());
|
|
95 if (pid == 0) {
|
|
96 close(0);
|
|
97 close(1);
|
|
98 close(2);
|
|
99 dup2(breakdown, 1);
|
|
100 execlp(fyb, fyb, programs[p], programs[i], scorepipes, NULL);
|
|
101 exit(0);
|
|
102 }
|
|
103 close(scorepipe[1]);
|
|
104
|
|
105 /* wait for it */
|
|
106 waitpid(pid, &st, 0);
|
|
107
|
|
108 if (WEXITSTATUS(st) != 0) {
|
|
109 st = 0;
|
|
110 } else {
|
|
111 /* read in the score */
|
|
112 st = 0;
|
|
113 SF(tmpi, read, -1, (scorepipe[0], &st, sizeof(int)));
|
|
114 scores[row + i] = st;
|
|
115 }
|
|
116 close(scorepipe[0]);
|
|
117
|
|
118 /* and cache it */
|
|
119 cacheF = fopen(cacheFilename, "w");
|
|
120 if (cacheF != NULL) {
|
|
121 putc(st, cacheF);
|
|
122 fclose(cacheF);
|
|
123 }
|
|
124 }
|
|
125
|
|
126 free(cacheFilename);
|
|
127 }
|
|
128
|
|
129 return NULL;
|
|
130 }
|
|
131
|
|
132 /* returns points won by p1 against p2 */
|
|
133 int winner(int p1, int p2)
|
|
134 {
|
|
135 signed char win;
|
|
136
|
|
137 if (p1 < p2) {
|
|
138 win = scores[p1*programCount + p2] * -1;
|
|
139 } else {
|
|
140 win = scores[p2*programCount + p1];
|
|
141 }
|
|
142
|
|
143 return win;
|
|
144 }
|
|
145
|
|
146 int ptscompar(const void *lv, const void *rv)
|
|
147 {
|
|
148 const struct ProgramScore *l = *((const struct ProgramScore **) lv);
|
|
149 const struct ProgramScore *r = *((const struct ProgramScore **) rv);
|
|
150
|
|
151 if (l->pts < r->pts) {
|
|
152 return -1;
|
|
153 } else if (l->pts > r->pts) {
|
|
154 return 1;
|
|
155 } else {
|
|
156 return 0;
|
|
157 }
|
|
158 }
|
|
159
|
|
160 int prccompar(const void *lv, const void *rv)
|
|
161 {
|
|
162 const struct ProgramScore *l = *((const struct ProgramScore **) lv);
|
|
163 const struct ProgramScore *r = *((const struct ProgramScore **) rv);
|
|
164
|
|
165 if (l->prc < r->prc) {
|
|
166 return -1;
|
|
167 } else if (l->prc > r->prc) {
|
|
168 return 1;
|
|
169 } else {
|
|
170 return 0;
|
|
171 }
|
|
172 }
|
|
173
|
|
174 int main(int argc, char **argv)
|
|
175 {
|
|
176 pthread_t *threads;
|
|
177 int i, j;
|
|
178 double *plainScores;
|
|
179 double *preciseScores;
|
|
180 struct ProgramScore **allScores;
|
|
181 char *scoreFilename;
|
|
182 FILE *scoreF;
|
|
183
|
|
184 if (argc < 4) {
|
|
185 fprintf(stderr, "Use: report <bfjoust program> <cache dir> <bfjoust files>\n");
|
|
186 return 1;
|
|
187 }
|
|
188
|
|
189 fyb = argv[1];
|
|
190 cachedir = argv[2];
|
|
191 programCount = argc - 3;
|
|
192 programs = argv + 3;
|
|
193
|
|
194 signal(SIGCHLD, SIG_DFL);
|
|
195
|
|
196 /* allocate space for the scores */
|
|
197 SF(scores, malloc, NULL, (programCount * programCount));
|
|
198
|
|
199 /* now run them in threads */
|
|
200 breakdown = open("breakdown.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666);
|
|
201 SF(threads, malloc, NULL, (programCount * sizeof(pthread_t)));
|
|
202 for (i = 0; i < programCount; i++) {
|
|
203 /*pthread_create(&threads[i], NULL, runnerThread, (void *) (size_t) i);*/
|
|
204 runnerThread((void *) (size_t) i);
|
|
205 }
|
|
206 close(breakdown);
|
|
207
|
|
208 /* wait for the threads to finish * /
|
|
209 for (i = 0; i < programCount; i++) {
|
|
210 pthread_join(threads[i], NULL);
|
|
211 } */
|
|
212
|
|
213 /* allocate the score buffers */
|
|
214 SF(allScores, malloc, NULL, (programCount * sizeof(struct ProgramScore *)));
|
|
215 for (i = 0; i < programCount; i++) {
|
|
216 SF(allScores[i], malloc, NULL, (sizeof(struct ProgramScore)));
|
|
217 allScores[i]->id = i;
|
|
218 }
|
|
219
|
|
220 /* now calculate the plain score for each */
|
|
221 SF(plainScores, malloc, NULL, (programCount * sizeof(double)));
|
|
222 for (i = 0; i < programCount; i++) {
|
|
223 double score = 0;
|
|
224 for (j = 0; j < programCount; j++) {
|
|
225 if (i == j) continue;
|
|
226
|
|
227 score += winner(i, j) / (double) RUNCOUNT;
|
|
228 }
|
|
229 allScores[i]->pts = plainScores[i] = score;
|
|
230 }
|
|
231
|
|
232 /* and the precise scores for each */
|
|
233 SF(preciseScores, malloc, NULL, (programCount * sizeof(double)));
|
|
234 for (i = 0; i < programCount; i++) {
|
|
235 double score = 0;
|
|
236 for (j = 0; j < programCount; j++) {
|
|
237 double value;
|
|
238 double win;
|
|
239
|
|
240 if (i == j) continue;
|
|
241
|
|
242 /* get the value of j */
|
|
243 value = (double) (plainScores[j] + programCount) / (double) (2*programCount-2);
|
|
244
|
|
245 /* and add the value if correct */
|
|
246 win = winner(i, j) / (double) RUNCOUNT;
|
|
247 if (win > 0) {
|
|
248 score += value * win;
|
|
249 }
|
|
250 }
|
|
251 allScores[i]->prc = preciseScores[i] = score * 200 / (programCount-1);
|
|
252
|
|
253 /* save it to the cache */
|
|
254 SF(scoreFilename, malloc, NULL, (strlen(cachedir) + strlen(programs[i]) + 8));
|
|
255 sprintf(scoreFilename, "%s/%s.score", cachedir, programs[i]);
|
|
256 scoreF = fopen(scoreFilename, "w");
|
|
257 if (scoreF != NULL) {
|
|
258 fprintf(scoreF, "%.1f", preciseScores[i]);
|
|
259 fclose(scoreF);
|
|
260 }
|
|
261 free(scoreFilename);
|
|
262 }
|
|
263
|
|
264 /* now print out the scores */
|
|
265 qsort(allScores, programCount, sizeof(struct ProgramScore *), prccompar);
|
|
266 printf("Pos ID Score Points Program\n");
|
|
267 for (i = programCount - 1; i >= 0; i--) {
|
|
268 struct ProgramScore *sc = allScores[i];
|
|
269 printf(" %2d %2d %6.2f %6.2f %s\n",
|
|
270 programCount - i, (int) sc->id, sc->prc, sc->pts, programs[sc->id]);
|
|
271 }
|
|
272 printf("\n");
|
|
273
|
|
274 /* and the full scoreboard */
|
|
275
|
|
276 /* header */
|
|
277 printf(" | ");
|
|
278 for (i = 0; i < programCount; i++) {
|
|
279 if (i < 10) printf(" ");
|
|
280 else printf("%1d ", (int) (i/10%10));
|
|
281 }
|
|
282 printf("|\n | ");
|
|
283 for (i = 0; i < programCount; i++) {
|
|
284 printf("%1d ", (int) (i%10));
|
|
285 }
|
|
286 printf("| score | pts | id | program\n");
|
|
287
|
|
288 /* scores */
|
|
289 for (i = 0; i < programCount; i++) {
|
|
290 printf("%2d | ", (int) i);
|
|
291 for (j = 0; j < programCount; j++) {
|
|
292 double win;
|
|
293
|
|
294 if (i == j) {
|
|
295 printf(" ");
|
|
296 continue;
|
|
297 }
|
|
298
|
|
299 win = winner(i, j);
|
|
300 if (win > 0) {
|
|
301 printf("+ ");
|
|
302 } else if (win < 0) {
|
|
303 printf("- ");
|
|
304 } else {
|
|
305 printf("0 ");
|
|
306 }
|
|
307 }
|
|
308
|
|
309 printf("| %5.1f | %5.1f | %2d | %s\n", preciseScores[i], plainScores[i], (int) i, programs[i]);
|
|
310 }
|
|
311
|
|
312 /* kill any poorly-performing programs */
|
|
313 if (programCount > KEEP_PROGRAMS) {
|
|
314 for (i = 0; i < programCount - KEEP_PROGRAMS; i++) {
|
|
315 char buf[1024];
|
|
316 snprintf(buf, 1024, "hg rm -f %s", programs[allScores[i]->id]);
|
|
317 system(buf);
|
|
318 system(buf + 3);
|
|
319 }
|
|
320 system("hg commit -m Trimming.");
|
|
321 }
|
|
322
|
|
323 return 0;
|
|
324 }
|