From e2463128a74fd2007dfbb9807cd3b2ff4b7f97d9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 23 Aug 2009 11:22:29 +0100 Subject: [PATCH] Runs glpsol, calculates optimal solution --- yarrg/web/routetrade | 81 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/yarrg/web/routetrade b/yarrg/web/routetrade index 1924503..a52531c 100644 --- a/yarrg/web/routetrade +++ b/yarrg/web/routetrade @@ -55,18 +55,38 @@ my $sd_condition= sub { } }; +my %islandpair; +# $islandpair{$a,$b}= [ $start_island_ix, $end_island_ix ] + +my $specific= !grep { !defined $_ } @islandids; +my $confusing= 0; + foreach my $src_i (0..$#islandids) { + my $src_isle= $islandids[$src_i]; my $src_cond= $sd_condition->('sell',$src_i); my @dst_conds; foreach my $dst_i ($src_i..$#islandids) { + my $dst_isle= $islandids[$dst_i]; my $dst_cond= $sd_condition->('buy',$dst_i); - if ($dst_i==$src_i and !defined $islandids[$src_i]) { + if ($dst_i==$src_i and !defined $src_isle) { # we always want arbitrage, but mentioning an arch # once shouldn't produce intra-arch trades $dst_cond= "($dst_cond AND sell.islandid = buy.islandid)"; } push @dst_conds, $dst_cond; + + if ($specific && !$confusing && + # With a circular route, do not carry goods round the loop + !($src_i==0 && $dst_i==$#islandids && + $src_isle == $islandids[$dst_i])) { + if ($islandpair{$src_isle,$dst_isle}) { + $confusing= 1; + } else { + $islandpair{$src_isle,$dst_isle}= + [ $src_i, $dst_i ]; + } + } } push @flow_conds, "$src_cond AND ( ".join(" @@ -85,8 +105,8 @@ my $stmt= " sum(buy.qty) dst_qty, commods.commodname commodname, commods.commodid commodid, - commods.unitmass mass, - commods.unitvolume volume, + commods.unitmass unitmass, + commods.unitvolume unitvolume, buy.price - sell.price unitprofit, min(sell.qty,buy.qty) tqty, min(sell.qty,buy.qty) * (buy.price-sell.price) profit @@ -130,6 +150,22 @@ my @flows; <& dumpqueryresults:end &> % } +% if (!$specific || $confusing || @islandids<=1) { + +

+% if (@islandids<=1) { +Route is trivial. +% } +% if (!$specific) { +Route contains archipelago(es), not just specific islands. +% } +% if ($confusing) { +Route is complex - it visits the same island several times +and isn't a simple loop. +% } +Therefore, optimal trade pattern not calculated. + +% } else { # ========== OPTMISATION ========== <%perl> my $cplex= " @@ -170,13 +206,50 @@ Bounds End "; -# glpsol --cpxlp /dev/stdin {'debug'}) {

 <% $cplex |h %>
 
+<%perl> +} + +{ + my $input= pipethrough_prep(); + print $input $cplex or die $!; + my $output= pipethrough_run_along($input, undef, 'glpsol', + qw(glpsol --cpxlp /dev/stdin -o /dev/stdout)); + print "
\n" if $qa->{'debug'};
+	my $found_section= 0;
+	while (<$output>) {
+		print encode_entities($_) if $qa->{'debug'};
+		if (m/^\s*No\.\s+Column name\s+St\s+Activity\s/) {
+			die if $found_section>0;
+			$found_section= 1;
+			next;
+		}
+		next unless $found_section==1;
+		next if m/^[- ]+$/;
+		if (!/\S/) {
+			$found_section= 2;
+			next;
+		}
+		m/^\s*\d+\s+f(\d+)\s+\S+\s+(\d+)\s/ or die "$_ ?";
+		die if $1 >= @flows;
+		$flows[$1]{Optimal}= $2;
+	}
+	print "
\n" if $qa->{'debug'}; + pipethrough_run_finish($output, 'glpsol'); + die unless $found_section; +}; + +print join ' ', map { $_->{Optimal} } @flows; + + +% } # ========== OPTIMISATION ========== <%init> use CommodsWeb; +use Commods; -- 2.30.2