Example #1
0
 /// <summary>
 /// Method to be called by the "Pause EA" button.
 /// </summary>
 public void PauseEA()
 {
     if (_ea != null && _ea.RunState == RunState.Running)
     {
         _ea.RequestPause();
     }
 }
Example #2
0
 private void Stop_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         _ea.RequestPause();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
    private void Start()
    {
        int populationSize = 100;
        NetworkActivationScheme activationScheme = NetworkActivationScheme.CreateAcyclicScheme();

        NeatGenomeParameters neatParams = new NeatGenomeParameters();

        neatParams.ActivationFn    = TanH.__DefaultInstance;
        neatParams.FeedforwardOnly = activationScheme.AcyclicNetwork;

        IGenomeDecoder <NeatGenome, IBlackBox> neatDecoder = new NeatGenomeDecoder(activationScheme);

        IGenomeFactory <NeatGenome> neatFactory = new NeatGenomeFactory(3, 3, neatParams);
        List <NeatGenome>           genomeList  = neatFactory.CreateGenomeList(populationSize, 0);
        ArenaEvaluator evaluator = GetComponent <ArenaEvaluator>();

        evaluator.Initialize(neatDecoder);

        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);

        // Evolution parameters
        NeatEvolutionAlgorithmParameters neatEvolutionParams = new NeatEvolutionAlgorithmParameters();

        neatEvolutionParams.SpecieCount = 10;
        ISpeciationStrategy <NeatGenome> speciationStrategy           = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);
        IComplexityRegulationStrategy    complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, 10);

        NeatEvolutionAlgorithm <NeatGenome> ea = GetComponent <UnityEvolutionAlgorithm>();

        ea.Construct(neatEvolutionParams, speciationStrategy, complexityRegulationStrategy, new NullGenomeListEvaluator <NeatGenome, IBlackBox>());
        ea.Initialize(evaluator, neatFactory, genomeList);
        ea.UpdateScheme = new UpdateScheme(1); // This needs to be set AFTER Initialize is called

        ea.PausedEvent += (sender, e) =>
        {
            //ea.StartContinue();
        };
        ea.GenerationEvent += (sender, gen) =>
        {
            Debug.Log($"Generation {gen}");
            Debug.Log($"Highest fitness: {ea.CurrentChampGenome.EvaluationInfo.Fitness}");
            nnMesh.GenerateMesh(ea.CurrentChampGenome);
            ea.RequestPause();
            StartCoroutine(PauseRoutine(ea));
        };

        ea.StartContinue();
    }
        private EvaluationInfo[] TestGenome(NeatGenome genome, int iterations, int runs, bool createRecordings, bool generalize)
        {
            if (runs <= 0)
            {
                return(new EvaluationInfo[0]);
            }

            if (_ea != null && _ea.RunState == RunState.Running)
            {
                _ea.RequestPause();
            }

            // Create the genome decoder
            IGenomeDecoder <NeatGenome, IBlackBox> decoder = CreateGenomeDecoder();

            // Decode the genome (genotype => phenotype)
            IBlackBox phenome = decoder.Decode(genome);

            //TODO FIXME We had problems with the logger, it hangs at some point probably a buffer issue on the windows cmd???
            //We had to disable the console appender on log4net and just log to the file, so there is now a console writeline instead
            Debug.On = true;
            _logger.Info($"Testing phenome {(generalize ? "generalization " : "")}(ID: {genome.Id})...");
            Console.WriteLine($"Testing phenome (ID: {genome.Id})...");

            EvaluationInfo[] fitness = new EvaluationInfo[runs];

            CreateExperimentDirectoryIfNecessary();

            // run evaluations
            double minFitness = double.MaxValue;
            double maxFitness = double.MinValue;

            for (int i = 0; i < runs; i++)
            {
                if (generalize)
                {
                    fitness[i]          = _evaluator.TestPhenomeGeneralization(phenome, iterations);
                    fitness[i].GenomeId = genome.Id;
                }
                else
                {
                    fitness[i] = _evaluator.TestPhenome(phenome, iterations);
                }

                if (fitness[i].ObjectiveFitnessMean < minFitness)
                {
                    minFitness = fitness[i].ObjectiveFitnessMean;
                }

                if (fitness[i].ObjectiveFitnessMean > maxFitness)
                {
                    maxFitness = fitness[i].ObjectiveFitnessMean;
                }

                if (createRecordings)
                {
                    if (Recorder != null)
                    {
                        string recordingFile = generalize
                            ? $"{RecordingGeneralizationFile($"{genome.Id}_{i}")}"
                            : $"{RecordingFile($"{genome.Id}_{i}")}";

                        string tapeFile = generalize
                            ? $"{TapeGeneralizationFile($"{genome.Id}_{i}")}"
                            : $"{TapeFile($"{genome.Id}_{i}")}";

                        using (Bitmap bmp = Recorder.LifetimeToBitmap())
                        {
                            bmp.Save(recordingFile, ImageFormat.Png);
                        }

                        if (Recorder.FinalTuringTape != null)
                        {
                            using (Bitmap bmp = Recorder.TapeToBitmap())
                            {
                                bmp.Save(tapeFile, ImageFormat.Png);
                            }
                        }
                    }
                    else
                    {
                        _logger.Warn("Recorder was null");
                        break;
                    }

                    _logger.Info($"Run {i}: Achieved fitness: {fitness[i].ObjectiveFitnessMean:F4}");
                    Console.WriteLine($"Run {i}: Achieved fitness: {fitness[i].ObjectiveFitnessMean:F4}");
                }
            }

            // evaluate runs
            if (runs > 1)
            {
                double[] fit = new double[runs];
                for (int i = 0; i < runs; i++)
                {
                    fit[i] = fitness[i].ObjectiveFitnessMean;
                }

                double mean = Utilities.Mean(fit);
                double sd   = Utilities.StandartDeviation(fit);
                //TODO FIXME We had problems with the logger, it hangs at some point probably a buffer issue on the windows cmd???
                //We had to disable the console appender on log4net and just log to the file, so there is now a console writeline instead
                _logger.Info($"Done. Average fitness: {mean:F4}, min fitness: {minFitness:F4}, max fitness: {maxFitness:F4}, sd: {sd:F4}");
                Console.WriteLine($"Done. Average fitness: {mean:F4}, min fitness: {minFitness:F4}, max fitness: {maxFitness:F4}, sd: {sd:F4}");
            }

            return(fitness);
        }