public IEnumerator StartPlayback(int generation)
        {
            var lastSimulatedGeneration = evolution.SimulationData.BestCreatures.Count;

            if (generation < 1 ||  generation > lastSimulatedGeneration)
            {
                Debug.LogError(string.Format("Attempted to show invalid generation: {0}. Simulated up to {1}", generation, lastSimulatedGeneration));
                yield break;
            }

            if (this.playbackScene != null && this.playbackScene.IsValid())
            {
                yield return(SceneController.UnloadAsync(this.playbackScene));
            }

            this.CurrentGeneration = generation;

            var sceneLoadConfig = new SceneController.SimulationSceneLoadConfig(
                this.evolution.SimulationData.CreatureDesign,
                1,
                this.evolution.SimulationData.SceneDescription,
                SceneController.SimulationSceneType.BestCreatures,
                evolution.GetLegacySimulationOptions()
                );
            var context      = new SceneController.SimulationSceneLoadContext();
            var sceneContext = new PlaybackSceneContext(this.evolution.SimulationData, this);

            yield return(SceneController.LoadSimulationScene(sceneLoadConfig, context, sceneContext));

            this.physicsScene  = context.PhysicsScene;
            this.playbackScene = context.Scene;

            var chromosome = evolution.SimulationData.BestCreatures[generation - 1].Chromosome;

            this.CurrentBest = context.Creatures[0];
            evolution.ApplyBrain(this.CurrentBest, chromosome);

            trackedCamera.Target = this.CurrentBest;

            this.CurrentBest.SetOnBestCreatureLayer();

            this.CurrentBest.Alive = false;
            this.CurrentBest.gameObject.SetActive(false);

            this.CurrentBest.Alive = true;
            this.CurrentBest.gameObject.SetActive(true);

            AutoPlay();

            if (PlaybackDidBegin != null)
            {
                PlaybackDidBegin();
            }
        }
Example #2
0
        private IEnumerator SimulateGeneration()
        {
            var solutions     = new Solution[Settings.PopulationSize];
            var solutionIndex = 0;
            // Prepare batch simulation
            int actualBatchSize      = Settings.SimulateInBatches ? Settings.BatchSize : Settings.PopulationSize;
            int numberOfBatches      = (int)Math.Ceiling((double)this.Settings.PopulationSize / actualBatchSize);
            int firstChromosomeIndex = 0;

            // Cache values that can be changed during the simulation
            this.cachedSettings = this.Settings;

            if (NewGenerationDidBegin != null)
            {
                NewGenerationDidBegin();
            }

            for (int i = 0; i < numberOfBatches; i++)
            {
                this.currentBatchNumber = i + 1;
                int remainingCreatures = Settings.PopulationSize - (i * actualBatchSize);
                int currentBatchSize   = Math.Min(actualBatchSize, remainingCreatures);

                var sceneLoadConfig = new SceneController.SimulationSceneLoadConfig(
                    this.SimulationData.CreatureDesign,
                    currentBatchSize,
                    this.SimulationData.SceneDescription,
                    SceneController.SimulationSceneType.Simulation,
                    GetLegacySimulationOptions()
                    );

                var context      = new SceneController.SimulationSceneLoadContext();
                var sceneContext = new SimulationSceneContext(this.SimulationData);

                yield return(SceneController.LoadSimulationScene(sceneLoadConfig, context, sceneContext));

                this.batchPhysicsScene = context.PhysicsScene;

                var batch = context.Creatures;
                this.currentCreatureBatch = batch;

                var chromosomeCount = Math.Min(this.SimulationData.CurrentChromosomes.Length, batch.Length);
                var chromosomes     = new float[chromosomeCount][];
                for (int c = 0; c < chromosomeCount; c++)
                {
                    chromosomes[c] = this.SimulationData.CurrentChromosomes[c + firstChromosomeIndex];
                }
                firstChromosomeIndex += batch.Length;
                ApplyBrains(batch, chromosomes);

                yield return(SimulateBatch());

                // Evaluate creatures and destroy the scene after extracting all
                // required performance statistics
                for (int j = 0; j < batch.Length; j++)
                {
                    var creature = batch[j];
                    solutions[solutionIndex++] = new Solution()
                    {
                        Encodable = creature.brain.Network,
                        Stats     = creature.GetStatistics(this.cachedSettings.SimulationTime)
                    };
                }

                yield return(SceneManager.UnloadSceneAsync(context.Scene));
            }

            EvaluateSolutions(solutions);
        }