- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm.hs
More file actions
Latest commit
271 lines (246 loc) · 8.31 KB
/
Copy pathAlgorithm.hs
File metadata and controls
271 lines (246 loc) · 8.31 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
------------------------------------------------------------
-- sule
-- 2018.1
------------------------------------------------------------
moduleAlgorithm
( PathFinder
, TreeFinder
, StateGen
, shortestPath
, mst
, edgeT
) where
importControl.Concurrent
importData.Hashable
importqualifiedData.HashMapasH
importData.Tree
importqualifiedData.SetasS
--------------------------------------------------
--|general type of search algorithm, state->action->states map
-- action has type : state -> H.Map state w
typeSearchstatew
=state-> (state->H.Mapstatew) ->H.Mapstatestate
--|general type of search algorithm, IO version
typeIOSearchstatew
=MVarstate->MVar (H.Mapstatestate) ->MVar (H.Mapstatestate) ->state-> (state->H.Mapstatew) ->IO()
typeIOPathFinderstate=state->state->IO [state]
typePathFinderstate=state->state-> [state]
typeTreeFinderstate=state->Treestate
typeStateGenstatew=state->H.Mapstatew
--------------------------------------------------
--|dijkstra search algorithm
dijkstra::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=> (state, w)
->state
->H.Mapstate (w, state)
->StateGenstatew
->H.Mapstatestate
->H.Mapstatestate
dijkstra (state, sw) target open stateGen close
|H.null open'' = close
|H.member target close = close
|otherwise= dijkstra (bestState, bestW) target open' stateGen close'
where
close' =H.insert bestState preState close
open' =H.delete bestState open''
(bestState, preState, bestW) =
H.foldWithKey
(\rst (bw, pst) a@(_, _, rbw)->
if rbw > bw
then (rst, pst, bw)
else a)
(state, state, maxBound)
open''
open'' =
H.unionWith
(\a@(rsw, _) b@(rsw', _) ->
if rsw' > rsw
then a
else b)
open
nexts
nexts =H.map (\w -> (w + sw, state)) $stateGen state `H.difference` close
--------------------------------------------------
--|get hash table of shortest path
shortestPathTable::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=>state->Searchstatew
shortestPathTable target initSt stateGen =
dijkstra (initSt, 0 ) target open stateGen $H.singleton initSt initSt
where
open =H.map (\w -> (w, initSt)) $stateGen initSt
--------------------------------------------------
--|concurrent version dijkstra
conDijkstra::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=>MVarstate
->MVar (H.Mapstatestate)
->MVar (H.Mapstatestate)
-> (state, w)
->H.Mapstate (w, state)
->StateGenstatew
->H.Mapstatestate
->IO()
conDijkstra key mbox nbox (state, sw) open stateGen close
|H.null open'' =do
tryTakeMVar mbox
tryPutMVar mbox close
return()
|otherwise=do
skey <- tryReadMVar key
case skey of
Just _ ->return()
Nothing->do
box <- tryReadMVar nbox
tryTakeMVar mbox
tryPutMVar mbox close
case box of
Just cont ->
ifH.null$H.intersection cont close -- need fix
then conDijkstra
key
mbox
nbo
(bestState, bestW)
open'
stateGen
close'
elsedo
tryPutMVar key state
return()
Nothing->
conDijkstra key mbox nbox (bestState, bestW) open' stateGen close'
where
close' =H.insert bestState preState close
open' =H.delete bestState open''
(bestState, preState, bestW) =
H.foldWithKey
(\rst (bw, pst) a@(_, _, rbw)->
if rbw > bw
then (rst, pst, bw)
else a)
(state, state, maxBound)
open''
open'' =
H.unionWith
(\a@(rsw, _) b@(rsw', _) ->
if rsw' > rsw
then a
else b)
open
nexts
nexts =H.map (\w -> (w + sw, state)) $stateGen state `H.difference` close
--------------------------------------------------
--|concurrent bidirectional search, based on concurrent version dijkstra
conBDS::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=>StateGenstatew
->StateGenstatew
->IOPathFinderstate
conBDS genS genT s t =do
box1 <- newEmptyMVar ::IO (MVar (H.Mapstatestate))
box2 <- newEmptyMVar ::IO (MVar (H.Mapstatestate))
key <- newEmptyMVar ::IO (MVarstate)
flag <- newEmptyMVar ::IO (MVarBool)
forkIO $do
shortestPathTableIO key box1 box2 s genS
putMVar flag True
shortestPathTableIO key box2 box1 t genT
takeMVar flag
skey <- tryReadMVar key
case skey of
Just sk ->do
Just htbl1 <- tryReadMVar box1
Just htbl2 <- tryReadMVar box2
return$reverse (getStatesR htbl2 sk) ++ [sk] ++ getStatesR htbl1 sk
Nothing->do
Just htbl1 <- tryReadMVar box1
return$ t : getStatesR htbl1 t
--------------------------------------------------
--|get hash table of shortest path, IO version
shortestPathTableIO::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=>IOSearchstatew
shortestPathTableIO key box1 box2 initSt stateGen =
conDijkstra key box1 box2 (initSt, 0) open stateGen $
H.singleton initSt initSt
where
open =H.map (\w -> (w, initSt)) $stateGen initSt
--------------------------------------------------
--|Prim : Minimum Spanning Tree Algorithm
prim::
(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=> (state, w) -- init state and weight
->H.Mapstate (w, state) -- open table, init with initState's adj states, expand automatically
->StateGenstatew-- state expand algorithm
->H.Mapstatestate-- close table, init with k:initState v:initState
->H.Mapstatestate-- every state only have one parent state, so the tree can be stored as a Map
prim (state, sw) open stateGen close
|H.null open'' = close
|otherwise= prim (bestState, bestW) open' stateGen close'
where
close' =H.insert bestState preState close
open' =H.delete bestState open''
(bestState, preState, bestW) =
H.foldWithKey
(\rst (bw, pst) a@(_, _, rbw)->
if rbw > bw
then (rst, pst, bw)
else a)
(state, state, maxBound)
open''
open'' =
H.unionWith
(\a@(rsw, _) b@(rsw', _) ->
if rsw' > rsw
then a
else b)
open
nexts
nexts =H.map (const (sw, state)) $stateGen state `H.difference` close
--------------------------------------------------
--|camputing the minimum spanning tree
mstTable::(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)
=>Searchstatew
mstTable initSt stateGen =
prim (initSt, 0 ) open stateGen $H.singleton initSt initSt
where
open =H.map (\w -> (w, initSt)) $stateGen initSt
--------------------------------------------------
--|helper function to get list of states
getStatesR::
(Hashablestate, Eqstate,Ordstate)
=>H.Mapstatestate
->state
-> [state]
getStatesR h st =
caseH.lookup st h of
Just st' ->
if st' == st
then[]
else st' : getStatesR h st'
Nothing->[]
--------------------------------------------------
--|based on dijkstra
shortestPath:: (Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)=>
StateGenstatew->PathFinderstate
shortestPath stateGen s t
|s==t =[]
|otherwise= t:ans
where
resTbl = shortestPathTable t s stateGen
ans=getStatesR resTbl t
-------------------------------------------- ------
--|based on prim
mst::(Ordw, Numw, Boundedw, Hashablestate, Eqstate,Ordstate)=>
StateGenstatew->TreeFinderstate
mst gen st = mapToTree (mstTable st gen) st
mapToTree::(Hashablestate, Eqstate,Ordstate)=>H.Mapstatestate->TreeFinderstate
mapToTree m = unfoldTree (\r->(r,S.toList $S.delete r $(H.!) htree r))
where
htree = reverseHashTree m
reverseHashTree::(Hashablestate, Eqstate,Ordstate)=>H.Mapstatestate->H.Mapstate (S.Setstate)
reverseHashTree tree =H.foldWithKey (H.adjust.S.insert) (H.fromList $zip (H.keys tree) $repeatS.empty) tree
edgeT::Treea->[(a,a)]
edgeT (Node l chds) =concat$zip (repeat l) (map rootLabel chds) :map edgeT chds