#!/bin/bash
#
# usage: general_times.sh  <type> <variables> <trials>
#
# (C) Emilia Oikarinen, 2005

type=$1
variables=$2
trials=$3

DIR=../bin
PROG=../programs

# checking that needed directories exist

if test -d results
then 
    true
else mkdir results
fi

if test -d seeds
then 
    true
else mkdir seeds
fi

if test -d tmp
then 
    true
else mkdir tmp
fi

SEEDS=seeds/$type.$variables.$trials

FILE1=results/lpeq-times-$type.$variables.$trials
FILE2=results/naive-times-$type.$variables.$trials

INPUT1=tmp/input1.sm
INPUT2=tmp/input2.sm
TEMP1=tmp/tmp1
TEMP2=tmp/tmp2

case "$type" in
    
    '3sat_const_ratio')
        const_ratio=4
        clauses=`echo "$const_ratio*$variables" | bc`
        ;;

    '3sat_varying_ratio')
        clauses=$variables
        variables=40
        ;; 

    'knapsack')
        profit2=`expr $variables - 1`
        ;;
esac   

if test -f $FILE1
then
    i=`cat $FILE1 | wc -l`
    i=`expr $i + 1`
else
    i=1
fi

while test $i -le $trials
  do

  echo -n $i" " >> $FILE1
  echo -n $i" " >> $FILE2 
  
  # creating input files for different problem types
  case "$type" in
      
      '3sat_const_ratio' | '3sat_varying_ratio')
          seed=$RANDOM # seed for rsat
          echo -n $seed >> $SEEDS
          $DIR/rsat -s$seed 3 $variables $clauses | $DIR/lparse -d all > $TEMP1
          ;;
    
      'color')
          seed=$RANDOM # seed for planar
          echo -n $seed >> $SEEDS
          $DIR/planar $variables $seed | cat $PROG/color.lp - | \
                                                    $DIR/lparse -c c=4 > $TEMP1
        ;;    
      
      'hc')
          seed=$RANDOM # seed for planar
          echo -n $seed >> $SEEDS
          $DIR/planar $variables $seed | cat $PROG/hc.lp - | $DIR/lparse \
                                                                       > $TEMP1
          ;;

      'queens1')
          cat $PROG/queens-x1.lp | $DIR/lparse -d all -cqueens=$variables \
                                                                       > $TEMP1
          cat $PROG/queens-x2.lp | $DIR/lparse -d all -cqueens=$variables \
                                                                       > $TEMP2
          ;;

      'queens2')
          cat $PROG/queens-x1.lp | $DIR/lparse -d all -cqueens=$variables \
                                                                       > $TEMP1
          cat $PROG/queens-y.lp | $DIR/lparse -d all -cqueens=$variables \
                                                                       > $TEMP2
          ;;

      'knapsack')
          $DIR/lparse -cmax_size=127 -cmin_profit=$variables $PROG/knapsack.lp\
                                                                       > $TEMP1
          $DIR/lparse -cmax_size=127 -cmin_profit=$profit2 $PROG/knapsack.lp \
                                                                       > $TEMP2
          ;;
  esac
  
  case "$type" in

      '3sat_const_ratio' | '3sat_varying_ratio' | 'color' | 'hc')

          seed=$RANDOM # seed for drop
          echo -n " "$seed >> $SEEDS
          $DIR/drop -s$seed $TEMP1 > $TEMP2
          ;;
  esac
  
  #shuffle the input

   seed=$RANDOM # seed to shuffle first program
  echo -n " "$seed >> $SEEDS
  $DIR/shuffle -s$seed $TEMP1 > $INPUT1
  
  seed=$RANDOM # seed to shuffle second program
  echo " "$seed >> $SEEDS
  $DIR/shuffle -s$seed $TEMP2 > $INPUT2

  # lpeq approach

  /usr/bin/time -f "%U %S" -o $TEMP1 ./lpeq.sh $INPUT1 $INPUT2 > $TEMP2
  cat $TEMP1 | awk '{t=$1+$2}; {printf("%f ",t)}' >> $FILE1
  fgrep points $TEMP2 | awk '{c=$5}; {printf("%d ",c)}' >> $FILE1 
  
  /usr/bin/time -f "%U %S" -o $TEMP1 ./lpeq.sh $INPUT2 $INPUT1 > $TEMP2
  cat $TEMP1 | awk '{t=$1+$2}; {printf("%f ",t)}' >> $FILE1
  fgrep points $TEMP2 | awk '{print $5}' >> $FILE1   

  # naive approach

  naive1=`./naive.sh $INPUT1 $INPUT2`
  naive2=`./naive.sh $INPUT2 $INPUT1` 
  echo $naive1" "$naive2 >> $FILE2

  i=`expr $i + 1` 

done 
