Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10145 was 8157, checked in by ascheibe, 12 years ago

#1877 fixed metaopt for ParameterVisibilityTest

File size: 19.1 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    }
[8157]75    public IConstrainedValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
76      get { return (ConstrainedValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
[5111]77    }
[8157]78    public IConstrainedValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
79      get { return (ConstrainedValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
[5111]80    }
[6489]81    public IValueParameter<StringValue> QualityMeasureNameParameter {
82      get { return (ValueParameter<StringValue>)Parameters[QualityMeasureNameName]; }
83    }
[8157]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    }
[4516]90    #endregion
91
92    #region Properties
[5337]93    public IAlgorithm Algorithm {
[5655]94      get { return CreateAlgorithm(AlgorithmType.Value, Problems.FirstOrDefault()); }
[5313]95    }
[5337]96    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
[5110]97      get { return AlgorithmTypeParameter.Value; }
98      set { AlgorithmTypeParameter.Value = value; }
[4516]99    }
[5337]100    public ConstrainedTypeValue<IProblem> ProblemType {
[5110]101      get { return ProblemTypeParameter.Value; }
102      set { ProblemTypeParameter.Value = value; }
103    }
[5337]104    public ConstrainedItemList<IProblem> Problems {
[4830]105      get { return ProblemsParameter.Value; }
106      set { ProblemsParameter.Value = value; }
[4516]107    }
[5144]108    public ParameterConfigurationTree ParameterConfigurationTree {
109      get { return ParameterConfigurationTreeParameter.Value; }
110      set { ParameterConfigurationTreeParameter.Value = value; }
[4516]111    }
[5110]112    public IntValue Repetitions {
113      get { return RepetitionsParameter.Value; }
114      set { RepetitionsParameter.Value = value; }
115    }
[5184]116    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
117      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
118    }
[5293]119    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
120      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
[5281]121    }
[5359]122    private SolutionCacheAnalyzer RunsAnalyzer {
123      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
[5303]124    }
[5522]125    private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
126      get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
[5576]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    }
[4516]134    #endregion
135
[5303]136    public MetaOptimizationProblem()
137      : base() {
[5337]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>()));
[6489]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
[5110]142      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
[6489]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")));
[5110]144
[5303]145      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
[5111]146      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
[5277]147      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
148      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
[6090]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()));
[5111]153
[6090]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
[4830]158      Maximization = new BoolValue(false);
159      SolutionCreator = new RandomParameterConfigurationCreator();
[5653]160      Evaluator = new PMOEvaluator();
[4516]161
[4830]162      InitializeOperators();
163      RegisterParameterEvents();
[5184]164      ParameterizeAnalyzer();
[4516]165      ParameterizeSolutionCreator();
166      ParameterizeEvaluator();
[4830]167      ParameterizeOperators();
[5110]168
[5337]169      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
[4516]170    }
171
[4830]172    [StorableConstructor]
173    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
[5303]174    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
175      : base(original, cloner) {
[4830]176      this.RegisterParameterEvents();
[4516]177    }
[4830]178    public override IDeepCloneable Clone(Cloner cloner) {
179      return new MetaOptimizationProblem(this, cloner);
180    }
[4516]181
182    #region Helpers
183    [StorableHook(HookType.AfterDeserialization)]
184    private void AfterDeserializationHook() {
[8157]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
[4830]186      RegisterParameterEvents();
[4516]187    }
[4830]188    private void RegisterParameterEvents() {
[4516]189      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
190      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
191      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[5110]192      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
[5313]193      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
[5110]194      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
[5313]195      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
[5655]196      Problems.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsAdded);
197      Problems.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<IProblem>>(Problems_ItemsRemoved);
[4516]198    }
[5303]199
[4516]200    private void InitializeOperators() {
[4830]201      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
[6090]202      Operators.Add(new ReferenceQualityAnalyzer());
[4830]203      Operators.Add(new BestParameterConfigurationAnalyzer());
[5359]204      Operators.Add(new SolutionCacheAnalyzer());
[5522]205      Operators.Add(new PMOPopulationDiversityAnalyzer());
[5576]206      Operators.Add(new PMOProblemQualitiesAnalyzer());
207      Operators.Add(new PMOBestSolutionHistoryAnalyzer());
[4516]208    }
209    private void ParameterizeSolutionCreator() {
210    }
211    private void ParameterizeEvaluator() {
[5653]212      ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[4516]213    }
214    private void ParameterizeAnalyzer() {
[5184]215      if (BestParameterConfigurationAnalyzer != null) {
216        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
217      }
[5293]218      if (ReferenceQualityAnalyzer != null) {
219        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[5281]220      }
[5303]221      if (RunsAnalyzer != null) {
222        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[6489]223      }
[5522]224      if (PMOPopulationDiversityAnalyzer != null) {
225        PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
226        PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
[5303]227      }
[5576]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      }
[4516]234    }
235    private void ParameterizeOperators() {
[5184]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      }
[4516]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    }
[5313]265    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
266      AlgorithmType_ValueChanged(sender, e);
[4516]267    }
[5313]268
269    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
[5337]270      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
271      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
[6489]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
[4516]277    }
[5313]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;
[5655]286      Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
[5313]287    }
[5655]288
[6489]289    private void Problems_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
[5655]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) {
[5665]292        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, e.Items.Single().Value), e.Items.Single().Value);
[5655]293
294        // special for DataAnalysisProblem: Because of wiring between algorithm and problem, ParameterConfigurationTree needs to be recreated on Reset event
[5927]295        var dap = e.Items.Single().Value as IDataAnalysisProblem;
[5655]296        if (dap != null) {
297          dap.Reset += new EventHandler(DataAnalysisProblem_Reset);
298        }
299      }
300    }
301
[6489]302    private void Problems_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IProblem>> e) {
[5655]303      if (e.Items.Single().Index == 0) {
304        ParameterConfigurationTreeParameter.ActualValue = null;
305
[5927]306        var dap = e.Items.Single().Value as IDataAnalysisProblem;
[5655]307        if (dap != null) {
308          dap.Reset -= new EventHandler(DataAnalysisProblem_Reset);
309        }
310      }
311    }
312
313    private void DataAnalysisProblem_Reset(object sender, EventArgs e) {
[5665]314      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, Problems.First()), Problems.First());
[5655]315    }
[4516]316    #endregion
[5313]317
[5655]318    private IAlgorithm CreateAlgorithm(Type algorithmType, IProblem problem) {
[5337]319      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
[5655]320      algorithm.Problem = problem;
[5313]321      return algorithm;
322    }
323
[5337]324    public void ImportAlgorithm(IAlgorithm algorithm) {
[5313]325      AlgorithmType.Value = algorithm.GetType();
[5337]326      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
[5665]327      if (algorithm.Problem != null) {
328        Problems.Clear();
329        Problems.Add((IProblem)algorithm.Problem);
330      }
331      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm, Problems.First());
[5313]332    }
[4516]333  }
334}
Note: See TracBrowser for help on using the repository browser.