shellchecked and refactored

master
Peter Babič 9 years ago
parent d33f18e6c0
commit 05dd94dd81
  1. 51
      robocode-svm

@ -18,8 +18,6 @@ height=500
robot1="sample.Corners" robot1="sample.Corners"
robot2="sample.Fire" robot2="sample.Fire"
# Prepare the data directory
mkdir -p "$datadir/";
# Iterate over arguments # Iterate over arguments
while :; do while :; do
@ -32,21 +30,18 @@ while :; do
;; ;;
# Call a "show_help" function to display a synopsis, then exit. # Call a "show_help" function to display a synopsis, then exit.
-h|-\?|--help) -h|-\?|--help)
echo "Generate the data required for SVM classification." 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 ""
echo "USAGE: robocode-svm [OPTION] [COORDINATES]" echo "USAGE: robocode-svm [OPTION] [COORDINATES]"
echo " -v, --visualize Shows visual run of the battle" echo " -v, --visualize Shows visual run of the battle"
echo " -x The x coordinate of the second robot, used with -v" 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 " -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 " -c, --clean Regenerate the battle data (may take a very long time)"
echo " -h, --help Show this help" echo " -h, --help Show this help"
exit exit
;; ;;
# Just accuracy
#-a|--accuracy)
#show_accuracy
#exit
#;;
# Unrecognized parameter # Unrecognized parameter
-?*) -?*)
printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2 printf 'WARNING: Unknown option (ignored): %s\n' "$1" >&2
@ -54,21 +49,29 @@ while :; do
# Default case: If no more options then break out of the loop. # Default case: If no more options then break out of the loop.
*) *)
break break
esac esac
# "command" reduces the chance of fatal errors in many shells.
# "command" reduces the chance of fatal errors in many shells. command shift
command shift else
else break
break fi
fi
done done
# Prepare the data directory
mkdir -p "$datadir/"
touch "$results"
# Generate the battle data (run 1000 battles) for a SVM classification
# If the data file has required amount if data # If the data file has required amount if data
lines=$(wc -l < "$results") lines=$(wc -l < "$results")
if [[ "$lines" -eq "$rounds" ]] ; then if [[ "$lines" -eq "$rounds" ]] ; then
echo "Data are already generated." # 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
else else
# Repeat the battle desired number of times (till the battle data file does not contain $rounds entries # 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." echo "Generating the data for SVM predition. Depending on the machine, this may take very long time."
@ -105,17 +108,7 @@ else
# Print a dot to infrom user tht something is happening # Print a dot to infrom user tht something is happening
printf "." printf "."
done done
printf "\nData generation successful.\n" printf "\nData generation successful. Run the command again the show the accuracy.\n"
fi 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

Loading…
Cancel
Save