-
Notifications
You must be signed in to change notification settings - Fork 0
/
GASTest.hs
229 lines (207 loc) · 9.23 KB
/
GASTest.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module GASTest where
import GAS
import Levels
import ProblemState
import Search
import TestPP
import Control.Arrow (second)
import Control.Monad (foldM)
newtype Tree = Tree { getTree :: Int }
deriving (Eq, Ord, Show)
instance ProblemState Tree Int where
successors (Tree n)
| n == 2 = [(1, Tree 4), (2, Tree 6)] -- 4 instead of 5 => cycle
| otherwise = [(1, Tree $ 2 * n + 1), (2, Tree $ 2 * n + 2)]
isGoal (Tree n) = n == 13
testAddObject :: TestPP ()
testAddObject = tests 1 5
[ testVal "addCircle.red.(0,0)" 1 " r" $
show $ addCircle Red (0, 0) emptyLevel
, testVal "addCircle.blue.(17,22)" 1 " b" $
show $ addCircle Blue (17, 22) emptyLevel
, testVal "addArrow.north.(0,0)" 1 " ^" $
show $ addArrow North (0, 0) emptyLevel
, testVal "addArrow.west.(17,22)" 1 " <" $
show $ addArrow West (17, 22) emptyLevel
, testVal "addSquare.red.north.(0,0)" 1 "R^ " $
show $ addSquare Red North (0, 0) emptyLevel
, testVal "addSquare.blue.west.(17,22)" 1 "B< " $
show $ addSquare Blue West (0, 0) emptyLevel
, testVal "addSquare.red.north.addArrow.north.(0,0)" 1 "R^^" $
show $ addSquare Red North (0, 0) $ addArrow North (0, 0) emptyLevel
, testVal "addSquare.red.north.addCircle.red.(17,22)" 1 "R^r" $
show $ addSquare Red North (17, 22) $ addCircle Red (17, 22) emptyLevel
]
testShowLevels :: TestPP ()
testShowLevels = tests 2 15
[ testVal "show.level0" 1 "Rv \n\
\ \n\
\ r" $ show level0
, testVal "show.level1" 1 "Bv \n\
\ b\n\
\ r\n\
\R^ " $ show level1
, testVal "show.level2" 1 "R> | b| r| \n\
\ | g| |G< \n\
\ |B^ | | " $ show level2
, testVal "show.level6" 1 " g| | |Rv | \n\
\ | b| | |B< \n\
\ | |G^ | | \n\
\ | | r| | " $ show level6
, testVal "show.level7" 1 "Bv | | b\n\
\ | | \n\
\ >| | ^" $ show level7
]
testMove :: TestPP ()
testMove = tests 3 15
-- no moves
[ testVal "move.level0.(1,0)" 1 (show level0) $ show $ move (1, 0) level0
, testVal "move.level0.(2,0)" 1 (show level0) $ show $ move (2, 0) level0
-- move with no pushes
, testVal "move.level0.1" 1 "Rv \n\
\ r" $ show $ movesLevel0 !! 1
, testVal "move.level0.2" 1 "Rvr" $ show $ movesLevel0 !! 2
, testVal "move.level0.3" 1 " r\n\
\Rv " $ show $ movesLevel0 !! 3
-- push 1 square
, testVal "move.level1.2" 1 " b\n\
\Bvr\n\
\R^ " $ show $ movesLevel1 !! 2
, testVal "move.level1.3" 1 " b\n\
\ r\n\
\Bv \n\
\R^ " $ show $ movesLevel1 !! 3
, testVal "move.level1.4" 1 " b\n\
\Bvr\n\
\R^ " $ show $ movesLevel1 !! 4
-- push 2 squares
, testVal "move.level6.3" 1 " g| | | \n\
\ |G^b|Rv |B< \n\
\ | | | \n\
\ | | r| " $ show $ movesLevel6 !! 3
-- change heading
, testVal "move.level7.2" 1 " | | b\n\
\ | | \n\
\B>>| | ^" $ show $ movesLevel7 !! 2
, testVal "move.level7.4" 1 " | | b\n\
\ | | \n\
\ >| |B^^" $ show $ movesLevel7 !! 4
, testVal "move.level7.6" 1 " | |B^b\n\
\ | | \n\
\ >| | ^" $ show $ movesLevel7 !! 6
]
where
movesLevel0 = moveMany [(0, 0), (1, 0), (2, 0)] level0
movesLevel1 = moveMany [(0, 0), (1, 0), (2, 0), (4, 0)] level1
movesLevel6 = moveMany [(0, 3), (2, 2), (1, 4)] level6
movesLevel7 = moveMany [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2)] level7
testLimitedDfs :: TestPP ()
testLimitedDfs = tests 4 15
[ testVal "limitedDfs.Tree.0" 1 [0] $ listTo 0
, testVal "limitedDfs.Tree.1" 1 [0, 1, 2] $ listTo 1
, testVal "limitedDfs.Tree.2" 1 [0, 1, 3, 4, 2, 6] $ listTo 2
, testVal "limitedDfs.Tree.3" 1 [0, 1, 3, 7, 8, 4, 9, 10, 2, 6, 13, 14] $
listTo 3
]
where
listTo = map (getTree . nodeState) . limitedDfs (Tree 0) False
testIterativeDeepening :: TestPP ()
testIterativeDeepening = tests 5 10
[ testVal "iterativeDeepening.Tree.state" 1 (Tree 13) $ nodeState node
, testVal "iterativeDeepening.Tree.number" 1 20 $ number
]
where
(node, number) = iterativeDeepening (Tree 0) False
testExtractPath :: TestPP ()
testExtractPath = tests 6 10
[ testVal "extractPath.Tree" 1 [(2, 2), (2, 6), (1, 13)] $
map (second getTree) $ extractPath node
]
where
(node, _) = iterativeDeepening (Tree 0) False
testSuccessors :: TestPP ()
testSuccessors = tests 7 10
[ testVal "successors.level0.1" 1 [((0, 0), "Rv \n\
\ r")] $
map (second show) $ successorsLevel01
, testVal "successors.level0.2" 1 [((1,0),"Rvr")] $
map (second show) $ successorsLevel02
, testVal "successors.level1.1" 1 [((0,0),"Bvb\n\
\ r\n\
\R^ "),
((3,0),"Bv \n\
\ b\n\
\R^r")] $
map (second show) $ successors level1
]
where
successorsLevel01 = successors level0
successorsLevel02 = successors $ snd $ head successorsLevel01
testIsGoal :: TestPP ()
testIsGoal = tests 8 10
[ testCond "isGoal.level0.0" 1 $ not $ isGoal $ movesLevel0 !! 0
, testCond "isGoal.level0.1" 1 $ not $ isGoal $ movesLevel0 !! 1
, testCond "isGoal.level0.2" 1 $ isGoal $ movesLevel0 !! 2
, testCond "isGoal.level1.0" 1 $ not $ isGoal $ movesLevel1 !! 0
, testCond "isGoal.level1.1" 1 $ not $ isGoal $ movesLevel1 !! 1
, testCond "isGoal.level1.2" 1 $ isGoal $ movesLevel1 !! 2
, testCond "isGoal.level7.0" 1 $ not $ isGoal $ movesLevel7 !! 0
, testCond "isGoal.level7.2" 1 $ not $ isGoal $ movesLevel7 !! 2
, testCond "isGoal.level7.4" 1 $ not $ isGoal $ movesLevel7 !! 4
, testCond "isGoal.level7.6" 1 $ isGoal $ movesLevel7 !! 6
]
where
movesLevel0 = moveMany [(0, 0), (1, 0), (2, 0)] level0
movesLevel1 = moveMany [(0, 0), (3, 0)] level1
movesLevel7 = moveMany [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2)] level7
testSolve :: TestPP ()
testSolve = tests 9 10
[ testCond "solve.level0" 1 $ isSolution level0 $ getSolution level0
, testCond "solve.level1" 1 $ isSolution level1 $ getSolution level1
, testCond "solve.level2" 1 $ isSolution level2 $ getSolution level2
, testCond "solve.level6" 1 $ isSolution level6 $ getSolution level6
, testCond "solve.level7" 1 $ isSolution level7 $ getSolution level7
]
where
getSolution = extractPath . fst . flip iterativeDeepening False
testHeuristic :: TestPP ()
testHeuristic = tests 10 20
[ testCond "heuristic.level1" 1 $
(isSolution level1 $ extractPath pH1) && nH1 < n1
, testCond "heuristic.level2" 1 $
(isSolution level2 $ extractPath pH2) && nH2 < n2
, testCond "heuristic.level6" 1 $
(isSolution level6 $ extractPath pH6) && nH6 < n6
]
where
(_, n1) = iterativeDeepening level1 False
(pH1, nH1) = iterativeDeepening level1 True
(_, n2) = iterativeDeepening level2 False
(pH2, nH2) = iterativeDeepening level2 True
(_, n6) = iterativeDeepening level6 False
(pH6, nH6) = iterativeDeepening level6 True
moveMany :: [Position] -> Level -> [Level]
moveMany positions level = out
where
out = level : zipWith move positions out
isSolution :: Level -> [(Position, Level)] -> Bool
isSolution level path = isValidPath && null rest
where
isValidPath = foldM (\from (pos, to) ->
if move pos from == to then Just to else Nothing)
level path /= Nothing
(_, _ : rest) = break (isGoal . snd) path
main :: IO ()
main = runTestPP $ sequence_ [ testAddObject
, testShowLevels
, testMove
, testLimitedDfs
, testIterativeDeepening
, testExtractPath
, testSuccessors
, testIsGoal
, testSolve
, testHeuristic
]