Esempio n. 1
0
        public Rocket(Vector2 location, Rectangle operatingArea, RocketController controller)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            CurrentLocation    = location;
            Controller         = controller;
            this.operatingArea = operatingArea;
        }
        private void InitSwarm()
        {
            if (swarm == null)
            {
                swarm = new Rocket[RocketSwarmSwize];
                for (var i = 0; i < RocketSwarmSwize; i++)
                {
                    swarm[i] = new Rocket(location, operationArea, new RocketController(RocketLifespan));
                }

                return;
            }

            var rocketsWithFitness = swarm.Select(rocket => new {
                Rocket  = rocket,
                Fitness = CalculateControllerFitness(rocket)
            }).ToArray();

            var totalFitness = rocketsWithFitness.Select(x => x.Fitness).Sum();
            var fitnessMap   = rocketsWithFitness.SelectMany(x =>
                                                             Enumerable.Range(0, x.Fitness).Select(_ => x.Rocket).ToArray()).ToArray();

            foreach (var i in Enumerable.Range(0, RocketSwarmSwize))
            {
                var firstRocket  = fitnessMap[random.Next(totalFitness)];
                var secondRocket = fitnessMap[random.Next(totalFitness)];

                var breakpoint = (int)(RocketLifespan * random.NextDouble());

                var mergedMoves = new Vector2[RocketLifespan];
                foreach (var j in Enumerable.Range(0, RocketLifespan))
                {
                    if (random.NextDouble() < 0.01)
                    {
                        mergedMoves[j] = RocketController.GenerateRandomMove();
                        continue;
                    }

                    if (j <= breakpoint)
                    {
                        mergedMoves[j] = firstRocket.Controller.Moves[j];
                    }
                    else
                    {
                        mergedMoves[j] = secondRocket.Controller.Moves[j];
                    }
                }

                var mergedController = new RocketController(mergedMoves);
                swarm[i] = new Rocket(location, operationArea, mergedController);
            }
        }