From 26b59c0a9a61e9f53a989c2fdbba7ee764f4ab0f Mon Sep 17 00:00:00 2001 From: Slash Date: Mon, 10 Aug 2015 00:22:53 +0200 Subject: [PATCH 1/2] deepseq awesomeness --- src/Main.hs | 54 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/Main.hs b/src/Main.hs index cefcad5..c8a0de2 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -5,8 +5,9 @@ module Main where +import Control.DeepSeq (deepseq, NFData(..)) import Data.Int -import Data.List +import Data.List (zip4) import qualified Data.ByteString.Lazy.Char8 as BS import System.Environment import System.Random @@ -24,15 +25,20 @@ import Opt import JSONDeser(readInput) import PowerPhrases -ptag :: String -ptag = "lilik0" -logfilename :: String -logfilename = "scores" +import Debug.Trace (trace) + +strategyTag :: String +strategyTag = "lilik0" + +logFileName :: String +logFileName = "scores" + +timeLimitRatio :: Double +timeLimitRatio = 1.0 + +memLimitRatio :: Double +memLimitRatio = 1.0 -timelimitratio :: Double -timelimitratio = 1.0 -memlimitratio :: Double -memlimitratio = 1.0 gccompperstep :: Integer gccompperstep = 100 @@ -78,7 +84,7 @@ main = do args <- getArgs let (commandswpoints,strat) = unzip results let (commandlists, points) = unzip commandswpoints let wordlists = map cmdToString commandlists - let outJSONstructs = zipWith3 (\x y z -> (JSONSer x y ptag z)) ids seeds wordlists + let outJSONstructs = zipWith3 (\x y z -> (JSONSer x y strategyTag z)) ids seeds wordlists BS.putStrLn $ encode outJSONstructs writelogfile logf (zip4 ids seeds points strat) where @@ -105,27 +111,29 @@ readFiles (x:xs) = do f <- BS.readFile x fs <- readFiles xs return (f:fs) +instance NFData Command where rnf x = seq x () + iterategc :: [GameComputation] -> Maybe (Int,Int64) -> Maybe Int -> IO [(([Command], Int), Int)] iterategc gcs tlimit mlimit = do rtl <- timeLimit tlimit rml <- memLimit mlimit - (gcresult rtl rml) + if rtl || rml + then return best + else if (and $ map finishedGameComputation gcs) + then return best + else let mona = (applyNtimes gccompperstep itf gcs) + mona1 = map getBestGameComputation gcs + in mona1 `deepseq` (iterategc mona tlimit mlimit) where - gcresult True _ = return best - gcresult _ True = return best - gcresult _ _ = if (and $ map finishedGameComputation gcs) - then return best - else iterategc (applyNtimes gccompperstep itf gcs) tlimit mlimit - where - itf gcs1 = map advanceGameComputation gcs1 - applyNtimes 0 _ accum = accum - applyNtimes n f accum = applyNtimes (n - 1) f (f accum) - best = map getBestGameComputation gcs + itf gcs1 = map advanceGameComputation gcs1 + applyNtimes 0 _ accum = accum + applyNtimes n f accum = applyNtimes (n - 1) f (f accum) + best = map getBestGameComputation gcs timeLimit :: Maybe (Int,Int64) -> IO Bool timeLimit Nothing = return False timeLimit (Just (itime,limit)) = do atime <- secTime let diff = (atime - (fromIntegral itime)) - return (((fromIntegral limit) * timelimitratio) < (fromIntegral diff)) + return (((fromIntegral limit) * timeLimitRatio) < (fromIntegral diff)) memLimit :: Maybe Int -> IO Bool @@ -137,7 +145,7 @@ secTime = do (TimeSpec s _) <- getTime Monotonic writelogfile :: Bool -> [(Int,Int,Int,Int)] -> IO () writelogfile False _ = return () -writelogfile _ els = writeFile logfilename scoredata +writelogfile _ els = writeFile logFileName scoredata where scoredata = foldl strlog "\n" els strlog x (a,b,c,d) = sa ++ sb ++ sc ++ sd ++ x ++ "\n" From 1d849d6fb1ddd4dbc099eb03266066eeb8a58535 Mon Sep 17 00:00:00 2001 From: Slash Date: Mon, 10 Aug 2015 00:40:41 +0200 Subject: [PATCH 2/2] added deepseq dependency, fixes --- icfp2015.cabal | 2 +- src/Main.hs | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/icfp2015.cabal b/icfp2015.cabal index 7c61263..175bb83 100644 --- a/icfp2015.cabal +++ b/icfp2015.cabal @@ -60,7 +60,7 @@ executable icfp2015 other-extensions: OverloadedStrings, DeriveGeneric, DeriveDataTypeable -- Other library packages from which modules are imported. - build-depends: base >=4.6 && <4.9, hashable >=1.2 && <1.3, containers >=0.5 && <0.6, QuickCheck >=2.7 && <2.9, bytestring >=0.10 && <0.11, aeson >=0.8 && <0.9, pqueue >=1.3 && <1.4, clock, random + build-depends: base >=4.6 && <4.9, hashable >=1.2 && <1.3, containers >=0.5 && <0.6, QuickCheck >=2.7 && <2.9, bytestring >=0.10 && <0.11, aeson >=0.8 && <0.9, pqueue >=1.3 && <1.4, clock, random, deepseq >= 1.3 && <1.4 -- Directories containing source files. hs-source-dirs: src diff --git a/src/Main.hs b/src/Main.hs index c8a0de2..10e2d3d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -80,13 +80,12 @@ main = do args <- getArgs let (ids,seeds,gamecomputations) = unzip3 loaddata inittime <- secTime results <- iterategc gamecomputations (timestruct timelimit inittime) memlimit - putStrLn "lol" let (commandswpoints,strat) = unzip results let (commandlists, points) = unzip commandswpoints let wordlists = map cmdToString commandlists let outJSONstructs = zipWith3 (\x y z -> (JSONSer x y strategyTag z)) ids seeds wordlists BS.putStrLn $ encode outJSONstructs - writelogfile logf (zip4 ids seeds points strat) + writeLogFile logf (zip4 ids seeds points strat) where timestruct Nothing _ = Nothing timestruct (Just maxtime) intime = Just (maxtime, intime) @@ -143,9 +142,9 @@ secTime :: IO Int64 secTime = do (TimeSpec s _) <- getTime Monotonic return s -writelogfile :: Bool -> [(Int,Int,Int,Int)] -> IO () -writelogfile False _ = return () -writelogfile _ els = writeFile logFileName scoredata +writeLogFile :: Bool -> [(Int,Int,Int,Int)] -> IO () +writeLogFile False _ = return () +writeLogFile _ els = writeFile logFileName scoredata where scoredata = foldl strlog "\n" els strlog x (a,b,c,d) = sa ++ sb ++ sc ++ sd ++ x ++ "\n"