Exemple #1
0
        public static BitSolution generateBestRandomSolution(BitState g, int playerId, int depth, int timeToRun)
        {
            BitSolution best     = null;
            double      maxScore = double.NegativeInfinity;
            int         count    = 0;

            var timer = new Stopwatch();

            timer.Start();
            while (timer.ElapsedMilliseconds <= timeToRun)
            {
                //var time = timer.ElapsedMilliseconds;
                //Console.Error.WriteLine("Time elapsed: {0} ms", time);
                var solution = randomSolution(g.Clone(), playerId, depth);
                if (solution.score > maxScore)
                {
                    maxScore = solution.score;
                    best     = solution;
                }
                count++;
                //time = timer.ElapsedMilliseconds;
                //Console.Error.WriteLine("Time elapsed: {0} ms", time);
            }
            timer.Stop();

            Console.Error.WriteLine("{0} solutions considered", count);

            return(best);
        }
Exemple #2
0
        public static BitSolution bfs(BitState g, int playerId, int timeToRun)
        {
            var timer = new Stopwatch();

            timer.Start();

            var open   = new List <Node>();
            var closed = new List <BitSolution>();

            open.Add(new Node(new BitSolution(), null, g.Clone()));
            while (timer.ElapsedMilliseconds <= timeToRun && open.Count > 0)
            {
                var sol = open[0].solution;
                var gs  = open[0].gameState;

                if (open[0].parent != null)
                {
                    closed.Remove(open[0].parent);
                }

                open.RemoveAt(0);

                foreach (Move m in gs.getMoves(playerId))
                {
                    var solClone = sol.Clone();
                    var gsClone  = gs.Clone();
                    solClone.moves.Add(m);
                    gsClone.play(m, playerId);

                    if (gsClone.getBot(playerId).isAlive)
                    {
                        open.Add(new Node(solClone, sol, gsClone));
                    }
                }

                closed.Add(sol);
            }
            timer.Stop();

            BitSolution best     = null;
            double      maxScore = double.NegativeInfinity;

            foreach (var s in closed)
            {
                if (s.score > maxScore)
                {
                    maxScore = s.score;
                    best     = s;
                }
            }

            return(best);
        }