protected override GeneticAlgorithm CreateGA() { NumberOfSimultaneousEvaluations = SimulationsGrid.x * SimulationsGrid.y; m_fitness = new CarFitness(); var chromosome = new CarChromosome(Config); var crossover = new UniformCrossover(); var mutation = new FlipBitMutation(); var selection = new EliteSelection(); var population = new Population(NumberOfSimultaneousEvaluations, NumberOfSimultaneousEvaluations, chromosome) { GenerationStrategy = new PerformanceGenerationStrategy() }; var ga = new GeneticAlgorithm(population, m_fitness, selection, crossover, mutation) { Termination = new CarTermination(), TaskExecutor = new ParallelTaskExecutor { MinThreads = population.MinSize, MaxThreads = population.MaxSize * 2 } }; ga.GenerationRan += delegate { m_lastPosition = Vector3.zero; m_evaluationPool.ReleaseAll(); }; ga.MutationProbability = .1f; return(ga); }
public void SetChromosome(CarChromosome chromosome, CarSampleConfig config) { Chromosome = chromosome; Chromosome.MaxDistance = 0; chromosome.MaxDistanceTime = 0; Distance = 0; DistanceTime = 0; m_startTime = Time.time; transform.rotation = Quaternion.identity; m_config = config; m_rb.isKinematic = false; m_rb.velocity = Vector2.zero; m_rb.angularVelocity = 0; var phenotypes = chromosome.GetPhenotypes(); m_polygon.points = phenotypes.Select(p => p.Vector).ToArray(); var wheelsMass = 0f; for (int i = 0; i < phenotypes.Length; i++) { var p = phenotypes[i]; PrepareWheel(i, m_polygon.points[p.WheelIndex], p.WheelRadius); wheelsMass += p.WheelRadius; } // The car mass should be greater than wheels sum mass, because the WheelJoint2d get crazy otherwise. // If we comment the line bellow and enable the car mass should be greater than wheels sum mass, because the WheelJoint2d get crazy otherwise. m_rb.mass = 1 + m_polygon.points.Sum(p => p.magnitude) * VectorMagnitudeMass + wheelsMass; if (m_cam != null) { m_cam.StartFollowing(gameObject); } StartCoroutine("CheckTimeout"); }