Pathologically Eclectic Rubbish Lister PerlMonks

### comment on

 Need Help??
```my @order = sort {
\$Costs[\$a]+\$Trims[\$a]*\$Costs[\$b] <=> \$Costs[\$b]+\$Trims[\$b]*\$Costs[
+\$a]
} 0..\$#Costs;

I'm pretty sure that this is the optimal solution, but I'm too lazy to write down the complete mathematical proof.

The basic idea is that the terms represent the general cost difference of swapping the first and the second filter¹!

So by sorting you get a solution which can't be improved by a pairwise swapping and any permutation can be represented by a sequence of pairwise swaps.

Cheers Rolf

¹) see restructured formula for c in Re^2: Evolving a faster filter? (combinatorics) and you will notice that these are the first elements and the rest is fix.

UPDATE:

basic transformations lead to a simpler solution:

```my @order = sort {
\$Costs[\$a]/(1-\$Trims[\$a]) <=> \$Costs[\$b]/(1-\$Trims[\$b])
} 0..\$#Costs;

which can be precalculated to save calculation time

```my @order = sort {
\$weight[\$a] <=> \$weight[\$b]
} 0..\$#Costs;

(the edge case of a division by zero must be handled as practically infinity)

In reply to Re^3: Evolving a faster filter? (optimal solution?) by LanX
in thread Evolving a faster filter? by Ovid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2021-01-19 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The STEM quote I most wish I'd made is:

Results (199 votes). Check out past polls.

Notices?