Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GaussianProcessEvolution/HeuristicLab.Problems.GaussianProcessTuning/Problem.cs @ 8753

Last change on this file since 8753 was 8753, checked in by gkronber, 12 years ago

#1967 initial import of Gaussian process evolution plugin

File size: 8.5 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.PluginInfrastructure;
11using HeuristicLab.Problems.DataAnalysis;
12
13namespace HeuristicLab.Problems.GaussianProcessTuning {
14  [StorableClass]
15  [Creatable("Problems")]
16  [Item("Gaussian Process Configuration Problem", "The Gaussian process configuration problem for genetic programming.")]
17  public class Problem : SingleObjectiveHeuristicOptimizationProblem<Evaluator, ISymbolicExpressionTreeCreator> {
18    private const string GaussianProcessConfigurationParameterName = "GaussianProcessConfiguration";
19    private const string MaxGaussianProcessConfigurationLengthParameterName = "MaxConfigurationLength";
20    private const string MaxGaussianProcessConfigurationParameterName = "MaxConfigurationDepth";
21    private const string GaussianProcessConfigurationGrammarParameterName = "Grammar";
22    private const string DimensionParameterName = "Dimension";
23    private const string InterpreterParameterName = "Interpreter";
24    private const string ProblemDataParameterName = "ProblemData";
25
26
27    public IFixedValueParameter<IntValue> MaxGaussianProcessConfigurationLengthParameter {
28      get { return (IFixedValueParameter<IntValue>)Parameters[MaxGaussianProcessConfigurationLengthParameterName]; }
29    }
30    public IFixedValueParameter<IntValue> MaxGaussianProcessConfigurationDepthParameter {
31      get { return (IFixedValueParameter<IntValue>)Parameters[MaxGaussianProcessConfigurationParameterName]; }
32    }
33    public IValueParameter<Grammar> GrammarParameter {
34      get { return (IValueParameter<Grammar>)Parameters[GaussianProcessConfigurationGrammarParameterName]; }
35    }
36    public IFixedValueParameter<IntValue> DimensionParameter {
37      get { return (IFixedValueParameter<IntValue>)Parameters[DimensionParameterName]; }
38    }
39    public IValueParameter<Interpreter> InterpreterParameter {
40      get { return (IValueParameter<Interpreter>)Parameters[InterpreterParameterName]; }
41    }
42    public IValueParameter<IRegressionProblemData> ProblemDataParameter {
43      get { return (IValueParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
44    }
45
46    [StorableConstructor]
47    protected Problem(bool deserializing)
48      : base(deserializing) {
49    }
50    protected Problem(Problem original, Cloner cloner)
51      : base(original, cloner) {
52      RegisterEventHandlers();
53    }
54    public Problem()
55      : base(new Evaluator(), new ProbabilisticTreeCreator()) {
56      Parameters.Add(new ValueParameter<Interpreter>(InterpreterParameterName, "The interpreter for Gaussian process configurations represented as trees.", new Interpreter()));
57      Parameters.Add(new FixedValueParameter<IntValue>(DimensionParameterName, "The dimension parameter for the Gaussian process configuration.", new IntValue(10)));
58      Parameters.Add(new FixedValueParameter<IntValue>(MaxGaussianProcessConfigurationParameterName, "Maximal depth of the Gaussian process configuration.", new IntValue(6)));
59      Parameters.Add(new FixedValueParameter<IntValue>(MaxGaussianProcessConfigurationLengthParameterName, "Maximal length of the Gaussian process configuration.", new IntValue(15)));
60      Parameters.Add(new ValueParameter<Grammar>(GaussianProcessConfigurationGrammarParameterName, "Grammar for the Gaussian process configuration.",
61                     new Grammar()));
62      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, "The problem data to learn a Gaussian process for.", new RegressionProblemData()));
63      Maximization.Value = false;
64      InitializeOperators();
65
66      RegisterEventHandlers();
67    }
68
69
70    [StorableHook(HookType.AfterDeserialization)]
71    private void AfterDeserialization() {
72      RegisterEventHandlers();
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new Problem(this, cloner);
77    }
78
79    private void InitializeOperators() {
80      Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
81      Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
82      Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
83      Operators.Add(new BestSolutionAnalyzer());
84      ParameterizeOperators();
85      ParameterizeAnalyzers();
86    }
87
88
89    private void RegisterEventHandlers() {
90      Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
91      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged +=
92        SymbolicExpressionTreeParameterOnActualNameChanged;
93      DimensionParameter.Value.ValueChanged += DimensionParameterOnValueChanged;
94      GrammarParameter.ValueChanged += new EventHandler(GrammarParameter_ValueChanged);
95    }
96
97    void GrammarParameter_ValueChanged(object sender, EventArgs e) {
98      GrammarParameter.Value.Dimension = DimensionParameter.Value.Value;
99    }
100
101    private void DimensionParameterOnValueChanged(object sender, EventArgs eventArgs) {
102      GrammarParameter.Value.Dimension = DimensionParameter.Value.Value;
103    }
104
105
106    protected override void OnEvaluatorChanged() {
107      base.OnEvaluatorChanged();
108      Evaluator.GaussianProcessConfigurationParameter.ActualName = GaussianProcessConfigurationParameterName;
109      Evaluator.QualityParameter.ActualNameChanged += QualityParameterOnActualNameChanged;
110      ParameterizeAnalyzers();
111      ParameterizeOperators();
112    }
113
114    protected override void OnSolutionCreatorChanged() {
115      base.OnSolutionCreatorChanged();
116      SolutionCreator.SymbolicExpressionTreeParameter.ActualName = GaussianProcessConfigurationParameterName;
117      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += SymbolicExpressionTreeParameterOnActualNameChanged;
118      ParameterizeAnalyzers();
119      ParameterizeOperators();
120    }
121
122    private void SymbolicExpressionTreeParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
123      ParameterizeAnalyzers();
124      ParameterizeOperators();
125    }
126
127    private void QualityParameterOnActualNameChanged(object sender, EventArgs eventArgs) {
128      ParameterizeAnalyzers();
129      ParameterizeOperators();
130    }
131
132    private void ParameterizeAnalyzers() {
133      var analyzers = Operators.OfType<IAnalyzer>();
134      foreach (var o in analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
135        o.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
136      }
137      foreach (var o in analyzers.OfType<BestSolutionAnalyzer>()) {
138        o.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
139      }
140    }
141
142    private void ParameterizeOperators() {
143      var operators = Parameters
144        .OfType<IValueParameter>()
145        .Select(p => p.Value)
146        .OfType<IOperator>()
147        .Union(Operators);
148      foreach (var o in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
149        o.SymbolicExpressionTreeGrammarParameter.ActualName = GaussianProcessConfigurationGrammarParameterName;
150      }
151      foreach (var o in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
152        o.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaxGaussianProcessConfigurationParameterName;
153        o.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaxGaussianProcessConfigurationLengthParameterName;
154      }
155      foreach (var op in operators.OfType<Evaluator>()) {
156        op.GaussianProcessConfigurationParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
157      }
158      foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
159        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
160        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
161      }
162      foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
163        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
164      }
165      foreach (var op in operators.OfType<ISymbolicExpressionTreeCreator>()) {
166        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
167      }
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.