A script to demonstrate Support Vector Machine (SVM) classification on outcomes of the battles of Robocode.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
robocode-svm/robocode-svm

119 lines
3.3 KiB

#!/usr/bin/env bash
set -u
# set -x
# File locations
datadir="data"
results="$datadir/battle.data"
settings="$datadir/settings.last.battle"
training="$datadir/training.scale.data"
testing="$datadir/testing.scale.data"
# Battle parameters
rounds=1000
width=500
height=500
robot1="sample.Corners"
robot2="sample.Fire"
# Delete the battle data so the will get freshly generated
flush_battle_data() {
rm -f "$datadir/*"
touch "$results"
}
# Help function
show_help() {
echo "USAGE: robocode-svm OPTIONS"
echo " -v, --visualize Shows visual run of the battle"
echo " -x The x coordinate of the second robot, used with -v"
echo " -y The y coordinate of the second robot, used with -v"
echo " -a, --accuracy Only show the accuracy of the SVM prediction"
echo " -c, --clean Clean run (deletes everything inside data folder)"
echo " -h, --help Show this help"
}
# Run battle without GUI, following the settings file, and returning the lines containing winners
run_battle() {
robocode -nodisplay -battle "$settings" | tail -2;
}
# Prepare the data directory
mkdir -p "$datadir/";
while :; do
case $1 in
# Regenerate data files
-c|--clean)
flush_battle_data
;;
# Call a "show_help" function to display a synopsis, then exit.
-h|-\?|--help)
show_help
exit
;;
# Unrecognized parameter
-?*)
printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2
;;
# Default case: If no more options then break out of the loop.
*)
break
esac
# "command" reduces the chance of fatal errors in many shells.
command shift
done
# Repeat the battle desired number of times
for i in $(seq 1 $rounds);
do
# Generate input parameters
number=$RANDOM; let "number %= $width"; x=$number
number=$RANDOM; let "number %= $height"; y=$number
number=$RANDOM; let "number %= 360"; alpha=$number
# Write current settings to a file; first robot starts in the middle, with fixed gun angle;
# the other one start at random known position, with random known gun angle
echo "#Battle Properties
robocode.battleField.width=$width
robocode.battleField.height=$height
robocode.battle.numRounds=1
robocode.battle.gunCoolingRate=0.07
robocode.battle.rules.inactivityTime=450
robocode.battle.selectedRobots=$robot1,$robot2
robocode.battle.initialPositions=($((width/2)),$((height/2)),0),($x,$y,$alpha)" > "$settings"
# If the robots are of the same name
if [ "$robot1" == "$robot2" ] ; then
# Their order number is in the bracket after the name
winner=$(run_battle | awk 'NR==1 {print substr($3,2,1)}')
else
# Otherwise we need to match the actual names
winner=$(run_battle | awk 'NR==1F {print $2}')
if [ "$winner" == "$robot1" ] ; then
winner=1
else
winner=2
fi
fi
# Inform user about the winner of current round and append the results to the file
echo "Round $i winner: $winner"
# echo "$winner 1:$x 2:$y" >> "$results"
echo "$winner 1:$x 2:$y 3:$alpha" >> "$results"
done
# Calculate the lines needed to split the data to 90% and 10%
tr=$(bc <<< "scale=0; $rounds * 0.9 / 1");
te=$(bc <<< "scale=0; ($rounds - $tr) / 1");
# Scale the results to interval <0, 1> and split them to training set and testing set;
# misuse tee for 'process substituion' and send its stdout do /dev/null
svm-scale -l 0 -u 1 "$results" | tee >(head -n "$tr" > "$training") >(tail -n "$te" > "$testing") > /dev/null