Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • manipulators for one and all parameters
  • SolutionCache to avoid multiple evaluations of equal solutions
  • RunsAnalyzer which stores all base level runs
  • ItemDictionaryView for runs
File size: 12.2 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Algorithms.GeneticAlgorithm;
32using HeuristicLab.Problems.TestFunctions;
33
34namespace HeuristicLab.Problems.MetaOptimization {
35  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
36  [Creatable("Problems")]
37  [StorableClass]
38  public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator> {
39    public const string AlgorithmTypeParameterName = "AlgorithmType";
40    public const string ProblemTypeParameterName = "ProblemType";
41    public const string ProblemsParameterName = "Problems";
42    public const string ParameterConfigurationTreeParameterName = "InitialParameterConfigurationTree";
43    public const string RepetitionsParameterName = "Repetitions";
44
45    public const string IntValueManipulatorParameterName = "IntValueManipulator";
46    public const string DoubleValueManipulatorParameterName = "DoubleValueManipulator";
47    public const string IntValueCrossoverParameterName = "IntValueCrossover";
48    public const string DoubleValueCrossoverParameterName = "DoubleValueCrossover";
49
50    #region Parameter Properties
51    public IValueParameter<EngineAlgorithm> AlgorithmTypeParameter {
52      get { return (ValueParameter<EngineAlgorithm>)Parameters[AlgorithmTypeParameterName]; }
53    }
54    public IValueParameter<ISingleObjectiveProblem> ProblemTypeParameter {
55      get { return (ValueParameter<ISingleObjectiveProblem>)Parameters[ProblemTypeParameterName]; }
56    }
57    public IValueParameter<ConstrainedItemList<ISingleObjectiveProblem>> ProblemsParameter {
58      get { return (ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; }
59    }
60    public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
61      get { return (ValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
62    }
63    public IValueParameter<IntValue> RepetitionsParameter {
64      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
65    }
66
67    public IValueParameter<IIntValueManipulator> IntValueManipulatorParameter {
68      get { return (ValueParameter<IIntValueManipulator>)Parameters[IntValueManipulatorParameterName]; }
69    }
70
71    public IValueParameter<IDoubleValueManipulator> DoubleValueManipulatorParameter {
72      get { return (ValueParameter<IDoubleValueManipulator>)Parameters[DoubleValueManipulatorParameterName]; }
73    }
74    #endregion
75
76    #region Properties
77    public EngineAlgorithm Algorithm {
78      get { return AlgorithmTypeParameter.Value; }
79      set { AlgorithmTypeParameter.Value = value; }
80    }
81    public ISingleObjectiveProblem Problem {
82      get { return ProblemTypeParameter.Value; }
83      set { ProblemTypeParameter.Value = value; }
84    }
85    public ConstrainedItemList<ISingleObjectiveProblem> Problems {
86      get { return ProblemsParameter.Value; }
87      set { ProblemsParameter.Value = value; }
88    }
89    public ParameterConfigurationTree ParameterConfigurationTree {
90      get { return ParameterConfigurationTreeParameter.Value; }
91      set { ParameterConfigurationTreeParameter.Value = value; }
92    }
93    public IntValue Repetitions {
94      get { return RepetitionsParameter.Value; }
95      set { RepetitionsParameter.Value = value; }
96    }
97    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
98      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
99    }
100    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
101      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
102    }
103    private RunsAnalyzer RunsAnalyzer {
104      get { return Operators.OfType<RunsAnalyzer>().FirstOrDefault(); }
105    }
106    #endregion
107
108    public MetaOptimizationProblem()
109      : base() {
110      Parameters.Add(new ValueParameter<EngineAlgorithm>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm()));
111      Parameters.Add(new ValueParameter<ISingleObjectiveProblem>(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem()));
112      Parameters.Add(new ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<ISingleObjectiveProblem>()));
113      Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
114      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
115
116      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
117      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
118      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
119      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
120      Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
121      Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
122      Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, "", validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
123      Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, "", validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
124
125      Maximization = new BoolValue(false);
126      SolutionCreator = new RandomParameterConfigurationCreator();
127      Evaluator = new ParameterConfigurationEvaluator();
128
129      InitializeOperators();
130      RegisterParameterEvents();
131      ParameterizeAnalyzer();
132      ParameterizeSolutionCreator();
133      ParameterizeEvaluator();
134      ParameterizeOperators();
135
136      Problems.Type = Problem.GetType();
137      Algorithm.Problem = Problem;
138      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
139    }
140
141    [StorableConstructor]
142    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
143    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
144      : base(original, cloner) {
145      // todo
146      this.RegisterParameterEvents();
147    }
148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new MetaOptimizationProblem(this, cloner);
150    }
151
152    #region Helpers
153    [StorableHook(HookType.AfterDeserialization)]
154    private void AfterDeserializationHook() {
155      RegisterParameterEvents();
156    }
157    private void RegisterParameterEvents() {
158      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
159      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
160      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
161      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
162      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
163    }
164
165    private void InitializeOperators() {
166      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
167      Operators.Add(new BestParameterConfigurationAnalyzer());
168      Operators.Add(new ReferenceQualityAnalyzer());
169      Operators.Add(new RunsAnalyzer());
170    }
171    private void ParameterizeSolutionCreator() {
172    }
173    private void ParameterizeEvaluator() {
174      ((ParameterConfigurationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
175    }
176    private void ParameterizeAnalyzer() {
177      if (BestParameterConfigurationAnalyzer != null) {
178        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
179      }
180      if (ReferenceQualityAnalyzer != null) {
181        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
182      }
183      if (RunsAnalyzer != null) {
184        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
185      }
186    }
187    private void ParameterizeOperators() {
188      foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
189        op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
190        op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
191      }
192      foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
193        op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
194      }
195    }
196
197    #endregion
198
199    #region Events
200
201    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
202      ParameterizeSolutionCreator();
203      ParameterizeEvaluator();
204      ParameterizeAnalyzer();
205      ParameterizeOperators();
206      OnSolutionCreatorChanged();
207    }
208    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
209      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
210      ParameterizeEvaluator();
211      ParameterizeAnalyzer();
212      OnEvaluatorChanged();
213    }
214    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
215      ParameterizeAnalyzer();
216    }
217    void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
218      Algorithm.Problem = Problem;
219      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
220    }
221    void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
222      Problems.Type = Problem.GetType();
223      Algorithm.Problem = Problem;
224      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
225    }
226    #endregion
227  }
228}
Note: See TracBrowser for help on using the repository browser.