Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/MetaOptimizationProblem.cs @ 9674

Last change on this file since 9674 was 8157, checked in by ascheibe, 13 years ago

#1877 fixed metaopt for ParameterVisibilityTest

File size: 19.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Linq;
24using HeuristicLab.Algorithms.GeneticAlgorithm;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Problems.DataAnalysis;
34using HeuristicLab.Problems.TestFunctions;
35
36namespace HeuristicLab.Problems.MetaOptimization {
37  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class MetaOptimizationProblem : SingleObjectiveHeuristicOptimizationProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator>, IStorableContent {
41    public string Filename { get; set; }
42
43    public const string AlgorithmTypeParameterName = "AlgorithmType";
44    public const string ProblemTypeParameterName = "ProblemType";
45    public const string ProblemsParameterName = "Problems";
46    public const string ParameterConfigurationTreeParameterName = "ParameterConfiguration";
47    public const string RepetitionsParameterName = "Repetitions";
48    public const string QualityMeasureNameName = "QualityMeasureName";
49
50    public const string IntValueManipulatorParameterName = "IntValueManipulator";
51    public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator";
52    public const string IntValueCrossoverParameterName = "IntValueCrossover";
53    public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover";
54    public const string QualityWeightParameterName = "QualityWeight";
55    public const string StandardDeviationWeightParameterName = "StandardDeviationWeight";
56    public const string EvaluatedSolutionsWeightParameterName = "EvaluatedSolutionsWeight";
57
58
59    #region Parameter Properties
60    public IValueParameter<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
61      get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
62    }
63    public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
64      get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
65    }
66    public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
67      get { return (ValueParameter<ConstrainedItemList<IProblem>>)Parameters[ProblemsParameterName]; }
68    }
69    public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
70      get { return (OptionalValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
71    }
72    public IValueParameter<IntValue> RepetitionsParameter {
73      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
74    }
75    public IConstrainedValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
76      get { return (ConstrainedValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
77    }
78    public IConstrainedValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
79      get { return (ConstrainedValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
80    }
81    public IValueParameter<StringValue> QualityMeasureNameParameter {
82      get { return (ValueParameter<StringValue>)Parameters[QualityMeasureNameName]; }
83    }
84    public IConstrainedValueParameter<IIntValueCrossover> IntValueCrossoverParameter {
85      get { return (ConstrainedValueParameter<IIntValueCrossover>)Parameters[IntValueCrossoverParameterName]; }
86    }
87    public IConstrainedValueParameter<IDoubleValueCrossover> DoubleValueCrossoverParameter {
88      get { return (ConstrainedValueParameter<IDoubleValueCrossover>)Parameters[DoubleValueCrossoverParameterName]; }
89    }
90    #endregion
91
92    #region Properties
93    public IAlgorithm Algorithm {
94      get { return CreateAlgorithm(AlgorithmType.Value, Problems.FirstOrDefault()); }
95    }
96    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
97      get { return AlgorithmTypeParameter.Value; }
98      set { AlgorithmTypeParameter.Value = value; }
99    }
100    public ConstrainedTypeValue<IProblem> ProblemType {
101      get { return ProblemTypeParameter.Value; }
102      set { ProblemTypeParameter.Value = value; }
103    }
104    public ConstrainedItemList<IProblem> Problems {
105      get { return ProblemsParameter.Value; }
106      set { ProblemsParameter.Value = value; }
107    }
108    public ParameterConfigurationTree ParameterConfigurationTree {
109      get { return ParameterConfigurationTreeParameter.Value; }
110      set { ParameterConfigurationTreeParameter.Value = value; }
111    }
112    public IntValue Repetitions {
113      get { return RepetitionsParameter.Value; }
114      set { RepetitionsParameter.Value = value; }
115    }
116    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
117      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
118    }
119    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
120      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
121    }
122    private SolutionCacheAnalyzer RunsAnalyzer {
123      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
124    }
125    private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
126      get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
127    }
128    private PMOProblemQualitiesAnalyzer PMOProblemQualitiesAnalyzer {
129      get { return Operators.OfType<PMOProblemQualitiesAnalyzer>().FirstOrDefault(); }
130    }
131    private PMOBestSolutionHistoryAnalyzer PMOBestSolutionHistoryAnalyzer {
132      get { return Operators.OfType<PMOBestSolutionHistoryAnalyzer>().FirstOrDefault(); }
133    }
134    #endregion
135
136    public MetaOptimizationProblem()
137      : base() {
138      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
139      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
140      Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
141      Parameters.Add(new OptionalValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized.")); // needs to be optional, when last problem is removed from list, it must be set null
142      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
143      Parameters.Add(new ValueParameter<StringValue>(QualityMeasureNameName, "The name of the quality result of the base-level algorithm. Subresults can be accessed by dot separator.", new StringValue("BestQuality")));
144
145      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
146      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
147      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
148      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
149      Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
150      Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
151      Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
152      Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
153
154      Parameters.Add(new ValueParameter<DoubleValue>(QualityWeightParameterName, new DoubleValue(1)));
155      Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationWeightParameterName, new DoubleValue(0.01)));
156      Parameters.Add(new ValueParameter<DoubleValue>(EvaluatedSolutionsWeightParameterName, new DoubleValue(0.0005)));
157
158      Maximization = new BoolValue(false);
159      SolutionCreator = new RandomParameterConfigurationCreator();
160      Evaluator = new PMOEvaluator();
161
162      InitializeOperators();
163      RegisterParameterEvents();
164      ParameterizeAnalyzer();
165      ParameterizeSolutionCreator();
166      ParameterizeEvaluator();
167      ParameterizeOperators();
168
169      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
170    }
171
172    [StorableConstructor]
173    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
174    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
175      : base(original, cloner) {
176      this.RegisterParameterEvents();
177    }
178    public override IDeepCloneable Clone(Cloner cloner) {
179      return new MetaOptimizationProblem(this, cloner);
180    }
181
182    #region Helpers
183    [StorableHook(HookType.AfterDeserialization)]
184    private void AfterDeserializationHook() {
185      if (!Parameters.ContainsKey(QualityMeasureNameName)) Parameters.Add(new ValueParameter<StringValue>(QualityMeasureNameName, "The name of the quality result of the base-level algorithm. Subresults can be accessed by dot separator.", new StringValue("BestQuality"))); // backwards compatibility
186      RegisterParameterEvents();
187    }
188    private void RegisterParameterEvents() {
189      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
190      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
191      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
192      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
193      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
194      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
195      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
196      Problems.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsAdded);
197      Problems.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsRemoved);
198    }
199
200    private void InitializeOperators() {
201      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
202      Operators.Add(new ReferenceQualityAnalyzer());
203      Operators.Add(new BestParameterConfigurationAnalyzer());
204      Operators.Add(new SolutionCacheAnalyzer());
205      Operators.Add(new PMOPopulationDiversityAnalyzer());
206      Operators.Add(new PMOProblemQualitiesAnalyzer());
207      Operators.Add(new PMOBestSolutionHistoryAnalyzer());
208    }
209    private void ParameterizeSolutionCreator() {
210    }
211    private void ParameterizeEvaluator() {
212      ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
213    }
214    private void ParameterizeAnalyzer() {
215      if (BestParameterConfigurationAnalyzer != null) {
216        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
217      }
218      if (ReferenceQualityAnalyzer != null) {
219        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
220      }
221      if (RunsAnalyzer != null) {
222        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
223      }
224      if (PMOPopulationDiversityAnalyzer != null) {
225        PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
226        PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
227      }
228      if (PMOProblemQualitiesAnalyzer != null) {
229        PMOProblemQualitiesAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
230      }
231      if (PMOBestSolutionHistoryAnalyzer != null) {
232        PMOBestSolutionHistoryAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
233      }
234    }
235    private void ParameterizeOperators() {
236      foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
237        op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
238        op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
239      }
240      foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
241        op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
242      }
243    }
244
245    #endregion
246
247    #region Events
248
249    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
250      ParameterizeSolutionCreator();
251      ParameterizeEvaluator();
252      ParameterizeAnalyzer();
253      ParameterizeOperators();
254      OnSolutionCreatorChanged();
255    }
256    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
257      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
258      ParameterizeEvaluator();
259      ParameterizeAnalyzer();
260      OnEvaluatorChanged();
261    }
262    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
263      ParameterizeAnalyzer();
264    }
265    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
266      AlgorithmType_ValueChanged(sender, e);
267    }
268
269    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
270      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
271      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
272      var newProblemType = this.ProblemType.ValidTypes.SingleOrDefault(t => t == typeof(SingleObjectiveTestFunctionProblem)) ?? this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
273      if (this.ProblemType.Value != newProblemType)
274        this.ProblemType.Value = newProblemType; // ProblemType_ValueChanged will add one problem and create ParameterConfigurationTree
275      else
276        ProblemType_ValueChanged(this, EventArgs.Empty); // call explicitly
277    }
278
279    private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
280      ProblemType_ValueChanged(sender, e);
281    }
282
283    private void ProblemType_ValueChanged(object sender, EventArgs e) {
284      Problems.Clear();
285      Problems.Type = ProblemType.Value;
286      Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
287    }
288
289    private void Problems_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
290      // the first problem in the list is always the instance for the algorithm - this way some basic wiring between problem and algorithm can be sustained
291      if (e.Items.Single().Index == 0) {
292        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, e.Items.Single().Value), e.Items.Single().Value);
293
294        // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event
295        var dap = e.Items.Single().Value as IDataAnalysisProblem;
296        if (dap != null) {
297          dap.Reset += new EventHandler(DataAnalysisProblem_Reset);
298        }
299      }
300    }
301
302    private void Problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
303      if (e.Items.Single().Index == 0) {
304        ParameterConfigurationTreeParameter.ActualValue = null;
305
306        var dap = e.Items.Single().Value as IDataAnalysisProblem;
307        if (dap != null) {
308          dap.Reset -= new EventHandler(DataAnalysisProblem_Reset);
309        }
310      }
311    }
312
313    private void DataAnalysisProblem_Reset(object sender, EventArgs e) {
314      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, Problems.First()), Problems.First());
315    }
316    #endregion
317
318    private IAlgorithm CreateAlgorithm(Type algorithmType, IProblem problem) {
319      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
320      algorithm.Problem = problem;
321      return algorithm;
322    }
323
324    public void ImportAlgorithm(IAlgorithm algorithm) {
325      AlgorithmType.Value = algorithm.GetType();
326      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
327      if (algorithm.Problem != null) {
328        Problems.Clear();
329        Problems.Add((IProblem)algorithm.Problem);
330      }
331      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm, Problems.First());
332    }
333  }
334}
Note: See TracBrowser for help on using the repository browser.