#!/bin/bash
###############################################################################
# This script is run for each candidate to evaluate it after all
# candidate configurations have been run on a single instance.
#
# Check the examples in examples/
#
# PARAMETERS:
# $1 is the candidate number
# $2 is the instance id
# $3 is the seed
# $4 is the instance name
# $5 is the number of candidates alive in this iteration
#
# ONLY FOR ELITIST RACE: The rest ($* after `shift 5') are the ids of the 
# candidates alive in this iteration. This list can be used to calculate the 
# hypervolume using only active candidates.
#
# RETURN VALUE:
# This script should print one numerical value: the cost that must be minimized.
# Exit with 0 if no error, with 1 in case of error
###############################################################################
error() {
    echo "`TZ=UTC date`: $0: error: $@"
    exit 1
}

CANDIDATE="$1"
INSTANCEID="$2"
SEED="$3"
INSTANCE="$4"
TOTALCANDIDATES="$5"
shift 5 || error "Not enough parameters"
ALLIDS=$*

STDOUT=c${CANDIDATE}-${INSTANCEID}-${SEED}.stdout
STDERR=c${CANDIDATE}-${INSTANCEID}-${SEED}.stderr

ALLFILES=
for FILENAME in $ALLIDS; do
    ALLFILES="$ALLFILES c${FILENAME}-${INSTANCEID}-${SEED}.stdout"
done

# # This may be used to introduce a delay if there are filesystem
# # issues.
# SLEEPTIME=1
# while [ ! -s "${STDOUT}" ]; do
#     sleep $SLEEPTIME
#     let "SLEEPTIME += 1"
# done

# This is an example of reading a number from the output of
# target-runner. It assumes that the objective value is the first number in
# the first column of the only line starting with a digit.
if [ -s "${STDOUT}" ]; then
    # You may need to update this to parse the output of your algorithm.
    COST=$(cat ${STDOUT} | grep -e '^[[:space:]]*[+-]\?[0-9]' | cut -f1)
    echo "$COST"
    TODELETE=$(comm -23 <(ls -1 c*-${INSTANCEID}-${SEED}.* | sort) <(ls -1 $ALLFILES))
    # Comment out if you wish to keep all output files around
    echo "rm -f $TODELETE"
    exit 0
else
    error "${STDOUT}: No such file or directory"
fi

