From da217b51966713b159d9f4796cd6d46bb98ec0a0 Mon Sep 17 00:00:00 2001 From: Christian Merten Date: Sun, 19 May 2019 15:57:06 +0200 Subject: [PATCH] add parallel computing features not sure if this is a real improvement --- .gitignore | 1 + AI/Rulebased.hs | 9 ++++++--- Card.hs | 4 ++++ Main.hs | 23 +++++++++++++++++------ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 63ee796..eb9d3cc 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ *.hi *.o *.prof +*.hp diff --git a/AI/Rulebased.hs b/AI/Rulebased.hs index 5fa773c..636d8f3 100644 --- a/AI/Rulebased.hs +++ b/AI/Rulebased.hs @@ -7,6 +7,8 @@ module AI.Rulebased ( mkAIEnv, testds, simplify ) where +import Control.Parallel.Strategies + import Data.Ord import Data.Monoid ((<>)) import Data.List @@ -163,6 +165,7 @@ compareGuess (c1, ops1) (c2, ops2) distributions :: Guess -> (Int, Int, Int, Int) -> [Distribution] distributions guess nos = helper (sortBy compareGuess $ M.toList guess) nos + `using` parList rdeepseq where helper [] _ = [] helper ((c, hs):[]) ns = map fst (distr c hs ns) helper ((c, hs):gs) ns = @@ -246,9 +249,9 @@ chooseStatistic = do 2 -> 2 3 -> 3 -- simulate only partially - 4 -> 2 - 5 -> 1 - 6 -> 1 + 4 -> 3 + 5 -> 2 + 6 -> 2 7 -> 1 8 -> 1 9 -> 1 diff --git a/Card.hs b/Card.hs index 545e944..d7cf155 100644 --- a/Card.hs +++ b/Card.hs @@ -6,6 +6,7 @@ module Card where import Data.List import System.Random (newStdGen) import Utils +import Control.DeepSeq class Countable a b where count :: a -> b @@ -57,6 +58,9 @@ instance Countable Card Int where instance Countable [Card] Int where count = sum . map count +instance NFData Card where + rnf (Card t c) = t `seq` c `seq` () + equals :: Colour -> Maybe Colour -> Bool equals col (Just x) = col == x equals col Nothing = True diff --git a/Main.hs b/Main.hs index b90a4ec..82b2c24 100644 --- a/Main.hs +++ b/Main.hs @@ -13,7 +13,23 @@ import AI.Human import AI.Rulebased main :: IO () -main = putStrLn "Hello World" +main = testAI 10 + +testAI :: Int -> IO () +testAI n = do + let acs = repeat runAI + vals <- sequence (take n acs) + putStrLn $ "average won points " ++ show (fromIntegral (sum vals) / fromIntegral n) + +runAI :: IO Int +runAI = do + env <- shuffledEnv + let ps = piles env + cs = handCards Hand3 ps + trs = filter (isTrump Spades) cs + if length trs >= 5 && any ((==32) . getID) cs + then fst <$> evalStateT (turn Hand1) env + else runAI env :: SkatEnv env = SkatEnv piles Nothing Spades playersExamp @@ -50,8 +66,3 @@ env2 = SkatEnv piles Nothing Spades playersExamp h3 = map (putAt Hand3) hand3 piles = Piles (h1 ++ h2 ++ h3) [] [] -testAI :: Int -> IO () -testAI n = do - let acs = repeat (shuffledEnv >>= evalStateT (turnGeneric playOpen 10 Hand1) ) - vals <- sequence (take n acs) - putStrLn $ "average won points " ++ show (fromIntegral (sum (map fst vals)) / fromIntegral n)