Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7623 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
RevLine 
[4516]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;
[5654]24using HeuristicLab.Algorithms.GeneticAlgorithm;
[5655]25using HeuristicLab.Collections;
[4839]26using HeuristicLab.Common;
[4516]27using HeuristicLab.Core;
[4839]28using HeuristicLab.Data;
[4516]29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
[4839]31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4516]32using HeuristicLab.PluginInfrastructure;
[5655]33using HeuristicLab.Problems.DataAnalysis;
[5110]34using HeuristicLab.Problems.TestFunctions;
[4516]35
36namespace HeuristicLab.Problems.MetaOptimization {
37  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
38  [Creatable("Problems")]
39  [StorableClass]
[5927]40  public sealed class MetaOptimizationProblem : SingleObjectiveHeuristicOptimizationProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator>, IStorableContent {
[5654]41    public string Filename { get; set; }
[6489]42
[5110]43    public const string AlgorithmTypeParameterName = "AlgorithmType";
44    public const string ProblemTypeParameterName = "ProblemType";
45    public const string ProblemsParameterName = "Problems";
[5328]46    public const string ParameterConfigurationTreeParameterName = "ParameterConfiguration";
[5110]47    public const string RepetitionsParameterName = "Repetitions";
[6489]48    public const string QualityMeasureNameName = "QualityMeasureName";
[4516]49
[5111]50    public const string IntValueManipulatorParameterName = "IntValueManipulator";
51    public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator";
52    public const string IntValueCrossoverParameterName = "IntValueCrossover";
53    public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover";
[6090]54    public const string QualityWeightParameterName = "QualityWeight";
55    public const string StandardDeviationWeightParameterName = "StandardDeviationWeight";
56    public const string EvaluatedSolutionsWeightParameterName = "EvaluatedSolutionsWeight";
[5111]57
[6090]58
[4516]59    #region Parameter Properties
[5337]60    public IValueParameter<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
61      get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
[4516]62    }
[5337]63    public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
64      get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
[4516]65    }
[5337]66    public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
67      get { return (ValueParameter<ConstrainedItemList<IProblem>>)Parameters[ProblemsParameterName]; }
[4516]68    }
[5144]69    public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
[6489]70      get { return (OptionalValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
[5110]71    }
72    public IValueParameter<IntValue> RepetitionsParameter {
73      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
74    }
[5111]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    }
[6489]83    public IValueParameter<StringValue> QualityMeasureNameParameter {
84      get { return (ValueParameter<StringValue>)Parameters[QualityMeasureNameName]; }
85    }
[4516]86    #endregion
87
88    #region Properties
[5337]89    public IAlgorithm Algorithm {
[5655]90      get { return CreateAlgorithm(AlgorithmType.Value, Problems.FirstOrDefault()); }
[5313]91    }
[5337]92    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
[5110]93      get { return AlgorithmTypeParameter.Value; }
94      set { AlgorithmTypeParameter.Value = value; }
[4516]95    }
[5337]96    public ConstrainedTypeValue<IProblem> ProblemType {
[5110]97      get { return ProblemTypeParameter.Value; }
98      set { ProblemTypeParameter.Value = value; }
99    }
[5337]100    public ConstrainedItemList<IProblem> Problems {
[4830]101      get { return ProblemsParameter.Value; }
102      set { ProblemsParameter.Value = value; }
[4516]103    }
[5144]104    public ParameterConfigurationTree ParameterConfigurationTree {
105      get { return ParameterConfigurationTreeParameter.Value; }
106      set { ParameterConfigurationTreeParameter.Value = value; }
[4516]107    }
[5110]108    public IntValue Repetitions {
109      get { return RepetitionsParameter.Value; }
110      set { RepetitionsParameter.Value = value; }
111    }
[5184]112    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
113      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
114    }
[5293]115    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
116      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
[5281]117    }
[5359]118    private SolutionCacheAnalyzer RunsAnalyzer {
119      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
[5303]120    }
[5522]121    private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
122      get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
[5576]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    }
[4516]130    #endregion
131
[5303]132    public MetaOptimizationProblem()
133      : base() {
[5337]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>()));
[6489]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
[5110]138      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
[6489]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")));
[5110]140
[5303]141      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
[5111]142      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
[5277]143      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
144      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
[6090]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()));
[5111]149
[6090]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
[4830]154      Maximization = new BoolValue(false);
155      SolutionCreator = new RandomParameterConfigurationCreator();
[5653]156      Evaluator = new PMOEvaluator();
[4516]157
[4830]158      InitializeOperators();
159      RegisterParameterEvents();
[5184]160      ParameterizeAnalyzer();
[4516]161      ParameterizeSolutionCreator();
162      ParameterizeEvaluator();
[4830]163      ParameterizeOperators();
[5110]164
[5337]165      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
[4516]166    }
167
[4830]168    [StorableConstructor]
169    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
[5303]170    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
171      : base(original, cloner) {
[4830]172      this.RegisterParameterEvents();
[4516]173    }
[4830]174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new MetaOptimizationProblem(this, cloner);
176    }
[4516]177
178    #region Helpers
179    [StorableHook(HookType.AfterDeserialization)]
180    private void AfterDeserializationHook() {
[6489]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
[4830]182      RegisterParameterEvents();
[4516]183    }
[4830]184    private void RegisterParameterEvents() {
[4516]185      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
186      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
187      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[5110]188      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
[5313]189      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
[5110]190      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
[5313]191      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
[5655]192      Problems.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsAdded);
193      Problems.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsRemoved);
[4516]194    }
[5303]195
[4516]196    private void InitializeOperators() {
[4830]197      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
[6090]198      Operators.Add(new ReferenceQualityAnalyzer());
[4830]199      Operators.Add(new BestParameterConfigurationAnalyzer());
[5359]200      Operators.Add(new SolutionCacheAnalyzer());
[5522]201      Operators.Add(new PMOPopulationDiversityAnalyzer());
[5576]202      Operators.Add(new PMOProblemQualitiesAnalyzer());
203      Operators.Add(new PMOBestSolutionHistoryAnalyzer());
[4516]204    }
205    private void ParameterizeSolutionCreator() {
206    }
207    private void ParameterizeEvaluator() {
[5653]208      ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[4516]209    }
210    private void ParameterizeAnalyzer() {
[5184]211      if (BestParameterConfigurationAnalyzer != null) {
212        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
213      }
[5293]214      if (ReferenceQualityAnalyzer != null) {
215        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[5281]216      }
[5303]217      if (RunsAnalyzer != null) {
218        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[6489]219      }
[5522]220      if (PMOPopulationDiversityAnalyzer != null) {
221        PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
222        PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
[5303]223      }
[5576]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      }
[4516]230    }
231    private void ParameterizeOperators() {
[5184]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      }
[4516]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    }
[5313]261    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
262      AlgorithmType_ValueChanged(sender, e);
[4516]263    }
[5313]264
265    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
[5337]266      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
267      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
[6489]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
[4516]273    }
[5313]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;
[5655]282      Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
[5313]283    }
[5655]284
[6489]285    private void Problems_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
[5655]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) {
[5665]288        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, e.Items.Single().Value), e.Items.Single().Value);
[5655]289
290        // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event
[5927]291        var dap = e.Items.Single().Value as IDataAnalysisProblem;
[5655]292        if (dap != null) {
293          dap.Reset += new EventHandler(DataAnalysisProblem_Reset);
294        }
295      }
296    }
297
[6489]298    private void Problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
[5655]299      if (e.Items.Single().Index == 0) {
300        ParameterConfigurationTreeParameter.ActualValue = null;
301
[5927]302        var dap = e.Items.Single().Value as IDataAnalysisProblem;
[5655]303        if (dap != null) {
304          dap.Reset -= new EventHandler(DataAnalysisProblem_Reset);
305        }
306      }
307    }
308
309    private void DataAnalysisProblem_Reset(object sender, EventArgs e) {
[5665]310      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, Problems.First()), Problems.First());
[5655]311    }
[4516]312    #endregion
[5313]313
[5655]314    private IAlgorithm CreateAlgorithm(Type algorithmType, IProblem problem) {
[5337]315      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
[5655]316      algorithm.Problem = problem;
[5313]317      return algorithm;
318    }
319
[5337]320    public void ImportAlgorithm(IAlgorithm algorithm) {
[5313]321      AlgorithmType.Value = algorithm.GetType();
[5337]322      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
[5665]323      if (algorithm.Problem != null) {
324        Problems.Clear();
325        Problems.Add((IProblem)algorithm.Problem);
326      }
327      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm, Problems.First());
[5313]328    }
[4516]329  }
330}
Note: See TracBrowser for help on using the repository browser.