#!/bin/bash

# 36PAA homework 4
# Jakub Holy
#
# PURPOSE: Acording to its parameter this script runs tests on one characteristic
# of instances of the knapsack problem (namely, all input values of the 
# instance generator expcept one have default values, only the one changes).
# Output is stored in the files hw4_results/(bb|hr|dp).avrg.(m|C|W|dk).(costs|steps)
#
ID=""
DIR="hw4_results"
FILENAME="instance"
DEST=${DIR}"/"${FILENAME}
#ID=0
M="0.5"
K=1
W=300
C=300
D="0"

if [ ! -d $DIR ]; then mkdir $DIR; fi
if [ $# -ne 1 ]; then echo "usage: run_hw4 (m|C|W|d)"; exit; fi
echo "run: Going to run [$1]"

# ------------------------------------------------------------------------- process()
# Params: $1=what_param $2=ID_Prefix $3=param's_value
function solvef ()
{
DEST2=${DEST}"."${1}$3
if [  -z $ID ]; then ID=`expr $2 \* 1000`; fi
#echo "$3" >> ${DIR}/$1.values
./generator  -I $ID -m $M -k $K -W $W -C $C -d $D > $DEST2
./solve4 $DEST2
ID=`expr $ID + 50`	#increment by number of instances per file
}

# Params: $1=what_param $2=ID_Prefix
function outputf ()
{
 process output (join outputs for all values of m)
for ALG in bb hr dp; do
    NAME=${DIR}/${ALG}.avrg
    cat $NAME"."${2}* | cut -f1  > $NAME.${1}.steps
    cat $NAME"."${2}* | cut -f2  > $NAME.${1}.costs
done
}
# ------------------------------------------------------------------------------
# ----------------------------------------------------------------------------- M
if [ "$1" = "m" ]; then 
    echo "Running on m"
    for M in 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9; do
	solvef  $1 10 $M
	outputf $1 10
    done
fi # if m

# ----------------------------------------------------------------------------- M
if [ "$1" = "C" ]; then 
    echo "Running on Cost"    
    # We've 30 items and the costs are distributed evenly
    for C in 30 60 90 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600; do
	solvef  $1 20 $C
	outputf $1 20
    done    
fi # if C

# ----------------------------------------------------------------------------- W
if [ "$1" = "W" ]; then 
    echo "Running on Weight"    
# Generator doesn't work for W=30
    for W in 31 60 90 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600; do
	solvef  $1 30 $W
	outputf $1 30
    done    
fi # if C

# ----------------------------------------------------------------------------- W
if [ "$1" = "d" ]; then 
    echo "Running on d"    
    for D in -1 1; do
	for K in -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6; do
	    # Small mistake: output for d=1 is also appended to the result for d=-1
	    solvef  "$1$D" 40 $k
	    outputf "$1$D" 40
	done
    done    
fi # if C