#!/usr/local/bin/perl

# Takes as input a solution file (steady-state probs) and a reward
# file (values given to states), and forms the dot product.


if ($#ARGV != 1) {
  printf("Usage: %s soln_file reward file\n", $0);
  die "\n";
}

open(SOLNFILE, $ARGV[0]);
open(REWFILE, $ARGV[1]);

$r = 0;

while ($l = <SOLNFILE>) {
  if ($l =~ /P(\[)+([0-9]+)(\])+[^0-9]+([0-9.]+)/) {
    $p[$2] = $4;
  }
}

while ($l = <REWFILE>) {
  if ($l =~ /([a-z0-9]+) := 0/) {
    $name = $1;
  }
  if ($l =~ /\+P(\[)+([0-9]+)(\])+\*([0-9.]+)/) {
    $r += $p[$2] * $4;
  }
}

printf("Total reward for %s: %f\n", $name, $r);

close REWFILE;
close SOLNFILE;
