예제 #1
0
        private static StopCondition ReadStopCondition(string type, string valStr)
        {
            StopCondition sc = new StopCondition();

            switch (type.ToLowerInvariant())
            {
            case "secs":
                sc.StopConditionType = StopConditionType.ElapsedClockTime;
                break;

            case "gens":
                sc.StopConditionType = StopConditionType.GenerationCount;
                break;

            default:
                Console.WriteLine($"Invalid stop condition type [{type}]");
                return(null);
            }

            int val;

            if (!int.TryParse(valStr, out val) || val <= 0)
            {
                Console.WriteLine($"Invalid stop condition value [${valStr}]");
                return(null);
            }
            sc.Value = val;
            return(sc);
        }
예제 #2
0
 private static IEvolutionAlgorithmHost CreateEvolutionAlgorithmHost(
     INeatExperiment <double> experiment,
     StopCondition stopCond)
 {
     return(stopCond.StopConditionType switch
     {
         StopConditionType.ElapsedClockTime => new EvolutionAlgorithmHostClockTime(experiment, stopCond.Value),
         StopConditionType.GenerationCount => new EvolutionAlgorithmHostGenerational(experiment, stopCond.Value),
         _ => throw new ArgumentException(nameof(stopCond)),
     });
예제 #3
0
        static void Main(string[] args)
        {
            // Intercept termination of the console app, to flush and close the output file stream
            // (apparently the 'finally' block below is not executed if the app is terminated with Ctrl-C).
            Console.CancelKeyPress += delegate {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            };

            // Read command line arguments.
            StopCondition stopCond = ArgUtils.ReadArgs(args, out string experimentId, out string filename);

            if (null == stopCond)
            {
                return;
            }

            // Initialise log4net (log to console).
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.properties"));

            // Create and configure a NEAT experiment instance.
            INeatExperiment <double> experiment = InitExperiment(experimentId);

            if (null == experiment)
            {
                return;
            }

            // Create an evolution algorithm host.
            IEvolutionAlgorithmHost eaHost = CreateEvolutionAlgorithmHost(experiment, stopCond);

            // Open and initialise the output file.
            __streamWriter = InitOutputFile(filename);
            try
            {
                // Run the main eficay sampling loop until the process is terminated.
                for (;;)
                {
                    Sample s = eaHost.Sample();
                    __streamWriter.WriteLine($"{s.ElapsedTimeSecs},{s.GenerationCount},{s.BestFitness:0.#####},{s.MeanFitness:0.#####},{s.MaxComplexity:0.#####},{s.MeanComplexity:0.#####},{s.EvaluationCount}");
                    __streamWriter.Flush();
                }
            }
            finally
            {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            }
        }
예제 #4
0
 private void Block(StopCondition stopCond)
 {
     // Enter monitor loop.
     if (stopCond.StopConditionType == StopConditionType.ElapsedClockTime)
     {
         Block(TimeSpan.FromSeconds(stopCond.Value));
     }
     else
     {
         Block(stopCond.Value);
     }
 }
예제 #5
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += delegate {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            };

            string        experimentId;
            string        filename;
            StopCondition stopCond = ArgUtils.ReadArgs(args, out experimentId, out filename);

            if (null == stopCond)
            {
                return;
            }

            // Initialise NEAT experiment.
            IGuiNeatExperiment experiment = InitExperiment(experimentId);

            if (null == experiment)
            {
                return;
            }

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

            // Initialise evolution algorithm host.
            EvolutionAlgorithmHost eaHost = new EvolutionAlgorithmHost(experiment, stopCond);

            __streamWriter = InitOutputFile(filename);
            try
            {
                for (;;)
                {
                    double secs;
                    int    gens;
                    double fitness = eaHost.Sample(out secs, out gens);
                    __streamWriter.WriteLine($"{secs},{gens},{fitness}");
                    __streamWriter.Flush();
                }
            }
            finally
            {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            }
        }
예제 #6
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += delegate {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            };

            string        experimentId;
            string        filename;
            StopCondition stopCond = ArgUtils.ReadArgs(args, out experimentId, out filename);

            if (null == stopCond)
            {
                return;
            }

            // Initialise NEAT experiment.
            IGuiNeatExperiment experiment = InitExperiment(experimentId);

            if (null == experiment)
            {
                return;
            }

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

            // Initialise evolution algorithm host.
            EvolutionAlgorithmHost eaHost = new EvolutionAlgorithmHost(experiment, stopCond);

            __streamWriter = InitOutputFile(filename);
            try
            {
                for (;;)
                {
                    Sample s = eaHost.Sample();
                    __streamWriter.WriteLine($"{s.ElapsedTimeSecs},{s.GenerationCount},{s.BestFitness:0.#####},{s.MeanFitness:0.#####},{s.MaxComplexity:0.#####},{s.MeanComplexity:0.#####},{s.EvaluationCount}");
                    __streamWriter.Flush();
                }
            }
            finally
            {
                if (null != __streamWriter)
                {
                    __streamWriter.Close();
                }
            }
        }
예제 #7
0
        private static IEvolutionAlgorithmHost CreateEvolutionAlgorithmHost(
            INeatExperiment <double> experiment,
            StopCondition stopCond)
        {
            switch (stopCond.StopConditionType)
            {
            case StopConditionType.ElapsedClockTime:
                return(new EvolutionAlgorithmHostClockTime(experiment, stopCond.Value));

            case StopConditionType.GenerationCount:
                return(new EvolutionAlgorithmHostGenerational(experiment, stopCond.Value));

            default:
                throw new ArgumentException(nameof(stopCond));
            }
        }
예제 #8
0
        private void Block(StopCondition stopCond)
        {
            // Enter monitor loop.
            switch (stopCond.StopConditionType)
            {
            case StopConditionType.ElapsedClockTime:
                Block(TimeSpan.FromSeconds(stopCond.Value));
                break;

            case StopConditionType.GenerationCount:
                BlockUntilGeneration(stopCond.Value);
                break;

            default:
                throw new ArgumentException(nameof(stopCond));
            }
        }
예제 #9
0
        public static StopCondition ReadArgs(string[] args, out string experimentId, out string filename)
        {
            experimentId = null;
            filename     = null;
            if (args.Length == 1 && args[0] == "sysinfo")
            {
                SysInfo.DumpSystemInfo();
                return(null);
            }

            if (args.Length == 4)
            {
                experimentId = args[0];
                StopCondition sc = ReadStopCondition(args[1], args[2]);

                // output filename
                filename = args[3];
                return(sc);
            }

            PrintHelp();
            return(null);
        }
예제 #10
0
        public static StopCondition ReadArgs(string[] args, out string experimentId, out string filename)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Format is:");
                Console.WriteLine("  efic {experiment} secs {n} {outputfilename}");
                Console.WriteLine("  efic {experiment} gens {n} {outputfilename}");

                Console.WriteLine("");
                Console.WriteLine("  Experiment options are:  ");
                Console.WriteLine("    binary11");
                Console.WriteLine("    sinewave");
                experimentId = null;
                filename     = null;
                return(null);
            }

            experimentId = args[0];
            StopCondition sc = ReadStopCondition(args[1], args[2]);

            // output filename
            filename = args[3];
            return(sc);
        }
예제 #11
0
 public EvolutionAlgorithmHost(IGuiNeatExperiment experiment, StopCondition stopCond)
 {
     _experiment = experiment;
     _stopCond   = stopCond;
     _stopwatch  = new Stopwatch();
 }