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