But I found some cases (very few, like 0.001%) where it does not work, ie. @array=(43,44,43); It would return (43,43) and (43,44).

Then it's not right and needs to be fixed!

I know about FAQ code, but it's very convenient if you can spend the extra computing time and memory, but not the programming time.

By all means go that route. You did say that you are limited to ~100 elements, so the following will not be a concern to you.

The problem is that the FAQ conflation means that in addition to storing the original arrays, you need to store duplicates of both their intersections and differences in addition to the counting hash. For small datasets it's not a problem, but once they start getting bigger it becomes a problem.

Each of these is a separate problem and should be deault with separately (in the FAQ or modules). Computing both (all three if you include union) in a single pass is an optimisation--but only if you need them all.

I also took a look at the local categorised answer for this, but that doesn't handle duplicates either. I'll get back to you when I've solved the problem.

I'm also on the track of an improvement to the algorithm. I noticed that it converges on a pretty good solution with very few iterations (often <100), but then no matter how many more iterations it never improves further.

However, on a different run with the same values, it will achieve a better solution, again arriving at it quickly and then never improving. and yet, sometime a thord run will find a further improvement which again is arrived at quickly. The key seems to be the state of the PRNG. Some starting points produce better results than others regardless of the number of iterations you apply.

This is where most of my effort has been going. I've been trying to analyse the algorithm to work out why once a non-optimal solution is arrived at (very quickly) even huge numbers of further iterations won't improve it. That required me to come up with a fast and repeatable shuffle implementation (and is the subject of another thread).

It's obviously similar to a "local minima" in a genetic algorithm, but I'm not yet seeing the pattern.

I'll get back to you when I have made progress on either problem.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^11: Divide array of integers into most similar value halves by BrowserUk
in thread Divide array of integers into most similar value halves by Pepe

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.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.