diff options
Diffstat (limited to 'src/pptdb.c')
| -rw-r--r-- | src/pptdb.c | 68 |
1 files changed, 36 insertions, 32 deletions
diff --git a/src/pptdb.c b/src/pptdb.c index 34056c4..712aabc 100644 --- a/src/pptdb.c +++ b/src/pptdb.c @@ -4,35 +4,43 @@ #include <tak.h> +float max_flats; uint64_t heights[16]; +int result, generate; FILE *training_fh = NULL; -int wc_col, bc_col, wc_row, bc_row, result, generate; static void write_input(void) { // Four numbers for capstone coords (col,row) - fprintf(training_fh,"%d,%d,%d,%d,",wc_col,wc_row,bc_col,bc_row); // Two numbers for flats remaining - fprintf(training_fh,"%d,%d,",white_count & 127, black_count & 127); + fprintf(training_fh,"%.6f,%.6f,", + (float)(white_count & 127)/max_flats, + (float)(black_count & 127)/max_flats); // Two layers of board_size * board_size: - int h, t; uint16_t mask; + float t; int h; uint16_t mask; for (int k = 0; k < board_size * board_size; k++) { - // stacks encoded as balanced ternary without capstones and walls - t = 0; mask = 1; + // stacks encoded as balanced ternary, without caps and walls + t = 0; h = COUNT_AT(k); - if (STONE_AT(k)) h--; - while (h-->0) { - t *= 3; - t += (colours[k] & mask) ? +1 : -1; - mask <<= 1; + if (h>0) { + mask = 1<<(h-1); + /* if (STONE_AT(k) != STONE_FLAT) h--; */ + while (h-->0) { + t += (colours[k] & mask) ? +1.0 : -1.0; + t /= 3; + mask >>= 1; + } } - fprintf(training_fh,"%d,",t); + fprintf(training_fh,"%.6f,",t); } + int val; for (int k = 0; k < board_size * board_size; k++) { - // walls with +- 1 - if (COUNT_AT(k) && STONE_AT(k) == STONE_STANDING) - fprintf(training_fh,"%d,",(colours[k] & 1) ? +1 : -1); - else fputs("0,", training_fh); + val = 0; + if (COUNT_AT(k)) { + if (STONE_AT(k) == STONE_STANDING) val = (colours[k] & 1) ? +1 : -1; + else if (STONE_AT(k) == STONE_CAPSTONE) val = (colours[k] & 1) ? +2 : -2; + } + fprintf(training_fh,"%d,",val); } } @@ -51,17 +59,7 @@ parse_line(const char *pt, const ssize_t read) { if (idx + 3 < read) { switch (pt[idx+3]) { case 'W': { stone = STONE_STANDING; break; } - case 'C': { - if (current_colour == C_BLACK) { - bc_col = col + 1; - bc_row = row + 1; - } else { - wc_col = col + 1; - wc_row = row + 1; - } - stone = STONE_CAPSTONE; - break; - } + case 'C': { stone = STONE_CAPSTONE; break; } default: { stone = STONE_FLAT; break; } } } else { @@ -102,7 +100,7 @@ parse_line(const char *pt, const ssize_t read) { // Generate training data if (generate) { write_input(); - fprintf(training_fh,"%d,%d\n", result, 1-result); + fprintf(training_fh,"%d\n", result); } // Parse next action while (idx<read && pt[idx++]!=','); @@ -135,19 +133,25 @@ main(int argc, char **argv) { if (argc > 3 && (!strncmp("generate", argv[3], 8))) { generate=1; + max_flats = (size == 5) ? 21.0 : 30.0; snprintf(td_fn, 64, "data/training-%d.csv",size); training_fh = fopen(td_fn, "w"); if (training_fh == NULL) exit(EXIT_FAILURE); + // Write header + fputs("\"White flats\",\"Black flats\",",training_fh); + for (int k = 0; k < size*size; k++) + fprintf(training_fh,"\"Stack %d\",",k); + for (int k = 0; k < size*size; k++) + fprintf(training_fh,"\"Wall %d\",",k); + fputs("\"Outcome\"\n",training_fh); } else generate=0; while ((read = getline(&line, &len, playtak_fh)) != -1) { // Reset everything reset_state(size); - bc_col = 0; wc_col = 0; - bc_row = 0; wc_row = 0; // Store the result of this game - if (line[read-4] == '0') result = 1; - else result = 0; + if (line[read-4] == '0') result = 0; + else result = 1; // Parse the line r = parse_line(line,read-4); // Adjust counts if we're not generating training data |
