Skip to content
This repository has been archived by the owner on Jun 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #322 from advancedtelematic/feat/stimulus-matrix
Browse files Browse the repository at this point in the history
add stimulus matrix
  • Loading branch information
rdanitz authored Jun 19, 2019
2 parents 6c123d9 + 2840f02 commit 6b77906
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/Test/StateMachine/Markov.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Test.StateMachine.Markov
, coverMarkov
, tabulateMarkov
, transitionMatrix
, stimulusMatrix
, historyObservations
, markovToDot
, markovToPs
Expand Down Expand Up @@ -274,6 +275,51 @@ transitionMatrix markov = enumMatrix go
. unMarkov
$ markov

enumMatrix'
:: forall state cmd a
. (Generic state, GEnum FiniteEnum (Rep state), GBounded (Rep state))
=> (Generic cmd, GEnum FiniteEnum (Rep cmd), GBounded (Rep cmd))
=> ((state, cmd) -> a)
-> Matrix a
enumMatrix' f = matrix m n (f . bimap g h)
where
g :: Int -> state
g = gtoFiniteEnum . pred -- We need the predecessor because 'matrix' starts
-- indexing from 1.

h :: Int -> cmd
h = gtoFiniteEnum . pred

m :: Int
m = length states

n :: Int
n = length cmds

states :: [state]
states = gfiniteEnumFromTo gminBound gmaxBound

cmds :: [cmd]
cmds = gfiniteEnumFromTo gminBound gmaxBound

stimulusMatrix
:: forall state cmd. (Ord state, Ord cmd)
=> (Generic state, GEnum FiniteEnum (Rep state), GBounded (Rep state))
=> (Generic cmd, GEnum FiniteEnum (Rep cmd), GBounded (Rep cmd))
=> Markov state cmd Double
-> Matrix Double
stimulusMatrix markov = enumMatrix' go
where
go :: (state, cmd) -> Double
go (state, cmd) = fromMaybe 0
(Map.lookup cmd =<< Map.lookup state availableCmds)

availableCmds :: Map state (Map cmd Double)
availableCmds
= fmap (Map.fromList . map (command &&& (/ 100) . probability))
. unMarkov
$ markov

------------------------------------------------------------------------

historyObservations :: forall model cmd m resp state cmd_ prob. Ord state
Expand Down
2 changes: 1 addition & 1 deletion test/ProcessRegistry.hs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ data Action_
| BadUnregister_
| WhereIs_
| Exit_
deriving (Show, Eq, Ord)
deriving (Show, Eq, Ord, Generic)

constructor :: Action r -> Action_
constructor act = case act of
Expand Down

0 comments on commit 6b77906

Please sign in to comment.