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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Drawing;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Regression;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
36 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
|
---|
37 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
|
---|
38 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
|
---|
39 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
|
---|
40 |
|
---|
41 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
|
---|
42 | [Item("SymbolicRegressionProblem", "Represents a symbolic regression problem.")]
|
---|
43 | [Creatable("Problems")]
|
---|
44 | [StorableClass]
|
---|
45 | public sealed class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {
|
---|
46 |
|
---|
47 | #region Parameter Properties
|
---|
48 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
49 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
50 | }
|
---|
51 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
52 | get { return MaximizationParameter; }
|
---|
53 | }
|
---|
54 | public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
|
---|
55 | get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
|
---|
56 | }
|
---|
57 | IParameter IProblem.SolutionCreatorParameter {
|
---|
58 | get { return SolutionCreatorParameter; }
|
---|
59 | }
|
---|
60 | public ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
|
---|
61 | get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
|
---|
62 | }
|
---|
63 | IParameter IProblem.EvaluatorParameter {
|
---|
64 | get { return EvaluatorParameter; }
|
---|
65 | }
|
---|
66 | public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
|
---|
67 | get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
|
---|
68 | }
|
---|
69 | public ValueParameter<IntValue> MaxExpressionLengthParameter {
|
---|
70 | get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
|
---|
71 | }
|
---|
72 | public ValueParameter<IntValue> MaxExpressionDepthParameter {
|
---|
73 | get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
|
---|
74 | }
|
---|
75 | public ValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
|
---|
76 | get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefiningBranches"]; }
|
---|
77 | }
|
---|
78 | public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
|
---|
79 | get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
|
---|
80 | }
|
---|
81 | public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
|
---|
82 | get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
|
---|
83 | }
|
---|
84 | IParameter IProblem.VisualizerParameter {
|
---|
85 | get { return VisualizerParameter; }
|
---|
86 | }
|
---|
87 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
88 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
89 | }
|
---|
90 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
91 | get { return BestKnownQualityParameter; }
|
---|
92 | }
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | #region Properties
|
---|
96 | public IntValue MaxExpressionLength {
|
---|
97 | get { return MaxExpressionLengthParameter.Value; }
|
---|
98 | set { MaxExpressionLengthParameter.Value = value; }
|
---|
99 | }
|
---|
100 | public IntValue MaxExpressionDepth {
|
---|
101 | get { return MaxExpressionDepthParameter.Value; }
|
---|
102 | set { MaxExpressionDepthParameter.Value = value; }
|
---|
103 | }
|
---|
104 | public SymbolicExpressionTreeCreator SolutionCreator {
|
---|
105 | get { return SolutionCreatorParameter.Value; }
|
---|
106 | set { SolutionCreatorParameter.Value = value; }
|
---|
107 | }
|
---|
108 | ISolutionCreator IProblem.SolutionCreator {
|
---|
109 | get { return SolutionCreatorParameter.Value; }
|
---|
110 | }
|
---|
111 | public ISymbolicRegressionEvaluator Evaluator {
|
---|
112 | get { return EvaluatorParameter.Value; }
|
---|
113 | set { EvaluatorParameter.Value = value; }
|
---|
114 | }
|
---|
115 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
116 | get { return EvaluatorParameter.Value; }
|
---|
117 | }
|
---|
118 | IEvaluator IProblem.Evaluator {
|
---|
119 | get { return EvaluatorParameter.Value; }
|
---|
120 | }
|
---|
121 | public ISymbolicExpressionGrammar FunctionTreeGrammar {
|
---|
122 | get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
|
---|
123 | }
|
---|
124 | public ISingleObjectiveSolutionsVisualizer Visualizer {
|
---|
125 | get { return VisualizerParameter.Value; }
|
---|
126 | set { VisualizerParameter.Value = value; }
|
---|
127 | }
|
---|
128 | ISolutionsVisualizer IProblem.Visualizer {
|
---|
129 | get { return VisualizerParameter.Value; }
|
---|
130 | }
|
---|
131 | public DoubleValue BestKnownQuality {
|
---|
132 | get { return BestKnownQualityParameter.Value; }
|
---|
133 | }
|
---|
134 | private List<ISymbolicExpressionTreeOperator> operators;
|
---|
135 | public IEnumerable<IOperator> Operators {
|
---|
136 | get { return operators.Cast<IOperator>(); }
|
---|
137 | }
|
---|
138 | #endregion
|
---|
139 |
|
---|
140 | public SymbolicRegressionProblem()
|
---|
141 | : base() {
|
---|
142 | SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
|
---|
143 | var evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
|
---|
144 | var grammar = new ArithmeticExpressionGrammar();
|
---|
145 | var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
|
---|
146 | var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
|
---|
147 | var interpreter = new SimpleArithmeticExpressionInterpreter();
|
---|
148 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
|
---|
149 | Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
|
---|
150 | Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
|
---|
151 | Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
|
---|
152 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
|
---|
153 | Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
|
---|
154 | Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
|
---|
155 | Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
|
---|
156 | Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));
|
---|
157 | Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));
|
---|
158 | Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
|
---|
159 |
|
---|
160 | creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
|
---|
161 | creator.MaxFunctionArgumentsParameter.ActualName = "MaxFunctionArguments";
|
---|
162 | creator.MaxFunctionDefinitionsParameter.ActualName = "MaxFunctionDefiningBranches";
|
---|
163 | DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
|
---|
164 | DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
|
---|
165 | ParameterizeSolutionCreator();
|
---|
166 | ParameterizeEvaluator();
|
---|
167 | ParameterizeVisualizer();
|
---|
168 |
|
---|
169 | Initialize();
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | [StorableConstructor]
|
---|
174 | private SymbolicRegressionProblem(bool deserializing) : base() { }
|
---|
175 |
|
---|
176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
177 | SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
|
---|
178 | clone.Initialize();
|
---|
179 | return clone;
|
---|
180 | }
|
---|
181 |
|
---|
182 | #region Events
|
---|
183 | void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
|
---|
184 | DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
|
---|
185 | }
|
---|
186 |
|
---|
187 | void DataAnalysisProblemData_Changed(object sender, EventArgs e) {
|
---|
188 | UpdateGrammar();
|
---|
189 | UpdatePartitioningParameters();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void UpdateGrammar() {
|
---|
193 | foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
|
---|
194 | varSymbol.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | private void UpdatePartitioningParameters() {
|
---|
199 | int trainingStart = DataAnalysisProblemData.TrainingSamplesStart.Value;
|
---|
200 | int validationEnd = DataAnalysisProblemData.TrainingSamplesEnd.Value;
|
---|
201 | int trainingEnd = trainingStart + (validationEnd - trainingStart) / 2;
|
---|
202 | int validationStart = trainingEnd;
|
---|
203 | var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
|
---|
204 | if (solutionVisualizer != null) {
|
---|
205 | solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue(validationStart);
|
---|
206 | solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(validationEnd);
|
---|
207 | }
|
---|
208 | Evaluator.SamplesStartParameter.Value = new IntValue(trainingStart);
|
---|
209 | Evaluator.SamplesEndParameter.Value = new IntValue(trainingEnd);
|
---|
210 | }
|
---|
211 |
|
---|
212 | public event EventHandler SolutionCreatorChanged;
|
---|
213 | private void OnSolutionCreatorChanged() {
|
---|
214 | var changed = SolutionCreatorChanged;
|
---|
215 | if (changed != null)
|
---|
216 | changed(this, EventArgs.Empty);
|
---|
217 | }
|
---|
218 | public event EventHandler EvaluatorChanged;
|
---|
219 | private void OnEvaluatorChanged() {
|
---|
220 | var changed = EvaluatorChanged;
|
---|
221 | if (changed != null)
|
---|
222 | changed(this, EventArgs.Empty);
|
---|
223 | }
|
---|
224 | public event EventHandler VisualizerChanged;
|
---|
225 | private void OnVisualizerChanged() {
|
---|
226 | var changed = VisualizerChanged;
|
---|
227 | if (changed != null)
|
---|
228 | changed(this, EventArgs.Empty);
|
---|
229 | }
|
---|
230 |
|
---|
231 | public event EventHandler OperatorsChanged;
|
---|
232 | private void OnOperatorsChanged() {
|
---|
233 | var changed = OperatorsChanged;
|
---|
234 | if (changed != null)
|
---|
235 | changed(this, EventArgs.Empty);
|
---|
236 | }
|
---|
237 |
|
---|
238 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
239 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
240 | ParameterizeSolutionCreator();
|
---|
241 | ParameterizeEvaluator();
|
---|
242 | ParameterizeVisualizer();
|
---|
243 | ParameterizeOperators();
|
---|
244 | OnSolutionCreatorChanged();
|
---|
245 | }
|
---|
246 | private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
247 | ParameterizeEvaluator();
|
---|
248 | ParameterizeVisualizer();
|
---|
249 | ParameterizeOperators();
|
---|
250 | }
|
---|
251 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
252 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
253 | ParameterizeEvaluator();
|
---|
254 | ParameterizeVisualizer();
|
---|
255 | OnEvaluatorChanged();
|
---|
256 | }
|
---|
257 |
|
---|
258 | private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
259 | ParameterizeVisualizer();
|
---|
260 | OnVisualizerChanged();
|
---|
261 | }
|
---|
262 |
|
---|
263 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
264 | ParameterizeVisualizer();
|
---|
265 | }
|
---|
266 |
|
---|
267 | #endregion
|
---|
268 |
|
---|
269 | #region Helpers
|
---|
270 | [StorableHook(HookType.AfterDeserialization)]
|
---|
271 | private void Initialize() {
|
---|
272 | InitializeOperators();
|
---|
273 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
274 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
275 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
276 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
277 | VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
|
---|
278 | }
|
---|
279 |
|
---|
280 | private void InitializeOperators() {
|
---|
281 | operators = new List<ISymbolicExpressionTreeOperator>();
|
---|
282 | operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
|
---|
283 | ParameterizeOperators();
|
---|
284 | UpdateGrammar();
|
---|
285 | UpdatePartitioningParameters();
|
---|
286 | }
|
---|
287 |
|
---|
288 | private void ParameterizeSolutionCreator() {
|
---|
289 | SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
|
---|
290 | SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
|
---|
291 | SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
|
---|
292 | }
|
---|
293 | private void ParameterizeEvaluator() {
|
---|
294 | Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
295 | Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
|
---|
296 | Evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
|
---|
297 | Evaluator.SamplesStartParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value);
|
---|
298 | Evaluator.SamplesEndParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
|
---|
299 | }
|
---|
300 | private void ParameterizeVisualizer() {
|
---|
301 | if (Visualizer != null) {
|
---|
302 | var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
|
---|
303 | if (solutionVisualizer != null) {
|
---|
304 | solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
305 | solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
|
---|
306 | solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
|
---|
307 | solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | private void ParameterizeOperators() {
|
---|
313 | foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
|
---|
314 | op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
|
---|
315 | op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
|
---|
316 | op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
|
---|
317 | }
|
---|
318 | foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
|
---|
319 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
320 | op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
|
---|
321 | }
|
---|
322 | foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
|
---|
323 | op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
324 | op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
325 | }
|
---|
326 | foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
|
---|
327 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
328 | }
|
---|
329 | foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
|
---|
330 | }
|
---|
331 | }
|
---|
332 | #endregion
|
---|
333 | }
|
---|
334 | }
|
---|