Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6406 was 6090, checked in by cneumuel, 14 years ago

#1215

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