#!/usr/bin/perl

if ($#ARGV != 1) { die "Usage: convertAMPL_pcpsp.pl <file.pcpsp> <file.prec> ";}

#open file
$filename = $ARGV[0];

printf "data;\n";

printf STDERR  "Reading file $filename\n";
open (FH, "<", "$filename") || die "Can't open $filename: $!";
while(<FH>)
{
	chomp $_;
	my @col = split(/:/,$_);
	if ($col[0] eq 'NBLOCKS') { 
		printf "param NBLOCKS := $col[1];\n";
		$nb = $col[1];
	}
	if ($col[0] eq 'NPERIODS') {
	 	printf "param NPERIODS := $col[1];\n";
 		$nt = $col[1];
	}
	if ($col[0] eq 'NDESTINATIONS') {
	 	printf "param NDESTINATIONS := $col[1];\n";
 		$nd = $col[1];
	}
	if ($col[0] eq 'NRESOURCE_SIDE_CONSTRAINTS') { 
		printf "param NRESOURCE_SIDE_CONSTRAINTS := $col[1];\n";
 		$nr = $col[1];
	}
	if ($col[0] eq 'DISCOUNT_RATE') {
	 	printf "param DISCOUNT_RATE := $col[1];\n"; 
	}
	if ($col[0] eq 'RESOURCE_CONSTRAINT_LIMITS')
	{
		for($i = 0 ; $i < $nr*$nt ; $i++)
		{
				$_ = <FH>;
				chomp $_;
				my @col = split(/ /,$_);
				if($col[2] eq 'L')
				{
					$ub[$col[0]][$col[1]] = $col[3];
					$lb[$col[0]][$col[1]] = -infinity;
				}
				elsif ($col[2] eq 'G')
				{
					$ub[$col[0]][$col[1]] = infinity;
					$lb[$col[0]][$col[1]] = $col[3];
				}
				elsif ($col[2] eq 'I')
				{
					$ub[$col[0]][$col[1]] = $col[4];
					$lb[$col[0]][$col[1]] = $col[3];
				}
			}

			printf "param RESOURCE_CONSTRAINT_UB_LIMITS :";
			for ($t = 0 ; $t < $nt ; $t++) {printf " $t";}
			printf ":= ";
			for ($r = 0 ; $r < $nr ; $r++)
			{
				printf "\n$r";
				for ($t = 0 ; $t < $nt ; $t++) {printf " $ub[$r][$t]";}
			}
			printf ";\n";

			printf "param RESOURCE_CONSTRAINT_LB_LIMITS :";
			for ($t = 0 ; $t < $nt ; $t++) {printf " $t";}
			printf ":= ";
			for ($r = 0 ; $r < $nr ; $r++)
			{
				printf "\n$r";
				for ($t = 0 ; $t < $nt ; $t++) {printf " $lb[$r][$t]";}
			}
			printf ";\n";
		}

		if ($col[0] eq 'OBJECTIVE_FUNCTION')
		{
			print "param OBJECTIVE_FUNCTION :";
			for($d = 0 ; $d < $nd ; $d++)
			{
				printf(" $d");
			}
			printf(" :=");
			for($i = 0 ; $i < $nb ; $i++)
			{
				$_ = <FH>;
				chomp $_;
				my @col = split(/ /,$_);
				print "\n$col[0]";
				for($d = 0 ; $d < $nd ; $d++)
				{
					printf " $col[$d+1]";
				}
			}
			printf ";\n"
		}

		if ($col[0] eq 'RESOURCE_CONSTRAINT_COEFFICIENTS')
		{
			print "param: RESOURCE_CONSTRAINT_COEFFICIENTS :=";
			while(<FH>)
			{
				chomp $_;
				my @col = split(/ /,$_);
				if ($col[0] eq "EOF") { break; }
				else {
					printf "\n$col[0] $col[2] $col[1] $col[3]";
				}
			}
			printf ";\n";
		}
}

close(FH);

$filename = $ARGV[1];

printf STDERR  "Reading file $filename\n";
open (FH, "<", "$filename") || die "Can't open $filename: $!";

#printf("param: NPREC :=");
#for ($i = 0 ; $i < $nb ; $i++)
#{
#	$_ = <FH>;
#	chomp $_;
#	my @col = split(/ /,$_);
#	if ($col[0] != $i) { die "ERROR: row $i does not coincide (block $col[0]\n";}
#	$nprec[$i] = $col[1];
#	printf "\n$col[0] $col[1]"
#}
#printf ";\n";
#
### Rewind and re-read precedences
#seek FH, 0, SEEK_SET or die "Cannot seek on file $filename: $!";


for ($i = 0 ; $i < $nb ; $i++)
{
	$_ = <FH>;
	chomp $_;
	my @col = split(/ /,$_);
	if ($col[0] != $i) { die "ERROR: row $i does not coincide (block $col[0]\n";}
	printf("set PREC[$col[0]] :=");
	for ($j = 0 ; $j < $col[1] ; $j++)
	{
		printf " $col[2+$j]";
	}
	printf(";\n");
}



	


