···11{-# LANGUAGE DataKinds #-}
22{-# LANGUAGE TypeOperators #-}
33{-# LANGUAGE RankNTypes #-}
44+{-# LANGUAGE ScopedTypeVariables #-}
45---------------------------------
56-- |
67-- Module : CHMM
···15161617import Papa
1718import GHC.TypeNats
1818-import Data.Proxy
1919+import Data.Proxy (Proxy(..))
1920import Linear.V
2021import Combinatorics
2122import qualified Data.Vector as Vec
22232424+import HMM
2325-----------------------
24262527trajectory :: forall n t a . (KnownNat n, KnownNat t) => Proxy t -> V n a -> V (n ^ t) (V n a)
···2729 where
2830 t' = fromIntegral $ natVal p
29313232+runCHMMInfer :: [Int] -> Int -> R
3333+runCHMMInfer evs l = let v = Vec.fromList [0 .. dim (swarmRobotsHMM ^. tModel) - 1]
3434+ n = length evs
3535+ in
3636+ reifyDimNat n $ \t' ->
3737+ reifyVectorNat v $ \n' ->
3838+ let ts = trajectory t' n'
3939+ valid_ts = V $ Vec.filter (Vec.all (>=l) . toVector) $ toVector ts
4040+ in
4141+ sum $ cHMMInfer swarmRobotsHMM (V $ Vec.fromList evs) <$> valid_ts
4242+4343+4444+4545+4646+4747+
+15-24
src/hmm.hs
···15151616module HMM where
17171818-import Prelude (undefined)
1919-import System.IO (print)
2018import Text.Printf (printf)
21192220import Papa
···232230 t += 1
233231 mzero
234232235235--- ** [/WIP/] Cost based HMM(cHMM)
233233+-- ** Cost based HMM(cHMM)
236234-- Inference algo from cost-HMM paper
237237-238235cHMMInfer :: forall s u t b i. (KnownNat s, KnownNat u, KnownNat t, Eq b, Ord b, Integral i, Show i, Show b)
239236 => HMM s u R b -- ^ HMM model
240240- -> V t i -- ^ state trajectory for time @1 .. t@; represented as @0@-based index of state
241237 -> V t b -- ^ evidence/observation trajectory for time @1 .. t@
242242- -> IO R
243243-cHMMInfer hmm xs evs = do
244244- let r = (\x y z -> (x * y) / z) <$> y0 <*> y1 <*> y2 $ zs
245245- print zs
246246- print $ y0 zs
247247- print $ y1 zs
248248- return r
238238+ -> V t i -- ^ state trajectory for time @1 .. t@; represented as @0@-based index of state
239239+ -> R
240240+cHMMInfer hmm evs xs = (\x y z -> (x * y) / z) <$> y0 <*> y1 <*> y2 $ zs
249241 where
250242 y0 :: Vec.Vector (i, b) -> R
251243 y0 = foldl' (\m (s,o') ->
···258250 m *
259251 bool
260252 (hmm ^?! tModel . ix (vs ^?! ix (idx - 1) . _1 . to fromIntegral) . ix (fromIntegral s))
261261- (((Linear.transpose $ hmm ^. prior) !*! hmm ^. tModel) ^?! ix 0 . ix (fromIntegral s))
253253+ ((Linear.transpose (hmm ^. prior) !*! hmm ^. tModel) ^?! ix 0 . ix (fromIntegral s))
262254 (idx == 0))
263255 1 vs
264256···277269 zs = Vec.zip (toVector xs) (toVector evs)
278270279271-- * Sample HMM Models
280280-281272umbrellaHMM :: HMM 2 2 R Bool
282273umbrellaHMM = mkHMM1 (V2 (V2 0.7 0.3) (V2 0.3 0.7)) (V2 (V2 0.9 0.2) (V2 0.1 0.8))
283283-274274+284275swarmRobotsHMM :: HMM 4 4 R Int
285276swarmRobotsHMM = mkHMM_
286277 (Just $ V $ Vec.replicate 4 (toV $ V1 0.25))
287278 (V $ Vec.fromList
288279 [
289289- (V $ Vec.fromList [0.7393859 , 0.2233470 , 0.0330670 , 0.0042001]),
290290- (V $ Vec.fromList [0.0928202 , 0.7172515 , 0.1719556 , 0.0179727]),
291291- (V $ Vec.fromList [0.0139341 , 0.1755173 , 0.6936519 , 0.1168966]),
292292- (V $ Vec.fromList [0.0047083 , 0.0403520 , 0.2561893 , 0.6987504])
280280+ V $ Vec.fromList [0.7393859 , 0.2233470 , 0.0330670 , 0.0042001],
281281+ V $ Vec.fromList [0.0928202 , 0.7172515 , 0.1719556 , 0.0179727],
282282+ V $ Vec.fromList [0.0139341 , 0.1755173 , 0.6936519 , 0.1168966],
283283+ V $ Vec.fromList [0.0047083 , 0.0403520 , 0.2561893 , 0.6987504]
293284 ])
294285 (V $ Vec.fromList [1..4])
295295- (Linear.transpose $ (V $ Vec.fromList
286286+ (Linear.transpose (V $ Vec.fromList
296287 [
297297- (V $ Vec.fromList [1.00000, 0.00000, 0.00000, 0.00000]),
298298- (V $ Vec.fromList [0.10000, 0.90000, 0.00000, 0.00000]),
299299- (V $ Vec.fromList [0.01000, 0.18000, 0.81000, 0.00000]),
300300- (V $ Vec.fromList [0.00100, 0.02700, 0.24300, 0.72900])
288288+ V $ Vec.fromList [1.00000, 0.00000, 0.00000, 0.00000],
289289+ V $ Vec.fromList [0.10000, 0.90000, 0.00000, 0.00000],
290290+ V $ Vec.fromList [0.01000, 0.18000, 0.81000, 0.00000],
291291+ V $ Vec.fromList [0.00100, 0.02700, 0.24300, 0.72900]
301292 ]))
302293303294-- * Main