summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP4.hs
diff options
context:
space:
mode:
Diffstat (limited to 'AufgabeFFP4.hs')
-rw-r--r--AufgabeFFP4.hs24
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 @@
1module AufgabeFFP4 1module AufgabeFFP4
2where 2where
3 3
4import List
5
4type Weight = Int -- Gewicht 6type Weight = Int -- Gewicht
5type Value = Int -- Wert 7type Value = Int -- Wert
6type MaxWeight = Weight -- Hoechstzulaessiges Rucksackgewicht 8type 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?
38succKnp2 :: NodeKnp -> [(Value, Weight, MaxWeight, [Object], SolKnp)]
39succKnp2 (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
49goalKnp :: NodeKnp -> Bool 41goalKnp :: NodeKnp -> Bool
42goalKnp (_, _, _, [], _) = True
50goalKnp (_, w, limit, ((w', _) : _), _) = (w + w') > limit 43goalKnp (_, 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
81cmpObject :: Object -> Object -> Ordering
82cmpObject (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
88knapsack :: Objects -> MaxWeight -> (SolKnp, Value) 87knapsack :: Objects -> MaxWeight -> (SolKnp, Value)
89knapsack objects limit = (psol, v) 88knapsack 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