From 56f3f25099253cf4a1d4b9118160b2d1f52cf5c3 Mon Sep 17 00:00:00 2001 From: Andrea Bellandi Date: Mon, 10 Aug 2015 10:18:35 +0200 Subject: [PATCH] sistemate flag e pphrases --- icfp2015.cabal | 2 +- src/Datatypes/Game.hs | 2 +- src/Main.hs | 27 ++++++++++++--------------- src/Opt.hs | 6 +++--- src/Strategy0.hs | 2 +- src/StrategyManager.hs | 2 +- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/icfp2015.cabal b/icfp2015.cabal index 175bb83..0b45249 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, deepseq >= 1.3 && <1.4 + 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, pqueue >=1.3 && <1.4, clock, random, deepseq >= 1.3 && <1.4 -- Directories containing source files. hs-source-dirs: src diff --git a/src/Datatypes/Game.hs b/src/Datatypes/Game.hs index 2999a41..681ac63 100644 --- a/src/Datatypes/Game.hs +++ b/src/Datatypes/Game.hs @@ -1,4 +1,4 @@ -module Datatypes.Game (Game(..), Command(..), isCompleted, new, notifyCommand, powerCounterToScore, powerPhrasesAsCommands, commandsToString) where -- FIXME exports +module Datatypes.Game (Game(..), Command(..), isCompleted, new, notifyCommand, powerCounterToScore, powerPhrasesAsCommands, commandsToString,stringToCommands) where -- FIXME exports import Data.Hashable (hash) import qualified Data.List as List diff --git a/src/Main.hs b/src/Main.hs index f2cffac..a209ea1 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -13,15 +13,14 @@ import System.Random import System.Clock import GHC.Generics import Data.Aeson -import Data.Maybe import StrategyManager import Strategy0 -import Datatypes.Game(Game,Command,commandsToString) -import VM +import Datatypes.Game(Game,Command,commandsToString,stringToCommands) +--import VM import Opt import JSONDeser(readInput) -import PowerPhrases + strategyTag :: String strategyTag = "lilik0" @@ -52,7 +51,7 @@ type Id = Int type Seed = Int -strategies :: Game -> StdGen -> Maybe [Command] -> GameComputation +strategies :: Game -> StdGen -> [[Command]] -> GameComputation strategies g sgen cmd = [MkStrategyWrapper (initst g sgen cmd :: Strategy0)] -- = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1), @@ -70,10 +69,10 @@ main = do initTime <- secTime let files = optFile opt let maxTime = optTime opt let maxMem = optMem opt - let powerPhrase = optPowerPhrase opt + let powerPhrases = optPowerPhrase opt let logFile = optLog opt rng <- getStdGen - initialData <- createComputationsFromFiles files rng powerPhrase + initialData <- createComputationsFromFiles files rng powerPhrases let (_, _,gameComputations) = unzip3 initialData commandResults <- iterateGame gameComputations (timeStruct maxTime initTime) maxMem let stringResults = map (\(cmds,score,algoIdx) -> (commandsToString cmds,score,algoIdx)) commandResults @@ -89,19 +88,17 @@ main = do initTime <- secTime jsonBuilder (idx, seed, _) (strCmds, _, _) = (JSONSer idx seed strategyTag strCmds) logFileBuilder (idx, seed, _) (_ ,score , algoIdx) = (idx, seed, score, algoIdx) -createComputationsFromFiles :: [String] -> StdGen -> Maybe String -> IO [(Id,Seed,GameComputation)] -createComputationsFromFiles fileNames randomGen powerPhrase = do inputs <- readFiles fileNames - let igames = map readInput inputs - let cstruct = compstruct igames - return (gcstruct cstruct) +createComputationsFromFiles :: [String] -> StdGen -> [String] -> IO [(Id,Seed,GameComputation)] +createComputationsFromFiles fileNames randomGen powerPhrases = do inputs <- readFiles fileNames + let igames = map readInput inputs + let cstruct = compstruct igames + return (gcstruct cstruct) where compstruct ig = concat (map genf ig) genf (i,g) = zipWith (\x (y,z) -> (x,y,z)) (replicate (length g) i) g gcstruct cst = map (\(x,y,z) -> (x,y,j z)) cst where - j game = strategies game randomGen (powerCommands powerPhrase) - powerCommands Nothing = Nothing - powerCommands (Just a) = Just (mapMaybe charToCommand a) + j game = strategies game randomGen (map stringToCommands powerPhrases) readFiles :: [String] -> IO [BS.ByteString] readFiles [] = return [] diff --git a/src/Opt.hs b/src/Opt.hs index a17986c..f6ca4de 100644 --- a/src/Opt.hs +++ b/src/Opt.hs @@ -14,7 +14,7 @@ data Flag = File String data Options = Options { optFile :: [String], optTime :: Maybe Int, optMem :: Maybe Int, - optPowerPhrase :: Maybe String, + optPowerPhrase :: [String], optSeedNumber :: Int, optCores :: Int, optLog :: Bool @@ -24,7 +24,7 @@ data Options = Options { optFile :: [String], startOptions = Options { optFile = [], optTime = Nothing, optMem = Nothing, - optPowerPhrase = Nothing, + optPowerPhrase = [], optSeedNumber = 0, optCores = 1, optLog = True @@ -48,7 +48,7 @@ options = [ Option "f" ["filename"] "Memory Limit in MB" , Option "p" ["phrasepower"] (ReqArg - (\arg opt -> return opt { optPowerPhrase = Just arg }) + (\arg opt -> return opt { optPowerPhrase = (arg:(optPowerPhrase opt)) }) "POWERPHRASE") "Power Phrase" , Option "n" ["seednumber"] diff --git a/src/Strategy0.hs b/src/Strategy0.hs index e52dde4..a81c02d 100644 --- a/src/Strategy0.hs +++ b/src/Strategy0.hs @@ -24,7 +24,7 @@ instance Strategy Strategy0 where advance = strategy0advance getbest = strategy0getbest -strategy0initst :: Game -> StdGen -> Maybe [Command] -> Strategy0 +strategy0initst :: Game -> StdGen -> [[Command]] -> Strategy0 strategy0initst game _ _ = (Strategy0 (firstQueue, firstList)) where firstQueue = PQ.singleton (fullScore game, -(length $ Game.units game), snd . Unit.pivot . head . Game.units $ game) game firstList = [] diff --git a/src/StrategyManager.hs b/src/StrategyManager.hs index cb8b3bb..e0703de 100644 --- a/src/StrategyManager.hs +++ b/src/StrategyManager.hs @@ -23,7 +23,7 @@ initWrapper :: Strategy a => a -> StrategyWrapper initWrapper = MkStrategyWrapper class Strategy a where - initst :: Game -> StdGen -> Maybe [Command] -> a + initst :: Game -> StdGen -> [[Command]] -> a advance :: a -> Either a ([Command], Int) getbest :: a -> ([Command], Int)