Esempio n. 1
0
        private static void LogBestParameters(
            Dictionary <string, IAllele> bestParameters,
            BbobRunnerConfiguration bbobConfig,
            string pathToInstanceFolder)
        {
            var bestParamsConsole = string.Join(
                " ",
                GenericBbobEntryPoint <TLearnerModel, TPredictorModel, TSamplingStrategy> .FormatParametersAsEnumerable(bestParameters));

            // evaluate the best found config on all instances. print commands to execute python + compute average performance
            var pythonCommand = string.Concat(
                bbobConfig.PythonBin,
                " ",
                bbobConfig.PathToExecutable,
                $" {bbobConfig.FunctionId} {"{0}"} ",
                bestParamsConsole);
            var instances = BbobUtils.CreateInstanceList(pathToInstanceFolder);

            Thread.Sleep(500);
            LoggingHelper.WriteLine(VerbosityLevel.Info, "################################################");
            LoggingHelper.WriteLine(VerbosityLevel.Info, "Commands to evaluate parameters:");

            var instanceResults = new List <double>(instances.Count);

            foreach (var instance in instances)
            {
                var bbobRunner = new BbobRunner(bbobConfig.FunctionId, bestParameters, bbobConfig.PythonBin, bbobConfig.PathToExecutable);
                var runTask    = bbobRunner.Run(instance, new CancellationToken(false));

                var currentResult = runTask.Result;

                // read instance id
                var instanceId = -1;
                using (var reader = File.OpenText(instance.Path))
                {
                    instanceId = int.Parse(reader.ReadLine());
                }

                var finalCommand = string.Format(pythonCommand, instanceId);
                LoggingHelper.WriteLine(VerbosityLevel.Info, finalCommand, false);
                LoggingHelper.WriteLine(VerbosityLevel.Info, FormattableString.Invariant($"result={currentResult.Value}"), false);
                instanceResults.Add(currentResult.Value);
            }

            LoggingHelper.WriteLine(VerbosityLevel.Info, "################################################");
            var averageResult = instanceResults.Any() ? instanceResults.Average() : double.NaN;

            if (!File.Exists("averageResults.csv"))
            {
                var header = string.Join(";", Enumerable.Range(0, instances.Count).Select(i => $"instance_{i}")) + ";average\r\n";
                File.WriteAllText("averageResults.csv", header);
            }

            var averageInvariant = string.Format(CultureInfo.InvariantCulture, "{0}\r\n", averageResult);
            var resultLine       = string.Join(";", instanceResults.Select(r => string.Format(CultureInfo.InvariantCulture, "{0}", r)));

            File.AppendAllText("averageResults.csv", string.Concat(resultLine, ";", averageInvariant));
            LoggingHelper.WriteLine(VerbosityLevel.Info, FormattableString.Invariant($"Average Result={averageResult}"));
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a cancellable task that runs the algorithm on the given instance.
        /// </summary>
        /// <param name="instance">Instance to run on.</param>
        /// <param name="cancellationToken">Token that should regurlarly be checked for cancellation.
        /// If cancellation is detected, the task has to be stopped.</param>
        /// <returns>
        /// A task that returns everything important about the run on completion.
        /// </returns>
        public Task <ContinuousResult> Run(InstanceFile instance, CancellationToken cancellationToken)
        {
            // Define process to start BBOB.
            var processInfo = this.BuildProcessStartInfo(instance);

            return(Task.Run(
                       function: () =>
            {
                // Start process.
                var timer = new Stopwatch();
                timer.Start();
                using var process = Process.Start(processInfo);
                // Process needs to be canceled if cancellation token is canceled.
                var processRegistration = cancellationToken.Register(
                    () => ProcessUtils.CancelProcess(process));

                // Wait until end of process.
                process.WaitForExit();

                // If the process was cancelled, clean up resources and escalate it up.
                if (cancellationToken.IsCancellationRequested)
                {
                    this.CleanUp(process, processRegistration);
                    cancellationToken.ThrowIfCancellationRequested();
                }

                // If the process was not cancelled, first check the console output for result.
                var output = process.StandardOutput.ReadToEnd();
                timer.Stop();

                var functionValue = BbobRunner.ExtractFunctionValue(output);

                // Then clean up resources.
                this.CleanUp(process, processRegistration);

                // Finally return the result.
                return new ContinuousResult(functionValue, timer.Elapsed);
            },
                       cancellationToken: cancellationToken));
        }