#!/usr/bin/perl

use 5.034.10;
use utf8;

# This is not an revisting of Leontief input-output tables

# It's a matter of practical data management that this could help only "local" problems, I think
# this could be interpreted as the full data are only available by sharing different levels of 
# data adquisition, so the full problem could be of unknown and big size. However, it's not a 
# enormmeous group of data. At every there are multi or national systems, which could simplify
# the problem

# my $TH = 5;   # five years of time horizon to plan 
# my $interest = 0.028;   # middle interest rates over the time period  
# my @eff;  # is a decisive variable to improve better traansportation

# my $sumcosts;

my @Se; # sources of energy
#
my $nSe = scalar @Se;

my @T1e;
#
my $nT1e = scalar @T1e;

my @Fe; # fabrication of energy by types 
#
my $nFe = scalar @Fe;

my @T2e; # capacity of transport from @Fe to @Ce
#
my $nT2e = scalar @T2e;

my @De; # demand of energy
#
my $nDe = scalar @De;

my @Ce; # final consumption (could be forked by sectors or activities)
#
my $nCe = scalar @Ce;


my @prio; # lista de prioridades, por ejemplo por nivel crítico de agotamiento 


# my @Pe; # Price factible > source to fabrication and consumption cost 



# Storage is another type of source, as i see
# my @CFe; # marginal cost plus margin (not much)
my @factibilidad1 ;  # matrix 2D boolean of possible and impossible transport @Se -> @T1e =>  array[0..$nSe-1][0..$nT1e-1] 
my @factibilidad2 ;  # idem. 2D                                               @T1e -> @Fe =>  array[0..$nT1e-1][0..$nFe-1]
my @factibilidad3 ;  # idem. 2D                                               @Fe -> @T2e =>  array[0..$nFe-1][0..$nTe-1] 
my @factibilidad4 ;  # idem. 2D                                               @T2e -> @Ce =>  array[0..$nTe-1][0..$nCe-1]                                                
# my @transportation_cost ; # maintaining and fullcosts (:-/) associated

# lista de variables de tamaños de listas

# lista de variables de 
# my $sumf = 0;
# my $sumd = 0;

my @Cecurrent;
@Cecurrent = sort { $prio[$a] <=> $prio[$b] } @Ce;   # this is a point in which a expanded sub could do better the job

my $MAXITER = 1_000_000_000;

my $shortcut = 0;
my $iter = 0;
my $min_shortcut = 9e99;

while (++$iter < $MAXITER){
    $shortcut = 0;
    for my $s (@Se){
	for my $t1 (@T1e){
	    next if ($factibilidad1[$s][$t1]);
	    for my $f (@Fe){
		next if ($factibilidad2[$s][$f] == 0);
#	$sumf += $f;
	
		for my $t2 (@T2e){                                # otra matriz de más de 1 dimesión (?)
		    next if ($factibilidad2[$f][$t2] == 0);    
	    
		    for my $d (@De){                                # esta no es lista de una dimensión, son más (?)
#		$d = $f if ($d > $f);
			next if ($factibilidad3[$t2][$d] == 0);
#	        $sumd += $d;
			my $sum_done = $d;
	       
			for my $cec (@Cecurrent){
			    $sum_done -= $cec;
			    if ($sum_done <= 0){
				$sum_done += $cec;      # deshacer
				$shortcut += $cec;
			    }    	
			# se recorre la lista y todo lo que se puede colocar, se coloca en consumo
			}
		    }
		}
	    }
	}
    }
    if ($shortcut <= $min_shortcut){
	$min_shortcut = $shortcut;
	say "$shortcut";
#	dump_report();
    }
}

exit 2;



# my $sumt = 0;
    
#    $t = $d if ($d < $t);
#    $sumt += $treal;
# }

# $sumt = $sumd if ($sumd < $sumt);






	    


# esto creo que se ve mejor el estiramiento en el link simbólico

__END__

sub eff {
    my ($t, $Ce[], $Fe[]);
    
    return ($eff[$t], $Fe[]));
}


