#!/usr/bin/perl -w

open(LOG, "<part3-random.log");

%searchmethods = ();

## keys are searchmethod names; values are array references
%times  = ();
%scores = ();
%steps  = ();
%dims   = ();

while (<LOG>) {

    chomp();
    my ($dataname, $initname, $searchtype, $scoretype, $score, $time,
	$steps, $dim, $strucpenal) = split /,\s*/;

    # Some of the initial data has no dimension information. Skip it.
    next if (!defined $dim);

    my $searchmethod = "${dataname}-${scoretype}-${searchtype}";
    $searchmethods{$searchmethod}++;

    push @{$times{$searchmethod}}, $time;
    push @{$scores{$searchmethod}}, $score;
    push @{$steps{$searchmethod}}, $steps;
    push @{$dims{$searchmethod}},  $dim;
}
close(LOG);

for my $method (sort keys %searchmethods) {
  print "Number of different starting graphs: "
    . $searchmethods{$method} . "\n";

  &summarize_info($method,
		  $times{$method}, $scores{$method},
		  $steps{$method}, $dims{$method}
		 );
}

sub summarize_info {
  my ($method, $tiref, $scref, $stref, $diref) = @_;

  @times  = @{$tiref};
  @scores = @{$scref};
  @steps  = @{$stref};
  @dims   = @{$diref};

  print "$method times  " . &stats(@times)  . "\n";
  print "$method scores " . &stats(@scores) . "\n";
  print "$method steps  " . &stats(@steps)  . "\n";
  print "$method dims   " . &stats(@dims)   . "\n";
  print "\n";
}

sub stats {
  local (@data) = @_;

  my $sum = 0;
  my $sqsum = 0;

  my $min = $data[0];
  my $max = $min;

  for my $d (@data) {
    $sum += $d;
    $sqsum += ($d**2);

    if ($d < $min) { $min = $d; }
    if ($d > $max) { $max = $d; }
  }

  my $mean = $sum/scalar(@data);
  my $secondmoment = $sqsum/scalar(@data);
  my $variance = $secondmoment - ($mean**2);
  my $stddev = sqrt($variance);

  my $fmt = "%.3f";
  return(sprintf("mean $fmt,\tstd dev $fmt\t($fmt - $fmt)",
		 $mean, $stddev, $min, $max));
}
