Exemplo n.º 1
0
        private IEnumerator RunSingleGeneration(int populationSize,
                                                int generation,
                                                GenerationComplete callback)
        {
            FileLogger.Log("==== Generation {0}, N={1}", generation, populationSize);

            Creature[] population = CreatePopulation(populationSize, generation);

            // reset player
            player.transform.position = resetPlayerPosition;
            player.transform.rotation = resetPlayerQuaternion;

            // simulation loop
            float startTime = Time.time;

            do
            {
                yield return(WaitForNextUpdate());
            } while (Time.time - startTime < TerminationTime &&
                     population.Select(t => t)
                     .Any(t => t.state != Creature.State.Dead));


            float duration = Time.time - startTime;

            FileLogger.Log("  completed in {1}s", generation, duration);

            // evaluate fitness of population
            float[] fitnesses = population
                                .Select(t => FitnessEvaluator.ComputeForCreature(t))
                                .ToArray();

            // keep generation
            PreviousGenerationDnasByFitness = new SortedList <float, DNA>(populationSize, Comparer);
            for (int i = 0; i < populationSize; i++)
            {
                PreviousGenerationDnasByFitness.Add(fitnesses[i], population[i].DNA);

                // also add to Hall of Fame
                HallOfFame.Add(fitnesses[i], population[i].DNA);
            }

            // add to Hall of Fame
            HallOfFame.KeepFirstN(HallOfFameCount);

            FileLogger.Log("  population fitness evaluation;");
            PreviousGenerationDnasByFitness.ForEach((kvp, i) => {
                FileLogger.Log("  {0}. [fitness={1}, dna={2}]", i + 1, kvp.Key, kvp.Value);
            }
                                                    );

            callback(generation, duration, PreviousGenerationDnasByFitness, HallOfFame);

            FileLogger.Log("==== Generation {0} complete", generation);

            // end all creatures
            population.ForEach(t => t.EndCreature());
        }
Exemplo n.º 2
0
        private IEnumerator RunMultipleGenerations(int populationSize, GenerationComplete callback, int numGenerations)
        {
            for (int gen = 1; gen <= numGenerations; ++gen)
            {
                yield return(StartCoroutine(RunSingleGeneration(populationSize, gen, callback)));

                FileLogger.Log("\n");
            }
        }
Exemplo n.º 3
0
 public void Initialize(LevelConfig levelConfig, LevelDepthConfig levelDepthConfig, GenerationComplete callback)
 {
     completeCallback   = callback;
     depthConfig        = levelDepthConfig;
     config             = levelConfig;
     mapPopulatorPrefab = Resources.Load <MapPopulator>("MapPopulator");
     world = new Rect(0, 0, config.Width, config.Height);
     rooms = new List <MazeRoom>();
     Debug.Log("Creating world with size: " + config.Width + ", " + config.Height);
 }
Exemplo n.º 4
0
 protected virtual void OnGenerationComplete(object sender, ProjectItemGenerationCompleteEventArgs args)
 {
     GenerationComplete?.Invoke(sender, args);
 }
Exemplo n.º 5
0
 public void Run(int populationSize, GenerationComplete callback, int numRuns)
 {
     resetPlayerPosition   = player.transform.position;
     resetPlayerQuaternion = player.transform.rotation;
     StartCoroutine(RunMultipleGenerations(populationSize, callback, numRuns));
 }
Exemplo n.º 6
0
 /// <summary>
 /// Invokes the <see cref="GenerationComplete"/> event
 /// </summary>
 /// <param name="ge"><see cref="GaEventArgs{TIndividual, TGene}"/> for the completed generation</param>
 protected virtual void OnGenerationComplete(GaEventArgs <TIndividual, TGene> ge)
 {
     GenerationComplete?.Invoke(ge);
 }