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 | //TODO configure training start / end
|
---|
36 | //TODO configure analyzer validation
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
39 | [StorableClass]
|
---|
40 | public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, ISymbolicDataAnalysisProblem, IStorableContent
|
---|
41 | where T : class,IDataAnalysisProblemData
|
---|
42 | where U : class, ISymbolicDataAnalysisEvaluator<T>
|
---|
43 | where V : class, ISymbolicDataAnalysisSolutionCreator {
|
---|
44 | #region parameter names & descriptions
|
---|
45 | private const string ProblemDataParameterName = "ProblemData";
|
---|
46 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
47 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
48 | private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
|
---|
49 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
50 | private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions";
|
---|
51 | private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments";
|
---|
52 |
|
---|
53 | private const string ProblemDataParameterDescription = "";
|
---|
54 | private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
|
---|
55 | private const string SymoblicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
|
---|
56 | 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.";
|
---|
57 | private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
|
---|
58 | private const string MaximumFunctionDefinitionsParameterDescription = "Maximal number of automatically defined functions";
|
---|
59 | private const string MaximumFunctionArgumentsParameterDescription = "Maximal number of arguments of automatically defined functions.";
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | #region parameter properties
|
---|
63 | IParameter IDataAnalysisProblem.ProblemDataParameter {
|
---|
64 | get { return ProblemDataParameter; }
|
---|
65 | }
|
---|
66 | public IValueParameter<T> ProblemDataParameter {
|
---|
67 | get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
68 | }
|
---|
69 | public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
70 | get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
71 | }
|
---|
72 | public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
73 | get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
74 | }
|
---|
75 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
|
---|
76 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
|
---|
77 | }
|
---|
78 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
79 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
80 | }
|
---|
81 | public IFixedValueParameter<IntValue> MaximumFunctionDefinitionsParameter {
|
---|
82 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionDefinitionsParameterName]; }
|
---|
83 | }
|
---|
84 | public IFixedValueParameter<IntValue> MaximumFunctionArgumentsParameter {
|
---|
85 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumFunctionArgumentsParameterName]; }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | #region properties
|
---|
90 | public string Filename { get; set; }
|
---|
91 | public override Image ItemImage { get { return VSImageLibrary.Type; } }
|
---|
92 |
|
---|
93 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
|
---|
94 | get { return ProblemData; }
|
---|
95 | }
|
---|
96 | public T ProblemData {
|
---|
97 | get { return ProblemDataParameter.Value; }
|
---|
98 | set { ProblemDataParameter.Value = value; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
|
---|
102 | get { return SymbolicExpressionTreeGrammarParameter.Value; }
|
---|
103 | set { SymbolicExpressionTreeGrammarParameter.Value = value; }
|
---|
104 | }
|
---|
105 | public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
106 | get { return SymbolicExpressionTreeInterpreterParameter.Value; }
|
---|
107 | set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public IntValue MaximumSymbolicExpressionTreeDepth {
|
---|
111 | get { return MaximumSymbolicExpressionTreeDepthParameter.Value; }
|
---|
112 | }
|
---|
113 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
114 | get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
|
---|
115 | }
|
---|
116 | public IntValue MaximumFunctionDefinitions {
|
---|
117 | get { return MaximumFunctionDefinitionsParameter.Value; }
|
---|
118 | }
|
---|
119 | public IntValue MaximumFunctionArguments {
|
---|
120 | get { return MaximumFunctionArgumentsParameter.Value; }
|
---|
121 | }
|
---|
122 | #endregion
|
---|
123 |
|
---|
124 | [StorableConstructor]
|
---|
125 | protected SymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
|
---|
126 | [StorableHook(HookType.AfterDeserialization)]
|
---|
127 | private void AfterDeserialization() {
|
---|
128 | RegisterEventHandlers();
|
---|
129 | }
|
---|
130 | protected SymbolicDataAnalysisProblem(SymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
|
---|
131 | : base(original, cloner) {
|
---|
132 | RegisterEventHandlers();
|
---|
133 | }
|
---|
134 |
|
---|
135 | protected SymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
|
---|
136 | : base(evaluator, solutionCreator) {
|
---|
137 | Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
|
---|
138 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
|
---|
139 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymoblicExpressionTreeInterpreterParameterDescription));
|
---|
140 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, MaximumSymbolicExpressionTreeDepthParameterDescription, new IntValue()));
|
---|
141 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription, new IntValue()));
|
---|
142 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionDefinitionsParameterName, MaximumFunctionDefinitionsParameterDescription, new IntValue()));
|
---|
143 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumFunctionArgumentsParameterName, MaximumFunctionArgumentsParameterDescription, new IntValue()));
|
---|
144 |
|
---|
145 | SymbolicExpressionTreeGrammar = new TypeCoherentExpressionGrammar();
|
---|
146 | SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
|
---|
147 |
|
---|
148 | UpdateGrammar();
|
---|
149 | UpdateDatasetPartitions();
|
---|
150 | RegisterEventHandlers();
|
---|
151 | InitializeOperators();
|
---|
152 | }
|
---|
153 |
|
---|
154 | protected virtual void UpdateDatasetPartitions() {
|
---|
155 | Evaluator.SamplesStartParameter.Value.Value = ProblemData.TrainingPartitionStart.Value;
|
---|
156 | Evaluator.SamplesEndParameter.Value.Value = ProblemData.TrainingPartitionEnd.Value;
|
---|
157 |
|
---|
158 | foreach (var analyzer in Operators.OfType<ISymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
|
---|
159 | analyzer.ValidationSamplesStartParameter.Value = new IntValue(ProblemData.TrainingPartitionStart.Value);
|
---|
160 | analyzer.ValidationSamplesEndParameter.Value = new IntValue(ProblemData.TrainingPartitionEnd.Value);
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | protected virtual void UpdateGrammar() {
|
---|
165 | foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Variable>()) {
|
---|
166 | varSymbol.VariableNames = ProblemData.AllowedInputVariables;
|
---|
167 | }
|
---|
168 | foreach (var varSymbol in SymbolicExpressionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.VariableCondition>()) {
|
---|
169 | varSymbol.VariableNames = ProblemData.AllowedInputVariables;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | private void InitializeOperators() {
|
---|
174 | Operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
|
---|
175 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
176 | Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
|
---|
177 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
178 | ParameterizeOperators();
|
---|
179 | }
|
---|
180 |
|
---|
181 | #region events
|
---|
182 | private void RegisterEventHandlers() {
|
---|
183 | ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
|
---|
184 | ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
|
---|
185 |
|
---|
186 | MaximumFunctionArguments.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
|
---|
187 | MaximumFunctionDefinitions.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
|
---|
188 | MaximumSymbolicExpressionTreeDepth.ValueChanged += new EventHandler(MaximumSymbolicExpressionTreeDepth_ValueChanged);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
|
---|
192 | ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
|
---|
193 | OnProblemDataChanged();
|
---|
194 | }
|
---|
195 |
|
---|
196 | private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
|
---|
197 | UpdateGrammar();
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void MaximumSymbolicExpressionTreeDepth_ValueChanged(object sender, EventArgs e) {
|
---|
201 | if (MaximumSymbolicExpressionTreeDepth != null && MaximumSymbolicExpressionTreeDepth.Value < 3)
|
---|
202 | MaximumSymbolicExpressionTreeDepth.Value = 3;
|
---|
203 | }
|
---|
204 |
|
---|
205 | protected override void OnSolutionCreatorChanged() {
|
---|
206 | base.OnSolutionCreatorChanged();
|
---|
207 | SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
208 | ParameterizeOperators();
|
---|
209 | }
|
---|
210 |
|
---|
211 | private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
212 | ParameterizeOperators();
|
---|
213 | }
|
---|
214 |
|
---|
215 | protected override void OnEvaluatorChanged() {
|
---|
216 | base.OnEvaluatorChanged();
|
---|
217 | ParameterizeOperators();
|
---|
218 | }
|
---|
219 |
|
---|
220 | public event EventHandler ProblemDataChanged;
|
---|
221 | protected virtual void OnProblemDataChanged() {
|
---|
222 | UpdateGrammar();
|
---|
223 | UpdateDatasetPartitions();
|
---|
224 | ParameterizeOperators();
|
---|
225 |
|
---|
226 | var handler = ProblemDataChanged;
|
---|
227 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
228 |
|
---|
229 | OnReset();
|
---|
230 | }
|
---|
231 | #endregion
|
---|
232 |
|
---|
233 | protected virtual void ParameterizeOperators() {
|
---|
234 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
|
---|
235 |
|
---|
236 | foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
237 | op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameterName;
|
---|
238 | }
|
---|
239 | foreach (var op in operators.OfType<ISymbolicExpressionTreeSizeConstraintOperator>()) {
|
---|
240 | op.MaximumSymbolicExpressionTreeDepthParameter.ActualName = MaximumSymbolicExpressionTreeDepthParameterName;
|
---|
241 | op.MaximumSymbolicExpressionTreeLengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameterName;
|
---|
242 | }
|
---|
243 | foreach (var op in operators.OfType<ISymbolicExpressionTreeArchitectureAlteringOperator>()) {
|
---|
244 | op.MaximumFunctionArgumentsParameter.ActualName = MaximumFunctionArgumentsParameterName;
|
---|
245 | op.MaximumFunctionDefinitionsParameter.ActualName = MaximumFunctionDefinitionsParameterName;
|
---|
246 | }
|
---|
247 | foreach (var op in operators.OfType<ISymbolicDataAnalysisEvaluator<T>>()) {
|
---|
248 | op.ProblemDataParameter.ActualName = ProblemDataParameterName;
|
---|
249 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
250 | op.SamplesStartParameter.Value.Value = ProblemData.TrainingPartitionStart.Value;
|
---|
251 | op.SamplesEndParameter.Value.Value = ProblemData.TrainingPartitionEnd.Value;
|
---|
252 | }
|
---|
253 | foreach (var op in operators.OfType<ISymbolicExpressionTreeCrossover>()) {
|
---|
254 | op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
255 | op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
256 | }
|
---|
257 | foreach (var op in operators.OfType<ISymbolicExpressionTreeManipulator>()) {
|
---|
258 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
259 | }
|
---|
260 | foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
261 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
262 | }
|
---|
263 | foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
|
---|
264 | op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameterName;
|
---|
265 | }
|
---|
266 | foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
|
---|
267 | op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
|
---|
268 | }
|
---|
269 | }
|
---|
270 |
|
---|
271 | public abstract void ImportProblemDataFromFile(string fileName);
|
---|
272 | }
|
---|
273 | }
|
---|