-foreach my $cstname (sort keys %avail_csts) {
- my $c= $avail_csts{$cstname};
- $cplex .= "
- ". sprintf("%-30s","$cstname:")." ".
- join("+", @{ $c->{Flows} }).
- " <= ".$c->{Qty}."\n";
+foreach my $limname (sort keys %avail_lims) {
+ my $c= $avail_lims{$limname};
+ $cplex .=
+ sprintf(" %-30s","$limname:")." ".
+ join("+", @{ $c->{SubflowVars} }).
+ " <= ".$c->{Qty}."\n";
+}
+
+foreach my $ci (0..($#islandids-1)) {
+ my @rel_subflows;
+
+ foreach my $f (@flows) {
+ next if $f->{Suppress};
+ my @relsubflow= grep {
+ $_->{Org} <= $ci &&
+ $_->{Dst} > $ci;
+ } @{ $f->{Subflows} };
+ next unless @relsubflow;
+ die unless @relsubflow == 1;
+ push @rel_subflows, @relsubflow;
+#print " RELEVANT $ci $relsubflow[0]->{Var} ";
+ }
+#print " RELEVANT $ci COUNT ".scalar(@rel_subflows)." ";
+ if (!@rel_subflows) {
+ foreach my $mv (qw(mass volume)) {
+ $sail_total[$ci]{$mv}= 0;
+ }
+ next;
+ }
+
+ my $applylimit= sub {
+ my ($mv, $max, $f2val) = @_;
+ $max= 1e9 unless defined $max;
+#print " DEFINED MAX $mv $max ";
+ $cplex .= "
+ ". sprintf("%-10s","${mv}_$ci:")." ".
+ join(" + ", map {
+#print " PART MAX $_->{Var} $_->{Flow}{Ix} ";
+ $f2val->($_->{Flow}) .' '. $_->{Var};
+ } @rel_subflows).
+ " <= $max";
+ };
+
+ $applylimit->('mass', $max_mass, sub { $_[0]{'unitmass'} *1e-3 });
+ $applylimit->('volume', $max_volume, sub { $_[0]{'unitvolume'}*1e-3 });
+ $applylimit->('capital',$max_capital,sub { $_[0]{'org_price'} });
+ $cplex.= "\n";