Created
May 8, 2014 20:02
-
-
Save tron1point0/cd3ad70022185eb04f72 to your computer and use it in GitHub Desktop.
Bounded knapsack problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
type Weight = Int | |
type Value = Int | |
type Item = (String,Weight,Value) | |
type Pack = ([String],Weight,Value) | |
knapsack :: (Pack -> Bool) -> [Item] -> [Pack] | |
knapsack f = sortBy (value) . filter (f) . map (foldl (pack) ([],0,0)) . tail . subsequences | |
where value (_,_,v1) (_,_,v2) = compare v2 v1 | |
pack (ns,ws,vs) (n,w,v) = (n:ns,ws+w,vs+v) | |
maxWeight max (_,weight,_) = weight <= max |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment