-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdateWDL.sh
executable file
·174 lines (144 loc) · 5.78 KB
/
updateWDL.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
# exit on errors
set -e
# set range of commits to be considered for the WDL fitting, and other options
# by default we start from the most recent WDL model change and go to master
default_firstrev=2054add23cf234f302c67709efc0d265c5a98eae
default_lastrev=HEAD
default_materialMin=17
default_EloDiffMax=5
firstrev=$default_firstrev
lastrev=$default_lastrev
materialMin=$default_materialMin
EloDiffMax=$default_EloDiffMax
while [[ $# -gt 0 ]]; do
case "$1" in
--firstrev)
firstrev="$2"
shift 2
;;
--lastrev)
lastrev="$2"
shift 2
;;
--materialMin)
materialMin="$2"
shift 2
;;
--EloDiffMax)
EloDiffMax="$2"
shift 2
;;
--help)
echo "Usage: $0 [OPTIONS]"
echo "Options:"
echo " --firstrev FIRSTREV First SF commit to collect games from (default: $default_firstrev)"
echo " --lastrev LASTREV Last SF commit to collect games from (default: $default_lastrev)"
echo " --materialMin MATERIALMIN Parameter passed to scoreWDL.py (default: $default_materialMin)"
echo " --EloDiffMax ELODIFFMAX Parameter passed to scoreWDLstat (default: $default_EloDiffMax)"
exit 0
;;
*)
break
;;
esac
done
echo "Running: $0 --firstrev $firstrev --lasttrev $lastrev --materialMin $materialMin --EloDiffMax $EloDiffMax"
echo "started at: " $(date)
# regex for book name
bookname="UHO_Lichess_4852_v..epd"
# path for PGN files
pgnpath=pgns
# clone repo(s) if needed, and pull latest revisions
for repo in "Stockfish"; do
if [[ ! -e "$repo" ]]; then
git clone https://github.com/official-stockfish/"$repo".git >&clone.log
fi
cd "$repo"
git checkout master >&checkout.log
git fetch origin >&fetch.log
git pull >&pull.log
cd ..
done
# get a SF revision list
cd Stockfish
revs=$(git rev-list $firstrev^..$lastrev)
get_pawn_value() {
# extract pawn value from "--NormalizeToPawnValue" or "--NormalizeData" string
local pawn=$(echo "$1" | awk '/--NormalizeToPawnValue/ {print $2}')
if [ -z "$pawn" ]; then
# return the rounded sum of the coefficients in as
pawn=$(echo "$1" | grep -oP '"as":\[\K[^\]]+' | tr ',' '\n' | awk '{s+=$1} END {printf "%.0f", s}')
fi
echo "$pawn"
}
get_normalize_data() {
# construct the "--NormalizeToPawnValue" or "--NormalizeData" string
local revision="$1"
local pawn=$(git grep 'const int NormalizeToPawnValue' "$revision" -- src/uci.h | grep -oP 'const int NormalizeToPawnValue = \K\d+')
if [ -z "$pawn" ]; then
pawn=$(git grep 'constexpr int NormalizeToPawnValue' "$revision" -- src/uci.cpp | grep -oP 'constexpr int NormalizeToPawnValue = \K\d+')
fi
if [ -z "$pawn" ]; then
line=$(git grep 'double m = std::clamp(material' "$revision" -- src/uci.cpp)
momMin="${line#*std::clamp(material, }"
momMin="${momMin%%,*}"
momMax="${line##*, }"
momMax="${momMax%%)*}"
momTarget="${line##* }"
momTarget="${momTarget%.0*}"
line=$(git grep 'constexpr double as\[\] = {' "$revision" -- src/uci.cpp | grep -oP 'constexpr double as\[\] = {.*')
as="${line#*constexpr double as[] = \{}"
as="${as%\};}"
as=$(sed 's/ //g' <<<"$as") # remove spaces
echo "--NormalizeData {\"momType\":\"material\",\"momMin\":$((momMin)),\"momMax\":$((momMax)),\"momTarget\":$((momTarget)),\"as\":[$as]}"
else
echo "--NormalizeToPawnValue $pawn"
fi
}
# get the currently valid value of NormalizeData
oldnormdata=$(get_normalize_data "$firstrev")
oldpawn=$(get_pawn_value "$oldnormdata")
oldepoch=$(git show --quiet --format=%ci $firstrev)
newepoch=$(git show --quiet --format=%ci $lastrev)
# build a regex pattern to match all revisions
regex_pattern=""
for rev in $revs; do
regex_pattern="${regex_pattern}.*$rev|"
newnormdata=$(get_normalize_data "$rev")
if [[ "$oldnormdata" != "$newnormdata" ]]; then
echo "Revision $rev has wrong NormalizeData ($newnormdata != $oldnormdata)"
exit 1
fi
done
# remove the trailing "|"
regex_pattern="${regex_pattern%|}"
cd ..
# compile scoreWDLstat if needed
make >&make.log
echo "Look recursively in directory $pgnpath for games with max nElo" \
"difference $EloDiffMax using" \
"books matching \"$bookname\" for SF revisions between $firstrev (from" \
"$oldepoch) and $lastrev (from $newepoch)."
# obtain the WDL data from games of the SF revisions of interest
./scoreWDLstat --dir $pgnpath -r --matchTC "60\+0.6" --matchThreads 1 --EloDiffMax $EloDiffMax --matchRev $regex_pattern --matchBook "$bookname" -o updateWDL.json >&scoreWDLstat.log
gamescount=$(grep -o '[0-9]\+ games' scoreWDLstat.log | grep -o '[0-9]\+')
if [[ $gamescount -eq 0 ]]; then
echo "No games found for revisions of interest."
exit 0
fi
# fit the new WDL model, keeping anchor at material 58
python scoreWDL.py updateWDL.json --plot save --pngName updateWDL.png --pngNameDistro updateWDLdistro.png --momType material --momTarget 58 --materialMin $materialMin --moveMin 1 --modelFitting optimizeProbability $oldnormdata >&scoreWDL.log
# extract the total number of positions, and the new NormalizeToPawnValue
poscount=$(awk -F '[() ,]' '/Retained \(W,D,L\)/ {sum = 0; for (i = 9; i <= NF; i++) sum += $i; printf "%.0f\n", sum; exit}' scoreWDL.log)
if [[ $poscount -eq 0 ]]; then
echo "No positions found."
exit 0
fi
newpawn=$(grep -oP 'const int NormalizeToPawnValue = \K\d+' scoreWDL.log)
if [[ $newpawn -ne $oldpawn ]]; then
echo "Based on $poscount positions from $gamescount games, NormalizeToPawnValue should change from $oldpawn to $newpawn."
else
echo "Based on $poscount positions from $gamescount games, NormalizeToPawnValue should stay at $oldpawn."
fi
echo "ended at: " $(date)