Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • made solution cache use less memory, by deleting unnecessary information
File size: 13.8 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;
[4839]24using HeuristicLab.Common;
[4516]25using HeuristicLab.Core;
[4839]26using HeuristicLab.Data;
[4516]27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
[4839]29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[4516]30using HeuristicLab.PluginInfrastructure;
[5110]31using HeuristicLab.Algorithms.GeneticAlgorithm;
32using HeuristicLab.Problems.TestFunctions;
[4516]33
34namespace HeuristicLab.Problems.MetaOptimization {
35  [Item("Meta Optimization Problem", "Represents a Meta Optimization Problem.")]
36  [Creatable("Problems")]
37  [StorableClass]
[5111]38  public sealed class MetaOptimizationProblem : SingleObjectiveProblem<IParameterConfigurationEvaluator, IParameterConfigurationCreator> {
[5110]39    public const string AlgorithmTypeParameterName = "AlgorithmType";
40    public const string ProblemTypeParameterName = "ProblemType";
41    public const string ProblemsParameterName = "Problems";
[5328]42    public const string ParameterConfigurationTreeParameterName = "ParameterConfiguration";
[5110]43    public const string RepetitionsParameterName = "Repetitions";
[4516]44
[5111]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
[4516]50    #region Parameter Properties
[5337]51    public IValueParameter<ConstrainedTypeValue<IAlgorithm>> AlgorithmTypeParameter {
52      get { return (ValueParameter<ConstrainedTypeValue<IAlgorithm>>)Parameters[AlgorithmTypeParameterName]; }
[4516]53    }
[5337]54    public IValueParameter<ConstrainedTypeValue<IProblem>> ProblemTypeParameter {
55      get { return (ValueParameter<ConstrainedTypeValue<IProblem>>)Parameters[ProblemTypeParameterName]; }
[4516]56    }
[5337]57    public IValueParameter<ConstrainedItemList<IProblem>> ProblemsParameter {
58      get { return (ValueParameter<ConstrainedItemList<IProblem>>)Parameters[ProblemsParameterName]; }
[4516]59    }
[5144]60    public IValueParameter<ParameterConfigurationTree> ParameterConfigurationTreeParameter {
61      get { return (ValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationTreeParameterName]; }
[5110]62    }
63    public IValueParameter<IntValue> RepetitionsParameter {
64      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
65    }
[5111]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    }
[4516]74    #endregion
75
76    #region Properties
[5337]77    public IAlgorithm Algorithm {
[5313]78      get { return CreateAlgorithm(AlgorithmType.Value, ProblemType.Value); }
79    }
[5337]80    public ConstrainedTypeValue<IAlgorithm> AlgorithmType {
[5110]81      get { return AlgorithmTypeParameter.Value; }
82      set { AlgorithmTypeParameter.Value = value; }
[4516]83    }
[5337]84    public ConstrainedTypeValue<IProblem> ProblemType {
[5110]85      get { return ProblemTypeParameter.Value; }
86      set { ProblemTypeParameter.Value = value; }
87    }
[5337]88    public ConstrainedItemList<IProblem> Problems {
[4830]89      get { return ProblemsParameter.Value; }
90      set { ProblemsParameter.Value = value; }
[4516]91    }
[5144]92    public ParameterConfigurationTree ParameterConfigurationTree {
93      get { return ParameterConfigurationTreeParameter.Value; }
94      set { ParameterConfigurationTreeParameter.Value = value; }
[4516]95    }
[5110]96    public IntValue Repetitions {
97      get { return RepetitionsParameter.Value; }
98      set { RepetitionsParameter.Value = value; }
99    }
[5184]100    private BestParameterConfigurationAnalyzer BestParameterConfigurationAnalyzer {
101      get { return Operators.OfType<BestParameterConfigurationAnalyzer>().FirstOrDefault(); }
102    }
[5293]103    private ReferenceQualityAnalyzer ReferenceQualityAnalyzer {
104      get { return Operators.OfType<ReferenceQualityAnalyzer>().FirstOrDefault(); }
[5281]105    }
[5303]106    private RunsAnalyzer RunsAnalyzer {
107      get { return Operators.OfType<RunsAnalyzer>().FirstOrDefault(); }
108    }
[4516]109    #endregion
110
[5303]111    public MetaOptimizationProblem()
112      : base() {
[5337]113      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IAlgorithm>>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new ConstrainedTypeValue<IAlgorithm>(typeof(GeneticAlgorithm))));
114      Parameters.Add(new ValueParameter<ConstrainedTypeValue<IProblem>>(ProblemTypeParameterName, "The problem type.", new ConstrainedTypeValue<IProblem>()));
115      Parameters.Add(new ValueParameter<ConstrainedItemList<IProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<IProblem>()));
[5184]116      Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationTreeParameterName, "Tree of algorithm parameters that should be optimized."));
[5110]117      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
118
[5303]119      var validIntManipulators = new ItemSet<IIntValueManipulator>(ApplicationManager.Manager.GetInstances<IIntValueManipulator>());
[5111]120      var validDoubleManipulators = new ItemSet<IDoubleValueManipulator>(ApplicationManager.Manager.GetInstances<IDoubleValueManipulator>());
[5277]121      var validIntCrossovers = new ItemSet<IIntValueCrossover>(ApplicationManager.Manager.GetInstances<IIntValueCrossover>());
122      var validDoubleCrossovers = new ItemSet<IDoubleValueCrossover>(ApplicationManager.Manager.GetInstances<IDoubleValueCrossover>());
[5293]123      Parameters.Add(new ConstrainedValueParameter<IIntValueManipulator>(IntValueManipulatorParameterName, "", validIntManipulators, validIntManipulators.Where(x => x.GetType() == typeof(NormalIntValueManipulator)).SingleOrDefault()));
124      Parameters.Add(new ConstrainedValueParameter<IDoubleValueManipulator>(DoubleValueManipulatorParameterName, "", validDoubleManipulators, validDoubleManipulators.Where(x => x.GetType() == typeof(NormalDoubleValueManipulator)).SingleOrDefault()));
125      Parameters.Add(new ConstrainedValueParameter<IIntValueCrossover>(IntValueCrossoverParameterName, "", validIntCrossovers, validIntCrossovers.Where(x => x.GetType() == typeof(NormalIntValueCrossover)).SingleOrDefault()));
126      Parameters.Add(new ConstrainedValueParameter<IDoubleValueCrossover>(DoubleValueCrossoverParameterName, "", validDoubleCrossovers, validDoubleCrossovers.Where(x => x.GetType() == typeof(NormalDoubleValueCrossover)).SingleOrDefault()));
[5111]127
[4830]128      Maximization = new BoolValue(false);
129      SolutionCreator = new RandomParameterConfigurationCreator();
[5111]130      Evaluator = new ParameterConfigurationEvaluator();
[4516]131
[4830]132      InitializeOperators();
133      RegisterParameterEvents();
[5184]134      ParameterizeAnalyzer();
[4516]135      ParameterizeSolutionCreator();
136      ParameterizeEvaluator();
[4830]137      ParameterizeOperators();
[5110]138
[5337]139      AlgorithmTypeParameter_ValueChanged(this, EventArgs.Empty);
[4516]140    }
141
[4830]142    [StorableConstructor]
143    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
[5303]144    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner)
145      : base(original, cloner) {
[4830]146      this.RegisterParameterEvents();
[4516]147    }
[4830]148    public override IDeepCloneable Clone(Cloner cloner) {
149      return new MetaOptimizationProblem(this, cloner);
150    }
[4516]151
152    #region Helpers
153    [StorableHook(HookType.AfterDeserialization)]
154    private void AfterDeserializationHook() {
[4830]155      RegisterParameterEvents();
[4516]156    }
[4830]157    private void RegisterParameterEvents() {
[4516]158      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
159      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
160      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[5110]161      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
[5313]162      AlgorithmType.ValueChanged += new EventHandler(AlgorithmType_ValueChanged);
[5110]163      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
[5313]164      ProblemType.ValueChanged += new EventHandler(ProblemType_ValueChanged);
[4516]165    }
[5303]166
[4516]167    private void InitializeOperators() {
[4830]168      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
169      Operators.Add(new BestParameterConfigurationAnalyzer());
[5293]170      Operators.Add(new ReferenceQualityAnalyzer());
[5303]171      Operators.Add(new RunsAnalyzer());
[4516]172    }
173    private void ParameterizeSolutionCreator() {
174    }
175    private void ParameterizeEvaluator() {
[5111]176      ((ParameterConfigurationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[4516]177    }
178    private void ParameterizeAnalyzer() {
[5184]179      if (BestParameterConfigurationAnalyzer != null) {
180        BestParameterConfigurationAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
181      }
[5293]182      if (ReferenceQualityAnalyzer != null) {
183        ReferenceQualityAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
[5281]184      }
[5303]185      if (RunsAnalyzer != null) {
186        RunsAnalyzer.ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
187      }
[4516]188    }
189    private void ParameterizeOperators() {
[5184]190      foreach (IParameterConfigurationCrossover op in Operators.OfType<IParameterConfigurationCrossover>()) {
191        op.ParentsParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
192        op.ChildParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
193      }
194      foreach (IParameterConfigurationManipulator op in Operators.OfType<IParameterConfigurationManipulator>()) {
195        op.ParameterConfigurationTreeParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
196      }
[4516]197    }
198
199    #endregion
200
201    #region Events
202
203    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
204      ParameterizeSolutionCreator();
205      ParameterizeEvaluator();
206      ParameterizeAnalyzer();
207      ParameterizeOperators();
208      OnSolutionCreatorChanged();
209    }
210    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
211      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
212      ParameterizeEvaluator();
213      ParameterizeAnalyzer();
214      OnEvaluatorChanged();
215    }
216    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
217      ParameterizeAnalyzer();
218    }
[5313]219    private void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
220      AlgorithmType_ValueChanged(sender, e);
[4516]221    }
[5313]222
223    private void AlgorithmType_ValueChanged(object sender, EventArgs e) {
[5337]224      IAlgorithm instance = (IAlgorithm)Activator.CreateInstance(AlgorithmType.Value);
225      this.ProblemType.ValidTypes = ApplicationManager.Manager.GetTypes(instance.ProblemType, true).ToList();
[5340]226      this.ProblemType.Value = this.ProblemType.ValidTypes.Where(t => t != typeof(MetaOptimizationProblem)).FirstOrDefault();
227      if (ProblemType.Value != null) {
228        ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
229      } else {
230        ParameterConfigurationTreeParameter.ActualValue = null;
231      }
[4516]232    }
[5313]233
234    private void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
235      ProblemType_ValueChanged(sender, e);
236    }
237
238    private void ProblemType_ValueChanged(object sender, EventArgs e) {
239      Problems.Clear();
240      Problems.Type = ProblemType.Value;
241      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(CreateAlgorithm(AlgorithmType.Value, ProblemType.Value));
242    }
[4516]243    #endregion
[5313]244
[5337]245    private IAlgorithm CreateAlgorithm(Type algorithmType, Type problemType) {
246      IAlgorithm algorithm = (IAlgorithm)Activator.CreateInstance(algorithmType);
[5313]247      algorithm.Problem = (IProblem)Activator.CreateInstance(problemType);
248      return algorithm;
249    }
250
[5337]251    public void ImportAlgorithm(IAlgorithm algorithm) {
[5313]252      AlgorithmType.Value = algorithm.GetType();
[5337]253      if (algorithm.Problem != null) ProblemType.Value = algorithm.Problem.GetType();
[5313]254      ParameterConfigurationTreeParameter.ActualValue = new ParameterConfigurationTree(algorithm);
[5337]255      if (algorithm.Problem != null) Problems.Add((IProblem)algorithm.Problem);
[5313]256    }
[4516]257  }
258}
Note: See TracBrowser for help on using the repository browser.