You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.2 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. {-# LANGUAGE BangPatterns #-}
  2. {-# LANGUAGE DeriveGeneric #-}
  3. {-# LANGUAGE ExistentialQuantification #-}
  4. {-# OPTIONS -Wall #-}
  5. module Main where
  6. import Control.DeepSeq (deepseq, NFData(..))
  7. import Data.Int
  8. import qualified Data.ByteString.Lazy.Char8 as BS
  9. import System.Environment
  10. import System.Random
  11. import System.Clock
  12. import GHC.Generics
  13. import Data.Aeson
  14. import Data.Maybe
  15. import StrategyManager
  16. import Strategy0
  17. import Datatypes.Game(Game,Command)
  18. import VM
  19. import Opt
  20. import JSONDeser(readInput)
  21. import PowerPhrases
  22. strategyTag :: String
  23. strategyTag = "lilik0"
  24. logFileName :: String
  25. logFileName = "scores"
  26. timeLimitRatio :: Double
  27. timeLimitRatio = 0.96
  28. memLimitRatio :: Double
  29. memLimitRatio = 1.0
  30. computationsPerStep :: Int
  31. computationsPerStep = 10
  32. data JSONSer = JSONSer { problemId :: Int,
  33. problemSeed :: Int,
  34. problemTag :: String,
  35. problemSolution :: String
  36. } deriving (Show, Generic)
  37. instance FromJSON JSONSer
  38. instance ToJSON JSONSer
  39. type Id = Int
  40. type Seed = Int
  41. strategies :: Game -> StdGen -> Maybe [Command] -> GameComputation
  42. strategies g sgen cmd = [MkStrategyWrapper (initst g sgen cmd :: Strategy0)]
  43. -- = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1),
  44. -- MkStrategyWrapper (initst g sgen cmd :: NullStrategy2)]
  45. -- example ::
  46. -- = [MkStrategyWrapper (init g sgen cmd :: Strat0),
  47. -- MkStrategyWrapper (init g sgen cmd :: Strat1),
  48. -- MkStrategyWrapper (init g sgen cmd :: Strat2)]
  49. main :: IO ()
  50. main = do initTime <- secTime
  51. args <- getArgs
  52. opt <- parseArgs args
  53. let files = optFile opt
  54. let maxTime = optTime opt
  55. let maxMem = optMem opt
  56. let powerPhrase = optPowerPhrase opt
  57. let logFile = optLog opt
  58. rng <- getStdGen
  59. initialData <- createComputationsFromFiles files rng powerPhrase
  60. let (_, _,gameComputations) = unzip3 initialData
  61. commandResults <- iterateGame gameComputations (timeStruct maxTime initTime) maxMem
  62. let stringResults = map (\(cmds,score,algoIdx) -> (cmdToString cmds,score,algoIdx)) commandResults
  63. let outJSONstructs = zipWith jsonBuilder initialData stringResults
  64. BS.putStrLn $ encode outJSONstructs
  65. writeLogFile logFile (zipWith logFileBuilder initialData stringResults)
  66. where
  67. timeStruct Nothing _ = Nothing
  68. timeStruct (Just stopTime) initialTime = Just (fromIntegral stopTime,fromIntegral initialTime)
  69. jsonBuilder (idx, seed, _) (strCmds, _, _) = (JSONSer idx seed strategyTag strCmds)
  70. logFileBuilder (idx, seed, _) (_ ,score , algoIdx) = (idx, seed, score, algoIdx)
  71. createComputationsFromFiles :: [String] -> StdGen -> Maybe String -> IO [(Id,Seed,GameComputation)]
  72. createComputationsFromFiles fileNames randomGen powerPhrase = do inputs <- readFiles fileNames
  73. let igames = map readInput inputs
  74. let cstruct = compstruct igames
  75. return (gcstruct cstruct)
  76. where
  77. compstruct ig = concat (map genf ig)
  78. genf (i,g) = zipWith (\x (y,z) -> (x,y,z)) (replicate (length g) i) g
  79. gcstruct cst = map (\(x,y,z) -> (x,y,j z)) cst
  80. where
  81. j game = strategies game randomGen (powerCommands powerPhrase)
  82. powerCommands Nothing = Nothing
  83. powerCommands (Just a) = Just (mapMaybe charToCommand a)
  84. readFiles :: [String] -> IO [BS.ByteString]
  85. readFiles [] = return []
  86. readFiles (x:xs) = do f <- BS.readFile x
  87. fs <- readFiles xs
  88. return (f:fs)
  89. instance NFData Command where rnf x = seq x ()
  90. iterateGame :: [GameComputation] -> Maybe (Double,Double) -> Maybe Int -> IO [FinishedGame]
  91. iterateGame gameComputations timeLimitData memLimitData = do alive <- checkComputationAlive
  92. if alive
  93. then nextPass
  94. else return bestGames
  95. where
  96. nextPass = (bestGames `deepseq` (iterateGame nextGameComputations timeLimitData memLimitData))
  97. nextGameComputations = (applyNtimes computationsPerStep advanceGameComputations gameComputations)
  98. checkComputationAlive = do timeLimitFlag <- timeLimit timeLimitData
  99. memLimitFlag <- memLimit memLimitData
  100. let finishedComputation = (and $ map finishedGameComputation gameComputations)
  101. return $ not (timeLimitFlag || memLimitFlag || finishedComputation)
  102. advanceGameComputations computations = map advanceGameComputation computations
  103. bestGames = map getBestGameComputation gameComputations
  104. timeLimit :: Maybe (Double, Double) -> IO Bool
  105. timeLimit Nothing = return False
  106. timeLimit (Just (initialTime,stopTime)) = do actualTime <- secTime
  107. let actualTimeD = fromIntegral actualTime
  108. let timeDifference = (actualTimeD - initialTime)
  109. return (stopTime <= timeDifference)
  110. memLimit :: Maybe Int -> IO Bool
  111. memLimit _ = return False
  112. secTime :: IO Int64
  113. secTime = do (TimeSpec s _) <- getTime Monotonic
  114. return s
  115. writeLogFile :: Bool -> [(Int,Int,Int,Int)] -> IO ()
  116. writeLogFile False _ = return ()
  117. writeLogFile _ els = writeFile logFileName scoredata
  118. where
  119. scoredata = foldl strlog "\n" els
  120. strlog x (a,b,c,d) = sa ++ sb ++ sc ++ sd ++ x ++ "\n\n"
  121. where
  122. sa = (show a) ++ " "
  123. sb = (show b) ++ " "
  124. sc = (show c) ++ " "
  125. sd = (show d) ++ " "
  126. applyNtimes :: Int -> (a -> a) -> a -> a
  127. applyNtimes 0 _ accum = accum
  128. applyNtimes n f accum = applyNtimes (n - 1) f (f accum)