Пример #1
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 (__streamWriter is not null)
            {
                __streamWriter.Close();
            }
        };

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

        if (stopCond is null || experimentId is null || filename is null)
        {
            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 (experiment is null)
        {
            return;
        }

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

        // Open and initialise the output file.
        __streamWriter = InitOutputFile(filename);
        try
        {
            // Run the main efficacy 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 (__streamWriter is not null)
            {
                __streamWriter.Close();
            }
        }
    }