Example #1
0
        private void TestOnSampleMap(string moves, bool isSuccessful, int?timeUnits)
        {
            var solution = Emulator.MakeExtendedSolution(this.sampleMap, "test", CommandsSerializer.Parse(moves), string.Empty);

            Assert.Equal(isSuccessful, solution.IsSuccessful);
            Assert.Equal(timeUnits, solution.TimeUnits);
        }
Example #2
0
        public void CloningSampleSolutionIsValid()
        {
            var map      = MapParser.Parse("(0,0),(10,0),(10,10),(0,10)#(0,0)#(4,2),(6,2),(6,7),(4,7);(5,8),(6,8),(6,9),(5,9)#B(0,1);F(0,2);L(0,3);R(0,4);C(0,5);C(0,6);C(0,7);X(0,9)", string.Empty);
            var commands = CommandsSerializer.Parse("WWWWWWWWWCDDDDDDESSSSSS#CDDDDDDDDESSSSSSSS#CSSDDDESSSSS#ESSSSSSSSSQDDDDD");
            var solution = Emulator.MakeExtendedSolution(map, "test", commands, string.Empty);

            Assert.True(solution.IsSuccessful);
            Assert.Equal(28, solution.TimeUnits);
        }
        private static void TestSerialization(
            string serializedMoves,
            params Command[]
            [] commands)
        {
            var parsedMoves         = CommandsSerializer.Parse(serializedMoves);
            var serializedBackMoves = CommandsSerializer.Serialize(commands);

            Assert.Equal(commands, parsedMoves);

            Assert.Equal(serializedMoves, serializedBackMoves);
        }
Example #4
0
        public static void Main(string[] args)
        {
            const int Iterations = 1;

            for (var i = 0; i < Iterations; ++i)
            {
                var baseDir = args.Length > 0 ? args[0] : FindSolutionDir();
                Directory.SetCurrentDirectory(baseDir);

                var strategies = StrategyFactory.GenerateStrategies().ToArray();
                var packFile   = "Data/booster-pack.txt";
                var mapToPack  = File.ReadAllLines(packFile).Select(line => line.Split(' ')).ToDictionary(tokens => tokens[0], tokens => tokens[1]);

                var totalTimeUnits = 0;
                var haveFailures   = false;

                var outputLock = new object();

                Parallel.ForEach(
                    Directory.EnumerateFiles("Data/maps", "*.desc"),
                    new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                    mapFile =>
                {
                    var log     = new List <string>();
                    var mapName = Path.GetFileNameWithoutExtension(mapFile);

                    void Log(string msg)
                    {
                        if (LogImmediately)
                        {
                            Console.WriteLine($"{mapName}: {msg}");
                        }
                        else
                        {
                            log.Add(msg);
                        }
                    }

                    if (!mapToPack.ContainsKey(mapName))
                    {
                        return;
                    }

                    string packedBoosters = mapToPack.ContainsKey(mapName) ? mapToPack[mapName] : string.Empty;
                    Log($"Processing {mapName} with extra boosters: [{packedBoosters}]");
                    var map            = MapParser.Parse(File.ReadAllText(mapFile), packedBoosters);
                    var solutionSuffix = packedBoosters != string.Empty ? "-packed" : string.Empty;

                    var extSolutionPath = $"Data/extended-solutions{solutionSuffix}/{mapName}.ext-sol";

                    // Delete broken solutions
                    if (File.Exists(extSolutionPath))
                    {
                        var oldSolution = ExtendedSolution.Load(extSolutionPath);
                        var oldCommands = CommandsSerializer.Parse(oldSolution.Commands);
                        if (!Emulator.MakeExtendedSolution(map, string.Empty, oldCommands, packedBoosters).IsSuccessful)
                        {
                            File.Delete(extSolutionPath);
                        }
                    }

                    var rng = new Random();
                    var currentStrategies = StrategiesLimit != null
                            ? strategies.OrderBy(s => rng.Next()).Take(StrategiesLimit.Value).ToArray()
                            : strategies;

                    var solutions = currentStrategies.AsParallel()
                                    .Where(strategy => !(mapName.Contains("294") && strategy.Name.Contains("DumbBfs")))
                                    .Select(strategy => (strategy, Emulator.MakeExtendedSolution(map, strategy, packedBoosters)));

                    var numSuccessful = 0;
                    foreach (var pair in solutions)
                    {
                        var(strategy, solution) = pair;
                        solution.SaveIfBetter(extSolutionPath);
                        if (solution.IsSuccessful)
                        {
                            Log($"  {strategy.Name}: {solution.TimeUnits}");
                            if (StrategiesLimit != null && ++numSuccessful >= StrategiesLimit)
                            {
                                break;
                            }
                        }
                    }

                    var best = ExtendedSolution.Load(extSolutionPath);
                    File.WriteAllText($"Data/solutions{solutionSuffix}/{mapName}.sol", best.Commands);
                    if (solutionSuffix != string.Empty)
                    {
                        File.WriteAllText($"Data/solutions{solutionSuffix}/{mapName}.buy", packedBoosters);
                    }

                    Log($"  BEST ({best.StrategyName}): {best.IsSuccessful}/{best.TimeUnits}");

                    lock (outputLock)
                    {
                        if (best.TimeUnits.HasValue)
                        {
                            totalTimeUnits += best.TimeUnits.Value;
                        }
                        else
                        {
                            haveFailures = true;
                        }

                        foreach (var line in log)
                        {
                            Console.WriteLine(line);
                        }
                    }
                });