Esempio n. 1
0
        public Result CountScore(Problem problem, Func<IProblemSolver> solverFactory, string[] powerPhrases, string folder)
        {
            long sum = 0;
            var lockerForLists = new object();
            List<Output> outputs = new List<Output>();
            List<GameBase.State> states = new List<GameBase.State>();
            List<int> scores = new List<int>();

            Parallel.For(0, problem.sourceSeeds.Length, new ParallelOptions() { MaxDegreeOfParallelism = Math.Max(1, Environment.ProcessorCount - 2) }, seedInd =>
            {
                var solver = solverFactory();
                var seed = problem.sourceSeeds[seedInd];
                try
                {
                    Console.WriteLine("Problem {0}: seed {1} started counting", problem.id, seed);
                    var stopwatch = Stopwatch.StartNew();
                    var solution = solver.Solve(problem, seed, powerPhrases);
                    stopwatch.Stop();
                    var output = new Output() { problemId = problem.id, seed = seed, solution = solution };
                    var game = new Game(problem, output, powerPhrases);
                    while (game.state == GameBase.State.UnitInGame || game.state == GameBase.State.WaitUnit)
                    {
                        game.Step();
                    }
                    lock (lockerForLists)
                    {
                        scores.Add(game.CurrentScore);
                        states.Add(game.state);
                        outputs.Add(output);
                    }
                    Console.WriteLine("Problem {0}: seed: {1}, score: {2}, time: {3}", problem.id, seed, game.CurrentScore,
                        stopwatch.Elapsed);
                    SaveOutput(folder + problem.id, output);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Problem {0}: seed {1} crashed {2}", problem.id, seed, ex.Message);
                    throw;
                }
            });
            var result = new Result { Outputs = outputs.ToArray(), EndStates = states.ToArray(), Scores = scores.ToArray() };
            SaveResult(folder + problem.id, result);
            return result;
        }
Esempio n. 2
0
 public void RecountScores()
 {
     var groupBy = GetSolves();
     var ungroup = groupBy.SelectMany(g => g);
     var groupByProblemAndName = ungroup.GroupBy(g => g.Item1 + "<->" + g.Item2);
     foreach (var g in groupByProblemAndName)
     {
         var problFolder = Path.Combine(idealSolvesFolder, g.First().Item1, g.First().Item2);
         var cb = new StringBuilder();
         var sum = 0;
         foreach (var seedTuple in g)
         {
             var seed = seedTuple.Item3;
             var solution = File.ReadAllText(problFolder + @"\" + seed).ParseAsJson<Output>().solution;
             var game = new Game(ProblemsSet.GetProblem(int.Parse(seedTuple.Item2)), new Output() { solution = solution, seed = int.Parse(seed) }, PowerDatas.GetPowerPhrases());
             while (game.state == GameBase.State.UnitInGame || game.state == GameBase.State.WaitUnit)
                 game.Step();
             var resultingScoring = game.CurrentScore;
             sum += game.CurrentScore;
             cb.AppendLine(seed + " " + resultingScoring);
         }
         var result = cb.ToString();
         File.WriteAllText(problFolder + @"\" + "score" + (sum / g.Count()) + ".txt", result);
     }
 }
Esempio n. 3
0
        public static void Solve(int problemnum, int seed, string[] magicSpells, int delay, bool visualize)
        {
            var problem = ProblemsSet.GetProblem(problemnum);
            //			var solver = new MuggleProblemSolver();
            var solver = SelectSolver(problem);

            var stopwatch = Stopwatch.StartNew();
            var solution = solver.Solve(problem, problem.sourceSeeds[seed], magicSpells);
            stopwatch.Stop();

            var game = new Game(problem, new Output { seed = problem.sourceSeeds[seed], solution = solution }, magicSpells);
            if (visualize)
            {
                var emulator = new Emulator(game, delay);
                emulator.Run();
            }
            else
            {
                while (game.state == GameBase.State.UnitInGame || game.state == GameBase.State.WaitUnit)
                {
                    game.Step();
                }
                Console.WriteLine("Score=" + game.CurrentScore);
                Console.WriteLine("Time=" + stopwatch.Elapsed);
                Console.ReadKey();
            }
        }