From 05dd94dd81d97a0fd78047179cdbffc74d8089ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Babi=C4=8D?= Date: Sun, 26 Apr 2015 13:26:27 +0200 Subject: [PATCH] shellchecked and refactored --- robocode-svm | 51 ++++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/robocode-svm b/robocode-svm index ed98867..d4e6364 100755 --- a/robocode-svm +++ b/robocode-svm @@ -18,8 +18,6 @@ height=500 robot1="sample.Corners" robot2="sample.Fire" -# Prepare the data directory -mkdir -p "$datadir/"; # Iterate over arguments while :; do @@ -32,21 +30,18 @@ while :; do ;; # Call a "show_help" function to display a synopsis, then exit. -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 " -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 @@ -54,21 +49,29 @@ while :; do # 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 + 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" -# 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." + # 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 # 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." @@ -105,17 +108,7 @@ else # Print a dot to infrom user tht something is happening printf "." done - printf "\nData generation successful.\n" + printf "\nData generation successful. Run the command again the show the accuracy.\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 - -