#!D:\perl\bin\perl.exe

# Creates an avarage gnuplot graph from multiple datasets

use Cwd;
open (OUTFILE_A, ">average.dat");
open (OUTFILE_B, ">average_min_sd.dat");
open (OUTFILE_C, ">average_plus_sd.dat");
$dirname = cwd();
my @data = ();
my $count =0;
my %params = ();
my @all;

opendir(DIR, $dirname) or die "can't opendir $dirname: $!";
while (defined($file = readdir(DIR))) {
	if ($file =~ /.*?result\.dat$/){
      print OUTFILE_A "# $dirname/$file\n";
      print OUTFILE_B "# $dirname/$file\n";
      print OUTFILE_C "# $dirname/$file\n";
      open (DATAFILE, "$dirname/$file");
      $count++;
      # Parse one of the result.dat files
      while ($line = <DATAFILE>){
      	  my @dataset =();
      	  if ($line !~ /^#.*$/){
      	  	 my ($number, $value) = split / /, $line;
      	  	 $data[$number] += $value;
      	  	 $all[$count][$number] = $value + 0;
      	  }	
      	  if ($line =~ /# (.*?) =(.*?)$/){
      	  	  $params{$1}=$2;
      	  }	
      }	
      close (DATAFILE);
      #$all[$count] =   $dataset  ;
    } 
}


for ($i=1; $i < @data; $i++){
	#bereken standaard-deviatie rond $i
	my $gem = $data[$i]/$count;
	my $sd = 0;
	for ($datarij=1; $datarij <= $count; $datarij++) {
	  $datapunt = $all[$datarij][$i];
	  #print "$datapunt ";
	  #print "i = $i ; datapunt = $datapunt ; gem = $gem \n";	
	  $sd += (($datapunt - $gem) * ($datapunt - $gem)) / ($count -1);
	}
	$sd = sqrt($sd);
    print OUTFILE_A "$i $gem \n";
    print OUTFILE_B "$i " . ($gem - $sd) . "\n";
    print OUTFILE_C "$i " . ($gem + $sd) . "\n";
    #print "\n $i " . $sd . " " . $gem . " " . ($gem - $sd) . " " . ($gem + $sd) . "\n";		
}	

closedir(DIR);
close (OUTFILE_A);
close (OUTFILE_B);
close (OUTFILE_C);

############################# Alleen gemiddelde-1000 ######################
open (OUTFILE, ">average1000.plt");
print OUTFILE "set terminal png small color\n";
print OUTFILE "set output \"average1000.png\"\n";
print OUTFILE "set data style lines\n";
print OUTFILE "set xlabel 'Iterations'\n";
print OUTFILE "set ylabel 'Average Tour Length (best sofar)'\n";
print OUTFILE "set xrange [0:1000]\n";
print OUTFILE "set yrange [770:840]\n";
print OUTFILE "set title \"";
foreach $param (sort(keys %params)){
   print OUTFILE "$param=$params{$param}\\n"
}
print OUTFILE "\"\n";   
print OUTFILE "plot 'average.dat' ";

############################# Alleen gemiddelde-5000 ######################
open (OUTFILE, ">average5000.plt");
print OUTFILE "set terminal png small color\n";
print OUTFILE "set output \"average5000.png\"\n";
print OUTFILE "set data style lines\n";
print OUTFILE "set xlabel 'Iterations'\n";
print OUTFILE "set ylabel 'Average Tour Length (best sofar)'\n";
print OUTFILE "set xrange [0:5000]\n";
print OUTFILE "set yrange [770:840]\n";
print OUTFILE "set title \"";
foreach $param (sort(keys %params)){
   print OUTFILE "$param=$params{$param}\\n"
}
print OUTFILE "\"\n";   
print OUTFILE "plot 'average.dat' ";


############################# Gemiddelde en SD ###########################
open (OUTFILE, ">average_with_SD.plt");
print OUTFILE "set terminal png small color\n";
print OUTFILE "set output \"average_SD.png\"\n";
print OUTFILE "set data style lines\n";
print OUTFILE "set xlabel 'Iterations'\n";
print OUTFILE "set ylabel 'Average Tour Length (best sofar)'\n";
print OUTFILE "set xrange [0:5000]\n";
print OUTFILE "set yrange [770:840]\n";
print OUTFILE "set title \"";
foreach $param (sort(keys %params)){
   print OUTFILE "$param=$params{$param}\\n"
}
print OUTFILE "\"\n";   
print OUTFILE "plot 'average.dat' , 'average_min_sd.dat' , 'average_plus_sd.dat' ";




