स्पष्ट रूप से स्टैक ओवरफ़्लो पर कोई समस्या नहीं कह सकता है, हालांकि मैं वर्तमान में यह समझने की कोशिश कर रहा हूं कि Knapsack समस्या के भीतर आइटम समूहों के रूप में बाधाओं को कैसे एकीकृत किया जाए। मेरे गणित कौशल इस परिस्थिति में काफी सीमित साबित हो रहे हैं, हालांकि मैं इस काम को इरादे के साथ-साथ यह समझने के लिए बहुत प्रेरित हूं कि प्रत्येक पहलू क्या करता है (उस क्रम में जब चीजें काम करते हैं तो चीजों को और अधिक समझ में आता है)।आइटम समूह के साथ Knapsack समीकरण
इसके साथ, मुझे Rosetta Code पर एक बिल्कुल सुंदर कार्यान्वयन मिला है और कुछ मूलभूत परिप्रेक्ष्य से इसे बेहतर ढंग से समझने में मेरी मदद करने के लिए कुछ परिवर्तनीय नामों को साफ़ किया गया है।
दुर्भाग्यवश मुझे यह पता लगाने में एक अविश्वसनीय मुश्किल समय है कि मैं आइटम समूह को शामिल करने के लिए इस तर्क को कैसे लागू कर सकता हूं। मेरा उद्देश्य फंतासी टीमों के निर्माण के लिए है, मेरा खुद का मूल्य & प्रति खिलाड़ी वजन (अंक/वेतन) की आपूर्ति करना, लेकिन समूह के बिना (मेरे मामले में स्थितियां) मैं ऐसा करने में असमर्थ हूं।
क्या कोई मुझे इस के लिए सही दिशा में इंगित करने में सक्षम होगा? मैं अन्य भाषाओं से कोड उदाहरणों की समीक्षा कर रहा हूं और पूरी तरह से समस्या के अतिरिक्त विवरणों की समीक्षा कर रहा हूं, हालांकि मैं संभवतः किसी भी माध्यम से लागू समूहों को प्राप्त करना चाहता हूं।
<?php
function knapSolveFast2($itemWeight, $itemValue, $i, $availWeight, &$memoItems, &$pickedItems)
{
global $numcalls;
$numcalls++;
// Return memo if we have one
if (isset($memoItems[$i][$availWeight]))
{
return array($memoItems[$i][$availWeight], $memoItems['picked'][$i][$availWeight]);
}
else
{
// At end of decision branch
if ($i == 0)
{
if ($itemWeight[$i] <= $availWeight)
{ // Will this item fit?
$memoItems[$i][$availWeight] = $itemValue[$i]; // Memo this item
$memoItems['picked'][$i][$availWeight] = array($i); // and the picked item
return array($itemValue[$i],array($i)); // Return the value of this item and add it to the picked list
}
else
{
// Won't fit
$memoItems[$i][$availWeight] = 0; // Memo zero
$memoItems['picked'][$i][$availWeight] = array(); // and a blank array entry...
return array(0,array()); // Return nothing
}
}
// Not at end of decision branch..
// Get the result of the next branch (without this one)
list ($without_i,$without_PI) = knapSolveFast2($itemWeight, $itemValue, $i-1, $availWeight,$memoItems,$pickedItems);
if ($itemWeight[$i] > $availWeight)
{ // Does it return too many?
$memoItems[$i][$availWeight] = $without_i; // Memo without including this one
$memoItems['picked'][$i][$availWeight] = array(); // and a blank array entry...
return array($without_i,array()); // and return it
}
else
{
// Get the result of the next branch (WITH this one picked, so available weight is reduced)
list ($with_i,$with_PI) = knapSolveFast2($itemWeight, $itemValue, ($i-1), ($availWeight - $itemWeight[$i]),$memoItems,$pickedItems);
$with_i += $itemValue[$i]; // ..and add the value of this one..
// Get the greater of WITH or WITHOUT
if ($with_i > $without_i)
{
$res = $with_i;
$picked = $with_PI;
array_push($picked,$i);
}
else
{
$res = $without_i;
$picked = $without_PI;
}
$memoItems[$i][$availWeight] = $res; // Store it in the memo
$memoItems['picked'][$i][$availWeight] = $picked; // and store the picked item
return array ($res,$picked); // and then return it
}
}
}
$items = array("map","compass","water","sandwich","glucose","tin","banana","apple","cheese","beer","suntan cream","camera","t-shirt","trousers","umbrella","waterproof trousers","waterproof overclothes","note-case","sunglasses","towel","socks","book");
$weight = array(9,13,153,50,15,68,27,39,23,52,11,32,24,48,73,42,43,22,7,18,4,30);
$value = array(150,35,200,160,60,45,60,40,30,10,70,30,15,10,40,70,75,80,20,12,50,10);
## Initialize
$numcalls = 0;
$memoItems = array();
$selectedItems = array();
## Solve
list ($m4, $selectedItems) = knapSolveFast2($weight, $value, sizeof($value)-1, 400, $memoItems, $selectedItems);
# Display Result
echo "<b>Items:</b><br>" . join(", ", $items) . "<br>";
echo "<b>Max Value Found:</b><br>$m4 (in $numcalls calls)<br>";
echo "<b>Array Indices:</b><br>". join(",", $selectedItems) . "<br>";
echo "<b>Chosen Items:</b><br>";
echo "<table border cellspacing=0>";
echo "<tr><td>Item</td><td>Value</td><td>Weight</td></tr>";
$totalValue = 0;
$totalWeight = 0;
foreach($selectedItems as $key)
{
$totalValue += $value[$key];
$totalWeight += $weight[$key];
echo "<tr><td>" . $items[$key] . "</td><td>" . $value[$key] . "</td><td>".$weight[$key] . "</td></tr>";
}
echo "<tr><td align=right><b>Totals</b></td><td>$totalValue</td><td>$totalWeight</td></tr>";
echo "</table><hr>";
?>
आप स्पष्ट रूप से समस्या वांछित अंतिम परिणाम को परिभाषित सकते हैं? इससे मैन्युअल रूप से इसे समझने के बजाय कोड को समय-समय पर समझने में मदद मिलेगी। – Traveller
यदि आप किसी प्रश्न के लिए बाउंटी सेट करते हैं तो आपको वास्तव में अधिक सक्रिय होने की कोशिश करनी चाहिए। – Traveller
क्या आपने [इस] (http://stackoverflow.com/questions/29729609/knapsack-with-selection-from-distinct-groups?rq=1) पोस्ट से जानकारी का उपयोग करने का प्रयास किया था? – Traveller