diff options
Diffstat (limited to 'AufgabeFFP4.hs')
| -rw-r--r-- | AufgabeFFP4.hs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/AufgabeFFP4.hs b/AufgabeFFP4.hs new file mode 100644 index 0000000..758bee4 --- /dev/null +++ b/AufgabeFFP4.hs | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | module AufgabeFFP4 | ||
| 2 | where | ||
| 3 | |||
| 4 | type Weight = Int -- Gewicht | ||
| 5 | type Value = Int -- Wert | ||
| 6 | type MaxWeight = Weight -- Hoechstzulaessiges Rucksackgewicht | ||
| 7 | type Object = (Weight, Value) -- Gegenstand als Gewichts-/Wertpaar | ||
| 8 | type Objects = [Object] -- Menge der anfaenglich gegebenen Gegenstaende | ||
| 9 | type SolKnp = [Object] -- Auswahl aus der Menge der anfaenglich | ||
| 10 | -- gegebenen Gegenstaende; moegliche | ||
| 11 | -- Rucksackbeladung, falls zulaessig | ||
| 12 | |||
| 13 | type NodeKnp = (Value, Weight, MaxWeight, [Object], SolKnp) | ||
| 14 | |||
| 15 | ------------------------------------------------------------------------------- | ||
| 16 | |||
| 17 | filterOne :: (a -> Bool) -> [a] -> [a] | ||
| 18 | filterOne p [] = [] | ||
| 19 | filterOne p (x:xs) | ||
| 20 | | p x = xs | ||
| 21 | | otherwise = x : filterOne p xs | ||
| 22 | |||
| 23 | succKnp :: NodeKnp -> [NodeKnp] | ||
| 24 | succKnp (v, w, limit, objects, psol) = | ||
| 25 | map ( | ||
| 26 | \(w', v') -> ( | ||
| 27 | v + v', | ||
| 28 | w + w', | ||
| 29 | limit, | ||
| 30 | filterOne (== (w', v')) objects, | ||
| 31 | (w', v') : psol | ||
| 32 | ) | ||
| 33 | ) objects2 | ||
| 34 | where | ||
| 35 | objects2 = filter (\(w', v') -> (w + w') <= limit) objects | ||
| 36 | |||
| 37 | -- TODO: delete? | ||
| 38 | succKnp2 :: NodeKnp -> [(Value, Weight, MaxWeight, [Object], SolKnp)] | ||
| 39 | succKnp2 (v, w, limit, objects, psol) = | ||
| 40 | [(v + v', | ||
| 41 | w + w', | ||
| 42 | limit, | ||
| 43 | [ (w'', v'') | (w'', v'') <- objects, (w'' >= w')], | ||
| 44 | (w', v') : psol) | ||
| 45 | | (w', v') <- objects, w + w' <= limit ] | ||
| 46 | |||
| 47 | ------------------------------------------------------------------------------- | ||
| 48 | |||
| 49 | goalKnp :: NodeKnp -> Bool | ||
| 50 | goalKnp (_, w, limit, ((w', _) : _), _) = (w + w') > limit | ||
| 51 | |||
| 52 | ------------------------------------------------------------------------------- | ||
| 53 | |||
| 54 | -- see lecture slide #167 ff. | ||
| 55 | data Stack a = EmptyStk | ||
| 56 | | Stk a (Stack a) | ||
| 57 | |||
| 58 | push :: a -> Stack a -> Stack a | ||
| 59 | push x s = Stk x s | ||
| 60 | |||
| 61 | pop :: Stack a -> Stack a | ||
| 62 | pop EmptyStk = error "pop from an empty stack" | ||
| 63 | pop (Stk _ s) = s | ||
| 64 | |||
| 65 | top :: Stack a -> a | ||
| 66 | top EmptyStk = error "top from an empty stack" | ||
| 67 | top (Stk x _) = x | ||
| 68 | |||
| 69 | emptyStack :: Stack a | ||
| 70 | emptyStack = EmptyStk | ||
| 71 | |||
| 72 | stackEmpty :: Stack a -> Bool | ||
| 73 | stackEmpty EmptyStk = True | ||
| 74 | stackEmpty _ = False | ||
| 75 | |||
| 76 | searchDfs :: (Eq node) => (node -> [node]) -> (node -> Bool) -> node -> [node] | ||
| 77 | searchDfs succ goal x = search' (push x emptyStack) | ||
| 78 | where | ||
| 79 | search' s | ||
| 80 | | stackEmpty s = [] | ||
| 81 | | goal (top s) = top s : search' (pop s) | ||
| 82 | | otherwise = | ||
| 83 | let x = top s | ||
| 84 | in search' (foldr push (pop s) (succ x)) | ||
| 85 | |||
| 86 | ------------------------------------------------------------------------------- | ||
| 87 | |||
| 88 | knapsack :: Objects -> MaxWeight -> (SolKnp, Value) | ||
| 89 | knapsack objects limit = (psol, v) | ||
| 90 | where (v, _, _, _, psol) = maximum (searchDfs succKnp goalKnp (0, 0, limit, objects, [])) | ||
| 91 | |||
| 92 | ------------------------------------------------------------------------------- | ||
| 93 | |||
| 94 | --binomDyn :: (Integer, Integer) -> Integer | ||
| 95 | --binomDyn (m, n) = ... | ||
| 96 | -- where ... dynamic compB... bndsB... | ||
| 97 | |||
