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