From 7ee86fd0ee5c6894fe8aa15b89f626220deafc63 Mon Sep 17 00:00:00 2001 From: tslil Date: Thu, 7 Jan 2021 16:34:07 -0500 Subject: Searching for a good representation --- extract.sh | 31 ++++++++++++++------------- src/pptdb.c | 69 ++++++++++++++++++++++++++++++------------------------------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/extract.sh b/extract.sh index 1e9443d..e091a9c 100755 --- a/extract.sh +++ b/extract.sh @@ -21,19 +21,24 @@ extract() { } process() { + num_games=$1 echo -e "Beginning to process data\n" - for i in 5 6; do + for i in 5; do echo "Size $i..." ./pptdb "$i" "data/playtak-$i" > "data/check-$i" tail -n21 "data/check-$i" - echo Stripping overflows and illegal games... + echo -e "\tStripping overflows and illegal games..." grep -Fvxf "data/check-$i" "data/playtak-$i" > "data/good-playtak-$i" - # echo -n "Generating training data... " - # ./pptdb "$i" "data/good-playtak-$i" generate - # echo "Shuffling data..." - # shuf "data/training-$i.csv" > "data/shuf-$i.csv" - # mv "data/shuf-$i.csv" "data/training-$i.csv" - echo + echo -e "\tChoosing $num_games from what remains ..." + shuf -n $num_games "data/good-playtak-$i" > "data/smalltak-$i" + echo -en "\tGenerating training data... " + ./pptdb "$i" "data/smalltak-$i" generate + echo -e "\tWrote $(wc -l data/training-$i.csv | cut -d\ -f1) samples. Shuffling these..." + tail -n+2 "data/training-$i.csv" | shuf > "data/shuf-$i.csv" + head -n 1 "data/training-$i.csv" | cat "data/shuf-$i.csv" > "data/smalltrain-$i.csv" + rm "data/shuf-$i.csv" + mv "data/smalltrain-5.csv" "data/training-5.csv " + echo -e "\n\tDone! Sample training data in data/training-$i.csv" done } @@ -41,12 +46,6 @@ if [ ! -f "data/playtak-5" ]; then extract notation,result fi -if [ ! -f "pptdb" ]; then - echo Preparing pptdb - make pptdb -fi - -# process +make pptdb -shuf -n10000 data/good-playtak-5 > data/smalltak-5 && ./pptdb 5 data/smalltak-5 generate && \ -head -n1 data/training-5.csv > t && tail -n+2 data/training-5.csv > data/train5.csv && shuf data/train5.csv >> t && mv t data/train5.csv +process 5000 diff --git a/src/pptdb.c b/src/pptdb.c index 7995219..5e49c26 100644 --- a/src/pptdb.c +++ b/src/pptdb.c @@ -11,37 +11,35 @@ FILE *training_fh = NULL; static void write_input(void) { - // Four numbers for capstone coords (col,row) + // Whose turn is it? + /* fprintf(training_fh,"%d,",current_colour == C_BLACK); */ // Two numbers for flats remaining - fprintf(training_fh,"%.6f,%.6f,", - (float)(white_count & 127)/max_flats, - (float)(black_count & 127)/max_flats); - // Two layers of board_size * board_size: - float t; int h; uint16_t mask; + /* fprintf(training_fh,"%.6f,%.6f,", */ + /* (float)(white_count & 127)/max_flats, */ + /* (float)(black_count & 127)/max_flats); */ + // Two data points for each square + float t; uint16_t mask; for (int k = 0; k < board_size * board_size; k++) { + const int h = COUNT_AT(k); // stacks encoded as balanced ternary t = 0; - h = COUNT_AT(k); - - if (h>0) { - mask = 1<<(h-1); - while (h-->0) { - t += (colours[k] & mask) ? +1 : -1; - t/=3; - mask >>= 1; - } - } - fprintf(training_fh,"%.6f,",t); - } - float val; - for (int k = 0; k < board_size * board_size; k++) { - val = 0; - if (COUNT_AT(k)) { - if (STONE_AT(k) == STONE_STANDING) val = (colours[k] & 1) ? +1/3 : -1/3; - else if (STONE_AT(k) == STONE_CAPSTONE) val = (colours[k] & 1) ? +1.0 : -1.0; - else val = (colours[k] & 1) ? +2/3 : -2/3; + mask = 1<<(h-1); + for (int j = 0; j < h; j++) { + t += (colours[k] & mask) ? +1 : -1; + t /= 3; + mask >>=1; } - fprintf(training_fh,"%.6f,",val); + // top stone in [-1, +1] ordered as |standing| < |flat| < |cap| + /* val = 0; */ + /* if (COUNT_AT(k)) { */ + /* if (STONE_AT(k) == STONE_STANDING) val = (colours[k] & 1) ? +1/4 : -1/4; */ + /* else if (STONE_AT(k) == STONE_CAPSTONE) val = (colours[k] & 1) ? +1.0 : -1.0; */ + /* else val = (colours[k] & 1) ? +1/2 : -1/2; */ + /* } */ + fprintf(training_fh,"%.8f,", + t*2); + /* (h > 0) ? ((colours[k] & 2) ? +2/3 : -2/3) : 0.0, */ + /* val */ } } @@ -93,15 +91,17 @@ parse_line(const char *pt, const ssize_t read) { if (r != ACT_OK) return r; - // Measure height of stacks exceeding 1 - for (int k = 0; k < board_size * board_size; k++) { - if (COUNT_AT(k)>1) heights[COUNT_AT(k)]+=1; + if (generate == 0) { + // Measure height of stacks exceeding 1 + for (int k = 0; k < board_size * board_size; k++) { + if (COUNT_AT(k)>1) heights[COUNT_AT(k)]+=1; + } } } // Generate training data, not too early in the game if (generate && ply > 10) { write_input(); - fprintf(training_fh,"%d\n", result); + fprintf(training_fh,"%d,%d\n", result, 1-result); } // Parse next action while (idx