Your question leaves lots of things unspecified. For example:

  1. How many items are to be considered?

    For a low number of items, a simple exhaustive search runs quickly enough:

    #! perl -slw use strict; use Data::Dump qw[ pp ]; use Algorithm::Combinatorics qw[ variations variations_with_repetition + ]; use List::Util qw[ sum ]; my %objects = map{ shift @$_ => $_ } map[ split ], <DATA>; pp \%objects; my $iter = variations( [ keys %objects ], 3 ); my( $bestValue, @bestSet ) = 0; while( my $set = $iter->next ) { my $cost = sum( map{ $objects{ $_ }[0] } @$set ); if( $cost <= 25 ) { my $value = sum( map{ $objects{ $_ }[ 1 ] } @$set ); if( $value >= $bestValue ) { $bestValue = $value; @bestSet = @$set; print "[@$set] => cost:$cost value:$value"; } } } print "Best: @bestSet => $bestValue"; #object cost($) value __DATA__ A 6 16 B 7 14 C 8 19 D 8 17 E 10 20

    Outputs:

    C:\test>1147022.pl { A => [6, 16], B => [7, 14], C => [8, 19], D => [8, 17], E => [10, 20 +] } [A D C] => cost:22 value:52 [A D E] => cost:24 value:53 [A C E] => cost:24 value:55 [A E C] => cost:24 value:55 [C A E] => cost:24 value:55 [C E A] => cost:24 value:55 [E A C] => cost:24 value:55 [E C A] => cost:24 value:55 Best: E C A => 55

    But for large numbers of items, this exhaustive search could take a very long time.

  2. How do you determine "best value"?

    Looking at the results above there are several ways of getting $55 worth of goods for $24 outlay; but they result in a 'profit' of 229%.

    However, for $22 outlay, you can get $52 worth of value resulting in a 236% 'markup'.

  3. You don't state whether you can purchase multiples of a given item?

    If you can, then switching from variations() to variations_with_repetition() gives a different set of results:

    C:\test>1147022.pl { A => [6, 16], B => [7, 14], C => [8, 19], D => [8, 17], E => [10, 20 +] } [A A A] => cost:18 value:48 [A A D] => cost:20 value:49 [A A C] => cost:20 value:51 [A A E] => cost:22 value:52 [A D C] => cost:22 value:52 [A D E] => cost:24 value:53 [A C C] => cost:22 value:54 [A C E] => cost:24 value:55 [A E C] => cost:24 value:55 [D C C] => cost:24 value:55 [C A E] => cost:24 value:55 [C D C] => cost:24 value:55 [C C D] => cost:24 value:55 [C C C] => cost:24 value:57 Best: C C C => 57

    And a different 'winner'.

You need to clarify your problem description.

Also, on the basis of previous experience of answering questions here; "simplifying" the problem description nearly always results in wasting a lot of people's time because they solve the problem you've described in a way that isn't applicable to the real problem. It's nearly always a false policy.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
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". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: maximizing value from a set of objects by BrowserUk
in thread maximizing value from a set of objects by BioPete

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.