Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1215

  • optional parameter values now can be null
  • problem type can now be set specifically
  • values are constrained to the encoding of the Problem
  • moved repetitions parameter to MetaOptimizationProblem
File size: 8.0 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<IMetaOptimizationEvaluator, IParameterConfigurationCreator> {
39    public const string AlgorithmTypeParameterName = "AlgorithmType";
40    public const string ProblemTypeParameterName = "ProblemType";
41    public const string ProblemsParameterName = "Problems";
42    public const string ParameterConfigurationParameterName = "InitialParameterConfigurationTree";
43    public const string RepetitionsParameterName = "Repetitions";
44
45    #region Parameter Properties
46    public IValueParameter<EngineAlgorithm> AlgorithmTypeParameter {
47      get { return (ValueParameter<EngineAlgorithm>)Parameters[AlgorithmTypeParameterName]; }
48    }
49    public IValueParameter<ISingleObjectiveProblem> ProblemTypeParameter {
50      get { return (ValueParameter<ISingleObjectiveProblem>)Parameters[ProblemTypeParameterName]; }
51    }
52    public IValueParameter<ConstrainedItemList<ISingleObjectiveProblem>> ProblemsParameter {
53      get { return (ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>)Parameters[ProblemsParameterName]; }
54    }
55    public IValueParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
56      get { return (ValueParameter<ParameterConfigurationTree>)Parameters[ParameterConfigurationParameterName]; }
57    }
58    public IValueParameter<IntValue> RepetitionsParameter {
59      get { return (ValueParameter<IntValue>)Parameters[RepetitionsParameterName]; }
60    }
61    #endregion
62
63    #region Properties
64    public EngineAlgorithm Algorithm {
65      get { return AlgorithmTypeParameter.Value; }
66      set { AlgorithmTypeParameter.Value = value; }
67    }
68    public ISingleObjectiveProblem Problem {
69      get { return ProblemTypeParameter.Value; }
70      set { ProblemTypeParameter.Value = value; }
71    }
72    public ConstrainedItemList<ISingleObjectiveProblem> Problems {
73      get { return ProblemsParameter.Value; }
74      set { ProblemsParameter.Value = value; }
75    }
76    public ParameterConfigurationTree AlgorithmParameterConfiguration {
77      get { return ParameterConfigurationParameter.Value; }
78      set { ParameterConfigurationParameter.Value = value; }
79    }
80    public IntValue Repetitions {
81      get { return RepetitionsParameter.Value; }
82      set { RepetitionsParameter.Value = value; }
83    }
84    #endregion
85
86    public MetaOptimizationProblem() : base() {
87      Parameters.Add(new ValueParameter<EngineAlgorithm>(AlgorithmTypeParameterName, "The algorithm which's parameters should be optimized.", new GeneticAlgorithm()));
88      Parameters.Add(new ValueParameter<ISingleObjectiveProblem>(ProblemTypeParameterName, "The problem type.", new SingleObjectiveTestFunctionProblem()));
89      Parameters.Add(new ValueParameter<ConstrainedItemList<ISingleObjectiveProblem>>(ProblemsParameterName, "The problems that should be evaluated.", new ConstrainedItemList<ISingleObjectiveProblem>()));
90      Parameters.Add(new ValueParameter<ParameterConfigurationTree>(ParameterConfigurationParameterName, "List of algorithm parameters that should be optimized."));
91      Parameters.Add(new ValueParameter<IntValue>(RepetitionsParameterName, "The number of evaluations for each problem.", new IntValue(3)));
92
93      Maximization = new BoolValue(false);
94      SolutionCreator = new RandomParameterConfigurationCreator();
95      Evaluator = new MetaOptimizationEvaluator();
96
97      InitializeOperators();
98      RegisterParameterEvents();
99      ParameterizeSolutionCreator();
100      ParameterizeEvaluator();
101      ParameterizeOperators();
102
103      Problems.Type = Problem.GetType();
104      Algorithm.Problem = Problem;
105      ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
106    }
107
108    [StorableConstructor]
109    private MetaOptimizationProblem(bool deserializing) : base(deserializing) { }
110    private MetaOptimizationProblem(MetaOptimizationProblem original, Cloner cloner) : base(original, cloner) {
111      // todo
112      this.RegisterParameterEvents();
113    }
114    public override IDeepCloneable Clone(Cloner cloner) {
115      return new MetaOptimizationProblem(this, cloner);
116    }
117
118    #region Helpers
119    [StorableHook(HookType.AfterDeserialization)]
120    private void AfterDeserializationHook() {
121      RegisterParameterEvents();
122    }
123    private void RegisterParameterEvents() {
124      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
125      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
126      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
127      AlgorithmTypeParameter.ValueChanged += new EventHandler(AlgorithmTypeParameter_ValueChanged);
128      ProblemTypeParameter.ValueChanged += new EventHandler(ProblemTypeParameter_ValueChanged);
129    }
130   
131    private void InitializeOperators() {
132      Operators.AddRange(ApplicationManager.Manager.GetInstances<IParameterConfigurationOperator>().Cast<IOperator>());
133      Operators.Add(new BestParameterConfigurationAnalyzer());
134    }
135    private void ParameterizeSolutionCreator() {
136    }
137    private void ParameterizeEvaluator() {
138      ((MetaOptimizationEvaluator)Evaluator).ParameterConfigurationParameter.ActualName = ((RandomParameterConfigurationCreator)SolutionCreator).ParameterConfigurationParameter.ActualName;
139    }
140    private void ParameterizeAnalyzer() {
141    }
142    private void ParameterizeOperators() {
143     
144    }
145
146    #endregion
147
148    #region Events
149
150    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
151      ParameterizeSolutionCreator();
152      ParameterizeEvaluator();
153      ParameterizeAnalyzer();
154      ParameterizeOperators();
155      OnSolutionCreatorChanged();
156    }
157    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
158      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
159      ParameterizeEvaluator();
160      ParameterizeAnalyzer();
161      OnEvaluatorChanged();
162    }
163    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
164      ParameterizeAnalyzer();
165    }
166    void AlgorithmTypeParameter_ValueChanged(object sender, EventArgs e) {
167      Algorithm.Problem = Problem;
168      ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
169    }
170    void ProblemTypeParameter_ValueChanged(object sender, EventArgs e) {
171      Problems.Type = Problem.GetType();
172      Algorithm.Problem = Problem;
173      ParameterConfigurationParameter.ActualValue = new ParameterConfigurationTree(Algorithm);
174    }
175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.