private static void Main()
        {
            var evaluator   = new StateEvaluator();
            var fogRevealer = new SimpleFogRevealer();
            var ai          = new GreedyAi(evaluator);
            var reader      = new StateReader(Console.ReadLine);
            var initData    = reader.ReadInitialization();
            var isFirst     = true;

            while (true)
            {
                var state     = reader.ReadState(initData);
                var countdown = new Countdown(isFirst ? 95 : 45);
                isFirst = false;
                fogRevealer.ConsiderStateBeforeMove(state, 20);
                // ReSharper disable once RedundantAssignment
                var actions = reader.ReadActions();
                EnsureActionsAreSame(state.GetPossibleActions(), actions);
                var action = ai.GetAction(state, countdown);
                WriteOutput(action);
                Console.Error.WriteLine(countdown);
                fogRevealer.RegisterAction(action);
                action.ApplyTo(state);
            }
            // ReSharper disable once FunctionNeverReturns
        }
Exemple #2
0
        public void RunCase(string input)
        {
            var state  = StateReader.Read(input);
            var ai     = new AlphabetaAi(evaluator);
            var action = ai.GetAction(state, 45);

            Console.WriteLine(action);
        }
        public void Debug()
        {
            var stateInput = "...1...|..344..|.44433.|2444443|.41421.|..113..|...0...|5 2|0 3|2 1|3 5|";
            var state      = StateReader.Read(stateInput);
            var action     = new AlphaBetaAi(evaluator, 1).GetAction(state, 100);

            Assert.That(action.ToString(), Is.EqualTo("MOVE&BUILD 0 SE SW"));
        }
        public void AssignValueToLastSearchTreeSizeProperty_DuringSearch(int depth, int expectedSize)
        {
            var state = StateReader.Read("002|022|200|0 0|1 0|-1 -1|-1 -1|");
            var ai    = new AlphaBetaAi(evaluator, depth);

            ai.GetAction(state, 1000);
            ai.LastSearchTreeSize.Should().Be(expectedSize);
        }
        public void AssignValueToLastDepthFullySearchedProperty_DuringSearch(int depth)
        {
            var state = StateReader.Read("00000|00000|00000|00000|00000|1 1|2 3|-1 -1|-1 -1|");
            var ai    = new AlphaBetaAi(evaluator, depth);

            ai.GetAction(state, 1000);
            ai.LastDepthFullySearched.Should().Be(depth);
        }
        public void MeasureSearchTreeSize()
        {
            var stateInput = "...2...|..303..|.13033.|0010031|.01412.|..121..|...1...|3 5|5 2|2 2|5 4|";
            var state      = StateReader.Read(stateInput);

            new AlphaBetaAi(evaluator).GetAction(state, 1); // heat up
            var ai = new AlphaBetaAi(evaluator, 3);

            ai.GetAction(state, 3000); // measure
            Console.WriteLine($"SearchTreeSize: {ai.LastSearchTreeSize}");
        }
Exemple #7
0
        public void CalculateNextMove()
        {
            var state = new StateReader("...0...|..000..|.00000.|0000000|.00000.|..000..|...0...|3 2|5 2|-1 -1|-1 -1|")
                        .ReadState(new InitializationData(7, 2));
            var            evaluator = new StateEvaluator();
            var            action    = new GreedyAi(evaluator).GetAction(state, 100);
            ExplainedScore score     = null;

            using (new MoveAndBuildAction(0, Direction.N, Direction.N).ApplyTo(state))
                score = evaluator.Evaluate(state, 0);
            using (action.ApplyTo(state))
                evaluator.Evaluate(state, 0).Should().BeGreaterOrEqualTo(score);
        }
Exemple #8
0
        public void AbWithDepth1_IsSameAsGreedy(string input)
        {
            var state            = StateReader.Read(input);
            var initialStateDump = state.ToString();

            var greedyAction = new GreedyAi(evaluator).GetAction(state, 100);
            var abAction     = new AlphabetaAi(evaluator, 1).GetAction(state, 100);

            Console.WriteLine(state.ToString());
            Console.WriteLine(abAction);

            state.ToString().Should().Be(initialStateDump, "ai.GetAction should not change state");
            abAction.ToString().Should().Be(greedyAction.ToString(), "ab-ai with depth=1 should be exactly gready ai");
        }
Exemple #9
0
        private static void Main()
        {
            var reader   = new StateReader(Console.ReadLine);
            var initData = reader.ReadInitialization();

            while (true)
            {
                var state   = reader.ReadState(initData);
                var actions = reader.ReadActions();
                var action  = actions.LastOrDefault();
                Console.WriteLine(action ?? "ACCEPT-DEFEAT");
            }
            // ReSharper disable once FunctionNeverReturns
        }
        public void BeEquivalentToGreedy_WhenSearchDepthIs1(string input)
        {
            var state            = StateReader.Read(input);
            var initialStateDump = state.ToString();

            var greedyAction = new GreedyAi(evaluator).GetAction(state, 100);
            var abAction     = new AlphaBetaAi(evaluator, 1).GetAction(state, 100);

            Console.WriteLine(state.ToString());
            Console.WriteLine($"[{abAction}]");

            state.ToString().Should().Be(initialStateDump, "ai.GetAction should not change state");
            abAction.ToString().Should().Be(greedyAction.ToString(), "ab-ai with depth=1 should be exactly gready ai");
        }
Exemple #11
0
        public void PerformanceTest()
        {
            var state = new StateReader("...0...|..000..|.00000.|0000000|.00010.|..000..|...1...|3 5|5 4|3 4|1 1|")
                        .ReadState(new InitializationData(7, 2));

            for (var j = 0; j < 10; j++)
            {
                var alphabetaAi = new AlphabetaAi(evaluator);
                for (var i = 0; i < 10; i++)
                {
                    var action = alphabetaAi.GetAction(state, 100);
                    action.ApplyTo(state);
                    state.ChangeCurrentPlayer();
                    //Console.WriteLine(state);
                    Console.WriteLine(action);
                }
            }
        }
        public void MeasurePerformance()
        {
            var state = new StateReader("...0...|..000..|.00000.|0000000|.00010.|..000..|...1...|3 5|5 4|3 4|1 1|")
                        .ReadState(new InitializationData(7, 2));
            var alphabetaAi = new AlphaBetaAi(evaluator)
            {
                LoggingEnabled = false
            };
            var totalNodesProcessed = 0;
            var totalDepths         = 0;
            var count = 50;

            for (var turn = 0; turn < count; turn++)
            {
                var action = alphabetaAi.GetAction(state, turn == 0 ? 100 : 50);
                action.ApplyTo(state);
                state.ChangeCurrentPlayer();
                totalNodesProcessed += alphabetaAi.LastSearchTreeSize;
                totalDepths         += alphabetaAi.LastDepthFullySearched;
            }
            Console.WriteLine($"Total Nodes Searched: {totalNodesProcessed}");
            Console.WriteLine($"Average search depth: {totalDepths / (double)count}");
        }
Exemple #13
0
        public void TreeSizeMeasurement()
        {
            var totalSize = new StatValue();

            foreach (var stateInput in GetStatesCollection())
            {
                var localSize = new StatValue();
                var state     = StateReader.Read(stateInput);
                var ai        = new AlphabetaAi(evaluator, 3)
                {
                    Logging = false
                };
                for (var i = 0; i < 1; i++)
                {
                    var action = ai.GetAction(state, 250);
                    localSize.Add(ai.LastSearchTreeSize);
                    action.ApplyTo(state);
                }
                Console.WriteLine(localSize.ToDetailedString());
                totalSize.AddAll(localSize);
            }
            Console.WriteLine("Total:");
            Console.WriteLine(totalSize.ToDetailedString());
        }