Bladeren bron

add parallel computing features not sure if this is a real improvement

parallel-distrs
Christian Merten 6 jaren geleden
bovenliggende
commit
da217b5196
4 gewijzigde bestanden met toevoegingen van 28 en 9 verwijderingen
  1. +1
    -0
      .gitignore
  2. +6
    -3
      AI/Rulebased.hs
  3. +4
    -0
      Card.hs
  4. +17
    -6
      Main.hs

+ 1
- 0
.gitignore Bestand weergeven

@@ -6,3 +6,4 @@
*.hi
*.o
*.prof
*.hp

+ 6
- 3
AI/Rulebased.hs Bestand weergeven

@@ -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


+ 4
- 0
Card.hs Bestand weergeven

@@ -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


+ 17
- 6
Main.hs Bestand weergeven

@@ -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)

Laden…
Annuleren
Opslaan