1
對於大學任務,我們必須研究揹包問題的各種解決方案,然後在Haskell和Python中實現解決方案。Haskell - 樹形遞歸避免控制堆棧溢出
我選擇了蠻力。我意識到有更好的算法,但這個選擇的原因超出了這篇文章的範圍。
但是,在我的兩次嘗試中,當使用HUGS時,最終都會出現控制堆棧溢出,但是在使用GHC時不會出現控制堆棧溢出。
調查似乎指向嚴格性/懶惰的問題,我的代碼最終會產生過多的thunk,而GHC的嚴格性分析似乎正在消除這個問題。
有人能指出我在下面提供的代碼中出錯的地方,並且讓我帶領如何解決問題。
注:我只有4個星期的Haskell經驗,所以認識到我的代碼與Haskell專家編寫的代碼相比是天真的。
編輯:添加幾個`seq
`語句使得程序在HUGS中工作。然而,這似乎有點破解。還有其他可能的改進嗎?我已經接受了一個答案,但任何進一步的意見將不勝感激。
module Main where
import Debug.Trace
import Data.Maybe
type ItemInfo = (Double,Double)
type Item = (ItemInfo,[Char])
type Solution = (ItemInfo,[Item])
-- FilterTerminationCondition should be a function that returns True if this branch of brute force should be stopped.
type FilterTerminationCondition = (Solution -> Bool)
-- FilterComparator should return which, out of two solutions, is better.
-- Both solutions will have passed FilterTerminationCondition succesfully.
type FilterComparator = (Solution -> Solution -> Solution)
-- FilterUsesTerminatingSolution is a boolean which indicates, when FilterTerminationCondition has caused a branch to end, whether to use the set of items that caused the end of the branch (True) or the set of items immeidately before (False).
type FilterUsesTerminatingSolution = Bool
-- A Filter should contain lambada functions for FilterTerminationCondition and FilterComparator
type Filter = (FilterTerminationCondition,FilterComparator,FilterUsesTerminatingSolution)
-- A series of functions to extract the various items from the filter.
getFilterTerminationCondition :: Filter -> FilterTerminationCondition
getFilterTerminationCondition (ftcond,fcomp,futs) = ftcond
getFilterComparator :: Filter -> FilterComparator
getFilterComparator (ftcond,fcomp,futs) = fcomp
getFilterUsesTerminatingSolution :: Filter -> FilterUsesTerminatingSolution
getFilterUsesTerminatingSolution (ftcond,fcomp,futs) = futs
-- Aliases for fst and snd that make the code easier to read later on.
getSolutionItems :: Solution -> [Item]
getSolutionItems (info,items) = items
getItemInfo :: Item -> ItemInfo
getItemInfo (iteminfo,itemname) = iteminfo
getWeight :: ItemInfo -> Double
getWeight (weight,profit) = weight
getSolutionInfo :: Solution -> ItemInfo
getSolutionInfo (info,items) = info
getProfit :: ItemInfo -> Double
getProfit (weight,profit) = profit
knapsack :: Filter -> [Item] -> Solution -> Maybe Solution -> Maybe Solution
knapsack filter [] currentsolution bestsolution = if (getFilterTerminationCondition filter) currentsolution == (getFilterUsesTerminatingSolution filter) then knapsackCompareValidSolutions filter currentsolution bestsolution else bestsolution
knapsack filter (newitem:remainingitems) currentsolution bestsolution = let bestsolutionwithout = knapsack filter remainingitems currentsolution bestsolution
currentsolutionwith = (((getWeight $ getSolutionInfo currentsolution)+(getWeight $ getItemInfo newitem),(getProfit $ getSolutionInfo currentsolution)+(getProfit $ getItemInfo newitem)),((getSolutionItems currentsolution) ++ [newitem]))
in if (getFilterTerminationCondition filter) currentsolutionwith then knapsackCompareValidSolutions filter (if (getFilterUsesTerminatingSolution filter) then currentsolutionwith else currentsolution) bestsolutionwithout else knapsack filter remainingitems currentsolutionwith bestsolutionwithout
knapsackCompareValidSolutions :: Filter -> Solution -> Maybe Solution -> Maybe Solution
knapsackCompareValidSolutions filter currentsolution bestsolution = let returnval = case bestsolution of
Nothing -> currentsolution
Just solution -> (getFilterComparator filter) currentsolution solution
in Just returnval
knapsackStart :: Filter -> [Item] -> Maybe Solution
knapsackStart filter allitems = knapsack filter allitems ((0,0),[]) Nothing
knapsackProblemItems :: [Item]
knapsackProblemItems =
[
((4.13, 1.40),"Weapon and Ammunition"),
((2.13, 2.74),"Water"),
((3.03, 1.55),"Pith Helmet"),
((2.26, 0.82),"Sun Cream"),
((3.69, 2.38),"Tent"),
((3.45, 2.93),"Flare Gun"),
((1.09, 1.77),"Olive Oil"),
((2.89, 0.53),"Firewood"),
((1.08, 2.77),"Kendal Mint Cake"),
((2.29, 2.85),"Snake Repellant Spray"),
((3.23, 4.29),"Bread"),
((0.55, 0.34),"Pot Noodles"),
((2.82,-0.45),"Software Engineering Textbook"),
((2.31, 2.17),"Tinned food"),
((1.63, 1.62),"Pork Pie")
]
knapsackProblemMaxDistance :: Double -> Filter
knapsackProblemMaxDistance maxweight = ((\solution -> (getWeight $ getSolutionInfo solution) > maxweight),(\solution1 solution2 -> if (getProfit $ getSolutionInfo solution1) > (getProfit $ getSolutionInfo solution2) then solution1 else solution2),False)
knapsackProblemMinWeight :: Double -> Filter
knapsackProblemMinWeight mindays = ((\solution -> (getProfit $ getSolutionInfo solution) >= mindays),(\solution1 solution2 -> if (getWeight $ getSolutionInfo solution1) < (getWeight $ getSolutionInfo solution2) then solution1 else solution2),True)
knapsackProblem1 = knapsackStart (knapsackProblemMaxDistance 20) knapsackProblemItems
knapsackProblem2 = knapsackStart (knapsackProblemMaxDistance 25) knapsackProblemItems
knapsackProblem3 = knapsackStart (knapsackProblemMinWeight 25) knapsackProblemItems
經過調查,原來是使用了序列與(變量?你會怎麼稱呼他們)currentSolution,bestSolution,以及returnval在knapsackCompareValidSolutions,時所做的代碼足夠effiecient它不再的原因控制堆棧溢出。 關於使用數據類型。我一直在試圖理解使用新類型或數據會給「使用類型」帶來什麼好處。你能解釋一下這會帶來什麼好處嗎? –
使用數據類型的好處主要在於可讀性和文檔。元組類型不告訴讀者任何東西,數據類型你知道發生了什麼。 –
再次感謝。我現在已經將數據類型的使用併入了代碼中,事實上,它不僅改進了可讀性,而且還以某種方式提高了它的效率(就WinHugs報告的減少量,單元和垃圾回收量而言) 。 不幸的是,我沒有足夠的聲望點來滿足您的答案,從而爲您提供您應得的額外聲望點。 –