Пример #1
0
        private void LogAiVariants(RaceState state, IGameAiLogger aiLogger, List <RaceSolution> variants)
        {
            var variantsToLog = variants.Cast <RaceSolution>().Reverse().Take(5).ToList();

            //var log = variantsToLog
            //    .Select(v => $"{v.Score.ToString(CultureInfo.InvariantCulture)} {v.CarCommands.StrJoin(",")}")
            //    .StrJoin("\n");
            //aiLogger?.LogText(log);
            aiLogger?.LogText(
                $"{{{string.Join(',', variantsToLog.Select(x => $"Score: {x.Score} DebugInfo: {x.Debug}"))}}}");
            var intensity = 1.0;

            foreach (var solution in variantsToLog)
            {
                var state2 = state.MakeCopy();
                foreach (var a in solution.CarCommands)
                {
                    var startFirst  = state2.FirstCar.Pos;
                    var startSecond = state2.SecondCar.Pos;
                    state2.FirstCar.NextCommand  = a.firstCarCommand;
                    state2.SecondCar.NextCommand = a.secondCarCommand;
                    state2.Tick();
                    var endFirst  = state2.FirstCar.Pos;
                    var endSecond = state2.SecondCar.Pos;
                    aiLogger?.LogLine(startFirst, a.firstCarCommand is ExchangeCommand ? startSecond : endFirst,
                                      intensity);
                    aiLogger?.LogLine(startSecond, a.secondCarCommand is ExchangeCommand ? startFirst : endSecond,
                                      intensity);
                }

                intensity *= 0.7;
            }
        }
        public (V FirstCarNextFlag, V SecondCarNextFlag) GetNextFlagsFor(RaceState state)
        {
            var flags = state.Track.Flags;
            var f     = flags[state.FlagsTaken % flags.Count];
            var s     = flags[(state.FlagsTaken + 1) % flags.Count];

            return(f, s);
        }
Пример #3
0
        public static RaceState Play(RaceState state, ISolver <RaceState, RaceSolution> racer, bool makeLog)
        {
            if (!makeLog)
            {
                return(new RaceController().Play(state, racer, null));
            }
            var filename = Path.Combine(
                TestContext.CurrentContext.TestDirectory,
                "racing",
                "visualizer",
                "last-log.js"
                );

            using var streamWriter = new StreamWriter(filename);
            var result = new RaceController().Play(state, racer, new JsonGameLogger(streamWriter));

            Console.WriteLine(result.Time + "\t" + result.FirstCar + "\t" + result.SecondCar);
            return(result);
        }
        public double EvaluateCommand(RaceState state,
                                      bool ifFirstCar, V thisFlag, ICarCommand command)
        {
            var car = ifFirstCar ? state.FirstCar : state.SecondCar;

            car.NextCommand = command;
            state.Tick();
            if (!car.IsAlive)
            {
                return(double.MinValue);
            }
            //var nextFlag = state.GetNextFlag();
            //var bonusToNextFlag = (car.Pos + car.V).DistTo(nextFlag) - car.Pos.DistTo(nextFlag);
            var evaluation =
                flagsTakenCoeff * car.FlagsTaken
                - distToFlagCoeff * thisFlag.DistTo(car.Pos);

            //- bonusToNextFlagCoef * bonusToNextFlag;
            return(evaluation);
        }
        public (V FirstCarNextFlag, V SecondCarNextFlag) GetNextFlagsFor(RaceState state)
        {
            var firstCar  = state.FirstCar;
            var secondCar = state.SecondCar;

            var flag1 = state.GetNextFlag();
            var flag2 = state.GetNextFlag(1);

            var dist1to1 = flag1.DistTo(firstCar.Pos + firstCar.V);
            var dist2to1 = flag1.DistTo(secondCar.Pos + secondCar.V);

            //правило: если одна из машинок ближе к обоим флагам, то она назначается на первый

            if (dist1to1 <= dist2to1)
            {
                return(flag1, flag2);
            }

            return(flag2, flag1);
        }
Пример #6
0
        // public IEnumerable<RaceState> Play(RaceState initialState, RaceSolution solution)
        // {
        //     var race = initialState.MakeCopy();
        //     var i = 0;
        //     while (!race.IsFinished && i < solution.Accelerations.Length)
        //     {
        //         var command = solution.Accelerations[i++];
        //         race.FirstCar.NextCommand = command;
        //         race.Tick();
        //         yield return race.MakeCopy();
        //     }
        // }

        public RaceState Play(RaceState initialState, ISolver <RaceState, RaceSolution> solver,
                              IGameLogger <RaceTrack, RaceState> logger, int aiTimeoutPerTickMs = timeoutPerTick)
        {
            var race = initialState.MakeCopy();

            logger?.LogStart(race.Track);
            while (!race.IsFinished)
            {
                var variants = solver.GetSolutions(race.MakeCopy(),
                                                   aiTimeoutPerTickMs).ToList();
                var aiLogger = logger?.GetAiLogger(0);
                LogAiVariants(race, aiLogger, variants);
                var variant = variants.Last();
                var(firstCommand, secondCommand) = variant.CarCommands[0];
                race.FirstCar.NextCommand        = firstCommand;
                race.SecondCar.NextCommand       = secondCommand;
                logger?.LogTick(race);
                race.Tick();
            }

            logger?.LogEnd(race);
            return(race);
        }
        public (V FirstCarNextFlag, V SecondCarNextFlag) GetNextFlagsFor(RaceState state)
        {
            V   flag      = null;
            var maxDist   = 0.0;
            var flagIndex = 0;

            for (var i = 1; i < state.Track.Flags.Count; i++)
            {
                var curF1 = state.Track.Flags[i];
                var curF2 = i == state.Track.Flags.Count - 1 ? state.Track.Flags[0] : state.Track.Flags[i + 1];

                var curDist = curF1.DistTo(curF2);
                if (curDist > maxDist)
                {
                    maxDist   = curDist;
                    flag      = curF2;
                    flagIndex = i + 1;
                }
            }

            var nextFlag = state.GetNextFlag();

            if (state.FlagsTaken % state.Track.Flags.Count >= flagIndex)
            {
                flag = state.Track.Flags[0];
            }

            var dist1ToNextFlag = nextFlag.DistTo(state.FirstCar.Pos + state.FirstCar.V);
            var dist2ToNextFlag = nextFlag.DistTo(state.SecondCar.Pos + state.SecondCar.V);

            if (dist1ToNextFlag < dist2ToNextFlag)
            {
                return(nextFlag, flag);
            }

            return(flag, nextFlag);
        }
 public void EvaluateMoves(RaceState state,
                           List <double> evList, (V, V) accelerations, V nextFlagForFirstCar, V nextFlagForSecondCar)
 public double EvaluateCarsExchange(RaceState state, V nextFlagForFirstCar, V nextFlagForSecondCar)
 {
     return(EvaluateCommands(state, nextFlagForFirstCar, nextFlagForSecondCar, new ExchangeCommand(), new ExchangeCommand()));
 }
 public void EvaluateMove(RaceState state,
                          List <double> evList, V acceleration, bool ifFirstCar, V thisFlag)
 {
     evList.Add(EvaluateCommand(state, ifFirstCar, thisFlag,
                                new MoveCommand(acceleration)));
 }
 public double EvaluateExchange(RaceState state,
                                bool ifFirstCar, V thisFlag)
 {
     return(EvaluateCommand(state, ifFirstCar, thisFlag, new ExchangeCommand()));
 }
Пример #12
0
 public (V FirstCarNextFlag, V SecondCarNextFlag) GetNextFlagsFor(RaceState state)
 {
     var res = predictSecondRange
               .SelectMany(n => new[] { (1, n), (n, 1) })
Пример #13
0
 public PreviousBest(List <ICarCommand> list, double sc, RaceState st)
 {
     CommandList = list;
     Score       = sc;
     State       = st;
 }