#!/bin/bash

FF=./macroff
ulimit -t 300
ulimit -v 1000000
ulimit -c 0

accumulate() {
  DIR=$2
  PDDL=$3
  PFILE=$4
  MACROFILE=$1
  echo --------------------------------------------------------------------
  echo Processing training step $PDDL - $PFILE
  echo --------------------------------------------------------------------
  rm -f ffua.out
  $FF -m P -r $MACROFILE -p $DIR -o $PDDL -f $PFILE
 echo -ne "$PDDL\t$PFILE\t" >> trainingcollection.out
 if test -f ffua.out; then
  echo -ne "$PROBLEM\t" >> trainingcollection.out
  echo -ne "1\t" >> trainingcollection.out
  if ./validate $DIR $PDDL $PFILE; then
   echo -ne "0\t" >> trainingcollection.out
  else
   echo -ne "1\t" >> trainingcollection.out
  fi
  cat ffua.out >> trainingcollection.out
  else
  echo -e "0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t1000000\t0\t0" >> trainingcollection.out
#   echo >> macrocollection.out
 fi
}

filtering() {
  DIR=$2
  PDDL=$3
  PFILE=$4
  MACROFILE=$1
  echo --------------------------------------------------------------------
  echo Performing static filtering $PDDL
  echo --------------------------------------------------------------------
  rm -f ffua.out
  $FF -m F -r $MACROFILE -p $DIR -o $PDDL -f $PFILE
}

run() {
 MACROS=$1
 DIR=$2
 PDDL=$3
 PFILE=$4
 PROBLEM=$5
 FILTER=$6

 echo --------------------------------------------------------------------
 echo $PDDL - $PFILE - $MACROS
 echo --------------------------------------------------------------------
 rm -f ffua.out
 if (( FILTER == 1 )); then
  $FF -q $MACROS -p $DIR -o $PDDL -f $PFILE -m D 
 else
  $FF -q $MACROS -p $DIR -o $PDDL -f $PFILE
 fi
 echo -ne "$PDDL\t$PFILE\t" >> macrocollection.out
 if test -f ffua.out; then
  echo -ne "$PROBLEM\t" >> macrocollection.out
  echo -ne "1\t" >> macrocollection.out
  if ./validate $DIR $PDDL $PFILE; then
   echo -ne "0\t" >> macrocollection.out
  else
   echo -ne "1\t" >> macrocollection.out
  fi
  cat ffua.out >> macrocollection.out
  else
  echo -e "0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t1000000\t0\t0" >> macrocollection.out
#   echo >> macrocollection.out
 fi
}

train_one_domain() {
  appname=$1
  path=$2
  domain=$3
  nrprobs=$#-4
  
  echo -ne "Starting time for training in domain $appname: " >> times.out
  date >> times.out
  echo -ne "" >> times.out
  count=0; 
  i=0;
  for name in "$@" ; do
    echo "count = $count"
    count=$(( $count + 1 ))
    if [ $count -ge 4 ] ; then
      problem[$i]=${name}
      echo "problem[$i] = $problem[$i]"
      i=$(( $i + 1 ))
    fi
  done

  rm -f $appname.macros
  #solve each training problem
  for ((i=0;i<$nrprobs;i++)) ; do
    accumulate $appname.macros $path $domain ${problem[$i]}
  done
  mv -f trainingcollection.out $appname.trainingcollection.out
  ./extract.pl $appname.nomacrossummary.txt $appname.trainingcollection.out
  
  #step 1 for static filtering and ranking
  cp $appname.macros $appname.macros.all
  filtering $appname.macros $path $domain ${problem[0]}
  cp $appname.macros $appname.macros.filtered
  rm -f *soln
  #generate one file for each macro
  ./macroff -q $appname.macros.filtered -m  E -p $path -o $domain -f ${problem[0]}
  for ((i=1;i<=15;i++)) ; do
    rm -f macrocollection.out
    macrofile="$appname.macros.filtered.$i"
    outputfile="$appname.collection.$i"
    if test -f $macrofile; then
      for ((j=0;j<=$nrprobs;j++)) ; do
            run $macrofile $path $domain ${problem[$j]}
      done
      mv -f macrocollection.out $outputfile
    fi
  done
  #reorder macros
  ./extract.pl $appname.summary.txt $appname.collection.*
  ./macroff -a $appname.summary.txt -q $appname.macros.filtered -m  R -p $path -o $domain -f ${problem[0]}
  #run with top k macros (iterate after k)
  for ((i=1;i<=15;i++)) ; do
    rm -f macrocollection.out
    macrofile="$appname.macros.filtered.top.$i"
    outputfile="$appname.collection.top.$i"
    if test -f $macrofile; then
      for ((j=0;j<=$nrprobs;j++)) ; do
            run $macrofile $path $domain ${problem[$j]}
      done
      mv -f macrocollection.out $outputfile
    fi
  done
  #process stats of running with top k macros
  ./extract.pl $appname.summary.top.txt $appname.collection.top.*
  # find the top k macros with the best performance and set their file as macros.best
  ./selectbestmacrofile.pl $appname.nomacrossummary.txt $appname.summary.top.txt $appname.macros.filtered.top $appname.macros.best
  echo -ne "End time for training in domain $appname: " >> times.out
  date >> times.out
  echo -ne "" >> times.out
}

