Пример #1
0
        /// <summary>
        /// Configures the current target algorithm and gray box handler.
        /// </summary>
        /// <param name="targetAlgorithmFactory">The <see cref="ITargetAlgorithmFactory{TTargetAlgorithm,TInstance,TResult}"/>.</param>
        /// <param name="parameterTree">The <see cref="ParameterTree"/>.</param>
        /// <param name="indexOfDesiredPostTuningRun">The index of the desired post tuning run.</param>
        /// <param name="postTuningGenomeInstancePair">The post tuning genome instance pair.</param>
        private void ConfigureTargetAlgorithmAndGrayBoxHandler(
            ITargetAlgorithmFactory <TGrayBoxTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            ParameterTree parameterTree,
            int indexOfDesiredPostTuningRun,
            GenomeInstancePairStringRepresentation postTuningGenomeInstancePair)
        {
            var genomeDoubleRepresentation =
                GenomeDoubleRepresentation.GetGenomeDoubleRepresentationFromGenomeIdentifierStringRepresentation(postTuningGenomeInstancePair.Genome);
            var genomeTransformation = new GenomeTransformation <CategoricalBinaryEncoding>(parameterTree);
            var genome           = genomeTransformation.ConvertBack(genomeDoubleRepresentation);
            var runnerDictionary = genome.GetFilteredGenes(parameterTree);

            this._configuredTargetAlgorithm = targetAlgorithmFactory.ConfigureTargetAlgorithm(runnerDictionary);

            // Set generation to -1, since this is a post tuning run.
            var tunerDataRecord = new TunerDataRecord <TResult>(
                NetworkUtils.GetFullyQualifiedDomainName(),
                -1,
                0,
                postTuningGenomeInstancePair.Instance,
                double.NaN,
                genomeTransformation.GetConverterColumnHeader(),
                genomeDoubleRepresentation,
                null);

            this._grayBoxHandler = new GrayBoxHandler <TInstance, TResult>(
                this._tunerConfiguration,
                this._configuredTargetAlgorithm,
                indexOfDesiredPostTuningRun,
                tunerDataRecord,
                false,
                null,
                null);
        }
Пример #2
0
        /// <summary>
        /// Gets the <see cref="GrayBoxSimulationGenomeStatsPair{TInstance, TResult}"/>s of the given genomes on the given instances.
        /// </summary>
        /// <param name="genomes">The genomes.</param>
        /// <param name="instances">The instances.</param>
        /// <param name="currentGeneration">The current generation.</param>
        /// <returns>The <see cref="GrayBoxSimulationGenomeStatsPair{TInstance, TResult}"/>s.</returns>
        private List <GrayBoxSimulationGenomeStatsPair <TInstance, TResult> > GetGenomeStatsPairs(
            IEnumerable <ImmutableGenome> genomes,
            IReadOnlyList <TInstance> instances,
            int currentGeneration)
        {
            var listOfGenomeStatsPairs = new List <GrayBoxSimulationGenomeStatsPair <TInstance, TResult> >();

            foreach (var genome in genomes)
            {
                var genomeStringRepresentation = this._genomeStringDictionary[genome];
                var blackBoxResults            = new Dictionary <TInstance, TResult>();
                var grayBoxResults             = new Dictionary <TInstance, TResult>();

                foreach (var instance in instances)
                {
                    var currentGenomeInstancePair = new GenomeInstancePairStringRepresentation(
                        genomeStringRepresentation,
                        this._instanceStringDictionary[instance]);

                    if (!this._predictionDictionary.TryGetValue(currentGenomeInstancePair, out var resultPair))
                    {
                        throw new ArgumentException(
                                  $"The prediction dictionary does not contain an entry for the following genome instance pair.{Environment.NewLine}{currentGenomeInstancePair}");
                    }

                    if (resultPair.GenerationID == currentGeneration)
                    {
                        this._listOfBlackBoxEvaluationRunTimesInCurrentGeneration.Add(resultPair.BlackBoxResult.Runtime);
                        this._listOfGrayBoxEvaluationRunTimesInCurrentGeneration.Add(resultPair.RuntimeUntilGrayBoxCancellation);
                    }

                    blackBoxResults.Add(instance, resultPair.BlackBoxResult);
                    grayBoxResults.Add(instance, resultPair.GrayBoxResult);
                }

                listOfGenomeStatsPairs.Add(
                    new GrayBoxSimulationGenomeStatsPair <TInstance, TResult>(
                        new GenomeStats <TInstance, TResult>(genome, blackBoxResults).ToImmutable(),
                        new GenomeStats <TInstance, TResult>(genome, grayBoxResults).ToImmutable()));
            }

            if (this._listOfBlackBoxEvaluationRunTimesInCurrentGeneration.Count != this._listOfGrayBoxEvaluationRunTimesInCurrentGeneration.Count)
            {
                throw new ArgumentException(
                          "The number of run times, given by the list of black box evaluation run times, does not equal the number of run times, given by the list of gray box evaluation run times.");
            }

            return(listOfGenomeStatsPairs);
        }
Пример #3
0
        /// <summary>
        /// Writes the faulty runs to disk.
        /// </summary>
        /// <param name="faultyRunIndices">The faulty run indices.</param>
        private void LogFaultyRuns(ConcurrentBag <int> faultyRunIndices)
        {
            var file = new FileInfo(
                Path.Combine(this._postTuningFile.DirectoryName !, $"faultyPostTuningRuns_{ProcessUtils.GetCurrentProcessId()}.csv"));

            var recorder = new StringArrayRecorder(
                file,
                GenomeInstancePairStringRepresentation.GetHeader(),
                true);

            var faultyRuns = faultyRunIndices.Select(index => this._listOfPostTuningGenomeInstancePairs[index]);

            recorder.WriteRows(
                faultyRuns.Select(gip => gip.ToStringArray()).ToList());
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SinglePostTuningRunner{TTargetAlgorithm,TInstance,TResult}"/> class.
        /// </summary>
        /// <param name="tunerConfiguration">The tuner configuration.</param>
        /// <param name="targetAlgorithmFactory">The target algorithm factory.</param>
        /// <param name="parameterTree">The parameter tree.</param>
        /// <param name="postTuningGenomeInstancePair">The post tuning genome instance pair..</param>
        /// <param name="indexOfDesiredPostTuningRun">The index of the desired post tuning run.</param>
        internal SinglePostTuningRunner(
            AlgorithmTunerConfiguration tunerConfiguration,
            ITargetAlgorithmFactory <TGrayBoxTargetAlgorithm, TInstance, TResult> targetAlgorithmFactory,
            ParameterTree parameterTree,
            GenomeInstancePairStringRepresentation postTuningGenomeInstancePair,
            int indexOfDesiredPostTuningRun)
        {
            GrayBoxUtils.ValidateAdditionalPostTuningParameters(tunerConfiguration, targetAlgorithmFactory, parameterTree);

            this._tunerConfiguration = tunerConfiguration;

            this.ConfigureTargetAlgorithmAndGrayBoxHandler(
                targetAlgorithmFactory,
                parameterTree,
                indexOfDesiredPostTuningRun,
                postTuningGenomeInstancePair);

            if (!targetAlgorithmFactory.TryToGetInstanceFromInstanceId(postTuningGenomeInstancePair.Instance, out this._instance))
            {
                throw new ArgumentException($"Cannot convert given instance id {postTuningGenomeInstancePair.Instance} to valid instance.");
            }
        }