#! /bin/sh

# $Id: genchart,v 1.2 1998/12/02 00:01:04 oliva Exp $

# Copyright 1998 Alexandre Oliva <oliva@dcc.unicamp.br>, Islene Calciolari Garcia <islene@dcc.unicamp.br>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

sed -e '/^$/d' \
    -e '/problem name/d' \
    -e '/^Problem title:/d' \
    -e '/^Nodes:/d' \
    -e '/^Arcs:/d' \
    -e '/^Number of scans:/d' \
    -e '/^Sum of distances:/d' \
    -e '/^Reading time:/d' \
    -e '/^Removing negative cycles/d' \
    -e '/^Removal time:/d' \
    -e 's/^Test set/Test/' \
    -e 's/two_q/twoq/' \
    -e 's/^Running time of SP computation/Time/' \
    -e 's/^Solving time/Time/' \
    -e 's/^Increased the weight of/Increased:/' \
    -e 's/ arcs$//' \
    -e '/^Test:/ { s/Test: //; s/sprand/random/; s/spacyc/acyclic/;
		   s/ [0-9][0-9][0-9]$//; 
		   s/ [0-9][0-9][0-9] -/ -/;
		   s/-lm.*/neg/; }' \
    -e '/Solver:/ { s%\./%Ours %; s%SPLIB/bin/%SPLIB %; }' |
gawk '
BEGIN { short_solvercount=0; }
! /:/ {
    if (tests[$0] == 0) {
	++testcount;
	test_by_index[testcount]=$0;
	tests[$0]=testcount;
    }
    current_test=tests[$0];
}
/^Solver/ {
    solver=substr($0, index($0, ":")+2);
    solver_set=substr(solver, 1, index(solver, " ")-1);
    if (solver_sets[solver_set] == 0) {
	++solver_setcount;
	solver_set_by_index[solver_setcount]=solver_set;
	solver_sets[solver_set]=solver_setcount;
    }
    short_solver=substr(solver, index(solver, " ")+1);
    if (short_solvers[short_solver] == 0) {
        ++short_solvercount;
	short_solver_by_index[short_solvercount]=short_solver;
	short_solvers[short_solver]=short_solvercount;
    }
    if (solvers[solver] == 0) {
	++solvercount;
	solver_by_index[solvercount]=solver;
	solver_to_short_solver[solvercount]=short_solvers[short_solver];
	solver_to_solver_set[solvercount]=solver_set;
	solvers[solver]=solvercount;
    }
    current_solver=solvers[solver];
}
/^Time/ {
    cur_index=current_test","current_solver;
    total_run_time[cur_index]=total_run_time[cur_index] + $2;
    ++run_count[cur_index];
}
END {
    print "\\long\\def\\genchart#1#2#3{\\begin{test}[tp]";
    print "\\begin{center}\\begin{picture}(430,150)";
    print "\\begin{barenv}\\setprecision{2}\\setstyle{\\small}";
    print "\\ifcase#1 do not use zero";
    for (current_test=0; ++current_test <= testcount; ) {
	print "\\or % " test_by_index[current_test];
	max_avg=0;
	for (current_solver=0; ++current_solver <= solvercount; ) {
	    cur_index=current_test","current_solver;
	    if (run_count[cur_index] > 0) {
		avg=total_run_time[cur_index]/run_count[cur_index];
		if (avg > max_avg)
		    max_avg=avg;
	    }
	}
	print "\\setstretch{" 100/max_avg "}";
	step=0+sprintf("%.1g", max_avg/5);
	print "\\setyaxis{0}{" 0+sprintf("%.1g", step+max_avg) "}{" step "}";
	print "\\setyname{Time (seconds)}";
	solver_set="";
	for (current_solver=0; ++current_solver <= solvercount; ) {
	    if (solver_set != solver_to_solver_set[current_solver]) {
		solver_set=solver_to_solver_set[current_solver];
		if (current_solver > 1) {
		    print "\\setwidth{10}\\sethspace{0.1}";
		    print "\\bar{}{0}[]";
		}
		print "\\setwidth{30}\\sethspace{0.1}";
	    }
	    cur_index=current_test","current_solver;
	    current_short_solver=solver_to_short_solver[current_solver];
	    if (current_short_solver == 1+short_solvercount/2) {
		print "\\setwidth{0}\\sethspace{0}";
		skiplen=solvercount*30+(solvercount-1)*0.1;
		print "\\bar{}{0}[\\vbox{\\vskip2\\baselineskip " solver_set "}]";
		print "\\setwidth{30}\\sethspace{0.1}";
	    }
	    if (run_count[cur_index] == 0)
		print "\\bar{}{0}[\\vbox{crash\\\\" short_solver_by_index[current_short_solver] "\\\\\\vskip\\baselineskip}]";
	    else
		print "\\bar{" 0+sprintf("%.2g",total_run_time[cur_index]/run_count[cur_index]) "}{" current_short_solver "}[" short_solver_by_index[current_short_solver] "]";
	}
    }
    print "\\fi\\end{barenv}\\end{picture}\\end{center}"
    print "\\begin{quote}#3\\end{quote}\\caption{#2}\\end{test}}";
}
'

exit 0
