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

115 lines
3.8 KiB

#!/usr/bin/env bash
set -u
9 years ago
#set -x
# File locations
9 years ago
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
9 years ago
rounds=6
width=500
height=500
robot1="sample.Corners"
robot2="sample.Fire"
9 years ago
9 years ago
# Iterate over arguments
while :; do
9 years ago
# If there are any
if [ $# -gt 0 ] ; then
case $1 in
# Regenerate data files
-c|--clean)
rm -rf "$datadir"
;;
9 years ago
# Call a "show_help" function to display a synopsis, then exit.
-h|-\?|--help)
echo "Generate the data required for SVM classification and show the accuracy"
echo "of the generated SVM model. If coordinates are supported, one visual battle"
echo "is shown to support the prediction."
echo ""
9 years ago
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 " -c, --clean Regenerate the battle data (may take a very long time)"
echo " -h, --help Show this help"
exit
;;
9 years ago
# Unrecognized parameter
-?*)
printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2
;;
9 years ago
# 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
# Prepare the data directory
mkdir -p "$datadir/"
touch "$results"
9 years ago
# If the data file has required amount if data
lines=$(wc -l < "$results")
if [[ "$lines" -eq "$rounds" ]] ; then
# 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
# Generate the battle data (run 1000 battles) for a SVM classification
9 years ago
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}')
9 years ago
if [ "$winner" == "$robot1" ] ; then
winner=1
else
winner=2
fi
9 years ago
# 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. Run the command again the show the accuracy.\n"
9 years ago
fi