1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 | * Author: Sabine Winkler
|
---|
21 | */
|
---|
22 |
|
---|
23 | #endregion
|
---|
24 |
|
---|
25 | using System;
|
---|
26 | using System.Drawing;
|
---|
27 | using System.Linq;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Common.Resources;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Data;
|
---|
32 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
33 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
34 | using HeuristicLab.Optimization;
|
---|
35 | using HeuristicLab.Parameters;
|
---|
36 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
37 | using HeuristicLab.PluginInfrastructure;
|
---|
38 | using HeuristicLab.Problems.DataAnalysis;
|
---|
39 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
40 | using HeuristicLab.Problems.GrammaticalEvolution.Mappers;
|
---|
41 | using HeuristicLab.Problems.Instances;
|
---|
42 |
|
---|
43 | namespace HeuristicLab.Problems.GrammaticalEvolution {
|
---|
44 | [StorableClass("9A1CB700-11F4-4508-B779-A88F962A7BC1")]
|
---|
45 | public abstract class GESymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem<T>,
|
---|
46 | IGESymbolicDataAnalysisProblem, IStorableContent,
|
---|
47 | IProblemInstanceConsumer<T>, IProblemInstanceExporter<T>
|
---|
48 | where T : class, IDataAnalysisProblemData
|
---|
49 | where U : class, IGESymbolicDataAnalysisEvaluator<T>
|
---|
50 | where V : class, IIntegerVectorCreator {
|
---|
51 |
|
---|
52 | #region parameter names & descriptions
|
---|
53 | private const string ProblemDataParameterName = "ProblemData";
|
---|
54 | private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar";
|
---|
55 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
56 | private const string MaximumSymbolicExpressionTreeLengthParameterName = "MaximumSymbolicExpressionTreeLength";
|
---|
57 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
58 | private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition";
|
---|
59 | private const string ValidationPartitionParameterName = "ValidationPartition";
|
---|
60 | private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
|
---|
61 | private const string BoundsParameterName = "Bounds";
|
---|
62 | private const string GenotypeToPhenotypeMapperParameterName = "GenotypeToPhenotypeMapper";
|
---|
63 | private const string ProblemDataParameterDescription = "";
|
---|
64 | private const string SymbolicExpressionTreeGrammarParameterDescription = "The grammar that should be used for symbolic expression tree.";
|
---|
65 | private const string SymbolicExpressionTreeInterpreterParameterDescription = "The interpreter that should be used to evaluate the symbolic expression tree.";
|
---|
66 | private const string MaximumSymbolicExpressionTreeLengthParameterDescription = "Maximal length of the symbolic expression.";
|
---|
67 | private const string RelativeNumberOfEvaluatedSamplesParameterDescription = "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation.";
|
---|
68 | private const string FitnessCalculationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to calculate the fitness of an individual.";
|
---|
69 | private const string ValidationPartitionParameterDescription = "The partition of the problem data training partition, that should be used to select the best model from (optional).";
|
---|
70 | private const string ApplyLinearScalingParameterDescription = "Flag that indicates if the individual should be linearly scaled before evaluating.";
|
---|
71 | private const string BoundsParameterDescription = "The integer number range in which the single genomes of a genotype are created.";
|
---|
72 | private const string GenotypeToPhenotypeMapperParameterDescription = "Maps the genotype (an integer vector) to the phenotype (a symbolic expression tree).";
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region parameter properties
|
---|
76 | IParameter IDataAnalysisProblem.ProblemDataParameter {
|
---|
77 | get { return ProblemDataParameter; }
|
---|
78 | }
|
---|
79 | public IValueParameter<T> ProblemDataParameter {
|
---|
80 | get { return (IValueParameter<T>)Parameters[ProblemDataParameterName]; }
|
---|
81 | }
|
---|
82 | public IValueParameter<ISymbolicDataAnalysisGrammar> SymbolicExpressionTreeGrammarParameter {
|
---|
83 | get { return (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; }
|
---|
84 | }
|
---|
85 | public IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
86 | get { return (IValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
87 | }
|
---|
88 | public IFixedValueParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter {
|
---|
89 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeLengthParameterName]; }
|
---|
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> ValidationPartitionParameter {
|
---|
98 | get { return (IFixedValueParameter<IntRange>)Parameters[ValidationPartitionParameterName]; }
|
---|
99 | }
|
---|
100 | public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
|
---|
101 | get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
|
---|
102 | }
|
---|
103 | public IValueParameter<IntMatrix> BoundsParameter {
|
---|
104 | get { return (IValueParameter<IntMatrix>)Parameters[BoundsParameterName]; }
|
---|
105 | }
|
---|
106 | public IValueParameter<IGenotypeToPhenotypeMapper> GenotypeToPhenotypeMapperParameter {
|
---|
107 | get { return (IValueParameter<IGenotypeToPhenotypeMapper>)Parameters[GenotypeToPhenotypeMapperParameterName]; }
|
---|
108 | }
|
---|
109 | #endregion
|
---|
110 |
|
---|
111 | #region properties
|
---|
112 | public string Filename { get; set; }
|
---|
113 | public static new Image StaticItemImage { get { return VSImageLibrary.Type; } }
|
---|
114 |
|
---|
115 | IDataAnalysisProblemData IDataAnalysisProblem.ProblemData {
|
---|
116 | get { return ProblemData; }
|
---|
117 | }
|
---|
118 | public T ProblemData {
|
---|
119 | get { return ProblemDataParameter.Value; }
|
---|
120 | set { ProblemDataParameter.Value = value; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public ISymbolicDataAnalysisGrammar SymbolicExpressionTreeGrammar {
|
---|
124 | get { return SymbolicExpressionTreeGrammarParameter.Value; }
|
---|
125 | set { SymbolicExpressionTreeGrammarParameter.Value = value; }
|
---|
126 | }
|
---|
127 | public ISymbolicDataAnalysisExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
128 | get { return SymbolicExpressionTreeInterpreterParameter.Value; }
|
---|
129 | set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
|
---|
130 | }
|
---|
131 |
|
---|
132 | public IntValue MaximumSymbolicExpressionTreeLength {
|
---|
133 | get { return MaximumSymbolicExpressionTreeLengthParameter.Value; }
|
---|
134 | }
|
---|
135 |
|
---|
136 | public PercentValue RelativeNumberOfEvaluatedSamples {
|
---|
137 | get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
|
---|
138 | }
|
---|
139 |
|
---|
140 | public IntRange FitnessCalculationPartition {
|
---|
141 | get { return FitnessCalculationPartitionParameter.Value; }
|
---|
142 | }
|
---|
143 | public IntRange ValidationPartition {
|
---|
144 | get { return ValidationPartitionParameter.Value; }
|
---|
145 | }
|
---|
146 | public BoolValue ApplyLinearScaling {
|
---|
147 | get { return ApplyLinearScalingParameter.Value; }
|
---|
148 | }
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | [StorableConstructor]
|
---|
152 | protected GESymbolicDataAnalysisProblem(bool deserializing) : base(deserializing) { }
|
---|
153 | [StorableHook(HookType.AfterDeserialization)]
|
---|
154 | private void AfterDeserialization() {
|
---|
155 | RegisterEventHandlers();
|
---|
156 | }
|
---|
157 | protected GESymbolicDataAnalysisProblem(GESymbolicDataAnalysisProblem<T, U, V> original, Cloner cloner)
|
---|
158 | : base(original, cloner) {
|
---|
159 | RegisterEventHandlers();
|
---|
160 | }
|
---|
161 |
|
---|
162 | protected GESymbolicDataAnalysisProblem(T problemData, U evaluator, V solutionCreator)
|
---|
163 | : base(evaluator, solutionCreator) {
|
---|
164 | Parameters.Add(new ValueParameter<T>(ProblemDataParameterName, ProblemDataParameterDescription, problemData));
|
---|
165 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(SymbolicExpressionTreeGrammarParameterName, SymbolicExpressionTreeGrammarParameterDescription));
|
---|
166 | Parameters.Add(new ValueParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, SymbolicExpressionTreeInterpreterParameterDescription));
|
---|
167 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumSymbolicExpressionTreeLengthParameterName, MaximumSymbolicExpressionTreeLengthParameterDescription));
|
---|
168 | Parameters.Add(new FixedValueParameter<IntRange>(FitnessCalculationPartitionParameterName, FitnessCalculationPartitionParameterDescription));
|
---|
169 | Parameters.Add(new FixedValueParameter<IntRange>(ValidationPartitionParameterName, ValidationPartitionParameterDescription));
|
---|
170 | Parameters.Add(new FixedValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, RelativeNumberOfEvaluatedSamplesParameterDescription, new PercentValue(1)));
|
---|
171 | Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, ApplyLinearScalingParameterDescription, new BoolValue(false)));
|
---|
172 | IntMatrix m = new IntMatrix(new int[,] { { 0, 100 } });
|
---|
173 | Parameters.Add(new ValueParameter<IntMatrix>(BoundsParameterName, BoundsParameterDescription, m));
|
---|
174 | Parameters.Add(new ValueParameter<IGenotypeToPhenotypeMapper>(GenotypeToPhenotypeMapperParameterName, GenotypeToPhenotypeMapperParameterDescription, new DepthFirstMapper()));
|
---|
175 |
|
---|
176 | SymbolicExpressionTreeInterpreterParameter.Hidden = true;
|
---|
177 | ApplyLinearScalingParameter.Hidden = true;
|
---|
178 |
|
---|
179 | SymbolicExpressionTreeGrammar = new GESymbolicExpressionGrammar(problemData.AllowedInputVariables, problemData.AllowedInputVariables.Count() * 3);
|
---|
180 | SymbolicExpressionTreeInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
|
---|
181 |
|
---|
182 | FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
183 | FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
|
---|
184 |
|
---|
185 | InitializeOperators();
|
---|
186 |
|
---|
187 | UpdateGrammar();
|
---|
188 | RegisterEventHandlers();
|
---|
189 | }
|
---|
190 |
|
---|
191 | private void DeregisterGrammarHandler() {
|
---|
192 | SymbolicExpressionTreeGrammarParameter.ValueChanged -= SymbolicExpressionTreeGrammarParameter_ValueChanged;
|
---|
193 | }
|
---|
194 | private void RegisterGrammarHandler() {
|
---|
195 | SymbolicExpressionTreeGrammarParameter.ValueChanged += SymbolicExpressionTreeGrammarParameter_ValueChanged;
|
---|
196 | }
|
---|
197 |
|
---|
198 | private void UpdateGrammar() {
|
---|
199 | DeregisterGrammarHandler();
|
---|
200 | // create a new grammar instance with the correct allowed input variables
|
---|
201 | SymbolicExpressionTreeGrammarParameter.Value =
|
---|
202 | new GESymbolicExpressionGrammar(ProblemData.AllowedInputVariables, ProblemData.AllowedInputVariables.Count() * 3);
|
---|
203 | RegisterGrammarHandler();
|
---|
204 | }
|
---|
205 |
|
---|
206 | private void InitializeOperators() {
|
---|
207 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IIntegerVectorOperator>());
|
---|
208 | Operators.Add(new SymbolicExpressionSymbolFrequencyAnalyzer());
|
---|
209 | Operators.Add(new SymbolicDataAnalysisVariableFrequencyAnalyzer());
|
---|
210 | Operators.Add(new MinAverageMaxSymbolicExpressionTreeLengthAnalyzer());
|
---|
211 | Operators.Add(new SymbolicExpressionTreeLengthAnalyzer());
|
---|
212 | ParameterizeOperators();
|
---|
213 | }
|
---|
214 |
|
---|
215 | #region events
|
---|
216 | private void RegisterEventHandlers() {
|
---|
217 | ProblemDataParameter.ValueChanged += new EventHandler(ProblemDataParameter_ValueChanged);
|
---|
218 | ProblemDataParameter.Value.Changed += (object sender, EventArgs e) => OnProblemDataChanged();
|
---|
219 |
|
---|
220 | RegisterGrammarHandler();
|
---|
221 | }
|
---|
222 |
|
---|
223 | private void ProblemDataParameter_ValueChanged(object sender, EventArgs e) {
|
---|
224 | ValidationPartition.Start = 0;
|
---|
225 | ValidationPartition.End = 0;
|
---|
226 | ProblemDataParameter.Value.Changed += (object s, EventArgs args) => OnProblemDataChanged();
|
---|
227 | OnProblemDataChanged();
|
---|
228 | }
|
---|
229 |
|
---|
230 | private void SymbolicExpressionTreeGrammarParameter_ValueChanged(object sender, EventArgs e) {
|
---|
231 | UpdateGrammar();
|
---|
232 | }
|
---|
233 |
|
---|
234 | protected override void OnEvaluatorChanged() {
|
---|
235 | base.OnEvaluatorChanged();
|
---|
236 | Evaluator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged);
|
---|
237 | ParameterizeOperators();
|
---|
238 | }
|
---|
239 |
|
---|
240 | private void Evaluator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
241 | ParameterizeOperators();
|
---|
242 | }
|
---|
243 |
|
---|
244 | public event EventHandler ProblemDataChanged;
|
---|
245 | protected virtual void OnProblemDataChanged() {
|
---|
246 | FitnessCalculationPartition.Start = ProblemData.TrainingPartition.Start;
|
---|
247 | FitnessCalculationPartition.End = ProblemData.TrainingPartition.End;
|
---|
248 |
|
---|
249 | UpdateGrammar();
|
---|
250 | ParameterizeOperators();
|
---|
251 |
|
---|
252 | var handler = ProblemDataChanged;
|
---|
253 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
254 |
|
---|
255 | OnReset();
|
---|
256 | }
|
---|
257 | #endregion
|
---|
258 |
|
---|
259 | protected virtual void ParameterizeOperators() {
|
---|
260 | var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators).ToList();
|
---|
261 |
|
---|
262 | foreach (var op in operators.OfType<ISymbolicExpressionTreeGrammarBasedOperator>()) {
|
---|
263 | op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
|
---|
264 | }
|
---|
265 | foreach (var op in operators.OfType<IGESymbolicDataAnalysisEvaluator<T>>()) {
|
---|
266 | op.ProblemDataParameter.ActualName = ProblemDataParameterName;
|
---|
267 | op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
|
---|
268 | op.EvaluationPartitionParameter.ActualName = FitnessCalculationPartitionParameter.Name;
|
---|
269 | op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
|
---|
270 | op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
|
---|
271 | op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.Name;
|
---|
272 | op.GenotypeToPhenotypeMapperParameter.ActualName = GenotypeToPhenotypeMapperParameter.Name;
|
---|
273 | op.SymbolicExpressionTreeGrammarParameter.ActualName = SymbolicExpressionTreeGrammarParameter.Name;
|
---|
274 | }
|
---|
275 | foreach (var op in operators.OfType<IIntegerVectorCrossover>()) {
|
---|
276 | op.ParentsParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
277 | op.ChildParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
278 | }
|
---|
279 | foreach (var op in operators.OfType<IIntegerVectorManipulator>()) {
|
---|
280 | op.IntegerVectorParameter.ActualName = SolutionCreator.IntegerVectorParameter.ActualName;
|
---|
281 | }
|
---|
282 | foreach (var op in operators.OfType<IIntegerVectorCreator>()) {
|
---|
283 | op.BoundsParameter.ActualName = BoundsParameter.Name;
|
---|
284 | op.LengthParameter.ActualName = MaximumSymbolicExpressionTreeLengthParameter.Name;
|
---|
285 | }
|
---|
286 | foreach (var op in operators.OfType<ISymbolicExpressionTreeAnalyzer>()) {
|
---|
287 | op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
|
---|
288 | }
|
---|
289 | foreach (var op in operators.OfType<ISymbolicDataAnalysisSingleObjectiveAnalyzer>()) {
|
---|
290 | op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
|
---|
291 | }
|
---|
292 | foreach (var op in operators.OfType<ISymbolicDataAnalysisMultiObjectiveAnalyzer>()) {
|
---|
293 | op.ApplyLinearScalingParameter.ActualName = ApplyLinearScalingParameter.Name;
|
---|
294 | }
|
---|
295 | foreach (var op in operators.OfType<ISymbolicDataAnalysisAnalyzer>()) {
|
---|
296 | op.SymbolicExpressionTreeParameter.ActualName = Evaluator.SymbolicExpressionTreeParameter.ActualName;
|
---|
297 | }
|
---|
298 | foreach (var op in operators.OfType<IGESymbolicDataAnalysisValidationAnalyzer<U, T>>()) {
|
---|
299 | op.RelativeNumberOfEvaluatedSamplesParameter.ActualName = RelativeNumberOfEvaluatedSamplesParameter.Name;
|
---|
300 | op.ValidationPartitionParameter.ActualName = ValidationPartitionParameter.Name;
|
---|
301 | }
|
---|
302 | foreach (var op in operators.OfType<ISymbolicDataAnalysisInterpreterOperator>()) {
|
---|
303 | op.SymbolicDataAnalysisTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | #region Import & Export
|
---|
308 | public virtual void Load(T data) {
|
---|
309 | Name = data.Name;
|
---|
310 | Description = data.Description;
|
---|
311 | ProblemData = data;
|
---|
312 | }
|
---|
313 |
|
---|
314 | public virtual T Export() {
|
---|
315 | return ProblemData;
|
---|
316 | }
|
---|
317 | #endregion
|
---|
318 | }
|
---|
319 | }
|
---|