3 This is part of the YARRG website. YARRG is a tool and website
4 for assisting players of Yohoho Puzzle Pirates.
6 Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
7 Copyright (C) 2009 Clare Boothby
9 YARRG's client code etc. is covered by the ordinary GNU GPL (v3 or later).
10 The YARRG website is covered by the GNU Affero GPL v3 or later, which
11 basically means that every installation of the website will let you
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Affero General Public License as
16 published by the Free Software Foundation, either version 3 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Affero General Public License for more details.
24 You should have received a copy of the GNU Affero General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
28 are used without permission. This program is not endorsed or
29 sponsored by Three Rings.
32 This Mason component is the core trade planner for a specific route.
35 ========== TODO ==========
36 16:36 <ceb> alpha,byrne,papaya,turtle,jorvik,luthien is my example
38 use POST for update. Hrrm.
42 adjustable potential cost of losses (rather than fixed 1e-BIG per league)
46 ========== TODO ==========
56 da_pageload= Date.now();
62 my $loss_per_league= 1e-7;
67 my $sd_condition= sub {
69 my $islandid= $islandids[$ix];
70 if (defined $islandid) {
71 return "${bs}.islandid = $islandid";
73 push @query_params, $archipelagoes[$ix];
74 return "${bs}_islands.archipelago = ?";
79 # $islandpair{$a,$b}= [ $start_island_ix, $end_island_ix ]
81 my $specific= !grep { !defined $_ } @islandids;
84 foreach my $src_i (0..$#islandids) {
85 my $src_isle= $islandids[$src_i];
86 my $src_cond= $sd_condition->('sell',$src_i);
88 foreach my $dst_i ($src_i..$#islandids) {
89 my $dst_isle= $islandids[$dst_i];
90 my $dst_cond= $sd_condition->('buy',$dst_i);
91 if ($dst_i==$src_i and !defined $src_isle) {
92 # we always want arbitrage, but mentioning an arch
93 # once shouldn't produce intra-arch trades
95 "($dst_cond AND sell.islandid = buy.islandid)";
97 push @dst_conds, $dst_cond;
99 if ($specific && !$confusing &&
100 # With a circular route, do not carry goods round the loop
101 !($src_i==0 && $dst_i==$#islandids &&
102 $src_isle == $islandids[$dst_i])) {
103 if ($islandpair{$src_isle,$dst_isle}) {
106 $islandpair{$src_isle,$dst_isle}=
111 push @flow_conds, "$src_cond AND (
118 SELECT sell_islands.islandname org_name,
119 sell_islands.islandid org_id,
120 sell.price org_price,
121 sell.qty org_qty_stall,
122 sell_stalls.stallname org_stallname,
123 sell.stallid org_stallid,
124 sell_uploads.timestamp org_timestamp,
125 buy_islands.islandname dst_name,
126 buy_islands.islandid dst_id,
128 buy.qty dst_qty_stall,
129 buy_stalls.stallname dst_stallname,
130 buy.stallid dst_stallid,
131 buy_uploads.timestamp dst_timestamp,
132 ".($qa->{ShowStalls} ? "
133 sell.qty org_qty_agg,
136 (SELECT sum(qty) FROM sell AS sell_agg
137 WHERE sell_agg.commodid = commods.commodid
138 AND sell_agg.islandid = sell.islandid
139 AND sell_agg.price = sell.price) org_qty_agg,
140 (SELECT sum(qty) FROM buy AS buy_agg
141 WHERE buy_agg.commodid = commods.commodid
142 AND buy_agg.islandid = buy.islandid
143 AND buy_agg.price = buy.price) dst_qty_agg,
145 commods.commodname commodname,
146 commods.commodid commodid,
147 commods.unitmass unitmass,
148 commods.unitvolume unitvolume,
150 buy.price - sell.price unitprofit
152 JOIN sell ON commods.commodid = sell.commodid
153 JOIN buy ON commods.commodid = buy.commodid
154 JOIN islands AS sell_islands ON sell.islandid = sell_islands.islandid
155 JOIN islands AS buy_islands ON buy.islandid = buy_islands.islandid
156 JOIN uploads AS sell_uploads ON sell.islandid = sell_uploads.islandid
157 JOIN uploads AS buy_uploads ON buy.islandid = buy_uploads.islandid
158 JOIN stalls AS sell_stalls ON sell.stallid = sell_stalls.stallid
159 JOIN stalls AS buy_stalls ON buy.stallid = buy_stalls.stallid
160 JOIN dists ON aiid = sell.islandid AND biid = buy.islandid
165 AND buy.price > sell.price
166 ORDER BY org_name, dst_name, commodname, unitprofit DESC,
167 org_price, dst_price DESC,
168 org_stallname, dst_stallname
171 my $sth= $dbh->prepare($stmt);
172 $sth->execute(@query_params);
175 my @cols= ({ NoSort => 1 });
179 foreach my $name (@_) {
180 my $col= { Name => $name, %$base };
181 $col->{Numeric}=1 if !$col->{Text};
186 if ($qa->{ShowStalls}) {
187 $addcols->({ Text => 1 }, qw(
188 org_name org_stallname
189 dst_name dst_stallname
192 $addcols->({Text => 1 }, qw(
196 $addcols->({ Text => 1 }, qw(commodname));
197 $addcols->({ DoReverse => 1 },
198 qw( org_price org_qty_agg dst_price dst_qty_agg
200 $addcols->({ DoReverse => 1, SortColKey => 'MarginSortKey' },
203 $addcols->({ DoReverse => 1 },
204 qw( unitprofit MaxQty
210 % if ($qa->{'debug'}) {
213 <% join(' | ',@query_params) |h %>
217 <& dumptable:start, qa => $qa, sth => $sth &>
220 % while ($got= $sth->fetchrow_hashref()) {
223 my $f= $flows[$#flows];
226 grep { $f->{$_} ne $got->{$_} }
227 qw(org_id org_price dst_id dst_price commodid)
229 # Make a new flow rather than adding to the existing one
232 Ix => scalar(@flows),
236 $f->{"org_stallid"}= $f->{"dst_stallid"}= 'all'
237 if !$qa->{ShowStalls};
240 foreach my $od (qw(org dst)) {
242 $got->{"${od}_stallname"}
244 $got->{"${od}_qty_stall"}
249 <& dumptable:row, qa => $qa, sth => $sth, row => $f &>
251 <& dumptable:end, qa => $qa &>
255 foreach my $f (@flows) {
257 $f->{MaxQty}= $f->{'org_qty_agg'} < $f->{'dst_qty_agg'}
258 ? $f->{'org_qty_agg'} : $f->{'dst_qty_agg'};
259 $f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'};
260 $f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'};
262 $f->{MarginSortKey}= sprintf "%d",
263 $f->{'dst_price'} * 10000 / $f->{'org_price'};
264 $f->{Margin}= sprintf "%3.1f%%",
265 $f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0;
267 $f->{ExpectedUnitProfit}=
268 $f->{'dst_price'} * (1.0 - $loss_per_league) ** $f->{'dist'}
271 my @uid= $f->{commodid};
272 foreach my $od (qw(org dst)) {
277 $f->{"${od}_stallid"}
278 if $qa->{ShowStalls};
280 $f->{UidLong}= join '_', @uid;
288 my $this= $uue % $base;
289 print STDERR "uue=$uue this=$this ";
294 $cmpu .= chr($this + ($this < 26 ? ord('a') :
295 $this < 52 ? ord('A')-26
297 print STDERR " uue=$uue this=$this cmpu=$cmpu\n";
298 die "$cmpu $uue ?" if length $cmpu > 20;
302 $f->{UidShort}= $cmpu;
304 if ($qa->{'debug'}) {
309 my $v= m/^[a-z]/ ? ord($&)-ord('a') :
310 m/^[A-Z]/ ? ord($&)-ord('A')+26 :
311 m/^[0-9]/ ? ord($&)-ord('0')+52 :
317 #print STDERR "(next)\n";
319 die "$f->{UidShort} $_ ?" unless defined $mul;
320 $outuid[$#outuid] += $v * $mul;
322 #print STDERR "$f->{UidShort} $_ $& v=$v mul=$mul ord()=".ord($&).
323 # "[vs.".ord('a').",".ord('A').",".ord('0')."]".
324 # " outuid=@outuid\n";
329 my $recons_long= join '_', @outuid;
330 $f->{UidLong} eq $recons_long or
331 die "$f->{UidLong} = $f->{UidShort} = $recons_long ?";
334 if (defined $qa->{"R$f->{UidShort}"} &&
335 !defined $qa->{"T$f->{UidShort}"}) {
342 % my $optimise= $specific && !$confusing && @islandids>1;
346 % if (@islandids<=1) {
350 Route contains archipelago(es), not just specific islands.
353 Route is complex - it visits the same island several times
354 and isn't a simple loop.
356 Therefore, optimal trade pattern not calculated.
358 % } else { # ========== OPTMISATION ==========
367 sprintf "%.20f %s", $_->{ExpectedUnitProfit}, $_->{Var}
374 foreach my $flow (@flows) {
375 if ($flow->{Suppress}) {
381 foreach my $od (qw(org dst)) {
382 my $cstname= join '_', (
387 $flow->{"${od}_price"},
388 $flow->{"${od}_stallid"},
391 push @{ $avail_csts{$cstname}{Flows} }, $flow->{Var};
392 $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty_agg"};
395 foreach my $cstname (sort keys %avail_csts) {
396 my $c= $avail_csts{$cstname};
398 ". sprintf("%-30s","$cstname:")." ".
399 join("+", @{ $c->{Flows} }).
400 " <= ".$c->{Qty}."\n";
406 ", map { "$_->{Var} >= 0" } @flows)."
411 if ($qa->{'debug'}) {
420 my $input= pipethrough_prep();
421 print $input $cplex or die $!;
422 my $output= pipethrough_run_along($input, undef, 'glpsol',
423 qw(glpsol --cpxlp /dev/stdin -o /dev/stdout));
424 print "<pre>\n" if $qa->{'debug'};
425 my $found_section= 0;
427 print encode_entities($_) if $qa->{'debug'};
428 if (m/^\s*No\.\s+Column name\s+St\s+Activity\s/) {
429 die if $found_section>0;
433 next unless $found_section==1;
440 m/^\s*\d+\s+f(\d+)\s+\S+\s+(\d+)\s/ or die "$_ ?";
441 my $flow= $flows[$ix] or die;
442 $flow->{OptQty}= $qty;
443 $flow->{OptProfit}= $flow->{'unitprofit'} * $qty;
444 $flow->{OptCapital}= $flow->{OptQty} * $flow->{'org_price'};
446 print "</pre>\n" if $qa->{'debug'};
447 pipethrough_run_finish($output, 'glpsol');
448 die unless $found_section;
451 $addcols->({ DoReverse => 1 }, qw(
454 $addcols->({ Total => 0, DoReverse => 1 }, qw(
460 % } # ========== OPTIMISATION ==========
464 % my $cdspan= $qa->{ShowStalls} ? ' colspan=2' : '';
465 % my $cdstall= $qa->{ShowStalls} ? '<th>Stall</th>' : '';
466 <table id="trades" rules=groups>
469 <% $qa->{ShowStalls} ? '<colgroup span=2>' : '' %>
480 <th<% $cdspan %>>Collect
481 <th<% $cdspan %>>Deliver
483 <th colspan=2>Collect
484 <th colspan=2>Deliver
488 <th colspan=3>Planned
493 <th>Island <% $cdstall %>
494 <th>Island <% $cdstall %>
512 <tr id="trades_sort">
513 % foreach my $col (@cols) {
517 % foreach my $flowix (0..$#flows) {
518 % my $flow= $flows[$flowix];
519 % my $rowid= "id_row_$flow->{UidShort}";
520 <tr id="<% $rowid %>" class="datarow<% $flowix & 1 %>">
521 <td><input type=hidden name=R<% $flow->{UidShort} %> value="">
522 <input type=checkbox name=T<% $flow->{UidShort} %> value=""
523 <% $flow->{Suppress} ? '' : 'checked' %> >
524 % foreach my $ci (1..$#cols) {
525 % my $col= $cols[$ci];
526 % my $v= $flow->{$col->{Name}};
527 % $col->{Total} += $v if defined $col->{Total};
528 % $v='' if !$col->{Text} && !$v;
529 % my $sortkey= $col->{SortColKey} ?
530 % $flow->{$col->{SortColKey}} : $v;
531 % $ts_sortkeys{$ci}{$rowid}= $sortkey;
532 <td <% $col->{Text} ? '' : 'align=right' %>><% $v |h %>
535 <tr id="trades_total">
538 % foreach my $ci (3..$#cols) {
539 % my $col= $cols[$ci];
541 % if (defined $col->{Total}) {
542 <% $col->{Total} |h %>
547 <& tabsort, cols => \@cols, table => 'trades', rowclass => 'datarow',
548 throw => 'trades_sort', tbrow => 'trades_total' &>
550 ts_sortkeys= <% to_json_protecttags(\%ts_sortkeys) %>;
551 function all_onload() {
554 window.onload= all_onload;
557 <input type=submit name=update value="Update">
559 % if ($optimise) { # ========== TRADING PLAN ==========
561 % my $iquery= $dbh->prepare('SELECT islandname FROM islands
562 % WHERE islandid = ?');
565 <h1>Voyage trading plan</h1>
567 % foreach my $i (0..$#islandids) {
569 <tr><td colspan=3><strong>
570 % $iquery->execute($islandids[$i]);
571 % my ($islandname) = $iquery->fetchrow_array();
573 Start at <% $islandname |h %>
575 Sail to <% $islandname |h %>
581 foreach my $od (qw(org dst)) {
582 foreach my $f (@flows) {
583 next if $f->{Suppress};
584 next unless $f->{"${od}_id"} == $islandids[$i];
585 next unless $f->{OptQty};
586 my $price= $f->{"${od}_price"};
587 my $stallname= $f->{"${od}_stallname"};
588 my $todo= \$flowlists{$od}{
590 (sprintf "%07d", ($od eq 'dst' ?
591 9999999-$price : $price)),
599 $$todo->{'commodname'}= $f->{'commodname'};
600 $$todo->{'stallname'}= $stallname;
601 $$todo->{Price}= $price;
602 $$todo->{Timestamp}= $f->{"${od}_timestamp"};
603 $$todo->{Qty} += $f->{OptQty};
604 $$todo->{Total}= $$todo->{Price} * $$todo->{Qty};
605 $$todo->{Stalls}= $f->{"${od}Stalls"};
606 if ($f->{'org_id'} == $f->{'dst_id'}) {
607 $$todo->{"${od}Arbitrage"}= 1;
614 my $show_flows= sub {
615 my ($od,$arbitrage,$collectdeliver) = @_;
618 % my $todo= $flowlists{$od};
619 % return unless $todo;
620 % foreach my $tkey (sort keys %$todo) {
621 % my $t= $todo->{$tkey};
622 % next if $t->{"${od}Arbitrage"} != $arbitrage;
623 % if (!$age_reported++) {
624 % my $age= $now - $t->{Timestamp};
625 % my $cellid= "da_${i}";
626 % $da_ages{$cellid}= $age;
628 (Data age: <span id="<% $cellid %>"><% prettyprint_age($age) %></span>)
629 % } elsif (!defined $total) {
633 % $total += $t->{Total};
634 % my $span= 0 + keys %{ $t->{Stalls} };
635 % my $td= "td rowspan=$span";
636 <tr class="datarow<% $dline %>">
637 <<% $td %>><% $collectdeliver %>
638 <<% $td %>><% $t->{'commodname'} |h %>
640 % my @stalls= sort keys %{ $t->{Stalls} };
642 % my $name= $stalls[$_[0]];
647 <<% $td %> align=right><% $t->{Price} |h %> poe ea.
648 <<% $td %> align=right><% $t->{Qty} |h %> unit(s)
649 <<% $td %> align=right><% $t->{Total} |h %> total
651 % foreach my $stallix (1..$#stalls) {
652 <tr class="datarow<% $dline %>">
653 % $pstall->($stallix);
659 % my $show_total= sub {
660 % my ($totaldesc)= @_;
661 % if (defined $total) {
664 <td colspan=2 align=right><% $totaldesc %>
665 <td align=right><% $total |h %> total
672 $show_flows->('dst',0,'Deliver'); $show_total->('Proceeds');
673 $show_flows->('org',1,'Collect'); $show_total->('(Arbitrage) outlay');
674 $show_flows->('dst',1,'Deliver'); $show_total->('(Arbitrage) proceeds');
675 $show_flows->('org',0,'Collect'); $show_total->('Outlay');
680 <& query_age:dataages, id2age => \%da_ages &>
682 % } # ========== TRADING PLAN ==========