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

121 lines
3.8 KiB

#!/usr/bin/env bash
set -u
#set -x
# File locations
dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
datadir="$dir/data"
results="$datadir/battle.data"
settings="$datadir/settings.last.battle"
training="$datadir/training.scale.data"
testing="$datadir/testing.scale.data"
# Battle parameters
rounds=6
width=500
height=500
robot1="sample.Corners"
robot2="sample.Fire"
# Prepare the data directory
mkdir -p "$datadir/";
# Iterate over arguments
while :; do
# If there are any
if [ $# -gt 0 ] ; then
case $1 in
# Regenerate data files
-c|--clean)
rm -rf "$datadir"
;;
# Call a "show_help" function to display a synopsis, then exit.
-h|-\?|--help)
echo "Generate the data required for SVM classification."
echo "USAGE: robocode-svm [OPTION] [COORDINATES]"
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 Regenerate the battle data (may take a very long time)"
echo " -h, --help Show this help"
exit
;;
# Just accuracy
#-a|--accuracy)
#show_accuracy
#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
else
break
fi
done
# Generate the battle data (run 1000 battles) for a SVM classification
# If the data file has required amount if data
lines=$(wc -l < "$results")
if [[ "$lines" -eq "$rounds" ]] ; then
echo "Data are already generated."
else
# Repeat the battle desired number of times (till the battle data file does not contain $rounds entries
echo "Generating the data for SVM predition. Depending on the machine, this may take very long time."
delta=$((rounds - lines))
for ((i = 1; i <= "$delta"; i++)) ; 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"
echo "robocode.battleField.width=$width"
echo "robocode.battleField.height=$height"
echo "robocode.battle.numRounds=1"
echo "robocode.battle.gunCoolingRate=0.07"
echo "robocode.battle.rules.inactivityTime=450"
echo "robocode.battle.selectedRobots=$robot1,$robot2"
echo "robocode.battle.initialPositions=($((width/2)),$((height/2)),0),($x,$y,$alpha)"
} > "$settings"
# Run battle without GUI, following the settings file, and returning the lines containing winners
winner=$(robocode -nodisplay -battle "$settings" | tail -2| awk 'NR==1F {print $2}')
if [ "$winner" == "$robot1" ] ; then
winner=1
else
winner=2
fi
# Append the result of the battle to the battle data file
echo "$winner 1:$x 2:$y 3:$alpha" >> "$results"
# Print a dot to infrom user tht something is happening
printf "."
done
printf "\nData generation successful.\n"
fi
# Calculate and print the acuuracy of classification, given the test sample
# 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