#!/bin/bash
###############################################################################
# target-runner for irace
#
# Called as:
#   target-runner <configuration_id> <instance_id> <seed> <instance> <params>
#
# Instance format (from train-instances.txt):
#   filepath,d,k,best_known
#
# Tuned parameters (passed as --name value pairs):
#   --use_vnd1 yes|no      --use_vnd2 yes|no
#   --use_vnd3 yes|no      --use_vnd4 yes|no
#   --bi_vnd1 yes|no       --bi_vnd2 yes|no
#   --bi_vnd3 yes|no       --bi_vnd4 yes|no
#   --shake_type 1|2|3
#   --vnd_order  <categorical: 2134, 1234, etc.>
###############################################################################

CONFIG_ID="$1"
INSTANCE_ID="$2"
SEED="$3"
INSTANCE="$4"
shift 4

# --- Parse tuned parameters ---
# NOTE: passed positionally as argv[5] (the iters slot); main.cpp does atoi(argv[5]),
# so this MUST be a bare number. The old "--iters 100000" parsed to atoi()=0, which
# skipped the entire VNS loop and made every configuration identical. See irace12_fixed.
ITERS="100000"
USE_VND1="yes"
USE_VND2="yes"
USE_VND3="yes"
USE_VND4="yes"
BI_VND1="no"
BI_VND2="no"
BI_VND3="no"
BI_VND4="no"
SHAKE_TYPE="1"
VND_ORDER="2134"

while [ $# -gt 0 ]; do
    case "$1" in
        --iters*)
            ITERS="${1#--iters}"
            if [ -z "$ITERS" ]; then ITERS="$2"; shift; fi
            shift ;;
        --use_vnd1*)
            USE_VND1="${1#--use_vnd1}"
            if [ -z "$USE_VND1" ]; then USE_VND1="$2"; shift; fi
            shift ;;
        --use_vnd2*)
            USE_VND2="${1#--use_vnd2}"
            if [ -z "$USE_VND2" ]; then USE_VND2="$2"; shift; fi
            shift ;;
        --use_vnd3*)
            USE_VND3="${1#--use_vnd3}"
            if [ -z "$USE_VND3" ]; then USE_VND3="$2"; shift; fi
            shift ;;
        --use_vnd4*)
            USE_VND4="${1#--use_vnd4}"
            if [ -z "$USE_VND4" ]; then USE_VND4="$2"; shift; fi
            shift ;;
        --bi_vnd1*)
            BI_VND1="${1#--bi_vnd1}"
            if [ -z "$BI_VND1" ]; then BI_VND1="$2"; shift; fi
            shift ;;
        --bi_vnd2*)
            BI_VND2="${1#--bi_vnd2}"
            if [ -z "$BI_VND2" ]; then BI_VND2="$2"; shift; fi
            shift ;;
        --bi_vnd3*)
            BI_VND3="${1#--bi_vnd3}"
            if [ -z "$BI_VND3" ]; then BI_VND3="$2"; shift; fi
            shift ;;
        --bi_vnd4*)
            BI_VND4="${1#--bi_vnd4}"
            if [ -z "$BI_VND4" ]; then BI_VND4="$2"; shift; fi
            shift ;;
        --shake_type*)
            SHAKE_TYPE="${1#--shake_type}"
            if [ -z "$SHAKE_TYPE" ]; then SHAKE_TYPE="$2"; shift; fi
            shift ;;
        --vnd_order*)
            VND_ORDER="${1#--vnd_order}"
            if [ -z "$VND_ORDER" ]; then VND_ORDER="$2"; shift; fi
            shift ;;
        *) shift ;;
    esac
done

# --- Parse instance string: filepath,d,k,best_known ---
IFS=',' read -r FILEPATH D K BK <<< "$INSTANCE"

# --- Resolve paths relative to project root (one level up from irace/) ---
BINDIR="$(cd "$(dirname "$0")/.." && pwd)"
EXE="${BINDIR}/dckmst"
FILEPATH="${BINDIR}/${FILEPATH}"

# --- Extract n from the instance file ---
if [[ "$FILEPATH" == *.tsp ]]; then
    N=$(grep -i "DIMENSION" "$FILEPATH" | head -1 | sed 's/[^0-9]//g')
else
    N=$(head -1 "$FILEPATH" | awk '{print $1}')
fi

# --- Compute k_max = max(1, round(alpha * sqrt(n))) ---
K_MAX=$(awk "BEGIN { v = ${K}; v = int(v - 1); if (v < 1) v = 1; print v }")

# --- Build flags ---
FLAGS="--max-time 300"
if [ "$USE_VND1" = "no" ]; then FLAGS="$FLAGS --no-vnd1"; fi
if [ "$USE_VND2" = "no" ]; then FLAGS="$FLAGS --no-vnd2"; fi
if [ "$USE_VND3" = "no" ]; then FLAGS="$FLAGS --no-vnd3"; fi
if [ "$USE_VND4" = "no" ]; then FLAGS="$FLAGS --no-vnd4"; fi
if [ "$BI_VND1"  = "yes" ]; then FLAGS="$FLAGS --vnd1bi"; fi
if [ "$BI_VND2"  = "yes" ]; then FLAGS="$FLAGS --vnd2bi"; fi
if [ "$BI_VND3"  = "yes" ]; then FLAGS="$FLAGS --vnd3bi"; fi
if [ "$BI_VND4"  = "yes" ]; then FLAGS="$FLAGS --vnd4bi"; fi
if [ "$SHAKE_TYPE" = "2" ]; then FLAGS="$FLAGS --shake2"; fi
if [ "$SHAKE_TYPE" = "3" ]; then FLAGS="$FLAGS --shake3"; fi
FLAGS="$FLAGS --vnd-order $VND_ORDER --best-known $BK"

# --- Run solver ---
OUTPUT=$("$EXE" "$D" "$K" "$FILEPATH" "$K_MAX" "$ITERS" "$SEED" $FLAGS 2>&1)

# --- Extract cost (the objective to minimize) ---
COST=$(echo "$OUTPUT" | grep "^cost:" | awk '{print $2}')

if [ -z "$COST" ]; then
    echo "ERROR: failed to extract cost from output" >&2
    echo "$OUTPUT" >&2
    exit 1
fi

echo "$COST"
exit 0
