Пример #1
0
        /// <summary>
        /// Runs the master.
        /// </summary>
        /// <param name="args">
        /// Arguments to configure the run, e.g. population size or port to use.
        /// </param>
        /// <param name="algorithmTunerBuilder">
        /// A function creating a <see cref="AlgorithmTuner{TTargetAlgorithm, TInstance, TResult, TLearnerModel, TPredictorModel, TSamplingStrategy}"/> instance.
        /// </param>
        /// <returns>
        /// The <see cref="Dictionary{String, IAllele}"/>, containing the best configuration.
        /// </returns>
        public static Dictionary <string, IAllele> Run(
            string[] args,
            Func <AlgorithmTunerConfiguration, string, string,
                  AlgorithmTuner <TTargetAlgorithm, TInstance, TResult, TLearnerModel, TPredictorModel, TSamplingStrategy> > algorithmTunerBuilder)
        {
            ProcessUtils.SetDefaultCultureInfo(CultureInfo.InvariantCulture);
            LoggingHelper.Configure($"consoleOutput_Master_{ProcessUtils.GetCurrentProcessId()}.log");
            Randomizer.Configure();

            var argsParser = new MasterArgumentParser();

            if (!ArgumentParserUtils.ParseArguments(argsParser, args))
            {
                return(null);
            }

            var configuration = CreateAlgorithmTunerConfiguration(argsParser);

            LoggingHelper.ChangeConsoleLoggingLevel(configuration.Verbosity);
            LoggingHelper.WriteLine(VerbosityLevel.Info, $"Configuration:{Environment.NewLine}{configuration}");

            // Create status file directories.
            Directory.CreateDirectory(configuration.StatusFileDirectory);
            if (configuration.ZipOldStatusFiles)
            {
                Directory.CreateDirectory(configuration.ZippedStatusFileDirectory);
            }

            using var runner = algorithmTunerBuilder.Invoke(
                      configuration,
                      argsParser.PathToTrainingInstanceFolder,
                      argsParser.PathToTestInstanceFolder);

            if (argsParser.StartFromExistingStatus)
            {
                runner.UseStatusDump(Path.Combine(configuration.StatusFileDirectory, AlgorithmTunerConfiguration.FileName));
            }

            if (configuration.EnableDataRecording)
            {
                runner.PrepareDataRecordDirectory(argsParser.StartFromExistingStatus);
            }

            // Run algorithm tuner.
            var bestParameters = runner.Run();

            LoggingHelper.WriteLine(
                VerbosityLevel.Info,
                $"Best Configuration:{Environment.NewLine}{string.Join(Environment.NewLine, bestParameters.Select(keyValuePair => $"{keyValuePair.Key}: {keyValuePair.Value}"))}");

            runner.CompleteAndExportGenerationHistory();

            return(bestParameters);
        }
Пример #2
0
        /// <summary>
        /// Creates a <see cref="AlgorithmTunerConfiguration"/> object depending on what the provided
        /// <see cref="MasterArgumentParser"/> has parsed.
        /// </summary>
        /// <param name="argsParser">
        /// <see cref="MasterArgumentParser"/> which has already parsed arguments.
        /// </param>
        /// <returns>
        /// The created <see cref="AlgorithmTunerConfiguration"/>.
        /// </returns>
        private static AlgorithmTunerConfiguration CreateAlgorithmTunerConfiguration(MasterArgumentParser argsParser)
        {
            var akkaConfiguration = CustomizeAkkaConfiguration(
                argsParser.OwnHostName,
                argsParser.Port,
                argsParser.ConfigurationBuilder.Verbosity >= VerbosityLevel.Trace);
            var builderForParsedConfiguration =
                argsParser.ConfigurationBuilder.SetAkkaConfiguration(akkaConfiguration);

            // If the tuning is not a continued one, just use the parsed arguments.
            if (!argsParser.StartFromExistingStatus)
            {
                return(builderForParsedConfiguration.Build());
            }

            // Else base the configuration on the one stored in the status file.
            var oldConfig = StatusBase.ReadFromFile <Status <TInstance, TResult, TLearnerModel, TPredictorModel, TSamplingStrategy> >(
                Path.Combine(argsParser.StatusFileDirectory, AlgorithmTunerConfiguration.FileName))
                            .Configuration;

            return(builderForParsedConfiguration.BuildWithFallback(oldConfig));
        }