summaryrefslogtreecommitdiffstats
path: root/AufgabeFFP4.hs
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2012-04-28 16:35:37 +0200
committermanuel <manuel@mausz.at>2012-04-28 16:35:37 +0200
commitd35f6ed47f2e984fe614fa76265f8dc884db0744 (patch)
tree7e669eafa027ae35f9e8f6efb2b05150219f9e81 /AufgabeFFP4.hs
parent9016d85976cfb582fc555ba9d0b29160e5efadc7 (diff)
downloadffp-d35f6ed47f2e984fe614fa76265f8dc884db0744.tar.gz
ffp-d35f6ed47f2e984fe614fa76265f8dc884db0744.tar.bz2
ffp-d35f6ed47f2e984fe614fa76265f8dc884db0744.zip
initial implementation of Aufgabe4 exercise 1
Diffstat (limited to 'AufgabeFFP4.hs')
-rw-r--r--AufgabeFFP4.hs97
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 @@
1module AufgabeFFP4
2where
3
4type Weight = Int -- Gewicht
5type Value = Int -- Wert
6type MaxWeight = Weight -- Hoechstzulaessiges Rucksackgewicht
7type Object = (Weight, Value) -- Gegenstand als Gewichts-/Wertpaar
8type Objects = [Object] -- Menge der anfaenglich gegebenen Gegenstaende
9type SolKnp = [Object] -- Auswahl aus der Menge der anfaenglich
10 -- gegebenen Gegenstaende; moegliche
11 -- Rucksackbeladung, falls zulaessig
12
13type NodeKnp = (Value, Weight, MaxWeight, [Object], SolKnp)
14
15-------------------------------------------------------------------------------
16
17filterOne :: (a -> Bool) -> [a] -> [a]
18filterOne p [] = []
19filterOne p (x:xs)
20 | p x = xs
21 | otherwise = x : filterOne p xs
22
23succKnp :: NodeKnp -> [NodeKnp]
24succKnp (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?
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-------------------------------------------------------------------------------
48
49goalKnp :: NodeKnp -> Bool
50goalKnp (_, w, limit, ((w', _) : _), _) = (w + w') > limit
51
52-------------------------------------------------------------------------------
53
54-- see lecture slide #167 ff.
55data Stack a = EmptyStk
56 | Stk a (Stack a)
57
58push :: a -> Stack a -> Stack a
59push x s = Stk x s
60
61pop :: Stack a -> Stack a
62pop EmptyStk = error "pop from an empty stack"
63pop (Stk _ s) = s
64
65top :: Stack a -> a
66top EmptyStk = error "top from an empty stack"
67top (Stk x _) = x
68
69emptyStack :: Stack a
70emptyStack = EmptyStk
71
72stackEmpty :: Stack a -> Bool
73stackEmpty EmptyStk = True
74stackEmpty _ = False
75
76searchDfs :: (Eq node) => (node -> [node]) -> (node -> Bool) -> node -> [node]
77searchDfs 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
88knapsack :: Objects -> MaxWeight -> (SolKnp, Value)
89knapsack 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