I have a perl script to which I need to add a capability that is well beyond my current thinking ability. I am not even sure where to look for suggestions. I will try to state the problem and would like any suggestions as to where to start in coding or where to look for information on solving this sort of problem. It is sort a big puzzle. I have a number of sizes of weights, for argument purposes (in actuality they will be decimal values with many places), say they are .01 .12 1.1 2.2 5.3 10.5 50.1 100.6 gram. each weight will also have a known precision but that complexity will only be used in the program once I figure out the basics. I need to write a program that calculates the smallest total number of weights to use to obtain a total that is as close to a desired as possible. If it were all in whole numbers it would be trivial. The reason I can not just use a huge number of the smallest weight is because each weight has an allowable error and if a huge number is used, the possible error also gets huge. My current program demonstrates this very well. I have a script that does the calculations with the actual weights i will be using but the user has to use a trial and error process to attempt to get the closest match with the best precision and it involves lots fiddling and no assurance of the best solution. I could probably keep playing with the script and maybe get a clue as to what my thinking is, as a relatively intelligent human, and try to write a program to mimic what I have found to be a good approach but i think i would be using intuition and have no idea how to program that. I could probably do it by brute force but also do not know how to approach that either.

In reply to optimization problem by spencoid

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.