[14741] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Threading;
|
---|
| 26 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
| 27 | using HeuristicLab.Analysis;
|
---|
| 28 | using HeuristicLab.Common;
|
---|
| 29 | using HeuristicLab.Core;
|
---|
| 30 | using HeuristicLab.Data;
|
---|
| 31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 32 | using HeuristicLab.Optimization;
|
---|
| 33 | using HeuristicLab.Parameters;
|
---|
| 34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[15064] | 36 | using HeuristicLab.Problems.Instances;
|
---|
[14741] | 37 | using HeuristicLab.Random;
|
---|
| 38 |
|
---|
[14768] | 39 | namespace HeuristicLab.Algorithms.EGO {
|
---|
[14741] | 40 | [StorableClass]
|
---|
| 41 | [Creatable(CreatableAttribute.Categories.Algorithms, Priority = 95)]
|
---|
[15064] | 42 | [Item("EfficientGlobalOptimizationAlgorithm", "Solves a problem by sequentially learning a model, solving a subproblem on the model and evaluating the best found solution for this subproblem.")]
|
---|
[14768] | 43 | public class EfficientGlobalOptimizationAlgorithm : BasicAlgorithm, ISurrogateAlgorithm<RealVector> {
|
---|
[14741] | 44 | #region Basic-Alg-Essentials
|
---|
| 45 | public override bool SupportsPause => true;
|
---|
| 46 | public override Type ProblemType => typeof(SingleObjectiveBasicProblem<IEncoding>);
|
---|
[15338] | 47 | public new SingleObjectiveBasicProblem<IEncoding> Problem {
|
---|
[14741] | 48 | get { return (SingleObjectiveBasicProblem<IEncoding>)base.Problem; }
|
---|
| 49 | set { base.Problem = value; }
|
---|
| 50 | }
|
---|
| 51 | #endregion
|
---|
| 52 |
|
---|
| 53 | #region ParameterNames
|
---|
| 54 | private const string GenerationSizeParameterName = "GenerationSize";
|
---|
| 55 | private const string InfillCriterionParameterName = "InfillCriterion";
|
---|
| 56 | private const string InfillOptimizationAlgorithmParameterName = "InfillOptimizationAlgorithm";
|
---|
| 57 | private const string InfillOptimizationRestartsParameterName = "InfillOptimizationRestarts";
|
---|
| 58 | private const string InitialEvaluationsParameterName = "Initial Evaluations";
|
---|
[14818] | 59 | private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
|
---|
[14741] | 60 | private const string MaximumRuntimeParameterName = "Maximum Runtime";
|
---|
| 61 | private const string RegressionAlgorithmParameterName = "RegressionAlgorithm";
|
---|
| 62 | private const string SeedParameterName = "Seed";
|
---|
| 63 | private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
|
---|
[14768] | 64 | private const string MaximalDataSetSizeParameterName = "MaximalDataSetSize";
|
---|
[14818] | 65 | private const string RemoveDuplicatesParamterName = "RemoveDuplicates";
|
---|
| 66 | private const string InitialSamplesParameterName = "InitialSamplesFile";
|
---|
| 67 | private const string BaselineVectorParameterName = "BaselineVector";
|
---|
[14833] | 68 | private const string InitialSamplingPlanParamterName = "InitialSamplingPlan";
|
---|
[14741] | 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | #region ResultNames
|
---|
| 72 | private const string BestQualityResultName = "Best Quality";
|
---|
| 73 | private const string BestSolutionResultName = "Best Solution";
|
---|
| 74 | private const string EvaluatedSoultionsResultName = "EvaluatedSolutions";
|
---|
| 75 | private const string IterationsResultName = "Iterations";
|
---|
| 76 | private const string RegressionSolutionResultName = "Model";
|
---|
| 77 | private const string QualitiesChartResultName = "Qualities";
|
---|
| 78 | private const string BestQualitiesRowResultName = "Best Quality";
|
---|
| 79 | private const string CurrentQualitiesRowResultName = "Current Quality";
|
---|
| 80 | private const string WorstQualitiesRowResultName = "Worst Quality";
|
---|
| 81 | #endregion
|
---|
| 82 |
|
---|
| 83 | #region ParameterProperties
|
---|
| 84 | public IFixedValueParameter<IntValue> GenerationSizeParemeter => Parameters[GenerationSizeParameterName] as IFixedValueParameter<IntValue>;
|
---|
| 85 | public IConstrainedValueParameter<IInfillCriterion> InfillCriterionParameter => Parameters[InfillCriterionParameterName] as IConstrainedValueParameter<IInfillCriterion>;
|
---|
| 86 | public IValueParameter<Algorithm> InfillOptimizationAlgorithmParameter => Parameters[InfillOptimizationAlgorithmParameterName] as IValueParameter<Algorithm>;
|
---|
| 87 | public IFixedValueParameter<IntValue> InfillOptimizationRestartsParemeter => Parameters[InfillOptimizationRestartsParameterName] as IFixedValueParameter<IntValue>;
|
---|
| 88 | public IFixedValueParameter<IntValue> InitialEvaluationsParameter => Parameters[InitialEvaluationsParameterName] as IFixedValueParameter<IntValue>;
|
---|
[14818] | 89 | public IFixedValueParameter<IntValue> MaximumEvaluationsParameter => Parameters[MaximumEvaluationsParameterName] as IFixedValueParameter<IntValue>;
|
---|
[14741] | 90 | public IFixedValueParameter<IntValue> MaximumRuntimeParameter => Parameters[MaximumRuntimeParameterName] as IFixedValueParameter<IntValue>;
|
---|
| 91 | public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
|
---|
| 92 | public IFixedValueParameter<IntValue> SeedParameter => Parameters[SeedParameterName] as IFixedValueParameter<IntValue>;
|
---|
[14768] | 93 | public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter => Parameters[SetSeedRandomlyParameterName] as IFixedValueParameter<BoolValue>;
|
---|
| 94 | public IFixedValueParameter<IntValue> MaximalDataSetSizeParameter => Parameters[MaximalDataSetSizeParameterName] as IFixedValueParameter<IntValue>;
|
---|
[14818] | 95 | public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => Parameters[RemoveDuplicatesParamterName] as IFixedValueParameter<BoolValue>;
|
---|
| 96 | public IFixedValueParameter<FileValue> InitialSamplesParameter => Parameters[InitialSamplesParameterName] as IFixedValueParameter<FileValue>;
|
---|
| 97 | public IValueParameter<RealVector> BaselineVectorParameter => Parameters[BaselineVectorParameterName] as IValueParameter<RealVector>;
|
---|
[15343] | 98 | public IConstrainedValueParameter<IInitialSampling<RealVector>> InitialSamplingPlanParameter => Parameters[InitialSamplingPlanParamterName] as IConstrainedValueParameter<IInitialSampling<RealVector>>;
|
---|
[14741] | 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | #region Properties
|
---|
[14818] | 102 | public int GenerationSize => GenerationSizeParemeter.Value.Value;
|
---|
| 103 | public IInfillCriterion InfillCriterion => InfillCriterionParameter.Value;
|
---|
| 104 | public Algorithm InfillOptimizationAlgorithm => InfillOptimizationAlgorithmParameter.Value;
|
---|
| 105 | public int InfillOptimizationRestarts => InfillOptimizationRestartsParemeter.Value.Value;
|
---|
| 106 | public int InitialEvaluations => InitialEvaluationsParameter.Value.Value;
|
---|
| 107 | public int MaximumEvaluations => MaximumEvaluationsParameter.Value.Value;
|
---|
| 108 | public int MaximumRuntime => MaximumRuntimeParameter.Value.Value;
|
---|
| 109 | public IDataAnalysisAlgorithm<IRegressionProblem> RegressionAlgorithm => RegressionAlgorithmParameter.Value;
|
---|
| 110 | public int Seed => SeedParameter.Value.Value;
|
---|
| 111 | public bool SetSeedRandomly => SetSeedRandomlyParameter.Value.Value;
|
---|
| 112 | public int MaximalDatasetSize => MaximalDataSetSizeParameter.Value.Value;
|
---|
| 113 | private IEnumerable<Tuple<RealVector, double>> DataSamples => Samples.Count > MaximalDatasetSize && MaximalDatasetSize > 0
|
---|
| 114 | ? Samples.Skip(Samples.Count - MaximalDatasetSize)
|
---|
| 115 | : Samples;
|
---|
| 116 | private bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value;
|
---|
| 117 | private RealVector BaselineVector => BaselineVectorParameter.Value;
|
---|
[15343] | 118 | private IInitialSampling<RealVector> InitialSamplingPlan => InitialSamplingPlanParameter.Value;
|
---|
[14741] | 119 | #endregion
|
---|
| 120 |
|
---|
| 121 | #region StorableProperties
|
---|
| 122 | [Storable]
|
---|
| 123 | private IRandom Random = new MersenneTwister();
|
---|
| 124 | [Storable]
|
---|
| 125 | private List<Tuple<RealVector, double>> Samples;
|
---|
[14768] | 126 | [Storable]
|
---|
| 127 | private List<Tuple<RealVector, double>> InitialSamples;
|
---|
[14741] | 128 | #endregion
|
---|
| 129 |
|
---|
| 130 | #region ResultsProperties
|
---|
[15338] | 131 | private double ResultsBestQuality {
|
---|
[14741] | 132 | get { return ((DoubleValue)Results[BestQualityResultName].Value).Value; }
|
---|
| 133 | set { ((DoubleValue)Results[BestQualityResultName].Value).Value = value; }
|
---|
| 134 | }
|
---|
[15338] | 135 | private RealVector ResultsBestSolution {
|
---|
[14741] | 136 | get { return (RealVector)Results[BestSolutionResultName].Value; }
|
---|
| 137 | set { Results[BestSolutionResultName].Value = value; }
|
---|
| 138 | }
|
---|
[15338] | 139 | private int ResultsEvaluations {
|
---|
[14741] | 140 | get { return ((IntValue)Results[EvaluatedSoultionsResultName].Value).Value; }
|
---|
| 141 | set { ((IntValue)Results[EvaluatedSoultionsResultName].Value).Value = value; }
|
---|
| 142 | }
|
---|
[15338] | 143 | private int ResultsIterations {
|
---|
[14741] | 144 | get { return ((IntValue)Results[IterationsResultName].Value).Value; }
|
---|
| 145 | set { ((IntValue)Results[IterationsResultName].Value).Value = value; }
|
---|
| 146 | }
|
---|
[14818] | 147 | private DataTable ResultsQualities => (DataTable)Results[QualitiesChartResultName].Value;
|
---|
| 148 | private DataRow ResultsQualitiesBest => ResultsQualities.Rows[BestQualitiesRowResultName];
|
---|
| 149 | private DataRow ResultsQualitiesWorst => ResultsQualities.Rows[WorstQualitiesRowResultName];
|
---|
| 150 | private DataRow ResultsQualitiesIteration => ResultsQualities.Rows[CurrentQualitiesRowResultName];
|
---|
[15338] | 151 | private IRegressionSolution ResultsModel {
|
---|
[14741] | 152 | get { return (IRegressionSolution)Results[RegressionSolutionResultName].Value; }
|
---|
| 153 | set { Results[RegressionSolutionResultName].Value = value; }
|
---|
| 154 | }
|
---|
| 155 | #endregion
|
---|
| 156 |
|
---|
| 157 | #region HLConstructors
|
---|
| 158 | [StorableConstructor]
|
---|
| 159 | protected EfficientGlobalOptimizationAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 160 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[15064] | 161 | protected void AfterDeseialization() {
|
---|
[14741] | 162 | RegisterEventhandlers();
|
---|
| 163 | }
|
---|
[15064] | 164 | protected EfficientGlobalOptimizationAlgorithm(EfficientGlobalOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) {
|
---|
[14741] | 165 | Random = cloner.Clone(Random);
|
---|
| 166 | if (original.Samples != null) Samples = original.Samples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
|
---|
[15064] | 167 | if (original.InitialSamples != null) InitialSamples = original.InitialSamples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
|
---|
[14741] | 168 | RegisterEventhandlers();
|
---|
| 169 | }
|
---|
| 170 | public override IDeepCloneable Clone(Cloner cloner) { return new EfficientGlobalOptimizationAlgorithm(this, cloner); }
|
---|
| 171 | public EfficientGlobalOptimizationAlgorithm() {
|
---|
[15064] | 172 | IProblemInstanceExporter dummy = new RegressionProblem(); //this variable is irrelevant
|
---|
| 173 | //the dummy variable enforces a using-Statement for HeuristicLab.Problems.Instances
|
---|
| 174 | //"new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>" requires no using using-Statement, but nontheless it requires HeuristicLab.Problems.Instances to be referenced
|
---|
| 175 | //Having HeuristicLab.Problems.Instances referenced but not used, causes the Essential-Unit-tests to fail.
|
---|
| 176 |
|
---|
[14768] | 177 | var cmaes = new CMAEvolutionStrategy.CMAEvolutionStrategy {
|
---|
[14741] | 178 | MaximumGenerations = 300,
|
---|
| 179 | PopulationSize = 50
|
---|
| 180 | };
|
---|
| 181 | var model = new GaussianProcessRegression {
|
---|
| 182 | Problem = new RegressionProblem()
|
---|
| 183 | };
|
---|
| 184 | model.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
|
---|
[14818] | 185 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(int.MaxValue)));
|
---|
[14741] | 186 | Parameters.Add(new FixedValueParameter<IntValue>(InitialEvaluationsParameterName, "", new IntValue(10)));
|
---|
[14833] | 187 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeParameterName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(-1)));
|
---|
[14741] | 188 | Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 189 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 190 | Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model));
|
---|
| 191 | Parameters.Add(new ValueParameter<Algorithm>(InfillOptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", cmaes));
|
---|
| 192 | Parameters.Add(new FixedValueParameter<IntValue>(InfillOptimizationRestartsParameterName, "Number of restarts of the SubAlgortihm to avoid local optima", new IntValue(1)));
|
---|
| 193 | Parameters.Add(new FixedValueParameter<IntValue>(GenerationSizeParameterName, "Number points that are sampled every iteration (stadard EGO: 1)", new IntValue(1)));
|
---|
[14768] | 194 | Parameters.Add(new FixedValueParameter<IntValue>(MaximalDataSetSizeParameterName, "The maximum number of sample points used to generate the model. Set 0 or less to use always all samples ", new IntValue(-1)));
|
---|
[14818] | 195 | Parameters.Add(new FixedValueParameter<BoolValue>(RemoveDuplicatesParamterName, "Wether duplicate samples should be replaced by a single sample with an averaged quality. This GREATLY decreases the chance of ill conditioned models (unbuildable models) but is not theoretically sound as the model ignores the increasing certainty in this region"));
|
---|
| 196 | Parameters.Add(new FixedValueParameter<FileValue>(InitialSamplesParameterName, "The file specifying some initial samples used to jump start the algorithm. These samples are not counted as evaluations. If InitialEvaluations is more than the samples specified in the file, the rest is uniformly random generated and evaluated.", new FileValue()));
|
---|
| 197 | Parameters.Add(new ValueParameter<RealVector>(BaselineVectorParameterName, "A vector used to create a baseline, this vector is evaluated once and is not part of the modeling process (has no influence on algorithm performance)"));
|
---|
[15064] | 198 | var eqi = new ExpectedQuantileImprovement();
|
---|
| 199 | eqi.MaxEvaluationsParameter.Value = MaximumEvaluationsParameter.Value;
|
---|
| 200 | var criteria = new ItemSet<IInfillCriterion> { new ExpectedImprovement(), new AugmentedExpectedImprovement(), new ExpectedQuality(), eqi, new MinimalQuantileCriterium(), new PluginExpectedImprovement() };
|
---|
| 201 | Parameters.Add(new ConstrainedValueParameter<IInfillCriterion>(InfillCriterionParameterName, "Decision what value should decide the next sample", criteria, criteria.First()));
|
---|
[15343] | 202 | var intialSamplingPlans = new ItemSet<IInitialSampling<RealVector>> { new UniformRandomSampling(), new LatinHyperCubeDesignCreator() };
|
---|
| 203 | Parameters.Add(new ConstrainedValueParameter<IInitialSampling<RealVector>>(InitialSamplingPlanParamterName, "Determies the initial samples from which the first model can be built.", intialSamplingPlans, intialSamplingPlans.First()));
|
---|
[14741] | 204 | SetInfillProblem();
|
---|
| 205 | RegisterEventhandlers();
|
---|
| 206 | }
|
---|
| 207 | #endregion
|
---|
[15064] | 208 | public void SetInitialSamples(RealVector[] individuals, double[] qualities) {
|
---|
| 209 | InitialSamples = individuals.Zip(qualities, (individual, d) => new Tuple<RealVector, double>(individual, d)).ToList();
|
---|
| 210 | }
|
---|
[14741] | 211 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
| 212 | base.Initialize(cancellationToken);
|
---|
| 213 | //encoding
|
---|
| 214 | var enc = Problem.Encoding as RealVectorEncoding;
|
---|
| 215 | if (enc == null) throw new ArgumentException("The EGO algorithm can only be applied to RealVectorEncodings");
|
---|
[14818] | 216 | var infillProblem = InfillOptimizationAlgorithm.Problem as InfillProblem;
|
---|
| 217 | if (infillProblem == null) throw new ArgumentException("InfillOptimizationAlgorithm has no InfillProblem. Troubles with Eventhandling?");
|
---|
[14741] | 218 |
|
---|
| 219 | //random
|
---|
| 220 | if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next();
|
---|
| 221 | Random.Reset(Seed);
|
---|
[14833] | 222 | Samples = InitialSamples?.ToList() ?? new List<Tuple<RealVector, double>>();
|
---|
[14741] | 223 |
|
---|
| 224 | //results
|
---|
| 225 | Results.Add(new Result(IterationsResultName, new IntValue(0)));
|
---|
[14768] | 226 | Results.Add(new Result(EvaluatedSoultionsResultName, new IntValue(Samples.Count)));
|
---|
[14741] | 227 | Results.Add(new Result(BestSolutionResultName, new RealVector(1)));
|
---|
| 228 | Results.Add(new Result(BestQualityResultName, new DoubleValue(Problem.Maximization ? double.MinValue : double.MaxValue)));
|
---|
| 229 | Results.Add(new Result(RegressionSolutionResultName, typeof(IRegressionSolution)));
|
---|
| 230 | var table = new DataTable(QualitiesChartResultName);
|
---|
| 231 | table.Rows.Add(new DataRow(BestQualitiesRowResultName));
|
---|
| 232 | table.Rows.Add(new DataRow(WorstQualitiesRowResultName));
|
---|
| 233 | table.Rows.Add(new DataRow(CurrentQualitiesRowResultName));
|
---|
| 234 | Results.Add(new Result(QualitiesChartResultName, table));
|
---|
[14833] | 235 | if (BaselineVector != null && BaselineVector.Length == enc.Length) Results.Add(new Result("BaselineValue", new DoubleValue(Evaluate(BaselineVector).Item2)));
|
---|
[14741] | 236 |
|
---|
[14833] | 237 |
|
---|
| 238 |
|
---|
| 239 | }
|
---|
| 240 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[14741] | 241 | //initial samples
|
---|
[14768] | 242 | if (Samples.Count < InitialEvaluations) {
|
---|
[14833] | 243 | var points = InitialSamplingPlan.GetSamples(InitialEvaluations - Samples.Count, Samples.Select(x => x.Item1).ToArray(), (RealVectorEncoding)Problem.Encoding, Random);
|
---|
[14768] | 244 | foreach (var t in points) {
|
---|
[14833] | 245 | try {
|
---|
| 246 | Samples.Add(Evaluate(t));
|
---|
| 247 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[15343] | 248 | }
|
---|
| 249 | finally {
|
---|
[14833] | 250 | Analyze();
|
---|
| 251 | }
|
---|
[14768] | 252 | }
|
---|
[14741] | 253 | }
|
---|
[14833] | 254 | //adaptive samples
|
---|
[14818] | 255 | for (ResultsIterations = 0; ResultsEvaluations < MaximumEvaluations; ResultsIterations++) {
|
---|
[14741] | 256 | try {
|
---|
[14768] | 257 | ResultsModel = BuildModel(cancellationToken);
|
---|
[14818] | 258 | if (ResultsModel == null) break;
|
---|
[14741] | 259 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 260 | for (var i = 0; i < GenerationSize; i++) {
|
---|
[14818] | 261 | var samplepoint = OptimizeInfillProblem(cancellationToken);
|
---|
[14741] | 262 | var sample = Evaluate(samplepoint);
|
---|
| 263 | Samples.Add(sample);
|
---|
| 264 | cancellationToken.ThrowIfCancellationRequested();
|
---|
| 265 | }
|
---|
| 266 |
|
---|
[15343] | 267 | }
|
---|
| 268 | finally {
|
---|
[14741] | 269 | Analyze();
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | #region Eventhandling
|
---|
| 275 | private void RegisterEventhandlers() {
|
---|
| 276 | DeregisterEventhandlers();
|
---|
| 277 | RegressionAlgorithmParameter.ValueChanged += OnModelAlgorithmChanged;
|
---|
| 278 | InfillOptimizationAlgorithmParameter.ValueChanged += OnInfillOptimizationAlgorithmChanged;
|
---|
| 279 | InfillOptimizationAlgorithm.ProblemChanged += InfillOptimizationProblemChanged;
|
---|
| 280 | InfillCriterionParameter.ValueChanged += InfillCriterionChanged;
|
---|
[14818] | 281 | InitialSamplesParameter.ToStringChanged += OnInitialSamplesChanged;
|
---|
[14741] | 282 |
|
---|
[14818] | 283 |
|
---|
[14741] | 284 | }
|
---|
| 285 | private void DeregisterEventhandlers() {
|
---|
| 286 | RegressionAlgorithmParameter.ValueChanged -= OnModelAlgorithmChanged;
|
---|
| 287 | InfillOptimizationAlgorithmParameter.ValueChanged -= OnInfillOptimizationAlgorithmChanged;
|
---|
| 288 | InfillOptimizationAlgorithm.ProblemChanged -= InfillOptimizationProblemChanged;
|
---|
| 289 | InfillCriterionParameter.ValueChanged -= InfillCriterionChanged;
|
---|
[14818] | 290 | InitialSamplesParameter.ToStringChanged -= OnInitialSamplesChanged;
|
---|
[14741] | 291 | }
|
---|
| 292 | private void OnInfillOptimizationAlgorithmChanged(object sender, EventArgs args) {
|
---|
| 293 | SetInfillProblem();
|
---|
| 294 | InfillOptimizationAlgorithm.ProblemChanged += InfillOptimizationProblemChanged;
|
---|
| 295 | }
|
---|
| 296 | private void InfillOptimizationProblemChanged(object sender, EventArgs e) {
|
---|
| 297 | InfillOptimizationAlgorithm.ProblemChanged -= InfillOptimizationProblemChanged;
|
---|
| 298 | SetInfillProblem();
|
---|
| 299 | InfillOptimizationAlgorithm.ProblemChanged += InfillOptimizationProblemChanged;
|
---|
| 300 | }
|
---|
| 301 | private void InfillCriterionChanged(object sender, EventArgs e) {
|
---|
| 302 | var infillProblem = InfillOptimizationAlgorithm.Problem as InfillProblem;
|
---|
| 303 | if (infillProblem == null) throw new ArgumentException("InfillOptimizationAlgorithm has no InfillProblem. Troubles with Eventhandling?");
|
---|
| 304 | infillProblem.InfillCriterion = InfillCriterion;
|
---|
| 305 | }
|
---|
| 306 | private void OnModelAlgorithmChanged(object sender, EventArgs args) {
|
---|
| 307 | RegressionAlgorithm.Problem = new RegressionProblem();
|
---|
| 308 | }
|
---|
[15064] | 309 | private void OnInitialSamplesChanged(object sender, EventArgs args) { }
|
---|
[14741] | 310 | protected override void OnExecutionTimeChanged() {
|
---|
| 311 | base.OnExecutionTimeChanged();
|
---|
| 312 | if (CancellationTokenSource == null) return;
|
---|
| 313 | if (MaximumRuntime == -1) return;
|
---|
| 314 | if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
|
---|
| 315 | }
|
---|
[14768] | 316 | public override void Pause() {
|
---|
[14818] | 317 | if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused) InfillOptimizationAlgorithm.Stop();
|
---|
| 318 | if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) RegressionAlgorithm.Stop();
|
---|
[14768] | 319 | base.Pause();
|
---|
[14741] | 320 | }
|
---|
[14768] | 321 | public override void Stop() {
|
---|
| 322 | if (InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Started || InfillOptimizationAlgorithm.ExecutionState == ExecutionState.Paused) InfillOptimizationAlgorithm.Stop();
|
---|
| 323 | if (RegressionAlgorithm.ExecutionState == ExecutionState.Started || RegressionAlgorithm.ExecutionState == ExecutionState.Paused) RegressionAlgorithm.Stop();
|
---|
| 324 | base.Stop();
|
---|
[14741] | 325 | }
|
---|
| 326 | #endregion
|
---|
| 327 |
|
---|
| 328 | #region helpers
|
---|
[14768] | 329 | private IRegressionSolution BuildModel(CancellationToken cancellationToken) {
|
---|
[14818] | 330 | var dataset = EgoUtilities.GetDataSet(DataSamples.ToList(), RemoveDuplicates);
|
---|
[14741] | 331 | var problemdata = new RegressionProblemData(dataset, dataset.VariableNames.Where(x => !x.Equals("output")), "output");
|
---|
| 332 | problemdata.TrainingPartition.Start = 0;
|
---|
[14768] | 333 | problemdata.TrainingPartition.End = dataset.Rows;
|
---|
| 334 | problemdata.TestPartition.Start = dataset.Rows;
|
---|
| 335 | problemdata.TestPartition.End = dataset.Rows;
|
---|
[14741] | 336 |
|
---|
| 337 | //train
|
---|
| 338 | var problem = (RegressionProblem)RegressionAlgorithm.Problem;
|
---|
| 339 | problem.ProblemDataParameter.Value = problemdata;
|
---|
| 340 | var i = 0;
|
---|
| 341 | IRegressionSolution solution = null;
|
---|
[14818] | 342 |
|
---|
[15064] | 343 | while (solution == null && i++ < 100) {
|
---|
[15338] | 344 | var results = EgoUtilities.SyncRunSubAlgorithm(RegressionAlgorithm, Random.Next(int.MaxValue), cancellationToken);
|
---|
[14741] | 345 | solution = results.Select(x => x.Value).OfType<IRegressionSolution>().SingleOrDefault();
|
---|
[14768] | 346 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[14741] | 347 | }
|
---|
[14768] | 348 |
|
---|
[14818] | 349 | //try creating a model with old hyperparameters and new dataset;
|
---|
| 350 | var gp = RegressionAlgorithm as GaussianProcessRegression;
|
---|
| 351 | var oldmodel = ResultsModel as GaussianProcessRegressionSolution;
|
---|
| 352 | if (gp != null && oldmodel != null) {
|
---|
| 353 | var n = Samples.First().Item1.Length;
|
---|
| 354 | var mean = (IMeanFunction)oldmodel.Model.MeanFunction.Clone();
|
---|
| 355 | var cov = (ICovarianceFunction)oldmodel.Model.CovarianceFunction.Clone();
|
---|
| 356 | if (mean.GetNumberOfParameters(n) != 0 || cov.GetNumberOfParameters(n) != 0) throw new ArgumentException("DEBUG: assumption about fixed paramters wrong");
|
---|
| 357 | var noise = 0.0;
|
---|
| 358 | double[] hyp = { noise };
|
---|
| 359 | try {
|
---|
| 360 | var model = new GaussianProcessModel(problemdata.Dataset, problemdata.TargetVariable,
|
---|
| 361 | problemdata.AllowedInputVariables, problemdata.TrainingIndices, hyp, mean, cov);
|
---|
| 362 | model.FixParameters();
|
---|
| 363 | var sol = new GaussianProcessRegressionSolution(model, problemdata);
|
---|
| 364 | if (solution == null || solution.TrainingMeanSquaredError > sol.TrainingMeanSquaredError) {
|
---|
| 365 | solution = sol;
|
---|
| 366 | }
|
---|
[15343] | 367 | }
|
---|
| 368 | catch (ArgumentException) { }
|
---|
[14818] | 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | if (!ResultsQualities.Rows.ContainsKey("DEBUG: Degenerates")) ResultsQualities.Rows.Add(new DataRow("DEBUG: Degenerates"));
|
---|
| 373 | var row = ResultsQualities.Rows["DEBUG: Degenerates"];
|
---|
| 374 | row.Values.Add(i - 1);
|
---|
| 375 | if (solution == null) Results.Add(new Result("Status", new StringValue("The Algorithm did not return a Model")));
|
---|
| 376 | else {
|
---|
| 377 | if (!ResultsQualities.Rows.ContainsKey("DEBUG: RMSE")) ResultsQualities.Rows.Add(new DataRow("DEBUG: RMSE"));
|
---|
| 378 | row = ResultsQualities.Rows["DEBUG: RMSE"];
|
---|
| 379 | row.Values.Add(Math.Sqrt(solution.TrainingMeanSquaredError));
|
---|
| 380 | }
|
---|
| 381 |
|
---|
[14768] | 382 | RegressionAlgorithm.Runs.Clear();
|
---|
[14741] | 383 | return solution;
|
---|
| 384 | }
|
---|
[14818] | 385 | private RealVector OptimizeInfillProblem(CancellationToken cancellationToken) {
|
---|
[14741] | 386 | //parameterize and check InfillProblem
|
---|
| 387 | var infillProblem = InfillOptimizationAlgorithm.Problem as InfillProblem;
|
---|
[15064] | 388 | if (infillProblem == null) throw new ArgumentException("InfillOptimizationAlgorithm does not have an InfillProblem.");
|
---|
| 389 | if (infillProblem.InfillCriterion != InfillCriterion) throw new ArgumentException("InfillCiriterion for Problem is not correctly set.");
|
---|
| 390 | var enc = Problem.Encoding as RealVectorEncoding;
|
---|
| 391 | infillProblem.Encoding.Bounds = enc.Bounds;
|
---|
| 392 | infillProblem.Encoding.Length = enc.Length;
|
---|
| 393 | infillProblem.Initialize(ResultsModel, Problem.Maximization);
|
---|
[14741] | 394 |
|
---|
[15064] | 395 |
|
---|
| 396 |
|
---|
[14741] | 397 | RealVector bestVector = null;
|
---|
| 398 | var bestValue = infillProblem.Maximization ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
| 399 | for (var i = 0; i < InfillOptimizationRestarts; i++) {
|
---|
| 400 | //optimize
|
---|
[15338] | 401 | var res = EgoUtilities.SyncRunSubAlgorithm(InfillOptimizationAlgorithm, Random.Next(int.MaxValue), cancellationToken);
|
---|
[14818] | 402 | cancellationToken.ThrowIfCancellationRequested();
|
---|
[14741] | 403 | //extract results
|
---|
[15064] | 404 | if (!res.ContainsKey(InfillProblem.BestInfillSolutionResultName)) throw new ArgumentException("The InfillOptimizationAlgorithm did not return a best solution");
|
---|
| 405 | var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector;
|
---|
| 406 | if (!res.ContainsKey(InfillProblem.BestInfillQualityResultName)) throw new ArgumentException("The InfillOptimizationAlgorithm did not return a best quality");
|
---|
| 407 | var d = res[InfillProblem.BestInfillQualityResultName].Value as DoubleValue;
|
---|
[14741] | 408 | if (d == null || v == null) throw new ArgumentException("The InfillOptimizationAlgorithm did not return the expected result types");
|
---|
| 409 | //check for improvement
|
---|
| 410 | if (infillProblem.Maximization != d.Value > bestValue) continue;
|
---|
| 411 | bestValue = d.Value;
|
---|
| 412 | bestVector = v;
|
---|
| 413 | }
|
---|
| 414 | InfillOptimizationAlgorithm.Runs.Clear();
|
---|
| 415 | return bestVector;
|
---|
| 416 | }
|
---|
[15064] | 417 |
|
---|
[14741] | 418 | private void Analyze() {
|
---|
| 419 | ResultsEvaluations = Samples.Count;
|
---|
| 420 | var max = Samples.ArgMax(x => x.Item2);
|
---|
| 421 | var min = Samples.ArgMin(x => x.Item2);
|
---|
| 422 | var best = Samples[Problem.Maximization ? max : min];
|
---|
| 423 | ResultsBestQuality = best.Item2;
|
---|
| 424 | ResultsBestSolution = best.Item1;
|
---|
| 425 | ResultsQualitiesBest.Values.Add(ResultsBestQuality);
|
---|
| 426 | ResultsQualitiesIteration.Values.Add(Samples[Samples.Count - 1].Item2);
|
---|
| 427 | ResultsQualitiesWorst.Values.Add(Samples[Problem.Maximization ? min : max].Item2);
|
---|
[14768] | 428 | Problem.Analyze(Samples.Select(x => GetIndividual(x.Item1)).ToArray(), Samples.Select(x => x.Item2).ToArray(), Results, Random);
|
---|
[15064] | 429 | if (Samples.Count != 0 && Samples[0].Item1.Length == 2) AnalyzeSampleDistribution();
|
---|
| 430 | AnalyzePredictionCorrelation();
|
---|
| 431 | }
|
---|
[14833] | 432 |
|
---|
[15064] | 433 | private void AnalyzeSampleDistribution() {
|
---|
| 434 | const string plotname = "DEBUG:Sample Distribution";
|
---|
| 435 | const string rowInit = "Initial Samples";
|
---|
| 436 | const string rowAll = "All Samples";
|
---|
| 437 | if (!Results.ContainsKey(plotname)) Results.Add(new Result(plotname, new ScatterPlot()));
|
---|
| 438 | var plot = (ScatterPlot)Results[plotname].Value;
|
---|
| 439 | if (!plot.Rows.ContainsKey(rowInit) && InitialSamples != null && InitialSamples.Count > 0)
|
---|
| 440 | plot.Rows.Add(new ScatterPlotDataRow(rowInit, "samples from inital file (already evaulated)", InitialSamples.Select(x => new Point2D<double>(x.Item1[0], x.Item1[1]))));
|
---|
| 441 | if (!plot.Rows.ContainsKey(rowAll)) plot.Rows.Add(new ScatterPlotDataRow(rowAll, "All samples", new Point2D<double>[0]));
|
---|
| 442 | else { plot.Rows[rowAll].Points.Clear(); }
|
---|
| 443 | plot.Rows[rowAll].Points.AddRange(Samples.Select(x => new Point2D<double>(x.Item1[0], x.Item1[1])));
|
---|
| 444 | }
|
---|
[14833] | 445 |
|
---|
[15064] | 446 | private void AnalyzePredictionCorrelation() {
|
---|
| 447 | const string plotName = "Prediction";
|
---|
| 448 | const string rowName = "Samples";
|
---|
| 449 | const string lastrowName = "Last Sample";
|
---|
| 450 | if (!Results.ContainsKey(plotName)) Results.Add(new Result(plotName, new ScatterPlot()));
|
---|
| 451 | var plot = (ScatterPlot)Results[plotName].Value;
|
---|
| 452 | if (!plot.Rows.ContainsKey(rowName)) plot.Rows.Add(new ScatterPlotDataRow(rowName, rowName, new List<Point2D<double>>()));
|
---|
| 453 | if (!plot.Rows.ContainsKey(lastrowName)) plot.Rows.Add(new ScatterPlotDataRow(lastrowName, lastrowName, new List<Point2D<double>>()));
|
---|
| 454 | var p = Samples[Samples.Count - 1];
|
---|
| 455 | if (ResultsModel != null) plot.Rows[rowName].Points.Add(new Point2D<double>(ResultsModel.Model.GetEstimation(p.Item1), p.Item2, p.Item1));
|
---|
| 456 | plot.VisualProperties.YAxisTitle = "True Objective Value";
|
---|
| 457 | plot.VisualProperties.XAxisTitle = "Predicted Objective Value";
|
---|
[14833] | 458 |
|
---|
[14741] | 459 | }
|
---|
[15064] | 460 |
|
---|
[14768] | 461 | private Individual GetIndividual(RealVector r) {
|
---|
| 462 | var scope = new Scope();
|
---|
| 463 | scope.Variables.Add(new Variable(Problem.Encoding.Name, r));
|
---|
| 464 | return new SingleEncodingIndividual(Problem.Encoding, scope);
|
---|
| 465 | }
|
---|
[15064] | 466 | private Tuple<RealVector, double> Evaluate(RealVector point) {
|
---|
| 467 | return new Tuple<RealVector, double>(point, Problem.Evaluate(GetIndividual(point), Random));
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | private void SetInfillProblem() {
|
---|
| 471 | InfillOptimizationAlgorithm.Problem = new InfillProblem { InfillCriterion = InfillCriterion };
|
---|
| 472 | }
|
---|
[14741] | 473 | #endregion
|
---|
| 474 | }
|
---|
| 475 | }
|
---|