diff options
Diffstat (limited to 'AufgabeFFP4.hs')
| -rw-r--r-- | AufgabeFFP4.hs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/AufgabeFFP4.hs b/AufgabeFFP4.hs index 758bee4..8e7cbf1 100644 --- a/AufgabeFFP4.hs +++ b/AufgabeFFP4.hs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | module AufgabeFFP4 | 1 | module AufgabeFFP4 |
| 2 | where | 2 | where |
| 3 | 3 | ||
| 4 | import List | ||
| 5 | |||
| 4 | type Weight = Int -- Gewicht | 6 | type Weight = Int -- Gewicht |
| 5 | type Value = Int -- Wert | 7 | type Value = Int -- Wert |
| 6 | type MaxWeight = Weight -- Hoechstzulaessiges Rucksackgewicht | 8 | type MaxWeight = Weight -- Hoechstzulaessiges Rucksackgewicht |
| @@ -27,26 +29,17 @@ succKnp (v, w, limit, objects, psol) = | |||
| 27 | v + v', | 29 | v + v', |
| 28 | w + w', | 30 | w + w', |
| 29 | limit, | 31 | limit, |
| 30 | filterOne (== (w', v')) objects, | 32 | filterOne (== (w', v')) objects2, |
| 31 | (w', v') : psol | 33 | (w', v') : psol |
| 32 | ) | 34 | ) |
| 33 | ) objects2 | 35 | ) objects2 |
| 34 | where | 36 | where |
| 35 | objects2 = filter (\(w', v') -> (w + w') <= limit) objects | 37 | objects2 = filter (\(w', v') -> (w + w') <= limit) objects |
| 36 | 38 | ||
| 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 | ------------------------------------------------------------------------------- | 39 | ------------------------------------------------------------------------------- |
| 48 | 40 | ||
| 49 | goalKnp :: NodeKnp -> Bool | 41 | goalKnp :: NodeKnp -> Bool |
| 42 | goalKnp (_, _, _, [], _) = True | ||
| 50 | goalKnp (_, w, limit, ((w', _) : _), _) = (w + w') > limit | 43 | goalKnp (_, w, limit, ((w', _) : _), _) = (w + w') > limit |
| 51 | 44 | ||
| 52 | ------------------------------------------------------------------------------- | 45 | ------------------------------------------------------------------------------- |
| @@ -85,9 +78,16 @@ searchDfs succ goal x = search' (push x emptyStack) | |||
| 85 | 78 | ||
| 86 | ------------------------------------------------------------------------------- | 79 | ------------------------------------------------------------------------------- |
| 87 | 80 | ||
| 81 | cmpObject :: Object -> Object -> Ordering | ||
| 82 | cmpObject (w, v) (w', v') | ||
| 83 | | w == w' = compare v v' | ||
| 84 | | otherwise = compare w w' | ||
| 85 | |||
| 86 | -- it's safe to use maximum here as it will look at the first value of each tuple only | ||
| 88 | knapsack :: Objects -> MaxWeight -> (SolKnp, Value) | 87 | knapsack :: Objects -> MaxWeight -> (SolKnp, Value) |
| 89 | knapsack objects limit = (psol, v) | 88 | knapsack objects limit = (psol, v) |
| 90 | where (v, _, _, _, psol) = maximum (searchDfs succKnp goalKnp (0, 0, limit, objects, [])) | 89 | where |
| 90 | (v, _, _, _, psol) = maximum (searchDfs succKnp goalKnp (0, 0, limit, sortBy cmpObject objects, [])) | ||
| 91 | 91 | ||
| 92 | ------------------------------------------------------------------------------- | 92 | ------------------------------------------------------------------------------- |
| 93 | 93 | ||
