#!/bin/bash

if [ $# -ne 7 ] ; then
  echo "Usage: run ALG DATADIR FRAC ROUNDS TIMELIMIT DELTA WORKDIR"
  exit
fi
  
ALG=$1
DATADIR=$2
FRAC=$3
ROUNDS=$4
TIMELIMIT=$5
DELTA=$6
WORKDIR=$7
STARTDIR=`pwd`
if [ $SEMIBOUND_DIR ] ; then
  BASEDIR=$SEMIBOUND_DIR
else
  echo "SEMIBOUND_DIR not defined!"
  exit
fi

if [ ! -d $DATADIR ] ; then 
  echo "Directory $DATADIR does not exist!"
  exit
fi
cd $DATADIR
DATADIR=`pwd`
cd $STARTDIR

# copy the data to workdir
if [ -e $WORKDIR ] ; then
  echo "Directory $WORKDIR already exists!"
  exit
fi
mkdir -p $WORKDIR
cd $WORKDIR
WORKDIR=`pwd`

cp $DATADIR/* .

# progressive validation specific stuff starts here
ROUNDS=`$BASEDIR/progressive/gen_samples $FRAC $ROUNDS`
cd $BASEDIR

cd algs/$ALG
for i in `seq 0 $ROUNDS`; do
  mv $WORKDIR/train $WORKDIR/train_$i
  train $WORKDIR/train_$i
  mv $WORKDIR/train_$i $WORKDIR/train
  if [ $i -lt $ROUNDS ] ; then
    predict $WORKDIR/test_$(($i+1)) $WORKDIR/train_$i $WORKDIR/test_$(($i+1))_pred
    cat $WORKDIR/test_$(($i+1))_pred >> $WORKDIR/test_preds
    cat $WORKDIR/test_$(($i+1)) >> $WORKDIR/train
  fi
  predict $WORKDIR/unlabeled $WORKDIR/train_$i $WORKDIR/unlabeled_${i}_preds
done;
cd $BASEDIR

for i in $WORKDIR/*_preds ; do
  algs/$ALG/labels $i > $WORKDIR/tmp
  mv $WORKDIR/tmp $i;
done;

tail -n `lines $WORKDIR/test_preds` $WORKDIR/labeled | algs/$ALG/labels > $WORKDIR/tmp
errors=`errors $WORKDIR/tmp $WORKDIR/test_preds`
batch_size=`lines $WORKDIR/test_1`
DELTASHARE=`perl -e "print $DELTA / 2"`
progressive/killer $TIMELIMIT progressive/pv_bound $DELTASHARE $ROUNDS $batch_size $errors > $WORKDIR/pv_estimates
bound_for_rand=`progressive/best_bound $WORKDIR/pv_estimates`

# compute the distance from the randomized to the final hypothesis
ensemble `ls $WORKDIR/unlabeled_*_preds | grep -v $WORKDIR/unlabeled_${ROUNDS}_preds` > $WORKDIR/unlabeled_ensemble
random_predict $WORKDIR/unlabeled_ensemble > $WORKDIR/unlabeled_ensemble_preds 
errors=`errors $WORKDIR/unlabeled_${ROUNDS}_preds $WORKDIR/unlabeled_ensemble_preds`
cases=`lines $WORKDIR/unlabeled_${ROUNDS}_preds`
DELTASHARE=`perl -e "print $DELTA / 2"`
bound_dist=`upper_bound $DELTASHARE $cases $errors`

# cheat and compute the error rate of the randomized ensemble on unlabeled data
algs/$ALG/labels $WORKDIR/unlabeled > $WORKDIR/tmp
errors=`errors $WORKDIR/tmp $WORKDIR/unlabeled_ensemble_preds`
cases=`lines $WORKDIR/tmp`
error_rate=`perl -e "print $errors / $cases"`

# output the bound
perl -e "print $bound_for_rand + $bound_dist" > $WORKDIR/results
echo "" $bound_for_rand $bound_dist $error_rate >> $WORKDIR/results
cat $WORKDIR/results
GLOBIGNORE=$WORKDIR/results:$WORKDIR/pv_estimates
rm $WORKDIR/*
