예제 #1
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var tasks = new Task <Tuple <Turn, double> > [threadsCount];

            for (var i = 0; i < threadsCount; i++)
            {
                tasks[i] = Task.Run(()
                                    => SearchBestMove(rocket, new Random(random.Next()), iterationsCount / threadsCount));
            }
            for (var i = 0; i < threadsCount; i++)
            {
                tasks[i].Wait();
            }
            var max  = double.MinValue;
            var turn = Turn.None;

            for (var i = 0; i < threadsCount; i++)
            {
                if (tasks[i].Result.Item2 > max)
                {
                    max  = tasks[i].Result.Item2;
                    turn = tasks[i].Result.Item1;
                }
            }

            return(rocket.Move(turn, level));
        }
        public Rocket GetNextMove(Rocket rocket)
        {
            // TODO: распараллелить запуск SearchBestMove
            //var bestMove = SearchBestMove(rocket, new Random(random.Next()), iterationsCount);
            //var newRocket = rocket.Move(bestMove.Item1, level);
            //return newRocket;

            var moves = new List <Tuple <Turn, double> > ();
            var tasks = new Task[threadsCount];

            for (int i = 0; i < threadsCount; i++)
            {
                tasks[i] = new Task(() =>
                {
                    Random random = new Random();
                    moves.Add(SearchBestMove(rocket,
                                             random, iterationsCount / threadsCount));
                });
                tasks[i].Start();
            }
            Task.WaitAll(tasks);
            var bestMove = moves.OrderBy(x => x.Item2).First();

            return(rocket.Move(bestMove.Item1, level));
        }
예제 #3
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var max = 0.0;
            Tuple <Turn, double> bestMove = null;
            var tasks = new Task <Tuple <Turn, double> > [threadsCount];

            for (var i = 0; i < threadsCount; i++)
            {
                tasks[i] = new Task <Tuple <Turn, double> >(() => SearchBestMove(rocket, new Random(random.Next()),
                                                                                 iterationsCount / threadsCount));
                tasks[i].Start();
            }

            foreach (var t in tasks)
            {
                var move = t.Result;
                if (move.Item2 > max)
                {
                    max      = move.Item2;
                    bestMove = move;
                }
            }

            var newRocket = rocket.Move(bestMove.Item1, level);

            return(newRocket);
        }
예제 #4
0
 private double EstimateMoves(Rocket rocket, IEnumerable <Turn> moves)
 {
     foreach (var turn in moves)
     {
         rocket = rocket.Move(turn, level);
     }
     return(rocket.TakenCheckpointsCount + 1 / (GetDistanceToNextCheckpoint(rocket) + 1));
 }
예제 #5
0
        public Rocket GetNextMove(Rocket rocket)
        {
            // TODO: распараллелить запуск GetScoredBestMove
            var bestMove  = SearchBestMove(rocket, new Random(random.Next()), iterationsCount);
            var newRocket = rocket.Move(bestMove.Item1, level);

            return(newRocket);
        }
예제 #6
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var threads = new HashSet <Task <Tuple <Turn, double> > >();

            for (int i = 0; i < threadsCount; i++)
            {
                threads.Add(Task.Run(() =>
                                     SearchBestMove(rocket, new Random(random.Next()), iterationsCount / threadsCount)));
            }
            Task.WhenAll(threads);

            var bestMove  = threads.OrderBy(t => t.Result.Item2).FirstOrDefault().Result;
            var newRocket = rocket.Move(bestMove.Item1, level);

            return(newRocket);
        }
예제 #7
0
파일: GameForm.cs 프로젝트: eiioS/pi
        private bool MoveRocket()
        {
            if (manualControls.Any())
            {
                if (rocket.IsCompleted(level))
                {
                    return(false);
                }
                var control = manualControls.First();
                for (var i = 0; i < skipTurns; ++i)
                {
                    rocket = rocket.Move(control, level);
                    channel[rocket.Time] = rocket;
                }

                return(true);
            }

            return(RewindTo(rocket.Time + (manualRewindInProgress ? 0 : skipTurns)));
        }
예제 #8
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var iterations = iterationsCount / threadsCount;
            var tasks      = new Task <Tuple <Turn, double> > [threadsCount];

            for (var i = 0; i < threadsCount; i++)
            {
                tasks[i] = new Task <Tuple <Turn, double> >(
                    () => SearchBestMove(rocket, new Random(random.Next()), iterations));
                tasks[i].Start();
            }
            Task.WaitAll(tasks);
            var bestMove = tasks
                           .OrderBy(o => o.Result.Item2)
                           .First()
                           .Result;
            var newRocket = rocket.Move(bestMove.Item1, level);

            return(newRocket);
        }
예제 #9
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var moves = new ConcurrentBag <Tuple <Turn, double> >();
            var tasks = new Task[threadsCount];

            for (int i = 0; i < threadsCount; i++)
            {
                tasks[i] = new Task(() =>
                {
                    Random random        = new Random();
                    var bestMoveInThread = SearchBestMove(rocket,
                                                          random, iterationsCount / threadsCount);
                    moves.Add(bestMoveInThread);
                });
                tasks[i].Start();
            }
            Task.WaitAll(tasks);
            var bestMove = moves.OrderBy(x => x.Item2).First();

            return(rocket.Move(bestMove.Item1, level));
        }
예제 #10
0
        public Rocket GetNextMove(Rocket rocket)
        {
            // TODO: распараллелить запуск SearchBestMove
            var tasks = Enumerable.Range(0, threadsCount).Select(_ =>
            {
                return(new Task <Tuple <Turn, double> >(() =>
                                                        SearchBestMove(rocket, new Random(random.Next()), iterationsCount / threadsCount)));
            }).ToArray();

            foreach (var t in tasks)
            {
                t.Start();
            }
            Task.WaitAll(tasks);

            var bestScore = tasks.Max(x => x.Result.Item2);
            var bestMove  = tasks.First(x => Math.Abs(x.Result.Item2 - bestScore) < 0.000000001).Result.Item1;
            var newRocket = rocket.Move(bestMove, level);

            return(newRocket);
        }
예제 #11
0
        public Rocket GetNextMove(Rocket rocket)
        {
            var taskList = new List <Task <Tuple <Turn, double> > >();

            for (var i = 0; i < threadsCount; i++)
            {
                taskList.Add(GetBestPath(rocket));
            }

            var task       = Task.WhenAll(taskList);
            var tempResult = task.Result;
            var maxScore   = tempResult.Max(value => value.Item2);
            var result     = tempResult.Where(value => value.Item2 == maxScore)
                             .Take(1)
                             .ToList();

            var bestMove  = result[0];
            var newRocket = rocket.Move(bestMove.Item1, level);

            return(newRocket);
        }