1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Common.Resources;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.PluginInfrastructure;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem, IStorableContent
|
---|
38 | where T : class,IDataAnalysisProblemData
|
---|
39 | where U : class, ISymbolicDataAnalysisEvaluator<T>
|
---|
40 | where V : class, ISymbolicDataAnalysisSolutionCreator {
|
---|
41 |
|
---|
42 | #region parameter names & descriptions
|
---|
43 | private const string ProblemDataParameterName = "ProblemData";
|
---|
44 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
45 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
46 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
47 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
48 | private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
|
---|
49 | private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
|
---|
50 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
51 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
52 | private const string ValidationPartitionParameterName = "ValidationPartition";
|
---|
53 |
|
---|
54 | private const string ProblemDataParameterDescription = "";
|
---|
55 | private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
|
---|
56 | private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
|
---|
57 | private const string MaximumSymbolicExpressionTreeDepthParameterDescription = "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.";
|
---|
58 | private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
|
---|
59 | private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
|
---|
60 | private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
|
---|
61 | private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
|
---|
62 | private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
|
---|
63 | private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used the select the best model from (optional).";
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | #region parameter properties
|
---|
67 | IParameter IDataAnalysisProblem.ProblemDataParameter {
|
---|
68 | get { return ProblemDataParameter; }
|
---|
69 | }
|
---|
70 | public IValueParameter<T> ProblemDataParameter {
|
---|
71 | get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
72 | }
|
---|
73 | public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
74 | get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
75 | }
|
---|
76 | public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
77 | get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
78 | }
|
---|
79 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
80 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
81 | }
|
---|
82 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
83 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
84 | }
|
---|
85 | public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
|
---|
86 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
|
---|
87 | }
|
---|
88 | public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
|
---|
89 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
|
---|
90 | }
|
---|
91 | public IFixedValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
92 | get { return (IFixedValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
93 | }
|
---|
94 | public IFixedValueParameter<IntRange> FitnessCalculationPartitionParameter {
|
---|
95 | get { return (IFixedValueParameter<IntRange>)Parameters[FitnessCalculationPartitionParameterName]; }
|
---|
96 | }
|
---|
97 | public IFixedValueParameter<IntRange> ValidationPartitionParamater {
|
---|
98 | get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
|
---|
99 | }
|
---|
100 | #endregion
|
---|
101 |
|
---|
102 | #region properties
|
---|
103 | public string Filename { get; set; }
|
---|
104 | public override Image ItemImage { get { return VSImageLibrary.Type; } }
|
---|
105 |
|
---|
106 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
|
---|
107 | get { return ProblemData; }
|
---|
108 | }
|
---|
109 | public T ProblemData {
|
---|
110 | get { return ProblemDataParameter.Value; }
|
---|
111 | set { ProblemDataParameter.Value = value; }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
|
---|
115 | get { return SymbolicExpressionTreeGrammarParameter.Value; }
|
---|
116 | set { SymbolicExpressionTreeGrammarParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
119 | get { return SymbolicExpressionTreeInterpreterParameter.Value; }
|
---|
120 | set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
124 | get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
|
---|
125 | }
|
---|
126 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
127 | get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
|
---|
128 | }
|
---|
129 | public IntValue MaximumFunctionDefinitions {
|
---|
130 | get { return MaximumFunctionDefinitionsParameter.Value; }
|
---|
131 | }
|
---|
132 | public IntValue MaximumFunctionArguments {
|
---|
133 | get { return MaximumFunctionArgumentsParameter.Value; }
|
---|
134 | }
|
---|
135 | public PercentValue RelativeNumberOfEvaluatedSamples {
|
---|
136 | get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
|
---|
137 | }
|
---|
138 |
|
---|
139 | public IntRange FitnessCalculationPartition {
|
---|
140 | get { return FitnessCalculationPartitionParameter.Value; }
|
---|
141 | }
|
---|
142 | public IntRange ValidationPartition {
|
---|
143 | get { return ValidationPartitionParamater.Value; }
|
---|
144 | }
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | [StorableConstructor]
|
---|
148 | protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
|
---|
149 | [StorableHook(HookType.AfterDeserialization)]
|
---|
150 | private void AfterDeserialization() {
|
---|
151 | RegisterEventHandlers();
|
---|
152 | }
|
---|
153 | protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
|
---|
154 | : base(original, cloner) {
|
---|
155 | RegisterEventHandlers();
|
---|
156 | }
|
---|
157 |
|
---|
158 | protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
|
---|
159 | : base(evaluator, solutionCreator) {
|
---|
160 | Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
|
---|
161 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
|
---|
162 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
|
---|
163 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription, new IntValue()));
|
---|
164 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription, new IntValue()));
|
---|
165 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription, new IntValue()));
|
---|
166 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription, new IntValue()));
|
---|
167 | Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription, new IntRange()));
|
---|
168 | Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription, new IntRange()));
|
---|
169 | Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
|
---|
170 |
|
---|
171 | SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
|
---|
172 | SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
173 |
|
---|
174 | FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
175 | FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
|
---|
176 |
|
---|
177 | InitializeOperators();
|
---|
178 |
|
---|
179 | UpdateGrammar();
|
---|
180 | RegisterEventHandlers();
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected virtual void UpdateGrammar() {
|
---|
184 | SymbolicExpressionTreeGrammar.MaximumFunctionArguments = MaximumFunctionArguments.Value;
|
---|
185 | SymbolicExpressionTreeGrammar.MaximumFunctionDefinitions = MaximumFunctionDefinitions.Value;
|
---|
186 | foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
|
---|
187 | varSymbol.VariableNames = ProblemData.AllowedInputVariables;
|
---|
188 | }
|
---|
189 | foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
|
---|
190 | varSymbol.VariableNames = ProblemData.AllowedInputVariables;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private void InitializeOperators() {
|
---|
195 | Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
|
---|
196 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
197 | Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
|
---|
198 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
199 | ParameterizeOperators();
|
---|
200 | }
|
---|
201 |
|
---|
202 | #region events
|
---|
203 | private void RegisterEventHandlers() {
|
---|
204 | ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
|
---|
205 | ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
|
---|
206 |
|
---|
207 | MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
|
---|
208 | MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
|
---|
209 | MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
|
---|
213 | ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
|
---|
214 | OnProblemDataChanged();
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
|
---|
218 | UpdateGrammar();
|
---|
219 | }
|
---|
220 |
|
---|
221 | private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
|
---|
222 | if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
|
---|
223 | MaximumSymbolicExpressionTreeDepth.Value = 3;
|
---|
224 | }
|
---|
225 |
|
---|
226 | protected override void OnSolutionCreatorChanged() {
|
---|
227 | base.OnSolutionCreatorChanged();
|
---|
228 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
229 | ParameterizeOperators();
|
---|
230 | }
|
---|
231 |
|
---|
232 | private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
233 | ParameterizeOperators();
|
---|
234 | }
|
---|
235 |
|
---|
236 | protected override void OnEvaluatorChanged() {
|
---|
237 | base.OnEvaluatorChanged();
|
---|
238 | ParameterizeOperators();
|
---|
239 | }
|
---|
240 |
|
---|
241 | public event EventHandler ProblemDataChanged;
|
---|
242 | protected virtual void OnProblemDataChanged() {
|
---|
243 | FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
244 | FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
|
---|
245 |
|
---|
246 | UpdateGrammar();
|
---|
247 | ParameterizeOperators();
|
---|
248 |
|
---|
249 | var handler = ProblemDataChanged;
|
---|
250 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
251 |
|
---|
252 | OnReset();
|
---|
253 | }
|
---|
254 | #endregion
|
---|
255 |
|
---|
256 | protected virtual void ParameterizeOperators() {
|
---|
257 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
258 |
|
---|
259 | foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
260 | op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
|
---|
261 | }
|
---|
262 | foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
263 | op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
|
---|
264 | op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
|
---|
265 | }
|
---|
266 | foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
|
---|
267 | op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
|
---|
268 | op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
|
---|
269 | }
|
---|
270 | foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
|
---|
271 | op.ProblemDataParameter.ActualName = ProblemDataParameterName;
|
---|
272 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
273 | op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
|
---|
274 | op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
|
---|
275 | }
|
---|
276 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
|
---|
277 | op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
278 | op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
279 | }
|
---|
280 | foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
281 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
282 | }
|
---|
283 | foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
284 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
285 | }
|
---|
286 | foreach (var op in operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
|
---|
287 | op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
|
---|
288 | op.ValidationPartitionParameter.ActualName = ValidationPartitionParamater.Name;
|
---|
289 | }
|
---|
290 | foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
|
---|
291 | op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
|
---|
292 | }
|
---|
293 | }
|
---|
294 |
|
---|
295 | public abstract void ImportProblemDataFromFile(string fileName);
|
---|
296 | }
|
---|
297 | }
|
---|