From c47e82d2e09f2518794446d3cbaeafe9690046ac Mon Sep 17 00:00:00 2001 From: Christian Merten Date: Sun, 19 May 2019 12:03:56 +0200 Subject: [PATCH] replace map with list to improve efficiency --- AI/Rulebased.hs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/AI/Rulebased.hs b/AI/Rulebased.hs index f0e8c53..5fa773c 100644 --- a/AI/Rulebased.hs +++ b/AI/Rulebased.hs @@ -197,7 +197,9 @@ abstract cs = foldr f (0, 0, 0, 0) cs Spades -> (clubs, spades + 1 + v*100, hearts, diamonds) Clubs -> (clubs + 1 + v*100, spades, hearts, diamonds) -remove789s :: Hand -> [Distribution] -> M.Map (Abstract, Abstract)(Distribution, Int) +remove789s :: Hand + -> [Distribution] + -> M.Map (Abstract, Abstract) (Distribution, Int) remove789s hand ds = foldl' f M.empty ds where f cleaned d = let (c1, c2) = reduce hand d @@ -207,10 +209,9 @@ remove789s hand ds = foldl' f M.empty ds reduce Hand2 (h1, _, h3, _) = (h1, h3) reduce Hand3 (h1, h2, _, _) = (h1, h2) -simplify :: Hand -> [Distribution] -> M.Map Distribution Int -simplify hand ds = M.foldl' f M.empty cleaned +simplify :: Hand -> [Distribution] -> [(Distribution, Int)] +simplify hand ds = M.elems cleaned where cleaned = remove789s hand ds - f m (d, n) = M.insert d n m onPlayed :: MonadPlayer m => CardS Played -> AI m () onPlayed c = do @@ -269,13 +270,13 @@ chooseStatistic = do realDisNo = length realDis reducedDis = simplify Hand3 realDis reducedDisNo = length reducedDis - piless = M.mapKeys (toPiles table) reducedDis + piless = map (\(d, n) -> (toPiles table d, n)) reducedDis limit = if depth == 1 && length table == 2 then 1 else min 10000 $ realDisNo `div` 2 liftIO $ putStrLn $ "possible distrs without simp " ++ show realDisNo liftIO $ putStrLn $ "possible distrs " ++ show reducedDisNo - vals <- M.toList <$> foldWithLimit limit runOnPiles M.empty (M.toList piless) + vals <- M.toList <$> foldWithLimit limit runOnPiles M.empty piless liftIO $ print vals return $ fst $ maximumBy (comparing snd) vals