X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2Fweb%2Froutetrade;h=4885782e58a440b7a773510a049daf363e5e0838;hb=38888dbd9221f97b1886ee545d0113c88432556a;hp=cb0ef9379e1a451ebbe3b5c3aff332794ada7850;hpb=7d910f97374604024af42c3497b82c35ed7715ab;p=ypp-sc-tools.db-live.git
diff --git a/yarrg/web/routetrade b/yarrg/web/routetrade
index cb0ef93..4885782 100644
--- a/yarrg/web/routetrade
+++ b/yarrg/web/routetrade
@@ -39,10 +39,18 @@ $dbh
@archipelagoes
$qa
%args>
+<&| script &>
+ da_pageload= Date.now();
+&script>
+
<%perl>
+my $now= time;
+my $loss_per_league= 1e-7;
+
my @flow_conds;
my @query_params;
+my %dists;
my $sd_condition= sub {
my ($bs, $ix) = @_;
@@ -78,10 +86,12 @@ foreach my $src_i (0..$#islandids) {
if ($specific && !$confusing &&
# With a circular route, do not carry goods round the loop
- !($src_i==0 && $dst_i==$#islandids &&
+ !(($src_i==0 || $src_i==$#islandids) &&
+ $dst_i==$#islandids &&
$src_isle == $islandids[$dst_i])) {
if ($islandpair{$src_isle,$dst_isle}) {
$confusing= 1;
+print "confusing $src_i $src_isle $dst_i $dst_isle\n";
} else {
$islandpair{$src_isle,$dst_isle}=
[ $src_i, $dst_i ];
@@ -98,56 +108,82 @@ my $stmt= "
SELECT sell_islands.islandname org_name,
sell_islands.islandid org_id,
sell.price org_price,
+ sell.qty org_qty_stall,
+ sell_stalls.stallname org_stallname,
+ sell.stallid org_stallid,
+ sell_uploads.timestamp org_timestamp,
buy_islands.islandname dst_name,
buy_islands.islandid dst_id,
buy.price dst_price,
-".($qa->{ShowStalls} ? "
- sell.stallid org_stallid,
- sell_stalls.stallname org_stallname,
- sell.qty org_qty,
- buy.stallid dst_stallid,
+ buy.qty dst_qty_stall,
buy_stalls.stallname dst_stallname,
- buy.qty dst_qty,
+ buy.stallid dst_stallid,
+ buy_uploads.timestamp dst_timestamp,
+".($qa->{ShowStalls} ? "
+ sell.qty org_qty_agg,
+ buy.qty dst_qty_agg,
" : "
- sum(sell.qty) org_qty,
- sum(buy.qty) dst_qty,
+ (SELECT sum(qty) FROM sell AS sell_agg
+ WHERE sell_agg.commodid = commods.commodid
+ AND sell_agg.islandid = sell.islandid
+ AND sell_agg.price = sell.price) org_qty_agg,
+ (SELECT sum(qty) FROM buy AS buy_agg
+ WHERE buy_agg.commodid = commods.commodid
+ AND buy_agg.islandid = buy.islandid
+ AND buy_agg.price = buy.price) dst_qty_agg,
")."
commods.commodname commodname,
commods.commodid commodid,
commods.unitmass unitmass,
commods.unitvolume unitvolume,
+ dist dist,
buy.price - sell.price unitprofit
FROM commods
- JOIN buy on commods.commodid = buy.commodid
- JOIN sell on commods.commodid = sell.commodid
- JOIN islands as sell_islands on sell.islandid = sell_islands.islandid
- JOIN islands as buy_islands on buy.islandid = buy_islands.islandid
-".($qa->{ShowStalls} ? "
- JOIN stalls as sell_stalls on sell.stallid = sell_stalls.stallid
- JOIN stalls as buy_stalls on buy.stallid = buy_stalls.stallid
-" : "")."
+ JOIN sell ON commods.commodid = sell.commodid
+ JOIN buy ON commods.commodid = buy.commodid
+ JOIN islands AS sell_islands ON sell.islandid = sell_islands.islandid
+ JOIN islands AS buy_islands ON buy.islandid = buy_islands.islandid
+ JOIN uploads AS sell_uploads ON sell.islandid = sell_uploads.islandid
+ JOIN uploads AS buy_uploads ON buy.islandid = buy_uploads.islandid
+ JOIN stalls AS sell_stalls ON sell.stallid = sell_stalls.stallid
+ JOIN stalls AS buy_stalls ON buy.stallid = buy_stalls.stallid
+ JOIN dists ON aiid = sell.islandid AND biid = buy.islandid
WHERE (
".join("
OR ", @flow_conds)."
)
AND buy.price > sell.price
-".($qa->{ShowStalls} ? "" : "
- GROUP BY commods.commodid, org_id, org_price, dst_id, dst_price
-")."
ORDER BY org_name, dst_name, commodname, unitprofit DESC,
- org_price, dst_price DESC
+ org_price, dst_price DESC,
+ org_stallname, dst_stallname
";
my $sth= $dbh->prepare($stmt);
$sth->execute(@query_params);
my @flows;
-my @cols;
+my $distquery= $dbh->prepare("
+ SELECT dist FROM dists WHERE aiid = ? AND biid = ?
+ ");
+my $distance= sub {
+ my ($from,$to)= @_;
+ my $d= $dists{$from}{$to};
+ return $d if defined $d;
+ $distquery->execute($from,$to);
+ $d = $distquery->fetchrow_array();
+ defined $d or die "$from $to ?";
+ $dists{$from}{$to}= $d;
+ return $d;
+};
+
+my @cols= ({ NoSort => 1 });
my $addcols= sub {
my $base= shift @_;
foreach my $name (@_) {
- push @cols, { Name => $name, %$base };
+ my $col= { Name => $name, %$base };
+ $col->{Numeric}=1 if !$col->{Text};
+ push @cols, $col;
}
};
@@ -162,9 +198,14 @@ if ($qa->{ShowStalls}) {
));
}
$addcols->({ Text => 1 }, qw(commodname));
-$addcols->({},
- qw( org_qty org_price dst_qty dst_price
- Margin unitprofit MaxQty
+$addcols->({ DoReverse => 1 },
+ qw( org_price org_qty_agg dst_price dst_qty_agg
+ ));
+$addcols->({ DoReverse => 1, SortColKey => 'MarginSortKey' },
+ qw( Margin
+ ));
+$addcols->({ DoReverse => 1 },
+ qw( unitprofit dist MaxQty
MaxCapital MaxProfit
));
@@ -179,23 +220,65 @@ $addcols->({},
<& dumptable:start, qa => $qa, sth => $sth &>
% {
-% my $f;
-% while ($f= $sth->fetchrow_hashref()) {
+% my $got;
+% while ($got= $sth->fetchrow_hashref()) {
<%perl>
- $f->{Ix}= @flows;
- $f->{Var}= "f$f->{Ix}";
+ my $f= $flows[$#flows];
+ if ( !$f ||
+ $qa->{ShowStalls} ||
+ grep { $f->{$_} ne $got->{$_} }
+ qw(org_id org_price dst_id dst_price commodid)
+ ) {
+ # Make a new flow rather than adding to the existing one
+
+ $f= {
+ Ix => scalar(@flows),
+ Var => "f".@flows,
+ %$got
+ };
+ $f->{"org_stallid"}= $f->{"dst_stallid"}= 'all'
+ if !$qa->{ShowStalls};
+ push @flows, $f;
+ }
+ foreach my $od (qw(org dst)) {
+ $f->{"${od}Stalls"}{
+ $got->{"${od}_stallname"}
+ } =
+ $got->{"${od}_qty_stall"}
+ ;
+ }
+
+%perl>
+<& dumptable:row, qa => $qa, sth => $sth, row => $f &>
+% }
+<& dumptable:end, qa => $qa &>
+% }
- $f->{MaxQty}= $f->{'org_qty'} < $f->{'dst_qty'}
- ? $f->{'org_qty'} : $f->{'dst_qty'};
+<%perl>
+
+if (!@flows) {
+ print 'No profitable trading opportunities were found.';
+ return;
+}
+
+foreach my $f (@flows) {
+
+ $f->{MaxQty}= $f->{'org_qty_agg'} < $f->{'dst_qty_agg'}
+ ? $f->{'org_qty_agg'} : $f->{'dst_qty_agg'};
$f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'};
$f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'};
+ $f->{MarginSortKey}= sprintf "%d",
+ $f->{'dst_price'} * 10000 / $f->{'org_price'};
$f->{Margin}= sprintf "%3.1f%%",
$f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0;
- $f->{"org_stallid"}= $f->{"dst_stallid"}= 'all'
- if !$qa->{ShowStalls};
+ $f->{ExpectedUnitProfit}=
+ $f->{'dst_price'} * (1.0 - $loss_per_league) ** $f->{'dist'}
+ - $f->{'org_price'};
+
+ $dists{'org_id'}{'dst_id'}= $f->{'dist'};
my @uid= $f->{commodid};
foreach my $od (qw(org dst)) {
@@ -260,17 +343,13 @@ die "$cmpu $uue ?" if length $cmpu > 20;
die "$f->{UidLong} = $f->{UidShort} = $recons_long ?";
}
- if ($qa->{"R$f->{UidShort}"} && !$qa->{"T$f->{UidShort}"}) {
+ if (defined $qa->{"R$f->{UidShort}"} &&
+ !defined $qa->{"T$f->{UidShort}"}) {
$f->{Suppress}= 1;
}
- push @flows, $f;
-
+}
%perl>
-<& dumptable:row, qa => $qa, sth => $sth, row => $f &>
-% }
-<& dumptable:end, qa => $qa &>
-% }
% my $optimise= $specific && !$confusing && @islandids>1;
% if (!$optimise) {
@@ -286,7 +365,7 @@ Route contains archipelago(es), not just specific islands.
Route is complex - it visits the same island several times
and isn't a simple loop.
% }
-Therefore, optimal trade pattern not calculated.
+Therefore, optimal voyage trade plan not calculated.
% } else { # ========== OPTMISATION ==========
<%perl>
@@ -296,7 +375,9 @@ Maximize
totalprofit:
".(join " +
- ", map { "$_->{unitprofit} $_->{Var}" } @flows)."
+ ", map {
+ sprintf "%.20f %s", $_->{ExpectedUnitProfit}, $_->{Var}
+ } @flows)."
Subject To
";
@@ -320,7 +401,7 @@ foreach my $flow (@flows) {
);
push @{ $avail_csts{$cstname}{Flows} }, $flow->{Var};
- $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty"};
+ $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty_agg"};
}
}
foreach my $cstname (sort keys %avail_csts) {
@@ -354,7 +435,9 @@ if ($qa->{'debug'}) {
qw(glpsol --cpxlp /dev/stdin -o /dev/stdout));
print "
\n" if $qa->{'debug'};
my $found_section= 0;
+ my $glpsol_out= '';
while (<$output>) {
+ $glpsol_out.= $_;
print encode_entities($_) if $qa->{'debug'};
if (m/^\s*No\.\s+Column name\s+St\s+Activity\s/) {
die if $found_section>0;
@@ -375,14 +458,15 @@ if ($qa->{'debug'}) {
$flow->{OptCapital}= $flow->{OptQty} * $flow->{'org_price'};
}
print "
\n" if $qa->{'debug'};
- pipethrough_run_finish($output, 'glpsol');
- die unless $found_section;
+ my $prerr= "\n=====\n$cplex\n=====\n$glpsol_out\n=====\n ";
+ pipethrough_run_finish($output,$prerr);
+ die $prerr unless $found_section;
};
-$addcols->({}, qw(
+$addcols->({ DoReverse => 1 }, qw(
OptQty
));
-$addcols->({ Total => 0 }, qw(
+$addcols->({ Total => 0, DoReverse => 1 }, qw(
OptCapital OptProfit
));
@@ -390,10 +474,11 @@ $addcols->({ Total => 0 }, qw(
% } # ========== OPTIMISATION ==========
+% my %ts_sortkeys;
% {
% my $cdspan= $qa->{ShowStalls} ? ' colspan=2' : '';
% my $cdstall= $qa->{ShowStalls} ? '