public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);

        IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        AgarEvaluator evaluator = new AgarEvaluator(_optimizer);

        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();


        IGenomeListEvaluator <NeatGenome> innerEvaluator = new UnitySingleGenomeEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _optimizer);

        IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(innerEvaluator,
                                                                                                             SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

        //ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
        ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

        return(ea);
    }
Example #2
0
        void RunExperiment(string XMLFile, string filename)
        {
            _filename = filename;
            _experiment = new SocialExperiment();

            // Write the header for the results file in CSV format.
            using (TextWriter writer = new StreamWriter(_filename))
                writer.WriteLine("Generation,Average,Best,Updates");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_after.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_before.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(XMLFile);
            _experiment.Initialize("EgalitarianSocialLearning", xmlConfig.DocumentElement);
            _experiment.TrialId = _trialNum;

            // Create the evolution algorithm and attach the update event.
            _ea = _experiment.CreateEvolutionAlgorithm();
            _ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1);
            _ea.UpdateEvent += new EventHandler(_ea_UpdateEvent);

            _experiment.Evaluator.TrialId = _trialNum;
            _experiment.Evaluator.DiversityFile = _filename.Replace(".csv", "_diversity.csv");

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();
        }
Example #3
0
    public void Initialize()
    {
        /*** Initialize experiment ***/
        experiment = new PCGNeatExperiment() as INeatExperiment;
        // Null because not reading settings from xmlFile
        experiment.Initialize("PCG Conetent EA", null);

        /*** Randomly generate population ***/
        // Set initial settings
        // ? means it is nullable
        int?popSize = experiment.DefaultPopulationSize;

        // Create a genome factory appropriate for the experiment.
        IGenomeFactory <NeatGenome> genomeFactory = experiment.CreateGenomeFactory();

        // Create an initial population of randomly generated genomes.
        // 0u is a struct for a 32 bit unsigned integer
        List <NeatGenome> genomeList = genomeFactory.CreateGenomeList(popSize.Value, 0u);

        // Check number of species is <= the number of the genomes.
        if (genomeList.Count < experiment.NeatEvolutionAlgorithmParameters.SpecieCount)
        {
            Debug.Log("Genome count must be >= specie count. Genomes=" + genomeList.Count
                      + "  Species=" + experiment.NeatEvolutionAlgorithmParameters.SpecieCount);
            return;
        }

        /*** Create the algorithm interface ***/
        contentEA = experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList);
    }
Example #4
0
        void RunExperiment(string XMLFile, string filename)
        {
            _filename   = filename;
            _experiment = new SocialExperiment();

            // Write the header for the results file in CSV format.
            using (TextWriter writer = new StreamWriter(_filename))
                writer.WriteLine("Generation,Average,Best,Updates");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_after.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            using (TextWriter writer = new StreamWriter(_filename.Replace(".csv", "_diversity_before.csv")))
                writer.WriteLine("Generation,Orientation Variance,Velocity Variance");

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(XMLFile);
            _experiment.Initialize("SimpleEvolution", xmlConfig.DocumentElement);

            // Create the evolution algorithm and attach the update event.
            _ea = _experiment.CreateEvolutionAlgorithm();
            _ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1);
            _ea.UpdateEvent += new EventHandler(_ea_UpdateEvent);

            _experiment.Evaluator.TrialId       = _trialNum;
            _experiment.Evaluator.DiversityFile = _filename.Replace(".csv", "_diversity.csv");

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();
        }
Example #5
0
    public void StartEA()
    {
        CleanUpScene();
        Utility.DebugLog = false;
        Utility.Log("Starting PhotoTaxis experiment");
        // print("Loading: " + popFileLoadPath);
        _ea = experiment.CreateEvolutionAlgorithm(popFileLoadPath);

        _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
        _ea.PausedEvent += new EventHandler(ea_PauseEvent);

        startTime = DateTime.Now;
        int evoSpeed = PlayerPrefs.GetInt("Evolution Speed");

        if (evoSpeed < 6 || evoSpeed > 10)
        {
            evoSpeed = 8;
        }
        evoSpeed = 25;

        Time.fixedDeltaTime = 0.045f;
        Time.timeScale      = evoSpeed;
        Target.GetComponent <TargetController>().Activate();
        _ea.StartContinue();
        EARunning = true;
    }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

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

            // Create a genome list evaluator. This packages up the genome decoder with the phenome evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelCoevolutionListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, PhenomeEvaluator);

            // Wrap a hall of fame evaluator around the baseline evaluator.
            genomeListEvaluator = new ParallelHallOfFameListEvaluator <NeatGenome, IBlackBox>(50, 0.5, ea, genomeListEvaluator, genomeDecoder, PhenomeEvaluator);

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

            // Finished. Return the evolution algorithm
            return(ea);
        }
Example #7
0
        /// <summary>
        /// Create a new instance of <see cref="NeatEvolutionAlgorithm{T}"/> for the given neat experiment.
        /// </summary>
        /// <param name="neatExperiment">A neat experiment instance; this conveys everything required to create a new evolution algorithm instance that is ready to be run.</param>
        /// <returns>A new instance of <see cref="NeatEvolutionAlgorithm{T}"/>.</returns>
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperiment <double> neatExperiment)
        {
            // Create a genomeList evaluator based on the experiment's configuration settings.
            var genomeListEvaluator = CreateGenomeListEvaluator(neatExperiment);

            // Create a MetaNeatGenome.
            var metaNeatGenome = CreateMetaNeatGenome(neatExperiment);

            // Create an initial population of genomes.
            NeatPopulation <double> neatPop = NeatPopulationFactory <double> .CreatePopulation(
                metaNeatGenome,
                connectionsProportion : neatExperiment.InitialInterconnectionsProportion,
                popSize : neatExperiment.PopulationSize);

            // Create a speciation strategy based on the experiment's configuration settings.
            var speciationStrategy = CreateSpeciationStrategy(neatExperiment);

            // Create an instance of the default connection weight mutation scheme.
            var weightMutationScheme = WeightMutationSchemeFactory.CreateDefaultScheme(neatExperiment.ConnectionWeightScale);

            // Pull all of the parts together into an evolution algorithm instance.
            var ea = new NeatEvolutionAlgorithm <double>(
                neatExperiment.NeatEvolutionAlgorithmSettings,
                genomeListEvaluator,
                speciationStrategy,
                neatPop,
                neatExperiment.ComplexityRegulationStrategy,
                neatExperiment.ReproductionAsexualSettings,
                neatExperiment.ReproductionSexualSettings,
                weightMutationScheme);

            return(ea);
        }
 /// <summary>
 /// Test that selects genomes that have never been evaluated.
 /// </summary>
 public static Predicate <TGenome> CreatePredicate_CheckforTrainingStatus(NeatEvolutionAlgorithm <NeatGenome> ea, int maxGen)
 {
     return(delegate(TGenome genome)
     {
         return !genome.EvaluationInfo.IsEvaluated || ea.CurrentGeneration == maxGen + 1;
     });
 }
Example #9
0
        public void Update(NeatEvolutionAlgorithm <NeatGenome> ea)
        {
            statsGrid.Children.Clear();
            statsGrid.RowDefinitions.Clear();

            #region grow/shrink

            statsGrid.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(1, GridUnitType.Auto)
            });

            TextBlock text = new TextBlock()
            {
                Text  = ea.ComplexityRegulationMode == ComplexityRegulationMode.Complexifying ? "Growing" : "Shrinking",
                Style = FindResource("valueTextStats_Centered") as Style,
            };

            Grid.SetColumn(text, 0);
            Grid.SetColumnSpan(text, 3);
            Grid.SetRow(text, statsGrid.RowDefinitions.Count - 1);

            statsGrid.Children.Add(text);

            #endregion

            AddPromptValue(this, statsGrid, "Generation", ea.CurrentGeneration.ToString("N0"));
            AddPromptValue(this, statsGrid, "Best Score", ea.Statistics._maxFitness.ToStringSignificantDigits(2));
            AddPromptValue(this, statsGrid, "Most Complex", ea.Statistics._maxComplexity.ToStringSignificantDigits(2));
        }
    public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy <NeatGenome> speciationStrategy = new KMeansClusteringStrategy <NeatGenome>(distanceMetric);

        IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        SimpleEvaluator evaluator = new SimpleEvaluator(_optimizer);

        IGenomeDecoder <NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();


        IGenomeListEvaluator <NeatGenome> innerEvaluator = new UnityParallelListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _optimizer);

        IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(innerEvaluator,
                                                                                                             SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

        //ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
        //ea.Initialize(innerEvaluator, genomeFactory, genomeList);//this can be used if the currentGeneration does not need to be updated at the start of the training
        ea.Initialize(innerEvaluator, genomeFactory, genomeList, true);

        return(ea);
    }
Example #11
0
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weight difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            PreyCaptureEvaluator evaluator = new PreyCaptureEvaluator(_trialsPerEvaluation, _gridSize, _preyInitMoves, _preySpeed, _sensorRange, _maxTimesteps);

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

            // TODO: evaluation scheme that re-evaluates existing genomes and takes average over time.
            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

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

            // Finished. Return the evolution algorithm
            return(ea);
        }
Example #12
0
    // Initialize with pre-existing xml data
    public void Initialize(string xmlData)
    {
        /*** Initialize experiment ***/
        experiment = new PCGNeatExperiment() as INeatExperiment;
        // Null because not reading settings from xmlFile
        experiment.Initialize("PCG Conetent EA", null);

        /*** Load population of genomes from xml string ***/
        List <NeatGenome> genomeList;

        using (XmlReader xr = XmlReader.Create(new StringReader(xmlData)))
        {
            genomeList = experiment.LoadPopulation(xr);
        }

        if (genomeList.Count == 0)
        {
            Debug.LogError("No genomes loaded from XML data from network. Check data is being read correctly.");
            return;
        }
        else
        {
            Debug.Log("Loaded " + genomeList.Count + " Genomes");
        }

        /*** Create the algorithm interface ***/
        contentEA = experiment.CreateEvolutionAlgorithm(genomeList[0].GenomeFactory, genomeList);
    }
Example #13
0
        public IEnumerator Start()
        {
            Debug.Assert(_populationSize > 5);
            Debug.Log($"Main thread is {Thread.CurrentThread.ManagedThreadId}");
            Application.runInBackground = true;

            _activationScheme = NetworkActivationScheme.CreateCyclicFixedTimestepsScheme(2, true);

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.ElitismProportion = _elitismProportion;
            _eaParams.SpecieCount       = _specieCount;

            _neatGenomeParams = new NeatGenomeParameters();
            _neatGenomeParams.AddConnectionMutationProbability    = _AddConnectionMutationProbability;
            _neatGenomeParams.DeleteConnectionMutationProbability = _DeleteConnectionMutationProbability;
            _neatGenomeParams.AddNodeMutationProbability          = _AddNodeMutationProbability;
            _neatGenomeParams.ConnectionWeightMutationProbability = _ConnectionWeightMutationProbability;
            _neatGenomeParams.InitialInterconnectionsProportion   = _InitialInterconnectionsProportion;
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            Debug.Log("Creating evaluator");
            yield return(new WaitForSeconds(0.1f));

            _evaluator = CreateEvaluator();

            Debug.Log("Creating algorithm");
            yield return(new WaitForSeconds(0.1f));

            _ea              = CreateEvolutionAlgorithm(_evaluator, _populationSize);
            _ea.UpdateEvent += _ea_UpdateEvent;
        }
    // Initialize with pre-existing xml data
    public void Initialize(string xmlData, PCGSharpHighLevelFeatures originalFeatures)
    {
        /*** Initialize experiment ***/
        experiment = new CPPNRepairExperiment() as INeatExperiment;
        // Null because not reading settings from xmlFile
        experiment.Initialize("PCG Conetent EA", null);

        /*** Load population of genomes from xml string ***/
        List <NeatGenome> genomeList;

        using (XmlReader xr = XmlReader.Create(new StringReader(xmlData)))
        {
            genomeList = experiment.LoadPopulation(xr);
        }

        if (genomeList.Count == 0)
        {
            Debug.LogError("No genomes loaded from XML data from network. Check data is being read correctly.");
            return;
        }
        else
        {
            Debug.Log("Loaded " + genomeList.Count + " Genomes");
        }
        Debug.Log(".........Population loaded");

        ((CPPNRepairExperiment)experiment).SetOriginalFeatures(originalFeatures);
        Debug.Log("........Original features added to experiment");

        /*** Create the algorithm interface ***/
        contentEA = experiment.CreateEvolutionAlgorithm(genomeList[0].GenomeFactory, genomeList);
        Debug.Log("........Content EA created");
    }
    public void StartEA()
    {
        //update the location of the Target
        Target.transform.position = new Vector3(UnityEngine.Random.Range(-distaceTargetAllowed, distaceTargetAllowed), Target.transform.position.y, UnityEngine.Random.Range(-distaceTargetAllowed, distaceTargetAllowed));
        if (Utility.DebugLog)
        {
            Utility.Log("Starting PhotoTaxis experiment");
            // Utility.Log("Loading: " + popFileLoadPath);
        }
        _ea       = experiment.CreateEvolutionAlgorithm(popFileSavePath);
        startTime = DateTime.Now;

        _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
        _ea.PausedEvent += new EventHandler(ea_PauseEvent);

        Generation = _ea.CurrentGeneration;


        var evoSpeed = timeScale;

        //   Time.fixedDeltaTime = 0.045f;
        Time.timeScale = evoSpeed;
        _ea.StartContinue();
        EARunning = true;
    }
Example #16
0
    public void StartEA()
    {
        if (carKnowsDrag)
        {
            NUM_INPUTS = 6;
        }
        if (!randomDrag)
        {
            Trials = 1;
        }

        SetupNewExperiment();

        //		mapLength = (int)((maxRain - minRain) / rainInterval);
//		map = new NeatGenome[1];
        //loadBest ();


        // print("Loading: " + popFileLoadPath);
        _ea       = experiment.CreateEvolutionAlgorithm(popFileSavePath);
        startTime = DateTime.Now;

        _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
        _ea.PausedEvent += new EventHandler(ea_PauseEvent);

        //   Time.fixedDeltaTime = 0.045f;
        _ea.StartContinue();
        EARunning = true;
    }
Example #17
0
        private void RemoveExistingExperiment()
        {
            if (_ea != null)
            {
                //_ea.Stop();
                _ea.Dispose();
            }

            if (_experiment != null)
            {
                //_experiment.
            }

            _experiment = null;
            _ea         = null;

            _experimentArgs = null;
            _hyperneatArgs  = null;

            _harness     = null;
            _harnessArgs = null;
            _evalArgs    = null;

            _winningBrain = null;
        }
Example #18
0
        private static void Main(string[] args)
        {
            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            SineExperiment experiment = new SineExperiment(true, new SineEvaluator(50));

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load("sine.config.xml");
            experiment.Initialize("DA MAGIC SUPER SINE", xmlConfig.DocumentElement);

            // Create evolution algorithm and attach update event.
            _ea = experiment.CreateEvolutionAlgorithm();
            _ea.UpdateEvent += ea_UpdateEvent;

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();

            // Hit return to quit.
            Console.ReadLine();

            // Get a genome decoder that can convert genomes to phenomes.
            IGenomeDecoder<NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder();

            // Get the champion genome
            NeatGenome champion = _ea.CurrentChampGenome;

            TestGenome(decoder, champion);
            
        }
Example #19
0
        /// <summary>
        /// Create a new instance of <see cref="NeatEvolutionAlgorithm{T}"/> for the given neat experiment, and neat population.
        /// </summary>
        /// <param name="neatExperiment">A neat experiment instance; this conveys everything required to create a new evolution algorithm instance that is ready to be run.</param>
        /// <param name="neatPop">A pre constructed/loaded neat population; this must be compatible with the provided neat experiment, otherwise an exception will be thrown.</param>
        /// <returns>A new instance of <see cref="NeatEvolutionAlgorithm{T}"/>.</returns>
        public static NeatEvolutionAlgorithm <double> CreateNeatEvolutionAlgorithm(
            INeatExperiment <double> neatExperiment,
            NeatPopulation <double> neatPop)
        {
            // Validate MetaNeatGenome and NeatExperiment are compatible; normally the former should have been created based on the latter, but this is not enforced.
            MetaNeatGenome <double> metaNeatGenome = neatPop.MetaNeatGenome;

            ValidateCompatible(neatExperiment, metaNeatGenome);

            // Create a genomeList evaluator based on the experiment's configuration settings.
            var genomeListEvaluator = CreateGenomeListEvaluator(neatExperiment);

            // Create a speciation strategy based on the experiment's configuration settings.
            var speciationStrategy = CreateSpeciationStrategy(neatExperiment);

            // Create an instance of the default connection weight mutation scheme.
            var weightMutationScheme = WeightMutationSchemeFactory.CreateDefaultScheme(neatExperiment.ConnectionWeightScale);

            // Pull all of the parts together into an evolution algorithm instance.
            var ea = new NeatEvolutionAlgorithm <double>(
                neatExperiment.NeatEvolutionAlgorithmSettings,
                genomeListEvaluator,
                speciationStrategy,
                neatPop,
                neatExperiment.ComplexityRegulationStrategy,
                neatExperiment.ReproductionAsexualSettings,
                neatExperiment.ReproductionSexualSettings,
                weightMutationScheme);

            return(ea);
        }
Example #20
0
        static void Main(string[] args)
        {
            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            XorExperiment experiment = new XorExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load("xor.config.xml");
            experiment.Initialize("XOR", xmlConfig.DocumentElement);

            // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
            _genomeFactory = experiment.CreateGenomeFactory();

            // Create an initial population of randomly generated genomes.
            _genomeList = _genomeFactory.CreateGenomeList(150, 0);

            // Create evolution algorithm and attach update event.
            _ea              = experiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList);
            _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();

            // Hit return to quit.
            Console.ReadLine();
        }
Example #21
0
        static void Main(string[] args)
        {
            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            XorExperiment experiment = new XorExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load("xor.config.xml");
            experiment.Initialize("XOR", xmlConfig.DocumentElement);

            // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
            _genomeFactory = experiment.CreateGenomeFactory();

            // Create an initial population of randomly generated genomes.
            _genomeList = _genomeFactory.CreateGenomeList(150, 0);

            // Create evolution algorithm and attach update event.
            _ea = experiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList);
            _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();

            // Hit return to quit.
            Console.ReadLine();
        }
Example #22
0
        public virtual void Initialize(string name, XmlElement xmlConfig)
        {
            _name                    = name;
            _populationSize          = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount             = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme        = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig,
                                                                    "DefaultComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description         = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _parallelOptions     = ExperimentUtils.ReadParallelOptions(xmlConfig);

            _eaParams                         = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount             = _specieCount;
            _neatGenomeParams                 = new NeatGenomeParameters();
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            DefaultComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(
                _complexityRegulationStr,
                _complexityThreshold);
            DefaultSpeciationStrategy     = new KMeansClusteringStrategy <NeatGenome>(new ManhattanDistanceMetric(1.0, 0.0, 10.0));
            DefaultNeatEvolutionAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(
                NeatEvolutionAlgorithmParameters,
                DefaultSpeciationStrategy,
                DefaultComplexityRegulationStrategy);
        }
Example #23
0
        public static void Main(string[] args)
        {
            NEATGame ng   = new NEATGame();
            Thread   play = new Thread(new ThreadStart(ng.Game));

            play.IsBackground = true;
            play.Start();

            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            KeepawayExperiment experiment = new KeepawayExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load("Keepaway.config.xml");
            experiment.Initialize("Keepaway", xmlConfig.DocumentElement);

            // Create evolution algorithm and attach update event.
            _ea              = experiment.CreateEvolutionAlgorithm();
            _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();
            //Hit return to quit
            //Console.ReadLine();
            NEATProgram np = new NEATProgram();

            np.WorkMethod();
            //ThreadPool.QueueUserWorkItem(new WaitCallback(WorkMethod), eventAuto);
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory <NeatGenome> genomeFactory, List <NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy <NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy <NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm <NeatGenome> ea = new NeatEvolutionAlgorithm <NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            _evaluator = CreateEvaluator();

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

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator <NeatGenome> innerEvaluator = new SerialGenomeListEvaluator <NeatGenome, IBlackBox>(genomeDecoder, _evaluator);
            /*new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, _evaluator, _parallelOptions);*/


            // 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 determined by examining each genome's evaluation info object.
            IGenomeListEvaluator <NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator <NeatGenome>(
                innerEvaluator,
                SelectiveGenomeListEvaluator <NeatGenome> .CreatePredicate_OnceOnly());

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

            // Finished. Return the evolution algorithm
            return(ea);
        }
Example #25
0
    private void btnSearchStart_Click(object sender, EventArgs e)
    {
        if (_eaRunner is not null)
        {   // Resume existing EA & update GUI state.
            _eaRunner.StartOrResume();
            UpdateUIState();
            return;
        }

        // Get the current neat experiment, with parameters set from the UI.
        INeatExperiment <double> neatExperiment = GetNeatExperiment();

        // Create evolution algorithm and runner.
        NeatEvolutionAlgorithm <double> ea = NeatUtils.CreateNeatEvolutionAlgorithm(neatExperiment, _neatPop);

        ea.Initialise();

        _eaRunner = new EvolutionAlgorithmRunner(ea, UpdateScheme.CreateTimeSpanUpdateScheme(TimeSpan.FromSeconds(1)));

        // Attach event listeners.
        _eaRunner.UpdateEvent += _eaRunner_UpdateEvent;

        // Start the algorithm & update GUI state.
        _eaRunner.StartOrResume();
        UpdateUIState();
    }
Example #26
0
 public void StartEA()
 {
     _ea              = experiment.CreateEvolutionAlgorithm(popLoadSavePath);
     _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
     _ea.PausedEvent += new EventHandler(ea_PauseEvent);
     _ea.StartContinue();
 }
Example #27
0
 public TetrisAI(IGameOrchestrator gameOrchestrator, Action <uint> onNewGen, Action <INetworkDefinition> onNewBestNetwork, bool load)
 {
     this.gameOrchestrator = gameOrchestrator ?? throw new ArgumentNullException(nameof(gameOrchestrator));
     tetrisEvaluator       = new TetrisEvaluator(gameOrchestrator, false);
     evolutionAlgorithm    = CreateEvolutionAlgorithm(load);
     this.onNewGen         = onNewGen;
     this.onNewBestNetwork = onNewBestNetwork;
 }
Example #28
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);
        }
        private NeatEvolutionAlgorithm <NeatGenome> CreateEvolutionAlgorithm()
        {
            int popSize = _experiment.DefaultPopulationSize;
            IGenomeFactory <NeatGenome>         genomeFactory = _experiment.CreateGenomeFactory();
            List <NeatGenome>                   genomeList    = genomeFactory.CreateGenomeList(popSize, 0);
            NeatEvolutionAlgorithm <NeatGenome> ea            = _experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList);

            return(ea);
        }
Example #30
0
    public void StartEA()
    {
        var camera = GameObject.FindGameObjectWithTag("MainCamera");

#if (ROTATECAMERA)
        camera.transform.position = new Vector3(0, 20, -200);
        camera.transform.rotation = Quaternion.Euler(0, 180, 0);
#endif
        //lock camera movement
        camera.GetComponent <GhostFreeRoamCamera>().allowMovement = false;
        camera.GetComponent <GhostFreeRoamCamera>().allowRotation = false;
        Cursor.visible   = true;
        Cursor.lockState = CursorLockMode.None;

        var evoSpeed = 100;

        if (_ea == null)
        {
            Utility.DebugLog = true;
            Utility.Log("Starting PhotoTaxis experiment");
            // print("Loading: " + popFileLoadPath);
            if (_loadOldPopulation)
            {
                _ea = experiment.CreateEvolutionAlgorithm(popFileSavePath);
            }
            else
            {
                _ea = experiment.CreateEvolutionAlgorithm();
            }

            startTime = DateTime.Now;

            _ea.UpdateEvent += ea_UpdateEvent;
            _ea.PausedEvent += ea_PauseEvent;

            //   Time.fixedDeltaTime = 0.045f;
            Time.timeScale = evoSpeed;
            _ea.StartContinue();
            _eaRunning = true;
        }
        else if (_ea.RunState == RunState.Paused)
        {
            if (SelectedController != null)
            {
                var selectedPhenome = SelectedController.Box;
                var selectedGenome  = _phenomesMap[selectedPhenome];
                selectedGenome.EvaluationInfo.SetFitness(float.MaxValue);
                SelectedGenomeId     = selectedGenome.Id;
                SelectedGeneration   = _ea.CurrentGeneration;
                SelectionHasBeenMade = true;
            }
            Time.timeScale = evoSpeed;
            _ea.StartContinue();
            _bestPhenomes.ForEach(StopEvaluation);
            _eaRunning = true;
        }
    }
Example #31
0
        private static void CreateXmlNetworkFile(NeatEvolutionAlgorithm <NeatGenome> _ea)
        {
            var doc = NeatGenomeXmlIO.SaveComplete(new List <NeatGenome>()
            {
                _ea.CurrentChampGenome
            }, false);

            doc.Save($"{NeatConsts.experimentName}/best.xml");
        }
Example #32
0
        private void GetEvolutionAlgorithm()
        {
            _EvolutionAlgorithm = new NeatEvolutionAlgorithm <NeatGenome>(_EvolutionAlgorithmParameters, _SpeciationStrategy, _ComplexityRegulationStrategy);
            _EvolutionAlgorithm.Initialize(_GenomeListEvaluator, _GenomeFactory, _GenomeList);

            _EvolutionAlgorithm.UpdateEvent += (object sender, EventArgs e) =>
            {
                UpdateEvent(_EvolutionAlgorithm.CurrentGeneration, _EvolutionAlgorithm.Statistics._maxFitness);
            };
        }
Example #33
0
            public Info(IPDExperiment exp, NeatEvolutionAlgorithm <NeatGenome> ea, IGenomeDecoder <NeatGenome, IBlackBox> decoder)
            {
                _exp     = exp;
                _genGet  = () => { return(ea.CurrentGeneration); };
                _boxGet  = () => { return(decoder.Decode(ea.CurrentChampGenome)); };
                _fitGet  = () => { return(ea.CurrentChampGenome.EvaluationInfo.Fitness); };
                _current = _genGet();

                OpponentInfo();
            }
    // Use this for initialization
    IEnumerator Start()
    {
        /*** Initialize experiment ***/
            INeatExperiment experiment = new TestExperiment() as INeatExperiment;
            // Null because not reading settings from xmlFile
            experiment.Initialize("this is a test",null);

            /*** Randomly generate population ***/
            // Set initial settings
            // ? means it is nullable
            int? popSize = experiment.DefaultPopulationSize;
            //double? initConnProportion = experiment.NeatGenomeParameters.InitialInterconnectionsProportion;

            // Create a genome factory appropriate for the experiment.
            IGenomeFactory<NeatGenome> genomeFactory = experiment.CreateGenomeFactory();

            // Create an initial population of randomly generated genomes.
            // 0u is a struct for a 32 bit unsigned integer
            List<NeatGenome> genomeList = genomeFactory.CreateGenomeList(popSize.Value, 0u);

            // Check number of species is <= the number of the genomes.
            if (genomeList.Count < experiment.NeatEvolutionAlgorithmParameters.SpecieCount)
            {
                Debug.Log("Genome count must be >= specie count. Genomes=" + genomeList.Count
                    + "  Species=" + experiment.NeatEvolutionAlgorithmParameters.SpecieCount);
                return false;
            }

            /*** Run the algorithm ***/
            ea = experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList);
            //for (int j = 0; j < 100; j++) {
                yield return new WaitForSeconds(0.5f);
                //Debug.Log(j);
                ea.StartContinue();
                NeatAlgorithmStats stats = ea.Statistics;
             	Debug.Log(stats._generation+", "+stats._maxFitness+", "+stats._meanFitness+", "+stats._totalEvaluationCount+", "+stats._maxComplexity);
            //}

            //NeatAlgorithmStats stats = ea.Statistics;
            //Debug.Log(stats._generation+", "+stats._maxFitness+", "+stats._meanFitness+", "+stats._totalEvaluationCount+", "+stats._maxComplexity);

            IGenomeDecoder<NeatGenome, IBlackBox> decoder = experiment.CreateGenomeDecoder();
            IBlackBox box = decoder.Decode(ea.CurrentChampGenome);
            FastAcyclicNetwork concrete = (FastAcyclicNetwork)box;
            Debug.Log("Num hidden nodes = " + concrete.hiddenNodeCount + ", num connections = " + concrete.connectionCount);

            box.InputSignalArray[0] = 0;
            box.InputSignalArray[1] = 0;

            box.Activate();

            for (int i = 0; i < box.OutputCount; i++) {
                Debug.LogWarning("Output["+i+"] = " + box.OutputSignalArray[i]);
        }
    }
Example #35
0
        public void StartTraining()
        {
            if (_evolutionAlgorithm == null)
            {
                _evolutionAlgorithm = _neuromonExperiment.CreateEvolutionAlgorithm(_genomeFactory, _genomePopulation);

                _evolutionAlgorithm.UpdateEvent += (sender, args) => HandleUpdateEvent(
                    _evolutionAlgorithm.CurrentGeneration, 
                    _evolutionAlgorithm.Statistics._maxFitness,
                    _evolutionAlgorithm.Statistics._meanFitness
                );

                _evolutionAlgorithm.PausedEvent += (sender, args) => OnTrainingPaused?.Invoke();
            }

            _evolutionAlgorithm.StartContinue();   
        }
Example #36
0
        static void Main(string[] args)
        {
            GridGameParameters gg = GridGameParameters.GetParameters(args);
            if (gg == null)
                return;

            experiment = new GridGameExperiment(gg.ConfigPath);
            experiment.Evaluator = new CondorEvaluator(gg, CondorGroup.Grad);
            ea = experiment.CreateEvolutionAlgorithm();
            ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1);
            ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

            using (TextWriter writer = new StreamWriter(gg.ResultsPath))
                writer.WriteLine("Generation,Best,Average");

            ea.StartContinue();

            while (!finished) Thread.Sleep(1000);
        }
Example #37
0
        static void Main(string[] args)
        {
            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            TicTacToeCoevolutionExperiment experiment = new TicTacToeCoevolutionExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load("tictactoe.config.xml");
            experiment.Initialize("TicTacToe", xmlConfig.DocumentElement);

            // Create evolution algorithm and attach update event.
            _ea = experiment.CreateEvolutionAlgorithm();
            _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();

            // Hit return to quit.
            Console.ReadLine();
        }
Example #38
0
        static void Main(string[] args)
        {
            GridGameParameters gg = GridGameParameters.GetParameters(args);
            if (gg == null)
                return;

            using (TextWriter writer = new StreamWriter(gg.ConfigPath))
            {
                XmlSerializer ser = new XmlSerializer(typeof(GridGameParameters));
                ser.Serialize(writer, gg);
            }

            experiment = new GridGameExperiment(gg.ConfigPath);
            ea = experiment.CreateEvolutionAlgorithm();
            ea.UpdateScheme = new SharpNeat.Core.UpdateScheme(1);
            ea.UpdateEvent += new EventHandler(ea_UpdateEvent);

            using (TextWriter writer = new StreamWriter(gg.ResultsPath))
                writer.WriteLine("Generation,Best,Average");

            ea.StartContinue();

            while (!finished) Thread.Sleep(1000);
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            PreyCaptureEvaluator evaluator = new PreyCaptureEvaluator(_trialsPerEvaluation, _gridSize, _preyInitMoves, _preySpeed, _sensorRange, _maxTimesteps);

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

            // TODO: evaulation scheme that re-evaulates existing genomes and takes average over time.
            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> genomeListEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

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

            // Finished. Return the evolution algorithm
            return ea;
        }
Example #40
0
        /// <summary>
        /// Log
        /// </summary>
        /// <param name="ea"></param>
        /// <param name="igle"></param>
        public void Log(NeatEvolutionAlgorithm<NeatGenome> ea)
        {
            Func<NeatGenome, double> costOfNeatGenome = neatGenome => neatGenome.EvaluationInfo.AuxFitnessArr[0]._value;
            // I made the evaluator public myself...
            IGenomeListEvaluator<NeatGenome> igle = ea._genomeListEvaluator;

            var champGenome = ea.GenomeList.Aggregate(
                (currentBest, candidate) =>
                costOfNeatGenome(currentBest) <= costOfNeatGenome(candidate)
                    ? currentBest
                    : candidate);

            var currentTime = DateTime.Now;
            var currentGeneration = ea.CurrentGeneration;
            var minimumCost = costOfNeatGenome(champGenome);
            var meanCost = ea.GenomeList.Average(costOfNeatGenome);
            var highestCost = ea.GenomeList.Max(costOfNeatGenome);
            var meanSpecieChampCost = ea.SpecieList.Average(
                specieList => specieList.GenomeList.Min(costOfNeatGenome));
            var champComplexity = champGenome.Complexity;
            var meanComplexity = ea.GenomeList.Average(neatGenome => neatGenome.Complexity);
            var maxComplexity = ea.GenomeList.Max(neatGenome => neatGenome.Complexity);
            //double totalEvaluationCount = ea.GenomeList.Sum(neatGenome => neatGenome.EvaluationInfo.EvaluationCount);
            var totalEvaluationCount = igle.EvaluationCount;

            var timeSinceLastCallToLog = currentTime - _oldCurrentTime;

            // To smooth out the evaluations per second statistic, we update only if at least one second has elapsed since it was last updated.
            // This was taken from NeatEvolutionAlgorithm.cs
            // Note that there, the documentation does not actually correspond to the implementation.
            //
            // I would actually write down the exact number I'm getting, calculating
            // sensible rolling averages during processing of results because I save
            // the result as a double instead of an integer.
            // For me, this does not really matter though: a generation takes far longer
            // than a second, and the Log function is called only at the end of a generation...
            //if (timeSinceLastCallToLog.Milliseconds > 999)
            //{
                var evaluationsSinceLastUpdate = totalEvaluationCount - _oldTotalEvaluationCount;
                var evaluationsPerSecond = ((double)TimeSpan.TicksPerSecond*evaluationsSinceLastUpdate)/(timeSinceLastCallToLog.Ticks);
                // Reset working variables
                _oldCurrentTime = currentTime;
                _oldTotalEvaluationCount = totalEvaluationCount;
                _oldEvaluationCountPerSecond = evaluationsPerSecond;
            //}

            IEnumerable<int> specieSizes = ea.SpecieList.Select(specie => specie.GenomeList.Count).ToList();
            String specieSizeString = String.Join(";", specieSizes);
            //ea.SpecieList.Aggregate("",(accumulatedString,specie) => specie.)

            _neatsim_logger.WriteLine(
                string.Format(
                    CultureInfo.InvariantCulture,
                    "{0:yyyy-MM-dd HH:mm:ss.fff},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}",
                    currentTime, //1
                    currentGeneration, //2
                    minimumCost, //3
                    meanCost, //4
                    highestCost, //5
                    meanSpecieChampCost, //6
                    champComplexity, //7
                    meanComplexity, //8
                    maxComplexity, //9
                    totalEvaluationCount, //10
                    _oldEvaluationCountPerSecond, //11
                    ea.ComplexityRegulationMode, //12
                    specieSizeString //13
                )
                );
            _neatsim_logger.Flush();

            var fitnessValues = ea.GenomeList.Select(individual => individual.EvaluationInfo.AuxFitnessArr[0]._value);
            String fitnessValueString = String.Join(";", fitnessValues);
            _fitness_logger.WriteLine(fitnessValueString);
            _fitness_logger.Flush();
        }
Example #41
0
        /// <summary>
        /// Trains a Markov model on a the training set of passwords, then evolves it against the target password database
        /// specified in the config file. At the end of the evolution, the champion model is evaluated for a larger number
        /// of guesses.
        /// </summary>
        /// <param name="trainingSetFile">The file containing the passwords from which to build the initial Markov model.</param>
        /// <param name="seedFile">The file to which the initial Markov model will be saved.</param>
        /// <param name="configFile">The file containing all the configuration parameters of the evolution.</param>
        /// <param name="resultsFile">The file to which the results will be saved at each generation.</param>
        /// <param name="validateSeed">If true, the seed model will first be validated against a large number of guesses.</param>
        //private static void RunExperiment(string trainingSetFile, string seedFile, string configFile, string resultsFile, bool validateSeed = false)
        private static void RunExperiment(string configFile, bool validateSeed = false)
        {
            Console.Write("Building Markov model...");
            
            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(configFile);
            XmlElement xmlConfigElement = xmlConfig.DocumentElement;

            // Set Training File
            string trainingSetFile = XmlUtils.GetValueAsString(xmlConfigElement, "TrainingFile");

            // Create seedFile
            string seedFile = XmlUtils.GetValueAsString(xmlConfigElement, "SeedFile");

            // Create results file.
            string resultsFile = XmlUtils.GetValueAsString(xmlConfigElement, "ResultsFile");

            Console.WriteLine("\nTraining File: {0}\nSeed File: {1}\nResults File: {2}", trainingSetFile, seedFile, resultsFile);
            

            // Load the training set passwords from file
            var passwords = PasswordUtil.LoadPasswords(trainingSetFile, 8); 
            
            // Create a Markov model from the passwords. This model will be used
            // as our seed for the evolution.
            int outputs = MarkovFilterCreator.GenerateFirstOrderMarkovFilter(seedFile, passwords);

            // Free up the memory used by the passwords
            passwords = null;

            Console.WriteLine("Done! Outputs: {0}", outputs);

            _experiment = new PasswordEvolutionExperiment();
            _experiment.OutputCount = outputs;
            
            // Initialize the experiment with the specifications in the config file.
            _experiment.Initialize("PasswordEvolution", xmlConfig.DocumentElement);

            // Set the passwords to be used by the fitness evaluator.
            // These are the passwords our models will try to guess.
            // PasswordsWithAccounts is the file used for validation. Its account values won't be changed.
            PasswordCrackingEvaluator.Passwords = _experiment.Passwords;
            PasswordCrackingEvaluator.PasswordsWithAccounts = new Dictionary<string,double>(_experiment.Passwords); // Makes a deep copy

            Console.WriteLine("Loading seed...");
            
            // Load the seed model that we created at the start of this function
            var seed = _experiment.LoadPopulation(XmlReader.Create(seedFile))[0];

            // Validates the seed model by running it for a large number of guesses 
            if (validateSeed)
            {
                Console.WriteLine("Validating seed model...");
                var seedModel = _experiment.CreateGenomeDecoder().Decode(seed);
                ValidateModel(seedModel, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
            }

            // Create evolution algorithm using the seed model to initialize the population
            Console.WriteLine("Creating population...");
            _ea = _experiment.CreateEvolutionAlgorithm(seed);

            // Attach an update event handler. This will be called at the end of every generation
            // to log the progress of the evolution (see function logEvolutionProgress below).
            _ea.UpdateEvent += new EventHandler(logEvolutionProgress);
            //_ea.UpdateScheme = new UpdateScheme(1);//.UpdateMode.
            
            // Setup results file
            using (TextWriter writer = new StreamWriter(resultsFile))
                writer.WriteLine("Generation,Champion Accounts,Champion Uniques,Average Accounts,Average Uniques,Total Accounts,Total Uniques");
            _generationalResultsFile = resultsFile;

            // Start algorithm (it will run on a background thread).
            Console.WriteLine("Starting evolution. Pop size: {0} Guesses: {1}", _experiment.DefaultPopulationSize, _experiment.GuessesPerIndividual);
            _ea.StartContinue();

            // Wait until the evolution is finished.
            while (_ea.RunState == RunState.Running) { Thread.Sleep(1000); }

            // Validate the resulting model.
            var decoder = _experiment.CreateGenomeDecoder();
            var champ = decoder.Decode(_ea.CurrentChampGenome);
            ValidateModel(champ, _experiment.Passwords, VALIDATION_GUESSES, _experiment.Hashed);
        }
Example #42
0
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            var parallelOptions = new ParallelOptions();

            // 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, parallelOptions);

            // Create complexity regulation strategy.
            var complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStrategy, _complexityThreshold);

            // Create the evolution algorithm.
            var evolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(NeatEvolutionAlgorithmParameters, speciationStrategy, complexityRegulationStrategy);

            // Create genome decoder.
            var genomeDecoder = CreateGenomeDecoder();

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            var genomeListEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, _neuromonPhenomeEvaluator, parallelOptions);

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

            return evolutionAlgorithm;
        }
Example #43
0
        private void startEvolution()
        {
            gens = 0;


            // Create evolution algorithm and attach update event.
            _ea = _experiment.CreateEvolutionAlgorithm();
            _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
            _experiment.Evaluator.LogDiversity = false;

            // Start algorithm (it will run on a background thread).
            _ea.StartContinue();
        }
        public virtual  void Initialize(string name, XmlElement xmlConfig)
        {
            _name = name;
            _populationSize = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig,
                "DefaultComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _parallelOptions = ExperimentUtils.ReadParallelOptions(xmlConfig);

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount = _specieCount;
            _neatGenomeParams = new NeatGenomeParameters();
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            DefaultComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(
                _complexityRegulationStr,
                _complexityThreshold);
            DefaultSpeciationStrategy = new KMeansClusteringStrategy<NeatGenome>(new ManhattanDistanceMetric(1.0, 0.0, 10.0));
            DefaultNeatEvolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(
                    NeatEvolutionAlgorithmParameters,
                    DefaultSpeciationStrategy,
                    DefaultComplexityRegulationStrategy);
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, new ParallelOptions());

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

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

           
            // Finished. Return the evolution algorithm
            return ea;
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            DoublePoleBalancingEvaluator evaluator;
            switch(_variantStr)
            {
                case "DoublePole":
                    evaluator = new DoublePoleBalancingEvaluator();
                    break;
                case "DoublePoleNv":
                    evaluator = new DoublePoleBalancingEvaluatorNv();
                    break;
                case "DoublePoleNvAntiWiggle":
                    evaluator = new DoublePoleBalancingEvaluatorNvAntiWiggle();
                    break;
                default:
                    throw new SharpNeatException(string.Format("DoublePoleBalancing experiment config XML specifies unknown variant [{0}]", _variantStr));
            }

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

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);

            // 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 genome's evaluation info object.
            IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
                                                                                    innerEvaluator,
                                                                                    SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());
            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }
    public void Initialize()
    {
        /*** Initialize experiment ***/
        experiment = new PCGNeatExperiment() as INeatExperiment;
        // Null because not reading settings from xmlFile
        experiment.Initialize("PCG Conetent EA",null);

        /*** Randomly generate population ***/
        // Set initial settings
        // ? means it is nullable
        int? popSize = experiment.DefaultPopulationSize;

        // Create a genome factory appropriate for the experiment.
        IGenomeFactory<NeatGenome> genomeFactory = experiment.CreateGenomeFactory();

        // Create an initial population of randomly generated genomes.
        // 0u is a struct for a 32 bit unsigned integer
        List<NeatGenome> genomeList = genomeFactory.CreateGenomeList(popSize.Value, 0u);

        // Check number of species is <= the number of the genomes.
        if (genomeList.Count < experiment.NeatEvolutionAlgorithmParameters.SpecieCount)
        {
            Debug.Log("Genome count must be >= specie count. Genomes=" + genomeList.Count
                + "  Species=" + experiment.NeatEvolutionAlgorithmParameters.SpecieCount);
            return;
        }

        /*** Create the algorithm interface ***/
        contentEA = experiment.CreateEvolutionAlgorithm(genomeFactory, genomeList);
    }
 public PCGNeatEA()
 {
     contentEA = null;
 }
    // Initialize with pre-existing xml data
    public void Initialize(string xmlData)
    {
        /*** Initialize experiment ***/
        experiment = new PCGNeatExperiment() as INeatExperiment;
        // Null because not reading settings from xmlFile
        experiment.Initialize("PCG Conetent EA",null);

        /*** Load population of genomes from xml string ***/
        List<NeatGenome> genomeList;
        using(XmlReader xr = XmlReader.Create(new StringReader(xmlData)))
        {
            genomeList = experiment.LoadPopulation(xr);
        }

        if(genomeList.Count == 0) {
            Debug.LogError("No genomes loaded from XML data from network. Check data is being read correctly.");
            return;
        }
        else
            Debug.Log("Loaded " + genomeList.Count + " Genomes");

        /*** Create the algorithm interface ***/
        contentEA = experiment.CreateEvolutionAlgorithm(genomeList[0].GenomeFactory, genomeList);
    }
Example #50
0
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, new ParallelOptions());

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = new NullComplexityRegulationStrategy();// ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(EvoParameters, speciationStrategy, complexityRegulationStrategy);

            // Create a genome list evaluator. This packages up the genome decoder with the phenome evaluator.
            if(Evaluator == null)
                Evaluator = CreateEvaluator();

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

            // Finished. Return the evolution algorithm
            return ea;
        }
Example #51
0
    public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
    {
        IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
        ISpeciationStrategy<NeatGenome> speciationStrategy = new KMeansClusteringStrategy<NeatGenome>(distanceMetric);

        IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

        NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

        // Create black box evaluator
        SimpleEvaluator evaluator = new SimpleEvaluator(_optimizer);

        IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder = CreateGenomeDecoder();

        IGenomeListEvaluator<NeatGenome> innerEvaluator = new UnityParallelListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _optimizer);

        IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(innerEvaluator,
            SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());

        //ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);
        ea.Initialize(innerEvaluator, genomeFactory, genomeList);

        return ea;
    }
Example #52
0
        static void Main(string[] args)
        {
            // Initialise log4net (log to console).
            XmlConfigurator.Configure(new FileInfo("log4net.properties"));

            // Experiment classes encapsulate much of the nuts and bolts of setting up a NEAT search.
            XorExperiment experiment = new XorExperiment();

            // Load config XML.
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load("xor.config.xml");
            experiment.Initialize("XOR", xmlConfig.DocumentElement);

            WriteHelp();

            // Read key commands from the console.
            for(;;)
            {
                // Read command.
                Console.Write(">");
                string cmdstring = Console.ReadLine();

                // Parse command.
                string[] cmdArgs = cmdstring.Split(' ');

                try
                {
                    // Process command.
                    switch(cmdArgs[0])
                    {
                        // Init commands.
                        case "randpop":
                        {
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot create population while algorithm is running.");
                                break;
                            }

                            // Attempt to parse population size arg.
                            if(cmdArgs.Length <= 1) {
                                Console.WriteLine("Error. Missing {size} argument.");
                                break;
                            }

                            int populationSize;
                            if(!int.TryParse(cmdArgs[1], out populationSize)) {
                                Console.WriteLine(string.Format("Error. Invalid {size} argument [{0}].", cmdArgs[1]));
                                break;
                            }

                            // Create a genome factory with our neat genome parameters object and the appropriate number of input and output neuron genes.
                            _genomeFactory = experiment.CreateGenomeFactory();

                            // Create an initial population of randomly generated genomes.
                            _genomeList = _genomeFactory.CreateGenomeList(populationSize, 0);
                            Console.WriteLine(string.Format("Created [{0}] random genomes.", populationSize));
                            break;
                        }
                        case "loadpop":
                        {
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot load population while algorithm is running.");
                                break;
                            }

                            // Attempt to get population filename arg.
                            if(cmdArgs.Length <= 1) {
                                Console.WriteLine("Error. Missing {filename} argument.");
                                break;
                            }

                            // Open and load population XML file.
                            using(XmlReader xr = XmlReader.Create(cmdArgs[1])) {
                                _genomeList = experiment.LoadPopulation(xr);
                            }
                            _genomeFactory = _genomeList[0].GenomeFactory;
                            Console.WriteLine(string.Format("Loaded [{0}] genomes.", _genomeList.Count));
                            break;
                        }
                        case "loadseed":
                        {   
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot load population while algorithm is running.");
                                break;
                            }

                            // Attempt to get genome filename arg.
                            if(cmdArgs.Length <= 1) {
                                Console.WriteLine("Error. Missing {filename} argument.");
                                break;
                            }

                            // Attempt to parse population size arg.
                            if(cmdArgs.Length <= 2) {
                                Console.WriteLine("Error. Missing {size} argument.");
                                break;
                            }

                            int populationSize;
                            if(!int.TryParse(cmdArgs[1], out populationSize)) {
                                Console.WriteLine(string.Format("Error. Invalid {size} argument [{0}].", cmdArgs[1]));
                                break;
                            }

                            // Open and load genome XML file.
                            using(XmlReader xr = XmlReader.Create(cmdArgs[1])) {
                                _genomeList = experiment.LoadPopulation(xr);
                            }

                            if(_genomeList.Count == 0) {
                                Console.WriteLine(string.Format("No genome loaded from file [{0}]", cmdArgs[1]));
                                _genomeList = null;
                                break;;
                            }

                            // Create genome list from seed.
                            _genomeFactory = _genomeList[0].GenomeFactory;
                            _genomeList = _genomeFactory.CreateGenomeList(populationSize, 0u, _genomeList[0]);
                            Console.WriteLine(string.Format("Created [{0}] genomes from loaded seed genome.", _genomeList.Count));
                            break;
                        }

                        // Execution control commands.
                        case "start":
                        {
                            if(null == _ea) 
                            {   // Create new evolution algorithm.
                                if(null == _genomeList) {
                                    Console.WriteLine("Error. No loaded genomes");
                                    break;
                                }
                                _ea = experiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList);
                                _ea.UpdateEvent += new EventHandler(ea_UpdateEvent);
                            }

                            Console.WriteLine("Starting...");
                            _ea.StartContinue();
                            break;
                        }
                        case "stop":
                        {
                            Console.WriteLine("Stopping. Please wait...");
                            _ea.RequestPauseAndWait();
                            Console.WriteLine("Stopped.");
                            break;
                        }
                        case "reset":
                        {
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot reset while algorithm is running.");
                                break;
                            }

                            _ea = null;
                            _genomeFactory = null;
                            _genomeList = null;
                            Console.WriteLine("Reset completed.");
                            break;
                        }

                        // Genome saving commands.
                        case "savepop":
                        {
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot save population while algorithm is running.");
                                break;
                            }
                            if(null == _genomeList) {
                                Console.WriteLine("Error. No population to save.");
                                break;
                            }

                            // Attempt to get population filename arg.
                            if(cmdArgs.Length <= 1) {
                                Console.WriteLine("Error. Missing {filename} argument.");
                                break;
                            }

                            // Save genomes to xml file.
                            XmlWriterSettings xwSettings = new XmlWriterSettings();
                            xwSettings.Indent = true;
                            using(XmlWriter xw = XmlWriter.Create(cmdArgs[1], xwSettings)) {
                                experiment.SavePopulation(xw, _genomeList);
                            }
                            Console.WriteLine(string.Format("[{0}] genomes saved to file [{1}]", _genomeList.Count, cmdArgs[1]));
                            break;
                        }
                        case "savebest":
                        {
                            if(null != _ea && _ea.RunState == RunState.Running) {
                                Console.WriteLine("Error. Cannot save population while algorithm is running.");
                                break;
                            }
                            if(null == _ea || null == _ea.CurrentChampGenome) {
                                Console.WriteLine("Error. No best genome to save.");
                                break;
                            }

                            // Attempt to get genome filename arg.
                            if(cmdArgs.Length <= 1) {
                                Console.WriteLine("Error. Missing {filename} argument.");
                                break;
                            }

                            // Save genome to xml file.
                            XmlWriterSettings xwSettings = new XmlWriterSettings();
                            xwSettings.Indent = true;
                            using(XmlWriter xw = XmlWriter.Create(cmdArgs[1], xwSettings)) {
                                experiment.SavePopulation(xw, new NeatGenome[] {_ea.CurrentChampGenome});
                            }

                            Console.WriteLine(string.Format("Best genome saved to file [{1}]", _genomeList.Count, cmdArgs[1]));
                            break;
                        }

                        // Misc commands
                        case "help":
                        {
                            WriteHelp();
                            break;
                        }
                        case "quit":
                        case "exit":
                        {
                            Console.WriteLine("Stopping. Please wait...");
                            _ea.RequestPauseAndWait();
                            Console.WriteLine("Stopped.");
                            goto quit;
                        }
                        case "":
                        {
                            // Do nothing.
                            break;
                        }
                        default:
                        {
                            Console.WriteLine(string.Format("Unknown command [{0}]", cmdArgs[0]));
                            break;
                        }
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(string.Format("Exception [{0}]", ex.Message));
                }
            }

            quit:
            Console.WriteLine("bye!");
        }
Example #53
0
        private static void RunExperiment()
        {
            // Create evolution algorithm using the seed model to initialize the population
            Console.WriteLine("Creating population...");
            Console.Write("Building Markov model...");

            // Load the XML configuration file
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(cp.ConfigFile);
            
            // Set Training File
            string trainingSetFile = cp.TrainingDb;

            // Create seedFile
            string seedFile = cp.SeedFile;

            // Create results file.
            string resultsFile = cp.ResultsFile;
            Console.WriteLine();
            Console.WriteLine("Training File: {0}", trainingSetFile);
            Console.WriteLine("Seed File: {0}", seedFile);
            Console.WriteLine("Results File: {0}", resultsFile);

            // Load the training set passwords from file
            var passwords = PasswordUtil.LoadPasswords(trainingSetFile, cp.PasswordLength);


            // Create a Markov model from the passwords. This model will be used
            // as our seed for the evolution.
            int outputs = MarkovFilterCreator.GenerateFirstOrderMarkovFilter(seedFile, passwords);

            // Free up the memory used by the passwords
            passwords = null;

            Console.WriteLine("Done! Outputs: {0}", outputs);

            _experiment = new PasswordEvolutionExperiment();
            _experiment.OutputCount = outputs;

            // Initialize the experiment with the specifications in the config file.
            _experiment.Initialize("PasswordEvolution", xmlConfig.DocumentElement); //cmd arguments

            if(cp.EvolutionDb != null)
			{
				Console.WriteLine("Using command-line password file for evolution: {0}", cp.EvolutionDb);
				Console.WriteLine("Password length: {0}", cp.PasswordLength);
				PasswordCrackingEvaluator.Passwords = PasswordUtil.LoadPasswords(cp.EvolutionDb, cp.PasswordLength);
				Console.WriteLine("PasswordCrackingEvaluator.Passwords = {0}", PasswordCrackingEvaluator.Passwords == null ? "NULL" : "NOT NULL");
			}
			else
			{
				// Set the passwords to be used by the fitness evaluator.
				// These are the passwords our models will try to guess.
				PasswordCrackingEvaluator.Passwords = _experiment.Passwords;
				Console.WriteLine("Using config file passwords for evolution.");
			}
			Accounts = PasswordCrackingEvaluator.Passwords;

            Console.WriteLine("Loading seed...");

            // Load the seed model that we created at the start of this function
            var seed = _experiment.LoadPopulation(XmlReader.Create(seedFile))[0];

            // Create evolution algorithm using the seed model to initialize the population
            Console.WriteLine("Creating population...");

            ce = new CondorEvaluator(cp.ExperimentDir, cp.ConfigFile, cp.ResultsFile, CondorGroup.Grad, outputs, PasswordCrackingEvaluator.Passwords, cp.PasswordLength);
            _ea = _experiment.CreateEvolutionAlgorithm(seed, ce);


            // Attach an update event handler. This will be called at the end of every generation
            // to log the progress of the evolution (see function logEvolutionProgress below).
			_ea.UpdateScheme = new UpdateScheme(1);
			_ea.UpdateEvent += new EventHandler(logEvolutionProgress);
            
            // Setup results file
            using (TextWriter writer = new StreamWriter(resultsFile))
                writer.WriteLine("Generation,Champion Accounts,Champion Uniques,Average Accounts,Average Uniques,Total Accounts,Total Uniques");
            //_generationalResultsFile = resultsFile;

            // Start algorithm (it will run on a background thread).
            Console.WriteLine("Starting evolution. Pop size: {0} Guesses: {1}", _experiment.DefaultPopulationSize, _experiment.GuessesPerIndividual);
            _ea.StartContinue();

            // Wait until the evolution is finished.
            while (_ea.RunState == RunState.Running) { Thread.Sleep(1000); }


        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

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

            // Create a genome list evaluator. This packages up the genome decoder with the phenome evaluator.
            IGenomeListEvaluator<NeatGenome> genomeListEvaluator = new ParallelCoevolutionListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, PhenomeEvaluator);

            // Wrap a hall of fame evaluator around the baseline evaluator.
            //genomeListEvaluator = new ParallelHallOfFameListEvaluator<NeatGenome, IBlackBox>(50, 0.5, ea, genomeListEvaluator, genomeDecoder, PhenomeEvaluator);

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

            // Finished. Return the evolution algorithm
            return ea;
        }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome2 population and their associated/parent genome2 factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome>[] CreateEvolutionAlgorithms(IGenomeFactory<NeatGenome> genomeFactory1, List<NeatGenome> genomeList1, 
                                                                              IGenomeFactory<NeatGenome> genomeFactory2, List<NeatGenome> genomeList2)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric1 = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy1 = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric1, _parallelOptions);

            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric2 = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy2 = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric2, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy1 = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy2 = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea1 = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy1, complexityRegulationStrategy1);
            NeatEvolutionAlgorithm<NeatGenome> ea2 = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy2, complexityRegulationStrategy2);

            // Create genome decoder.
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder1 = CreateGenomeDecoder();
            IGenomeDecoder<NeatGenome, IBlackBox> genomeDecoder2 = CreateGenomeDecoder();

            // Create phenome evaluators. Note we are evolving one population of X players and one of O players.
            ICoevolutionPhenomeEvaluator<IBlackBox> phenomeEvaluator1 = new TicTacToeHostParasiteEvaluator(SquareTypes.X);
            ICoevolutionPhenomeEvaluator<IBlackBox> phenomeEvaluator2 = new TicTacToeHostParasiteEvaluator(SquareTypes.O);

            // Create a genome list evaluator. This packages up the genome decoder with the phenome evaluator.
            HostParasiteCoevolutionListEvaluator<NeatGenome, IBlackBox> genomeListEvaluator1 = new HostParasiteCoevolutionListEvaluator<NeatGenome, IBlackBox>(_parasiteCount, _championCount, ea2, genomeDecoder1, phenomeEvaluator1);
            HostParasiteCoevolutionListEvaluator<NeatGenome, IBlackBox> genomeListEvaluator2 = new HostParasiteCoevolutionListEvaluator<NeatGenome, IBlackBox>(_parasiteCount, _championCount, ea1, genomeDecoder2, phenomeEvaluator2);

            // Initialize the evolution algorithms.
            ea1.Initialize(genomeListEvaluator1, genomeFactory1, genomeList1);
            ea2.Initialize(genomeListEvaluator2, genomeFactory2, genomeList2);

            // Set the evolution algorithms to update every generation.
            ea1.UpdateScheme = new UpdateScheme(1);
            ea2.UpdateScheme = new UpdateScheme(1);

            // Finished. Return the evolution algorithms
            return new []{ ea1, ea2 };
        }
Example #56
0
        private void btnSearchStart_Click(object sender, EventArgs e)
        {
            if(null != _ea)
            {   // Resume existing EA & update GUI state.
                _ea.StartContinue();
                UpdateGuiState();
                return;
            }

            // Initialise and start a new evolution algorithm.
            ReadAndUpdateExperimentParams();

            // Check number of species is <= the number of the genomes.
            if(_genomeList.Count < _selectedExperiment.NeatEvolutionAlgorithmParameters.SpecieCount) {
                __log.ErrorFormat("Genome count must be >= specie count. Genomes=[{0}] Species=[{1}]",
                                    _selectedExperiment.NeatEvolutionAlgorithmParameters.SpecieCount, _genomeList.Count);
                return;
            }

            // Create evolution algorithm.
            _ea = _selectedExperiment.CreateEvolutionAlgorithm(_genomeFactory, _genomeList);

            // Attach update event listener.
            _ea.UpdateEvent += new EventHandler(_ea_UpdateEvent);
            _ea.PausedEvent += new EventHandler(_ea_PausedEvent);

            // Notify any open views.
            if(null != _bestGenomeForm) { _bestGenomeForm.Reconnect(_ea); }
            if(null != _domainForm) { _domainForm.Reconnect(_ea); }
            foreach(TimeSeriesGraphForm graphForm in _timeSeriesGraphFormList) {
                graphForm.Reconnect(_ea);
            }
            foreach(SummaryGraphForm graphForm in _summaryGraphFormList) {
                graphForm.Reconnect(_ea);
            }

            // Create/open log file if the option is selected.
            if(chkFileWriteLog.Checked && null==_logFileWriter)
            {
                string filename = txtFileLogBaseName.Text + '_' + DateTime.Now.ToString("yyyyMMdd") + ".log";
                _logFileWriter = new StreamWriter(filename, true);
                _logFileWriter.WriteLine("ClockTime,Gen,BestFitness,MeanFitness,MeanSpecieChampFitness,ChampComplexity,MeanComplexity,MaxComplexity,TotalEvaluationCount,EvaluationsPerSec,SearchMode");
            }

            // Start the algorithm & update GUI state.
            _ea.StartContinue();
            UpdateGuiState();
        }
 public CPPNRepairEA()
 {
     contentEA = null;
 }
Example #58
0
 private void btnSearchReset_Click(object sender, EventArgs e)
 {
     _genomeFactory = null;
     _genomeList = null;
     // TODO: Proper cleanup of EA - e.g. main worker thread termination.
     _ea = null;
     _champGenomeFitness = 0.0;
     Logger.Clear();
     UpdateGuiState_ResetStats();
     UpdateGuiState();
 }
        /// <summary>
        /// Create and return a NeatEvolutionAlgorithm object ready for running the NEAT algorithm/search. Various sub-parts
        /// of the algorithm are also constructed and connected up.
        /// This overload accepts a pre-built genome population and their associated/parent genome factory.
        /// </summary>
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create IBlackBox evaluator.
            InvertedDoublePendulumEvaluator evaluator = new InvertedDoublePendulumEvaluator();

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

            // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
            IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator, _parallelOptions);
            //IGenomeListEvaluator<NeatGenome> innerEvaluator = new SerialGenomeListEvaluator<NeatGenome, IBlackBox>(genomeDecoder, evaluator);

            // 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 genome's evaluation info object.
            IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
                                                                                    innerEvaluator,
                                                                                    SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());
            // Initialize the evolution algorithm.
            ea.Initialize(selectiveEvaluator, genomeFactory, genomeList);

            // Finished. Return the evolution algorithm
            return ea;
        }
        public NeatEvolutionAlgorithm<NeatGenome> CreateEvolutionAlgorithm(IGenomeFactory<NeatGenome> genomeFactory, List<NeatGenome> genomeList, IGenomeListEvaluator<NeatGenome> eval = null)
        {
            // Create distance metric. Mismatched genes have a fixed distance of 10; for matched genes the distance is their weigth difference.
            IDistanceMetric distanceMetric = new ManhattanDistanceMetric(1.0, 0.0, 10.0);
            ISpeciationStrategy<NeatGenome> speciationStrategy = new ParallelKMeansClusteringStrategy<NeatGenome>(distanceMetric, _parallelOptions);

            // Create complexity regulation strategy.
            IComplexityRegulationStrategy complexityRegulationStrategy = new NullComplexityRegulationStrategy();// ExperimentUtils.CreateComplexityRegulationStrategy(_complexityRegulationStr, _complexityThreshold);

            // Create the evolution algorithm.
            NeatEvolutionAlgorithm<NeatGenome> ea = new NeatEvolutionAlgorithm<NeatGenome>(_eaParams, speciationStrategy, complexityRegulationStrategy);

            // Create the MC evaluator
            PasswordCrackingEvaluator.Passwords = _passwords;

            // Create genome decoder.
            IGenomeDecoder<NeatGenome, MarkovChain> genomeDecoder = CreateGenomeDecoder();

            // If we're running specially on Condor, skip this
            if (eval == null)
            {
                _evaluator = new PasswordCrackingEvaluator(_guesses, Hashed);

                // Create a genome list evaluator. This packages up the genome decoder with the genome evaluator.
                //    IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelGenomeListEvaluator<NeatGenome, MarkovChain>(genomeDecoder, _evaluator, _parallelOptions);
                IGenomeListEvaluator<NeatGenome> innerEvaluator = new ParallelNEATGenomeListEvaluator<NeatGenome, MarkovChain>(genomeDecoder, _evaluator, this);

                /*
                // 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 genome's evaluation info object.
                IGenomeListEvaluator<NeatGenome> selectiveEvaluator = new SelectiveGenomeListEvaluator<NeatGenome>(
                                                                                        innerEvaluator,
                                                                                        SelectiveGenomeListEvaluator<NeatGenome>.CreatePredicate_OnceOnly());
                */


                // Initialize the evolution algorithm.
                ea.Initialize(innerEvaluator, genomeFactory, genomeList);
            }
            else
                // Initialize the evolution algorithm.
                ea.Initialize(eval, genomeFactory, genomeList);

            

            // Finished. Return the evolution algorithm
            return ea;
        }