Ask everyone about a deformed backpack.

Source: Internet
Author: User
Ask everyone about a deformed backpack. there are N kinds of items and a backpack with a capacity of V. There is also a threshold T.
A maximum of n [I] items are available for the I-th item. the cost per item is c [I] and the value is w [I].
Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is greater than and closest to T.

Complete backpack code

/**
* Knapsack problem description: a backpack with a maximum weight of W now has n items, each weighing t, and the value of each item is v.
* To maximize the weight of a backpack (but not more than W) and maximize the value of a backpack.
* Idea: define a two-dimensional array. the one-dimensional array is the number of items (indicating each item), and the two-dimensional array is the weight (no more than the maximum, here is 10). The following array is,
* The principle of dynamic planning, max (opt (i-1, w), wi + opt (i-1, w-wi) among the maximum,
* Opt (i-1, w-wi) refers to the previous optimal solution
*/
// This is based on the dynamic planning principle.
// Max (opt (i-1, w), wi + opt (i-1, w-wi ))
// The maximum weight of a backpack
$ W = 10;
// There are four items, the weight of each item
$ Dx = array (3, 4, 5 );
// Value of each item
$ Qz = array (4, 5, 6 );
// Define an array
$ A = array ();
// Initialization
For ($ I = 0; $ I <= 10; $ I ++) {$ a [0] [$ I] = 0 ;}
For ($ j = 0; $ j <= 3; $ j ++) {$ a [$ j] [0] = 0 ;}
// Opt (i-1, w), wi + opt (i-1, w-wi)
For ($ j = 1; $ j <= 3; $ j ++ ){
For ($ I = 1; $ I <= 10; $ I ++ ){
$ A [$ j] [$ I] = $ a [$ j-1] [$ I];
// W = 10
If ($ dx [$ j-1] <= $ w ){
If (! Isset ($ a [$ j-1] [$ I-$ dx [$ j-1]) continue;
// Wi + opt (i-1, wi)
$ Tmp = $ a [$ j-1] [$ I-$ dx [$ j-1] + $ qz [$ j-1];
// Opt (i-1, w), wi + opt (i-1, w-wi) => comparison
If ($ tmp> $ a [$ j] [$ I]) {
$ A [$ j] [$ I] = $ tmp;
}
}
}
}
// Print This array and output the rightmost value.
For ($ j = 0; $ j <= 3; $ j ++ ){
For ($ I = 0; $ I <= 10; $ I ++ ){
Echo $ a [$ j] [$ I]. "";
} Echo"
";
}


?>


Reply to discussion (solution)

What problems have you encountered? Your results should be correct.
But I like to write it like this.

$w = 10;$ar = array(  array('w' => 3, 'v' => 4),  array('w' => 4, 'v' => 5),  array('w' => 5, 'v' => 6),);foreach($ar as $k=>$v) {  $v['k'][] = $k;  $res[] = $v;}$p = 0;for(;$p
 
  $v) {    if(in_array($i, $res[$p]['k'])) continue;    if($r['w'] + $v['w'] <= $w) {      $res[] = array(        'w' => $r['w'] + $v['w'],        'v' => $r['v'] + $v['v'],        'k' => array_merge($r['k'], array($i)),      );    }  }}foreach($res as $v) $t[] = $v['v'];array_multisort($t, SORT_DESC, $res); print_r($res);
 
Array(    [0] => Array        (            [w] => 9            [v] => 11            [k] => Array                (                    [0] => 1                    [1] => 2                )        )    [1] => Array        (            [w] => 9            [v] => 11            [k] => Array                (                    [0] => 2                    [1] => 1                )        )    [2] => Array        (            [w] => 8            [v] => 10            [k] => Array                (                    [0] => 0                    [1] => 2                )        )    [3] => Array        (            [w] => 8            [v] => 10            [k] => Array                (                    [0] => 2                    [1] => 0                )        )    [4] => Array        (            [w] => 7            [v] => 9            [k] => Array                (                    [0] => 0                    [1] => 1                )        )    [5] => Array        (            [w] => 7            [v] => 9            [k] => Array                (                    [0] => 1                    [1] => 0                )        )    [6] => Array        (            [w] => 5            [v] => 6            [k] => Array                (                    [0] => 2                )        )    [7] => Array        (            [w] => 4            [v] => 5            [k] => Array                (                    [0] => 1                )        )    [8] => Array        (            [w] => 3            [v] => 4            [k] => Array                (                    [0] => 0                )        ))

At present, this is the greatest value of a backpack filled with w = 10.

I am trying to find out: a combination of a total value close to a given threshold T when it is as full as possible

$f = 9;print_r(array_filter($res, function($a) use ($f) { return $a['v'] > 0.9*$f && $a['v']<1.1*$f;}));


Thank you! Very good B, the result is completely achieved

This algorithm is too concise to understand. how can I filter out repeated combinations? For example, 0 3 6, 3 0 6, 6 0 3

.... Array_multisort ($ t, SORT_DESC, $ res); // sort (existing) foreach ($ res as $ I => $ v) if (isset ($ res [$ i-1]) &! Array_diff ($ res [$ i-1] ['K'], $ v ['K']) unset ($ res [$ I]); // deduplicate $ res = array_values ($ res); // normalization


I did not do this algorithm according to the dynamic plan.
Instead, all possible combinations are recorded and checked by sorting.

Thank you very much for your advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.