-- -- A cop that uses smarter heuristics -- module CopStrategy where import Logging import CR import Syntax import KnowledgeBase import Graph import Utils import qualified Data.Map as M import Data.Maybe import Data.List ((\\), nub, sortBy) import System.IO.Unsafe import Control.Concurrent.MVar import Control.Monad import System.IO import CopInform ( informMessage ) import qualified CopPlan as CopPlan import CopState import qualified CopSimpleMoveFun as SimpleCop import qualified CopABMoveFun as ABCop ------------------------------------------------------------------------ -- evil name to see if we can break other people's parsers copName = "78094" copCRState = CRState { crCopStrat = simpleCopStrategy --abCopStrategy, --randomCopStrategy, , crRobberStrat = dummyRobberStrategy , crKB = Nothing } -- He will conduct a random walk randomCopStrategy = CopStrategy { csMkRegisterMsg = \ name -> return $ Register name CopFoot , csStoreWorldSkel = \wks -> do setKB $ mkInitialKnowledge wks kbCopCfg initState -- set trusts to zero , csStoreWorld = \w -> do kb <- getKB setKB (updateKnowledge kb w) , csMkInformMsg = informMessage , csMkPlanMsg = CopPlan.planMessage , csMkVoteMsg = mkVoteMsg , csMkMoveMsg = \_ -> SimpleCop.moveFun >>= \m -> return (m, NotDirty) , csDirtyStoreWorld = niceError ("dirty cops not yet implemented") , csDirtyMkInformMsg = niceError ("dirty cops not yet implemented") , csDirtyMkPlanMsg = niceError ("dirty cops not yet implemented") , csDirtyMkVoteMsg = niceError ("dirty cops not yet implemented") , csDirtyHandleVoteRes = niceError ("dirty cops not yet implemented") } simpleCopStrategy = randomCopStrategy { csMkMoveMsg = \_ -> SimpleCop.moveFun >>= \m -> return (m, NotDirty) } {- abCopStrategy = randomCopStrategy { csMkMoveMsg = \_ -> ABCop.moveFun >>= \m -> return (m, NotDirty) } -} ------------------------------------------------------------------------ -- Simple inform. Just guess that the robber is where they started. {- dummyInformMsg = do kb <- getKB return $ InformMsg [Inform (kb_robberName kb) (locOfNode $ kb_robberStart kb) Robber (kb_worldCount kb) (50) ] -} ------------------------------------------------------------------------ -- Needs to be correct for McGruffs to follow -- -- -- This plan message should be generated from an alpha-beta search of the future -- ------------------------------------------------------------------------ -- -- Doesn't use the plans yet -- -- TODO: Use them -- mkVoteMsg _ = do kb <- getKB let banks = kb_banks kb wc = kb_worldCount kb evs = kb_evidences kb me = kb_ownName kb justRobbed bank = case bank_lastTimeRobbed bank of Just t -> abs (wc - t) <= 1 _ -> False wasRobbed = any justRobbed banks canSmell = isJust (kb_smell kb) foundEvidence = any (\ev -> abs (ev_world ev - wc) <= 1) evs if wasRobbed || canSmell || foundEvidence -- vote for our own plan! then do let copNames = kb_copNames kb return $ VoteMsg (me : (copNames \\ [me])) -- vote based on trust and vote ourselves last else do copState <- getState let trusts = copTrust copState trustComp (_,a) (_, b) | a < b = GT | a == b = EQ | otherwise = LT copNames = map fst (sortBy trustComp trusts) debug $ "Trusts for cops: " ++ show trusts return $ VoteMsg (copNames ++ [me]) ------------------------------------------------------------------------ dummyRobberStrategy = niceError "robber strategy not defined"