|
@ -7,7 +7,6 @@ module Main where |
|
|
|
|
|
|
|
|
import Control.DeepSeq (deepseq, NFData(..)) |
|
|
import Control.DeepSeq (deepseq, NFData(..)) |
|
|
import Data.Int |
|
|
import Data.Int |
|
|
import Data.List (zip4) |
|
|
|
|
|
import qualified Data.ByteString.Lazy.Char8 as BS |
|
|
import qualified Data.ByteString.Lazy.Char8 as BS |
|
|
import System.Environment |
|
|
import System.Environment |
|
|
import System.Random |
|
|
import System.Random |
|
@ -18,15 +17,12 @@ import Data.Maybe |
|
|
|
|
|
|
|
|
import StrategyManager |
|
|
import StrategyManager |
|
|
import Strategy0 |
|
|
import Strategy0 |
|
|
import Datatypes |
|
|
|
|
|
import Datatypes.Game |
|
|
|
|
|
|
|
|
import Datatypes.Game(Game,Command) |
|
|
import VM |
|
|
import VM |
|
|
import Opt |
|
|
import Opt |
|
|
import JSONDeser(readInput) |
|
|
import JSONDeser(readInput) |
|
|
import PowerPhrases |
|
|
import PowerPhrases |
|
|
|
|
|
|
|
|
import Debug.Trace (trace) |
|
|
|
|
|
|
|
|
|
|
|
strategyTag :: String |
|
|
strategyTag :: String |
|
|
strategyTag = "lilik0" |
|
|
strategyTag = "lilik0" |
|
|
|
|
|
|
|
@ -34,19 +30,19 @@ logFileName :: String |
|
|
logFileName = "scores" |
|
|
logFileName = "scores" |
|
|
|
|
|
|
|
|
timeLimitRatio :: Double |
|
|
timeLimitRatio :: Double |
|
|
timeLimitRatio = 1.0 |
|
|
|
|
|
|
|
|
timeLimitRatio = 0.96 |
|
|
|
|
|
|
|
|
memLimitRatio :: Double |
|
|
memLimitRatio :: Double |
|
|
memLimitRatio = 1.0 |
|
|
memLimitRatio = 1.0 |
|
|
|
|
|
|
|
|
gccompperstep :: Integer |
|
|
|
|
|
gccompperstep = 100 |
|
|
|
|
|
|
|
|
computationsPerStep :: Int |
|
|
|
|
|
computationsPerStep = 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data JSONSer = JSONSer { problemId :: Int, |
|
|
data JSONSer = JSONSer { problemId :: Int, |
|
|
seed :: Int, |
|
|
|
|
|
tag :: String, |
|
|
|
|
|
solution :: String |
|
|
|
|
|
|
|
|
problemSeed :: Int, |
|
|
|
|
|
problemTag :: String, |
|
|
|
|
|
problemSolution :: String |
|
|
} deriving (Show, Generic) |
|
|
} deriving (Show, Generic) |
|
|
|
|
|
|
|
|
instance FromJSON JSONSer |
|
|
instance FromJSON JSONSer |
|
@ -55,8 +51,9 @@ instance ToJSON JSONSer |
|
|
type Id = Int |
|
|
type Id = Int |
|
|
type Seed = Int |
|
|
type Seed = Int |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
strategies :: Game -> StdGen -> Maybe [Command] -> GameComputation |
|
|
strategies :: Game -> StdGen -> Maybe [Command] -> GameComputation |
|
|
strategies g sgen cmd = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1)] |
|
|
|
|
|
|
|
|
strategies g sgen cmd = [MkStrategyWrapper (initst g sgen cmd :: Strategy0)] |
|
|
|
|
|
|
|
|
-- = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1), |
|
|
-- = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1), |
|
|
-- MkStrategyWrapper (initst g sgen cmd :: NullStrategy2)] |
|
|
-- MkStrategyWrapper (initst g sgen cmd :: NullStrategy2)] |
|
@ -67,42 +64,44 @@ strategies g sgen cmd = [MkStrategyWrapper (initst g sgen cmd :: NullStrategy1)] |
|
|
-- MkStrategyWrapper (init g sgen cmd :: Strat2)] |
|
|
-- MkStrategyWrapper (init g sgen cmd :: Strat2)] |
|
|
|
|
|
|
|
|
main :: IO () |
|
|
main :: IO () |
|
|
main = do args <- getArgs |
|
|
|
|
|
|
|
|
main = do initTime <- secTime |
|
|
|
|
|
args <- getArgs |
|
|
opt <- parseArgs args |
|
|
opt <- parseArgs args |
|
|
let files = optFile opt |
|
|
|
|
|
let timelimit = optTime opt |
|
|
|
|
|
let memlimit = optMem opt |
|
|
|
|
|
let powerp = optPowerPhrase opt |
|
|
|
|
|
let cores = optCores opt |
|
|
|
|
|
let logf = optLog opt |
|
|
|
|
|
|
|
|
let files = optFile opt |
|
|
|
|
|
let maxTime = optTime opt |
|
|
|
|
|
let maxMem = optMem opt |
|
|
|
|
|
let powerPhrase = optPowerPhrase opt |
|
|
|
|
|
let logFile = optLog opt |
|
|
rng <- getStdGen |
|
|
rng <- getStdGen |
|
|
loaddata <- createComputationsFromFiles files rng powerp |
|
|
|
|
|
let (ids,seeds,gamecomputations) = unzip3 loaddata |
|
|
|
|
|
inittime <- secTime |
|
|
|
|
|
results <- iterategc gamecomputations (timestruct timelimit inittime) memlimit |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
initialData <- createComputationsFromFiles files rng powerPhrase |
|
|
|
|
|
let (_, _,gameComputations) = unzip3 initialData |
|
|
|
|
|
commandResults <- iterateGame gameComputations (timeStruct maxTime initTime) maxMem |
|
|
|
|
|
let stringResults = map (\(cmds,score,algoIdx) -> (cmdToString cmds,score,algoIdx)) commandResults |
|
|
|
|
|
let outJSONstructs = zipWith jsonBuilder initialData stringResults |
|
|
BS.putStrLn $ encode outJSONstructs |
|
|
BS.putStrLn $ encode outJSONstructs |
|
|
writeLogFile logf (zip4 ids seeds points strat) |
|
|
|
|
|
|
|
|
writeLogFile logFile (zipWith logFileBuilder initialData stringResults) |
|
|
where |
|
|
where |
|
|
timestruct Nothing _ = Nothing |
|
|
|
|
|
timestruct (Just maxtime) intime = Just (maxtime, intime) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timeStruct Nothing _ = Nothing |
|
|
|
|
|
timeStruct (Just stopTime) initialTime = Just (fromIntegral stopTime,fromIntegral initialTime) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 :: [String] -> StdGen -> Maybe String -> IO [(Id,Seed,GameComputation)] |
|
|
createComputationsFromFiles fns sg pp = do inputs <- readFiles fns |
|
|
|
|
|
let igames = map readInput inputs |
|
|
|
|
|
let cstruct = compstruct igames |
|
|
|
|
|
return (gcstruct cstruct) |
|
|
|
|
|
|
|
|
createComputationsFromFiles fileNames randomGen powerPhrase = do inputs <- readFiles fileNames |
|
|
|
|
|
let igames = map readInput inputs |
|
|
|
|
|
let cstruct = compstruct igames |
|
|
|
|
|
return (gcstruct cstruct) |
|
|
where |
|
|
where |
|
|
compstruct ig = concat (map genf ig) |
|
|
compstruct ig = concat (map genf ig) |
|
|
genf (i,g) = zipWith (\x (y,z) -> (x,y,z)) (replicate (length g) i) g |
|
|
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 |
|
|
|
|
|
|
|
|
gcstruct cst = map (\(x,y,z) -> (x,y,j z)) cst |
|
|
where |
|
|
where |
|
|
j z = strategies z sg (ppascommands pp) |
|
|
|
|
|
ppascommands Nothing = Nothing |
|
|
|
|
|
ppascommands (Just a) = Just (mapMaybe charToCommand a) |
|
|
|
|
|
|
|
|
j game = strategies game randomGen (powerCommands powerPhrase) |
|
|
|
|
|
powerCommands Nothing = Nothing |
|
|
|
|
|
powerCommands (Just a) = Just (mapMaybe charToCommand a) |
|
|
|
|
|
|
|
|
readFiles :: [String] -> IO [BS.ByteString] |
|
|
readFiles :: [String] -> IO [BS.ByteString] |
|
|
readFiles [] = return [] |
|
|
readFiles [] = return [] |
|
@ -112,28 +111,27 @@ readFiles (x:xs) = do f <- BS.readFile x |
|
|
|
|
|
|
|
|
instance NFData Command where rnf x = seq x () |
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
iterateGame :: [GameComputation] -> Maybe (Double,Double) -> Maybe Int -> IO [FinishedGame] |
|
|
|
|
|
iterateGame gameComputations timeLimitData memLimitData = do alive <- checkComputationAlive |
|
|
|
|
|
if alive |
|
|
|
|
|
then nextPass |
|
|
|
|
|
else return bestGames |
|
|
|
|
|
where |
|
|
|
|
|
nextPass = (bestGames `deepseq` (iterateGame nextGameComputations timeLimitData memLimitData)) |
|
|
|
|
|
nextGameComputations = (applyNtimes computationsPerStep advanceGameComputations gameComputations) |
|
|
|
|
|
checkComputationAlive = do timeLimitFlag <- timeLimit timeLimitData |
|
|
|
|
|
memLimitFlag <- memLimit memLimitData |
|
|
|
|
|
let finishedComputation = (and $ map finishedGameComputation gameComputations) |
|
|
|
|
|
return $ not (timeLimitFlag || memLimitFlag || finishedComputation) |
|
|
|
|
|
advanceGameComputations computations = map advanceGameComputation computations |
|
|
|
|
|
bestGames = map getBestGameComputation gameComputations |
|
|
|
|
|
|
|
|
|
|
|
timeLimit :: Maybe (Double, Double) -> IO Bool |
|
|
timeLimit Nothing = return False |
|
|
timeLimit Nothing = return False |
|
|
timeLimit (Just (itime,limit)) = do atime <- secTime |
|
|
|
|
|
let diff = (atime - (fromIntegral itime)) |
|
|
|
|
|
return (((fromIntegral limit) * timeLimitRatio) < (fromIntegral diff)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timeLimit (Just (initialTime,stopTime)) = do actualTime <- secTime |
|
|
|
|
|
let actualTimeD = fromIntegral actualTime |
|
|
|
|
|
let timeDifference = (actualTimeD - initialTime) |
|
|
|
|
|
return (stopTime <= timeDifference) |
|
|
|
|
|
|
|
|
memLimit :: Maybe Int -> IO Bool |
|
|
memLimit :: Maybe Int -> IO Bool |
|
|
memLimit _ = return False |
|
|
memLimit _ = return False |
|
@ -147,9 +145,14 @@ writeLogFile False _ = return () |
|
|
writeLogFile _ els = writeFile logFileName scoredata |
|
|
writeLogFile _ els = writeFile logFileName scoredata |
|
|
where |
|
|
where |
|
|
scoredata = foldl strlog "\n" els |
|
|
scoredata = foldl strlog "\n" els |
|
|
strlog x (a,b,c,d) = sa ++ sb ++ sc ++ sd ++ x ++ "\n" |
|
|
|
|
|
|
|
|
strlog x (a,b,c,d) = sa ++ sb ++ sc ++ sd ++ x ++ "\n\n" |
|
|
where |
|
|
where |
|
|
sa = (show a) ++ " " |
|
|
sa = (show a) ++ " " |
|
|
sb = (show b) ++ " " |
|
|
sb = (show b) ++ " " |
|
|
sc = (show c) ++ " " |
|
|
sc = (show c) ++ " " |
|
|
sd = (show d) ++ " " |
|
|
sd = (show d) ++ " " |
|
|
|
|
|
|
|
|
|
|
|
applyNtimes :: Int -> (a -> a) -> a -> a |
|
|
|
|
|
applyNtimes 0 _ accum = accum |
|
|
|
|
|
applyNtimes n f accum = applyNtimes (n - 1) f (f accum) |
|
|
|
|
|
|