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 16:37 <ceb> if tehre are 2 rows which take the same object and sell it for the
39 same profit at two other islands, choose the shortest route as the
41 16:37 <ceb> coconut buy 10 sell 16, at luthien or jorvik, in that example
42 16:38 <ceb> Do you see what I mean?
44 16:38 <ceb> I don't know how hard this is, but can you show only the suggested
45 trades to start ith and have a button to show all?
46 16:39 <ceb> Also, maybe colour to highlight the suggested trades?
48 16:40 <ceb> columns should be sortable with the small arrows as before
50 16:51 <ceb> YPP interfaces have price then qty. You have qty then price. This
52 16:51 <ceb> the max column ordering is OK
53 16:51 <ceb> (alo profit and suggested are OK)
54 16:52 <Diziet> Err, you want qty on the left in those two but on the right in
56 16:53 <ceb> Yes (price and capital are not the same thing)
58 16:53 <ceb> for max the order in which you want to think about it is 'I need to
59 buy ten of them and that will cost me $total price)
62 16:46 <ceb> Also trading plan not functional but I guess you know that :-)
65 - potential cost of losses
67 - use POST for update. Hrrm.
69 ========== TODO ==========
83 my $sd_condition= sub {
85 my $islandid= $islandids[$ix];
86 if (defined $islandid) {
87 return "${bs}.islandid = $islandid";
89 push @query_params, $archipelagoes[$ix];
90 return "${bs}_islands.archipelago = ?";
95 # $islandpair{$a,$b}= [ $start_island_ix, $end_island_ix ]
97 my $specific= !grep { !defined $_ } @islandids;
100 foreach my $src_i (0..$#islandids) {
101 my $src_isle= $islandids[$src_i];
102 my $src_cond= $sd_condition->('sell',$src_i);
104 foreach my $dst_i ($src_i..$#islandids) {
105 my $dst_isle= $islandids[$dst_i];
106 my $dst_cond= $sd_condition->('buy',$dst_i);
107 if ($dst_i==$src_i and !defined $src_isle) {
108 # we always want arbitrage, but mentioning an arch
109 # once shouldn't produce intra-arch trades
111 "($dst_cond AND sell.islandid = buy.islandid)";
113 push @dst_conds, $dst_cond;
115 if ($specific && !$confusing &&
116 # With a circular route, do not carry goods round the loop
117 !($src_i==0 && $dst_i==$#islandids &&
118 $src_isle == $islandids[$dst_i])) {
119 if ($islandpair{$src_isle,$dst_isle}) {
122 $islandpair{$src_isle,$dst_isle}=
127 push @flow_conds, "$src_cond AND (
134 SELECT sell_islands.islandname org_name,
135 sell_islands.islandid org_id,
136 sell.price org_price,
137 buy_islands.islandname dst_name,
138 buy_islands.islandid dst_id,
140 ".($qa->{ShowStalls} ? "
141 sell.stallid org_stallid,
142 sell_stalls.stallname org_stallname,
144 buy.stallid dst_stallid,
145 buy_stalls.stallname dst_stallname,
148 sum(sell.qty) org_qty,
149 sum(buy.qty) dst_qty,
151 commods.commodname commodname,
152 commods.commodid commodid,
153 commods.unitmass unitmass,
154 commods.unitvolume unitvolume,
155 buy.price - sell.price unitprofit
157 JOIN buy on commods.commodid = buy.commodid
158 JOIN sell on commods.commodid = sell.commodid
159 JOIN islands as sell_islands on sell.islandid = sell_islands.islandid
160 JOIN islands as buy_islands on buy.islandid = buy_islands.islandid
161 ".($qa->{ShowStalls} ? "
162 JOIN stalls as sell_stalls on sell.stallid = sell_stalls.stallid
163 JOIN stalls as buy_stalls on buy.stallid = buy_stalls.stallid
169 AND buy.price > sell.price
170 ".($qa->{ShowStalls} ? "" : "
171 GROUP BY commods.commodid, org_id, org_price, dst_id, dst_price
173 ORDER BY org_name, dst_name, commodname, unitprofit DESC,
174 org_price, dst_price DESC
177 my $sth= $dbh->prepare($stmt);
178 $sth->execute(@query_params);
185 foreach my $name (@_) {
186 push @cols, { Name => $name, %$base };
190 if ($qa->{ShowStalls}) {
191 $addcols->({ Text => 1 }, qw(
192 org_name org_stallname
193 dst_name dst_stallname
196 $addcols->({Text => 1 }, qw(
200 $addcols->({ Text => 1 }, qw(commodname));
202 qw( org_qty org_price dst_qty dst_price
203 Margin unitprofit MaxQty
209 % if ($qa->{'debug'}) {
212 <% join(' | ',@query_params) |h %>
216 <& dumptable:start, qa => $qa, sth => $sth &>
219 % while ($f= $sth->fetchrow_hashref()) {
223 $f->{Var}= "f$f->{Ix}";
225 $f->{MaxQty}= $f->{'org_qty'} < $f->{'dst_qty'}
226 ? $f->{'org_qty'} : $f->{'dst_qty'};
227 $f->{MaxProfit}= $f->{MaxQty} * $f->{'unitprofit'};
228 $f->{MaxCapital}= $f->{MaxQty} * $f->{'org_price'};
230 $f->{Margin}= sprintf "%3.1f%%",
231 $f->{'dst_price'} * 100.0 / $f->{'org_price'} - 100.0;
233 $f->{"org_stallid"}= $f->{"dst_stallid"}= 'all'
234 if !$qa->{ShowStalls};
236 my @uid= $f->{commodid};
237 foreach my $od (qw(org dst)) {
242 $f->{"${od}_stallid"}
243 if $qa->{ShowStalls};
245 $f->{UidLong}= join '_', @uid;
253 my $this= $uue % $base;
254 print STDERR "uue=$uue this=$this ";
259 $cmpu .= chr($this + ($this < 26 ? ord('a') :
260 $this < 52 ? ord('A')-26
262 print STDERR " uue=$uue this=$this cmpu=$cmpu\n";
263 die "$cmpu $uue ?" if length $cmpu > 20;
267 $f->{UidShort}= $cmpu;
269 if ($qa->{'debug'}) {
274 my $v= m/^[a-z]/ ? ord($&)-ord('a') :
275 m/^[A-Z]/ ? ord($&)-ord('A')+26 :
276 m/^[0-9]/ ? ord($&)-ord('0')+52 :
282 #print STDERR "(next)\n";
284 die "$f->{UidShort} $_ ?" unless defined $mul;
285 $outuid[$#outuid] += $v * $mul;
287 #print STDERR "$f->{UidShort} $_ $& v=$v mul=$mul ord()=".ord($&).
288 # "[vs.".ord('a').",".ord('A').",".ord('0')."]".
289 # " outuid=@outuid\n";
294 my $recons_long= join '_', @outuid;
295 $f->{UidLong} eq $recons_long or
296 die "$f->{UidLong} = $f->{UidShort} = $recons_long ?";
299 if (defined $qa->{"R$f->{UidShort}"} &&
300 !defined $qa->{"T$f->{UidShort}"}) {
307 <& dumptable:row, qa => $qa, sth => $sth, row => $f &>
309 <& dumptable:end, qa => $qa &>
312 % my $optimise= $specific && !$confusing && @islandids>1;
316 % if (@islandids<=1) {
320 Route contains archipelago(es), not just specific islands.
323 Route is complex - it visits the same island several times
324 and isn't a simple loop.
326 Therefore, optimal trade pattern not calculated.
328 % } else { # ========== OPTMISATION ==========
336 ", map { "$_->{unitprofit} $_->{Var}" } @flows)."
342 foreach my $flow (@flows) {
343 if ($flow->{Suppress}) {
349 foreach my $od (qw(org dst)) {
350 my $cstname= join '_', (
355 $flow->{"${od}_price"},
356 $flow->{"${od}_stallid"},
359 push @{ $avail_csts{$cstname}{Flows} }, $flow->{Var};
360 $avail_csts{$cstname}{Qty}= $flow->{"${od}_qty"};
363 foreach my $cstname (sort keys %avail_csts) {
364 my $c= $avail_csts{$cstname};
366 ". sprintf("%-30s","$cstname:")." ".
367 join("+", @{ $c->{Flows} }).
368 " <= ".$c->{Qty}."\n";
374 ", map { "$_->{Var} >= 0" } @flows)."
379 if ($qa->{'debug'}) {
388 my $input= pipethrough_prep();
389 print $input $cplex or die $!;
390 my $output= pipethrough_run_along($input, undef, 'glpsol',
391 qw(glpsol --cpxlp /dev/stdin -o /dev/stdout));
392 print "<pre>\n" if $qa->{'debug'};
393 my $found_section= 0;
395 print encode_entities($_) if $qa->{'debug'};
396 if (m/^\s*No\.\s+Column name\s+St\s+Activity\s/) {
397 die if $found_section>0;
401 next unless $found_section==1;
408 m/^\s*\d+\s+f(\d+)\s+\S+\s+(\d+)\s/ or die "$_ ?";
409 my $flow= $flows[$ix] or die;
410 $flow->{OptQty}= $qty;
411 $flow->{OptProfit}= $flow->{'unitprofit'} * $qty;
412 $flow->{OptCapital}= $flow->{OptQty} * $flow->{'org_price'};
414 print "</pre>\n" if $qa->{'debug'};
415 pipethrough_run_finish($output, 'glpsol');
416 die unless $found_section;
422 $addcols->({ Total => 0 }, qw(
428 % } # ========== OPTIMISATION ==========
431 % my $cdspan= $qa->{ShowStalls} ? ' colspan=2' : '';
432 % my $cdstall= $qa->{ShowStalls} ? '<th>Stall</th>' : '';
436 <% $qa->{ShowStalls} ? '<colgroup span=2>' : '' %>
447 <th<% $cdspan %>>Collect
448 <th<% $cdspan %>>Deliver
450 <th colspan=2>Collect
451 <th colspan=2>Deliver
455 <th colspan=3>Suggested
460 <th>Island <% $cdstall %>
461 <th>Island <% $cdstall %>
479 % foreach my $flow (@flows) {
481 <td><input type=hidden name=R<% $flow->{UidShort} %> value="">
482 <input type=checkbox name=T<% $flow->{UidShort} %> value=""
483 <% $flow->{Suppress} ? '' : 'checked' %> >
484 % foreach my $ci (0..$#cols) {
485 % my $col= $cols[$ci];
486 % my $v= $flow->{$col->{Name}};
487 % $col->{Total} += $v if defined $col->{Total};
488 % $v='' if !$col->{Text} && !$v;
489 <td <% $col->{Text} ? '' : 'align=right' %>><% $v |h %>
495 % foreach my $ci (2..$#cols) {
496 % my $col= $cols[$ci];
498 % if (defined $col->{Total}) {
499 <% $col->{Total} |h %>
504 <input type=submit name=update value="Update">
506 % if ($optimise) { # ========== TRADING PLAN ==========
508 % my $iquery= $dbh->prepare('SELECT islandname FROM islands
509 % WHERE islandid = ?');
511 <h1>Voyage trading plan</h1>
513 % foreach my $i (0..$#islandids) {
514 <tr><td colspan=4><strong>
515 % $iquery->execute($islandids[$i]);
516 % my ($islandname) = $iquery->fetchrow_array();
518 Start at <% $islandname |h %>
520 Sail to <% $islandname |h %>
523 % foreach my $od (qw(dst org)) {
524 % my $sign= $od eq 'dst' ? -1 : +1;
525 % foreach my $f (sort {
526 % $a->{'commodname'} cmp $b->{'commodname'}
527 % or $sign * ($a->{"${od}_price"} <=> $b->{"${od}_price"})
528 % or $a->{"${od}_stallname"} cmp $b->{"${od}_stallname"}
530 % next if $f->{Suppress};
531 % next unless $f->{"${od}_id"} == $islandids[$i];
532 % next unless $f->{OptQty};
539 % } # ========== TRADING PLAN ==========