示例#1
0
        public void LongVTest()
        {
            var problem = new Problem { width = 6 };
            var unit = new Unit
            {
                members = new HashSet<Cell>(new[]
                                      {
                                          new Cell { x = 0, y = 2 },
                                          new Cell { x = 0, y = 3 },
                                          new Cell { x = 1, y = 4 },
                                          new Cell { x = 1, y = 3 },
                                          new Cell { x = 2, y = 2 }
                                      }),
                pivot = new Cell { x = 1, y = 0 }
            };

            var placedUnit = Game.SpawnUnit(unit, problem);

            var members = placedUnit.members.ToArray();
            Assert.AreEqual(new Cell { x = 1, y = 0 }, members[0]);
            Assert.AreEqual(new Cell { x = 1, y = 1 }, members[1]);
            Assert.AreEqual(new Cell { x = 2, y = 2 }, members[2]);
            Assert.AreEqual(new Cell { x = 2, y = 1 }, members[3]);
            Assert.AreEqual(new Cell { x = 3, y = 0 }, members[4]);
            Assert.AreEqual(new Cell { x = 2, y = -2 }, placedUnit.pivot);
        }
示例#2
0
 public string Solve(Problem problem, int seed, string[] magicSpells)
 {
     var game = new ConsoleGame(problem, seed, magicSpells);
     var emulator = new Emulator(game, -1);
     emulator.Run();
     var solution = game.Solution;
     return solution;
 }
示例#3
0
        public void SmallBacteriaTest()
        {
            var problem = new Problem { width = 10 };
            var unit = new Unit { members = new HashSet<Cell>(new[] { new Cell { x = 1, y = 1 } }), pivot = new Cell { x = 2, y = 2 } };

            var placedUnit = Game.SpawnUnit(unit, problem);
            Assert.AreEqual(new Cell { x = 4, y = 0 }, placedUnit.members.Single());
            Assert.AreEqual(new Cell { x = 4, y = 1 }, placedUnit.pivot);
        }
示例#4
0
        public void SinglePointTest_5()
        {
            var problem = new Problem { width = 5 };
            var unit = new Unit { members = new HashSet<Cell>(new[] { new Cell { x = 5, y = 5 } }), pivot = new Cell { x = 5, y = 5 } };

            var placedUnit = Game.SpawnUnit(unit, problem);

            Assert.AreEqual(new Cell { x = 2, y = 0 }, placedUnit.members.Single());
            Assert.AreEqual(new Cell { x = 2, y = 0 }, placedUnit.pivot);
        }
示例#5
0
        public static IProblemSolver SelectSolver(Problem problem)
        {
            //return new MuggleProblemSolver_MultiUnit(1);
            //return new AStarProblemSolver();
            return new MuggleProblemSolver_MultiUnit(1);

            if(square <= 100)
                return new MuggleProblemSolver_MultiUnit(2, 4);
            if(square < 3000)
                return new MuggleProblemSolver_MultiUnit(1);
            return new MuggleProblemSolver_MultiUnit(0);*/
        }
示例#6
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;
        }
		public string Solve(Problem problem, int seed, string[] powerPhrases)
		{
			var finalPowerPhraseBuilder = new SimplePowerPhraseBuilder(powerPhrases);
			var spelledPhrases = new bool[powerPhrases.Length];
			var solution = new List<MoveType>();
			var game = new SolverGame(problem, seed, powerPhrases);
			while (true)
			{
				switch (game.state)
				{
					case GameBase.State.WaitUnit:
						game.Step();
						break;
					case GameBase.State.UnitInGame:
						var reachablePositions = new ReachablePositionsWithWords(game.map, powerPhrases, spelledPhrases);
						var evaluatePositions = new EvaluatePositions2(game.map);
						var endPositions = reachablePositions.EndPositions(game.currentUnit);
						var estimated = new Dictionary<Unit, double>();
						var bestPosition = endPositions.ArgMax(p =>
						{
							double value;
							if (estimated.TryGetValue(p.Item1, out value))
								return value;
							return estimated[p.Item1] = evaluatePositions.Evaluate(p.Item1);
						});
						var score = evaluatePositions.Evaluate(bestPosition.Item1);
						var wayToBestPosition = bestPosition.Item2;
						var unitSolution = staticPowerPhraseBuilder.Build(wayToBestPosition.path);
						SolutionAdded(game, unitSolution);
						game.ApplyUnitSolution(unitSolution);
						spelledPhrases = wayToBestPosition.spelledWords;
						solution.AddRange(wayToBestPosition.path);
						break;
					case GameBase.State.EndInvalidCommand:
					case GameBase.State.EndPositionRepeated:
						throw new InvalidOperationException(string.Format("Invalid state: {0}", game.state));
					case GameBase.State.End:
						return finalPowerPhraseBuilder.Build(solution);
					default:
						throw new ArgumentOutOfRangeException();
				}
			}
		}
        public override string Solve(Problem problem, int seed, string[] powerPhrases)
        {
            var finalPowerPhraseBuilder = new SimplePowerPhraseBuilder(powerPhrases);
            var spelledPhrases = new bool[powerPhrases.Length];
            var solution = new List<MoveType>();
            var game = new SolverGame(problem, seed, powerPhrases);
            while (true)
            {
                switch (game.state)
                {
                    case GameBase.State.WaitUnit:
                        game.Step();
                        break;
                    case GameBase.State.UnitInGame:
                        if (nextUnitsPositions == null || !nextUnitsPositions.Any())
                        {
                            var bestPositions = FindBestPositions_Recursive(unitsAhead, game.map.Clone(),
                                new[] { game.currentUnit }.Concat(game.GetAllRestUnits()).ToArray(), 0, powerPhrases, spelledPhrases);
                            nextUnitsPositions = bestPositions.Item2.ToList();
                        }
                        var wayToBestPosition = nextUnitsPositions.First();
                        nextUnitsPositions = nextUnitsPositions.Skip(1).ToList();

                        var unitSolution = staticPowerPhraseBuilder.Build(wayToBestPosition.Item2.path);
                        CallEvent(game, unitSolution);
                        game.ApplyUnitSolution(unitSolution);
                        solution.AddRange(wayToBestPosition.Item2.path);
                        spelledPhrases = wayToBestPosition.Item2.spelledWords;
                        break;
                    case GameBase.State.EndInvalidCommand:
                    case GameBase.State.EndPositionRepeated:
                        throw new InvalidOperationException(string.Format("Invalid state: {0}", game.state));
                    case GameBase.State.End:
                        return finalPowerPhraseBuilder.Build(solution);
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
        }
示例#9
0
 public GameBase([NotNull] Problem problem, int seed, string[] knownMagicSpells)
 {
     this.problem = problem;
     this.seed = seed;
     this.knownMagicSpells = knownMagicSpells.Select(s => s.ToLower()).ToArray();
     ReStart();
 }
示例#10
0
 public GameBase([NotNull] Problem problem, int seed, string[] knownMagicSpells)
 {
     this.problem = problem;
     this.seed = seed;
     this.knownMagicSpells = knownMagicSpells.Select(s => s.ToLower()).ToArray();
     units = problem.units;
     UnitIndeces = new int[problem.sourceLength];
     var randomGenerator = new LinearCongruentalGenerator(seed);
     for (int i = 0; i < problem.sourceLength; i++)
         UnitIndeces[i] = randomGenerator.GetNext() % units.Length;
     ReStart();
 }
示例#11
0
 public ConsoleGame(Problem problem, int seed, string[] magicSpells)
     : base(problem, seed, magicSpells)
 {
 }
示例#12
0
 public string Solve(Problem problem, int seed, string[] magicSpells)
 {
     return magicSpells[0];
 }