Esempio n. 1
0
        private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeListEvaluator <NeatGenome> evaluator, int populationSize)
        {
            var genomeFactory = new NeatGenomeFactory(_inputCount.Value, _outputCount.Value, _neatGenomeParams);
            var genomeList    = genomeFactory.CreateGenomeList(populationSize, 0);

            return(CreateEvolutionAlgorithm(evaluator, genomeList));
        }
Esempio n. 2
0
        /// <summary>
        /// Loads a list of genomes from the save file fitting the experiment name and the ExperimentFileType.
        /// </summary>
        private static List <NeatGenome> ReadGenomes(INeatExperiment experiment, ExperimentFileType fileType, bool createNewGenesIfNotLoadable = true)
        {
            List <NeatGenome> genomeList    = null;
            NeatGenomeFactory genomeFactory = (NeatGenomeFactory)experiment.CreateGenomeFactory();

            string filePath = GetSaveFilePath(experiment.Name, fileType);

            try
            {
                using (XmlReader xr = XmlReader.Create(filePath))
                {
                    genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, genomeFactory);

                    if (genomeList != null && genomeList.Count > 0)
                    {
                        Utility.Log("Successfully loaded the genomes of the '" + fileType.ToString() + "' for the experiment '" + experiment.Name + "' from the location:\n" + filePath);
                    }
                }
            }
            catch (Exception e1)
            {
                Utility.Log("Error loading genome from file, could not find the file at: " + filePath + "\n" + e1.Message);

                if (createNewGenesIfNotLoadable)
                {
                    genomeList = genomeFactory.CreateGenomeList(experiment.DefaultPopulationSize, 0);
                }
            }
            return(genomeList);
        }
Esempio n. 3
0
        NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(bool load)
        {
            // Create a genome2 factory with our neat genome2 parameters object and the appropriate number of input and output neuron genes.
            var genomeFactory = new NeatGenomeFactory(TetrisEvaluator.NumInputs, TetrisEvaluator.NumOutputs);

            // Create an initial population of randomly generated genomes.
            List <NeatGenome> genomeList = null;

            if (load)
            {
                try
                {
                    using (var reader = XmlReader.Create("SavedProgress.xml"))
                        genomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(reader, true, genomeFactory);
                    Console.WriteLine("Loaded network!");
                }
                catch
                {
                    load = false;
                }
            }
            if (!load)
            {
                genomeList = genomeFactory.CreateGenomeList(150, 0);
            }

            var parallelOpts = new ParallelOptions()
            {
                MaxDegreeOfParallelism = -1
            };
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            var distanceMetric     = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOpts);

            // Create the evolution algorithm.
            var ea = new NeatEvolutionAlgorithm <NeatGenome>(new NeatEvolutionAlgorithmParameters {
                SpecieCount = 10
            }, speciationStrategy, new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Absolute, 50));

            // Create genome2 decoder.
            var genomeDecoder = new NeatGenomeDecoder(NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(2));

            // Create a genome2 list evaluator. This packages up the genome2 decoder with the genome2 evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, tetrisEvaluator, parallelOpts);

            // Wrap the list evaluator in a 'selective' evaulator that will only evaluate new genomes. That is, we skip re-evaluating any genomes
            // that were in the population in previous generations (elite genomes). This is determiend by examining each genome2's evaluation info object.
            //if (!EvaluateParents)
            //genomeListEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(genomeListEvaluator, SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());

            ea.UpdateEvent += Ea_UpdateEvent;

            // Initialize the evolution algorithm.
            ea.Initialize(genomeListEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return(ea);
        }
Esempio n. 4
0
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(BattleEvaluator <NeatGenome> evaluator, int populationSize)
        {
            Debug.Assert(populationSize > 5);
            _eaParams.SpecieCount = populationSize / 5;

            var genomeFactory = new NeatGenomeFactory(InputCount, OutputCount, _neatGenomeParams);
            var genomeList    = genomeFactory.CreateGenomeList(populationSize, 0);

            return(CreateEvolutionAlgorithm(evaluator, genomeList));
        }
Esempio n. 5
0
    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();
    }
Esempio n. 6
0
        public void LoadPopulation()
        {
            Debug.Assert(_GenomeFactory != null);

            var fi = new FileInfo(_GenomeFile);

            if (fi.Exists)
            {
                using (XmlReader xr = XmlReader.Create(_GenomeFile))
                {
                    _GenomeList = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, _GenomeFactory);
                }
            }
            else
            {
                _GenomeList = _GenomeFactory.CreateGenomeList(_PopulationSize, 0);
            }
        }
        /// <inheritdoc />
        /// <summary>
        ///     Creates the MCC algorithm container using the given agent and maze population sizes.
        /// </summary>
        /// <param name="populationSize1">The agent population size.</param>
        /// <param name="populationSize2">The maze population size.</param>
        /// <returns>The instantiated MCC algorithm container.</returns>
        public override IMCCAlgorithmContainer <NeatGenome, MazeGenome> CreateMCCAlgorithmContainer(
            int populationSize1, int populationSize2)
        {
            // Create a genome factory for the NEAT genomes
            IGenomeFactory <NeatGenome> neatGenomeFactory = new NeatGenomeFactory(AnnInputCount, AnnOutputCount,
                                                                                  NeatGenomeParameters);

            // Create a genome factory for the maze genomes
            IGenomeFactory <MazeGenome> mazeGenomeFactory = new MazeGenomeFactory(MazeGenomeParameters, MazeHeight,
                                                                                  MazeWidth, MazeQuadrantHeight, MazeQuadrantWidth);

            // Create an initial population of maze navigators
            var neatGenomeList = neatGenomeFactory.CreateGenomeList(populationSize1, 0);

            // Create an initial population of mazes
            // NOTE: the population is set to 1 here because we're just starting with a single, completely open maze space
            var mazeGenomeList = mazeGenomeFactory.CreateGenomeList(populationSize2, 0);

            // Create the evolution algorithm container
            return(CreateMCCAlgorithmContainer(neatGenomeFactory, mazeGenomeFactory, neatGenomeList,
                                               mazeGenomeList, false));
        }
Esempio n. 8
0
        private static void Train()
        {
            File.WriteAllText($"{NeatConsts.experimentName}/fitness.csv", "generation,firness\n");

            var neatGenomeFactory = new NeatGenomeFactory(NeatConsts.ViewX * NeatConsts.ViewY * NeatConsts.typeIds.Count, 1);
            var genomeList        = neatGenomeFactory.CreateGenomeList(NeatConsts.SpecCount, 0);
            var eaParams          = new NeatEvolutionAlgorithmParameters
            {
                SpecieCount = NeatConsts.SpecCount
            };

            //var distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            var distanceMetric = new ManhattanDistanceMetric();

            var parallelOptions    = new ParallelOptions();
            var speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, parallelOptions);
            //var speciationStrategy = new KMeansClusteringStrategy<NeatGenome>(distanceMetric);
            //var speciationStrategy = new RandomClusteringStrategy<NeatGenome>();

            var complexityRegulationStrategy = new NullComplexityRegulationStrategy();
            //var complexityRegulationStrategy = new DefaultComplexityRegulationStrategy(ComplexityCeilingType.Relative, 0.50);

            var ea = new NeatEvolutionAlgorithm <NeatGenome>(eaParams, speciationStrategy, complexityRegulationStrategy);
            var activationScheme    = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(1);
            var genomeDecoder       = new NeatGenomeDecoder(activationScheme);
            var phenomeEvaluator    = new GameEvaluator();
            var genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, phenomeEvaluator, parallelOptions);

            ea.Initialize(genomeListEvaluator, neatGenomeFactory, genomeList);
            ea.UpdateScheme = new UpdateScheme(NeatConsts.LogRate);
            ea.StartContinue();
            ea.UpdateEvent += Ea_UpdateEvent;
            while (ea.RunState != RunState.Paused)
            {
            }
            ea.Stop();
        }
        /// <summary>
        ///     Creates the coevolution algorithm container using the given agent and maze population sizes.
        /// </summary>
        /// <param name="populationSize1">The agent population size.</param>
        /// <param name="populationSize2">The maze population size.</param>
        /// <returns>The instantiated coevolution algorithm container.</returns>
        public override ICoevolutionAlgorithmContainer<NeatGenome, MazeGenome> CreateCoevolutionAlgorithmContainer(
            int populationSize1, int populationSize2)
        {
            // Create a genome factory for the NEAT genomes
            IGenomeFactory<NeatGenome> neatGenomeFactory = new NeatGenomeFactory(AnnInputCount, AnnOutputCount,
                NeatGenomeParameters);

            // Create a genome factory for the maze genomes
            IGenomeFactory<MazeGenome> mazeGenomeFactory = new MazeGenomeFactory(MazeGenomeParameters, _mazeHeight,
                _mazeWidth);

            // Create an initial population of maze navigators
            List<NeatGenome> neatGenomeList = neatGenomeFactory.CreateGenomeList(populationSize1, 0);

            // Create an initial population of mazes
            // NOTE: the population is set to 1 here because we're just starting with a single, completely open maze space
            List<MazeGenome> mazeGenomeList = mazeGenomeFactory.CreateGenomeList(populationSize2, 0);

            // Create the evolution algorithm container
            return CreateCoevolutionAlgorithmContainer(neatGenomeFactory, mazeGenomeFactory, neatGenomeList,
                mazeGenomeList);
        }