Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7932 was 6489, checked in by cneumuel, 13 years ago

#1215

  • fixed issue with multiple problems (by introducing valuesReadOnly to IOptimizable)
  • fixed error message when removing last problem instance
  • made quality measure name configurable
File size: 18.6 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
76    public IValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
77      get { return (ValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
78    }
79
80    public IValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
81      get { return (ValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
82    }
83    public IValueParameter<StringValue> QualityMeasureNameParameter {
84      get { return (ValueParameter<StringValue>)Parameters[QualityMeasureNameName]; }
85    }
86    #endregion
87
88    #region Properties
89    public IAlgorithm Algorithm {
90      get { return CreateAlgorithm(AlgorithmType.Value, Problems.FirstOrDefault()); }
91    }
92    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
93      get { return AlgorithmTypeParameter.Value; }
94      set { AlgorithmTypeParameter.Value = value; }
95    }
96    public ConstrainedTypeValue<IProblem> ProblemType {
97      get { return ProblemTypeParameter.Value; }
98      set { ProblemTypeParameter.Value = value; }
99    }
100    public ConstrainedItemList<IProblem> Problems {
101      get { return ProblemsParameter.Value; }
102      set { ProblemsParameter.Value = value; }
103    }
104    public ParameterConfigurationTree ParameterConfigurationTree {
105      get { return ParameterConfigurationTreeParameter.Value; }
106      set { ParameterConfigurationTreeParameter.Value = value; }
107    }
108    public IntValue Repetitions {
109      get { return RepetitionsParameter.Value; }
110      set { RepetitionsParameter.Value = value; }
111    }
112    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
113      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
114    }
115    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
116      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
117    }
118    private SolutionCacheAnalyzer RunsAnalyzer {
119      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
120    }
121    private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
122      get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
123    }
124    private PMOProblemQualitiesAnalyzer PMOProblemQualitiesAnalyzer {
125      get { return Operators.OfType<PMOProblemQualitiesAnalyzer>().FirstOrDefault(); }
126    }
127    private PMOBestSolutionHistoryAnalyzer PMOBestSolutionHistoryAnalyzer {
128      get { return Operators.OfType<PMOBestSolutionHistoryAnalyzer>().FirstOrDefault(); }
129    }
130    #endregion
131
132    public MetaOptimizationProblem()
133      : base() {
134      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
135      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
136      Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
137      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
138      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
139      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")));
140
141      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
142      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
143      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
144      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
145      Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
146      Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
147      Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
148      Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
149
150      Parameters.Add(new ValueParameter<DoubleValue>(QualityWeightParameterName, new DoubleValue(1)));
151      Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationWeightParameterName, new DoubleValue(0.01)));
152      Parameters.Add(new ValueParameter<DoubleValue>(EvaluatedSolutionsWeightParameterName, new DoubleValue(0.0005)));
153
154      Maximization = new BoolValue(false);
155      SolutionCreator = new RandomParameterConfigurationCreator();
156      Evaluator = new PMOEvaluator();
157
158      InitializeOperators();
159      RegisterParameterEvents();
160      ParameterizeAnalyzer();
161      ParameterizeSolutionCreator();
162      ParameterizeEvaluator();
163      ParameterizeOperators();
164
165      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
166    }
167
168    [StorableConstructor]
169    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
170    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
171      : base(original, cloner) {
172      this.RegisterParameterEvents();
173    }
174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new MetaOptimizationProblem(this, cloner);
176    }
177
178    #region Helpers
179    [StorableHook(HookType.AfterDeserialization)]
180    private void AfterDeserializationHook() {
181      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
182      RegisterParameterEvents();
183    }
184    private void RegisterParameterEvents() {
185      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
186      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
187      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
188      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
189      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
190      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
191      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
192      Problems.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsAdded);
193      Problems.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsRemoved);
194    }
195
196    private void InitializeOperators() {
197      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
198      Operators.Add(new ReferenceQualityAnalyzer());
199      Operators.Add(new BestParameterConfigurationAnalyzer());
200      Operators.Add(new SolutionCacheAnalyzer());
201      Operators.Add(new PMOPopulationDiversityAnalyzer());
202      Operators.Add(new PMOProblemQualitiesAnalyzer());
203      Operators.Add(new PMOBestSolutionHistoryAnalyzer());
204    }
205    private void ParameterizeSolutionCreator() {
206    }
207    private void ParameterizeEvaluator() {
208      ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
209    }
210    private void ParameterizeAnalyzer() {
211      if (BestParameterConfigurationAnalyzer != null) {
212        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
213      }
214      if (ReferenceQualityAnalyzer != null) {
215        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
216      }
217      if (RunsAnalyzer != null) {
218        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
219      }
220      if (PMOPopulationDiversityAnalyzer != null) {
221        PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
222        PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
223      }
224      if (PMOProblemQualitiesAnalyzer != null) {
225        PMOProblemQualitiesAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
226      }
227      if (PMOBestSolutionHistoryAnalyzer != null) {
228        PMOBestSolutionHistoryAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
229      }
230    }
231    private void ParameterizeOperators() {
232      foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
233        op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
234        op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
235      }
236      foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
237        op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
238      }
239    }
240
241    #endregion
242
243    #region Events
244
245    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
246      ParameterizeSolutionCreator();
247      ParameterizeEvaluator();
248      ParameterizeAnalyzer();
249      ParameterizeOperators();
250      OnSolutionCreatorChanged();
251    }
252    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
253      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
254      ParameterizeEvaluator();
255      ParameterizeAnalyzer();
256      OnEvaluatorChanged();
257    }
258    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
259      ParameterizeAnalyzer();
260    }
261    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
262      AlgorithmType_ValueChanged(sender, e);
263    }
264
265    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
266      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
267      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
268      var newProblemType = this.ProblemType.ValidTypes.SingleOrDefault(t => t == typeof(SingleObjectiveTestFunctionProblem)) ?? this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
269      if (this.ProblemType.Value != newProblemType)
270        this.ProblemType.Value = newProblemType; // ProblemType_ValueChanged will add one problem and create ParameterConfigurationTree
271      else
272        ProblemType_ValueChanged(this, EventArgs.Empty); // call explicitly
273    }
274
275    private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
276      ProblemType_ValueChanged(sender, e);
277    }
278
279    private void ProblemType_ValueChanged(object sender, EventArgs e) {
280      Problems.Clear();
281      Problems.Type = ProblemType.Value;
282      Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
283    }
284
285    private void Problems_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
286      // 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
287      if (e.Items.Single().Index == 0) {
288        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, e.Items.Single().Value), e.Items.Single().Value);
289
290        // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event
291        var dap = e.Items.Single().Value as IDataAnalysisProblem;
292        if (dap != null) {
293          dap.Reset += new EventHandler(DataAnalysisProblem_Reset);
294        }
295      }
296    }
297
298    private void Problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
299      if (e.Items.Single().Index == 0) {
300        ParameterConfigurationTreeParameter.ActualValue = null;
301
302        var dap = e.Items.Single().Value as IDataAnalysisProblem;
303        if (dap != null) {
304          dap.Reset -= new EventHandler(DataAnalysisProblem_Reset);
305        }
306      }
307    }
308
309    private void DataAnalysisProblem_Reset(object sender, EventArgs e) {
310      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, Problems.First()), Problems.First());
311    }
312    #endregion
313
314    private IAlgorithm CreateAlgorithm(Type algorithmType, IProblem problem) {
315      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
316      algorithm.Problem = problem;
317      return algorithm;
318    }
319
320    public void ImportAlgorithm(IAlgorithm algorithm) {
321      AlgorithmType.Value = algorithm.GetType();
322      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
323      if (algorithm.Problem != null) {
324        Problems.Clear();
325        Problems.Add((IProblem)algorithm.Problem);
326      }
327      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm, Problems.First());
328    }
329  }
330}
Note: See TracBrowser for help on using the repository browser.