Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • evaluation operator returns operatorgraph which creates a scope and an operation for each algorithm execution (each repetition and problem)
  • split ValueConfiguration into ParameterizedValueConfiguration and RangeValueConfiguration
File size: 15.4 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 = "ParameterConfiguration";
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<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
52      get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
53    }
54    public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
55      get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
56    }
57    public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
58      get { return (ValueParameter<ConstrainedItemList<IProblem>>)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 IAlgorithm Algorithm {
78      get { return CreateAlgorithm(AlgorithmType.Value, ProblemType.Value); }
79    }
80    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
81      get { return AlgorithmTypeParameter.Value; }
82      set { AlgorithmTypeParameter.Value = value; }
83    }
84    public ConstrainedTypeValue<IProblem> ProblemType {
85      get { return ProblemTypeParameter.Value; }
86      set { ProblemTypeParameter.Value = value; }
87    }
88    public ConstrainedItemList<IProblem> Problems {
89      get { return ProblemsParameter.Value; }
90      set { ProblemsParameter.Value = value; }
91    }
92    public ParameterConfigurationTree ParameterConfigurationTree {
93      get { return ParameterConfigurationTreeParameter.Value; }
94      set { ParameterConfigurationTreeParameter.Value = value; }
95    }
96    public IntValue Repetitions {
97      get { return RepetitionsParameter.Value; }
98      set { RepetitionsParameter.Value = value; }
99    }
100    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
101      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
102    }
103    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
104      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
105    }
106    private SolutionCacheAnalyzer RunsAnalyzer {
107      get { return Operators.OfType<SolutionCacheAnalyzer>().FirstOrDefault(); }
108    }
109    private PMOPopulationDiversityAnalyzer PMOPopulationDiversityAnalyzer {
110      get { return Operators.OfType<PMOPopulationDiversityAnalyzer>().FirstOrDefault(); }
111    }
112    private PMOProblemQualitiesAnalyzer PMOProblemQualitiesAnalyzer {
113      get { return Operators.OfType<PMOProblemQualitiesAnalyzer>().FirstOrDefault(); }
114    }
115    private PMOBestSolutionHistoryAnalyzer PMOBestSolutionHistoryAnalyzer {
116      get { return Operators.OfType<PMOBestSolutionHistoryAnalyzer>().FirstOrDefault(); }
117    }
118    #endregion
119
120    public MetaOptimizationProblem()
121      : base() {
122      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
123      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
124      Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
125      Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
126      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
127
128      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
129      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
130      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
131      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
132      Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
133      Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
134      Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, "", validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
135      Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, "", validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
136
137      Maximization = new BoolValue(false);
138      SolutionCreator = new RandomParameterConfigurationCreator();
139      Evaluator = new PMOEvaluator();
140
141      InitializeOperators();
142      RegisterParameterEvents();
143      ParameterizeAnalyzer();
144      ParameterizeSolutionCreator();
145      ParameterizeEvaluator();
146      ParameterizeOperators();
147
148      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
149    }
150
151    [StorableConstructor]
152    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
153    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
154      : base(original, cloner) {
155      this.RegisterParameterEvents();
156    }
157    public override IDeepCloneable Clone(Cloner cloner) {
158      return new MetaOptimizationProblem(this, cloner);
159    }
160
161    #region Helpers
162    [StorableHook(HookType.AfterDeserialization)]
163    private void AfterDeserializationHook() {
164      RegisterParameterEvents();
165    }
166    private void RegisterParameterEvents() {
167      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
168      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
169      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
170      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
171      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
172      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
173      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
174    }
175
176    private void InitializeOperators() {
177      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
178      Operators.Add(new BestParameterConfigurationAnalyzer());
179      Operators.Add(new ReferenceQualityAnalyzer());
180      Operators.Add(new SolutionCacheAnalyzer());
181      Operators.Add(new PMOPopulationDiversityAnalyzer());
182      Operators.Add(new PMOProblemQualitiesAnalyzer());
183      Operators.Add(new PMOBestSolutionHistoryAnalyzer());
184    }
185    private void ParameterizeSolutionCreator() {
186    }
187    private void ParameterizeEvaluator() {
188      ((PMOEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
189    }
190    private void ParameterizeAnalyzer() {
191      if (BestParameterConfigurationAnalyzer != null) {
192        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
193      }
194      if (ReferenceQualityAnalyzer != null) {
195        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
196      }
197      if (RunsAnalyzer != null) {
198        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
199      }
200      if (PMOPopulationDiversityAnalyzer != null) {
201        PMOPopulationDiversityAnalyzer.SolutionParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
202        PMOPopulationDiversityAnalyzer.StoreHistoryParameter.Value.Value = true;
203      }
204      if (PMOProblemQualitiesAnalyzer != null) {
205        PMOProblemQualitiesAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
206      }
207      if (PMOBestSolutionHistoryAnalyzer != null) {
208        PMOBestSolutionHistoryAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
209      }
210    }
211    private void ParameterizeOperators() {
212      foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
213        op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
214        op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
215      }
216      foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
217        op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
218      }
219    }
220
221    #endregion
222
223    #region Events
224
225    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
226      ParameterizeSolutionCreator();
227      ParameterizeEvaluator();
228      ParameterizeAnalyzer();
229      ParameterizeOperators();
230      OnSolutionCreatorChanged();
231    }
232    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
233      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
234      ParameterizeEvaluator();
235      ParameterizeAnalyzer();
236      OnEvaluatorChanged();
237    }
238    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
239      ParameterizeAnalyzer();
240    }
241    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
242      AlgorithmType_ValueChanged(sender, e);
243    }
244
245    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
246      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
247      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
248      this.ProblemType.Value = this.ProblemType.ValidTypes.SingleOrDefault(t => t == typeof(SingleObjectiveTestFunctionProblem)) ?? this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
249      if (ProblemType.Value != null) {
250        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
251        Problems.Add((IProblem)Activator.CreateInstance(this.ProblemType.Value));
252      } else {
253        ParameterConfigurationTreeParameter.ActualValue = null;
254      }
255    }
256
257    private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
258      ProblemType_ValueChanged(sender, e);
259    }
260
261    private void ProblemType_ValueChanged(object sender, EventArgs e) {
262      Problems.Clear();
263      Problems.Type = ProblemType.Value;
264      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
265    }
266    #endregion
267
268    private IAlgorithm CreateAlgorithm(Type algorithmType, Type problemType) {
269      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
270      algorithm.Problem = (IProblem)Activator.CreateInstance(problemType);
271      return algorithm;
272    }
273
274    public void ImportAlgorithm(IAlgorithm algorithm) {
275      AlgorithmType.Value = algorithm.GetType();
276      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
277      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm);
278      if (algorithm.Problem != null) Problems.Add((IProblem)algorithm.Problem);
279    }
280  }
281}
Note: See TracBrowser for help on using the repository browser.