Ejemplo n.º 1
0
        private void CreateEA()
        {
            _timer    = new Stopwatch();
            _didStart = true;

            _timer.Start();

            _logger.Info("\nCreating EA...");

            // Create evolution algorithm and attach events.
            if (SeedGenome != null)
            {
                NeatGenome seed = LoadGenomeFromXml(SeedGenome);
                seed.GenomeFactory = CreateGenomeFactory() as NeatGenomeFactory;
                _ea = CreateEvolutionAlgorithm(seed);
            }
            else
            {
                _ea = CreateEvolutionAlgorithm();
            }


            _ea.UpdateEvent += EAUpdateEvent;
            _ea.PausedEvent += EAPauseEvent;

            if (ExperimentStartedEvent != null)
            {
                try
                {
                    ExperimentEventArgs args = new ExperimentEventArgs();
                    args.Experiment = _number;
                    args.Directory  = CurrentDirectory;
                    args.TimeSpent  = _timer.Elapsed;
                    args.Comment    = _comment;

                    ExperimentStartedEvent(this, args);
                }
                catch (Exception ex)
                {
                    // Catch exceptions thrown by even listeners. This prevents listener exceptions from terminating the algorithm thread.
                    _logger.Warn($"ExperimentStartedEvent listener threw exception: {ex.Message}", ex);
                }
            }
        }
Ejemplo n.º 2
0
        private void CreateEA()
        {
            _timer    = new Stopwatch();
            _didStart = true;

            _timer.Start();

            _logger.Info("\nCreating EA...");

            // Create evolution algorithm and attach events.
            if (SeedGenome != null)
            {
                var fileInfo = new FileInfo(SeedGenome);
                var dirInfo  = new DirectoryInfo(SeedGenome);
                if (fileInfo.Exists)
                {
                    NeatGenome seed = LoadGenomeFromXml(SeedGenome);
                    seed.GenomeFactory = CreateGenomeFactory(new List <NeatGenome> {
                        seed
                    }) as NeatGenomeFactory;
                    _ea = CreateEvolutionAlgorithm(seed);
                }
                else if (dirInfo.Exists)
                {
                    var numSubDirs = dirInfo.GetDirectories().Length;
                    var subDir     = numSubDirs == 0
                        ? dirInfo
                        : dirInfo.EnumerateDirectories($"*{_number:D4}*").SingleOrDefault();
                    if (subDir != null)
                    {
                        var seeds   = subDir.EnumerateFiles("*.xml").Select(x => LoadGenomeFromXml(x.FullName)).ToList();
                        var factory = CreateGenomeFactory(seeds) as NeatGenomeFactory;
                        foreach (var neatGenome in seeds)
                        {
                            neatGenome.GenomeFactory = factory;
                        }
                        _ea = CreateEvolutionAlgorithm(factory, seeds);
                    }
                }
                else
                {
                    throw new Exception("Invalid SeedGenome parameter");
                }
            }
            else
            {
                _ea = CreateEvolutionAlgorithm();
            }


            _ea.UpdateEvent += EAUpdateEvent;
            _ea.PausedEvent += EAPauseEvent;

            if (ExperimentStartedEvent != null)
            {
                try
                {
                    ExperimentEventArgs args = new ExperimentEventArgs();
                    args.Experiment = _number;
                    args.Directory  = CurrentDirectory;
                    args.TimeSpent  = _timer.Elapsed;
                    args.Comment    = _comment;

                    ExperimentStartedEvent(this, args);
                }
                catch (Exception ex)
                {
                    // Catch exceptions thrown by even listeners. This prevents listener exceptions from terminating the algorithm thread.
                    _logger.Warn($"ExperimentStartedEvent listener threw exception: {ex.Message}", ex);
                }
            }
        }