public static Solution Solve(Problem problem)
        {
            var solution = new Solution(problem.NumberOfCars);
            var counter  = 0;
            IState <MakeRideAction> state = new CityState(problem.Cars.ToImmutableList(), new RidesView3(problem.Rides, problem.Bonus), 0);
            var node = MonteCarloTreeSearch <MakeRideAction> .Create(state);

            while ((node = MonteCarloTreeSearch <MakeRideAction> .GetTopActions(node, 1000, long.MaxValue).FirstOrDefault()) != null)
            {
                node.Parent = null;
                if (!node.Action.Car.Equals(Car.SkipRide))
                {
                    solution.CarActions[node.Action.Car.Id].Add(node.Action);
                }

                //Trace.WriteLine("");
                //Trace.WriteLine($"SELECTED ACTION {node.Action}");
                //Trace.WriteLine("");
                counter++;
                if (counter % 100 == 0)
                {
                    Trace.WriteLine(counter);
                    Console.WriteLine(counter);
                }
            }

            return(solution);
        }
        public static Solution Solve(Problem problem)
        {
            var solution = new Solution(problem.NumberOfCars);
            var counter  = 0;
            var rides    = new HashSet <Ride>(problem.Rides);

            foreach (var car in problem.Cars)
            {
                INode <MakeRideAction> node;
                var state = new CityCarState(problem, car, new HashSet <Ride>(rides), 0);
                while ((node = MonteCarloTreeSearch <MakeRideAction> .GetTopActions(state, 100, 100).FirstOrDefault()) != null)
                {
                    state.ApplyAction(node.Action);
                    solution.CarActions[node.Action.Car.Id].Add(node.Action);
                    rides.Remove(node.Action.Ride);
                    //Trace.WriteLine($"{node.Action}");
                    counter++;
                    if (counter % 100 == 0)
                    {
                        Trace.WriteLine("+");
                        Console.WriteLine("+");
                    }
                }
            }

            return(solution);
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            var game = new ConnectFourState();

            while (game.Actions.Any())
            {
                Console.WriteLine($"CurrentPlayer: {game.CurrentPlayer}");
                Console.WriteLine(game);
                Console.WriteLine("0123456");
                Console.WriteLine(SEPARATOR);

                var position = -1;
                while (position < 0 || position > 6)
                {
                    Console.WriteLine("Choose a free space: (0-6)");

                    var input = Console.ReadKey();
                    int.TryParse(input.KeyChar.ToString(), out position);
                }

                Console.WriteLine();
                game.ApplyAction(new ConnectFourAction(position));
                var computer = MonteCarloTreeSearch.GetTopActions(game, 50000, 1000).ToList();

                Console.WriteLine(SEPARATOR);
                if (computer.Count > 0)
                {
                    Console.WriteLine("Computer's ranked plays:");
                    foreach (var a in computer)
                    {
                        Console.WriteLine($"\t{a.Action}\t{a.NumWins}/{a.NumRuns} ({a.NumWins / a.NumRuns})");
                    }
                    game.ApplyAction(computer[0].Action);
                }

                position = -1;
            }

            Console.WriteLine(SEPARATOR);
            Console.WriteLine(game.ToString());
            Console.WriteLine("Game Over");
            Console.ReadKey();
        }