Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs @ 12952

Last change on this file since 12952 was 12952, checked in by bburlacu, 9 years ago

#1772: Add tracking of the number of trees that are mutated during the diversification phase by the SchemaEvaluator.

File size: 10.7 KB
Line 
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#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.EvolutionTracking;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Random;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tracking {
34  [Item("SchemaEvaluator", "An operator that builds schemas based on the heredity relationship in the genealogy graph.")]
35  [StorableClass]
36  public class SchemaEvaluator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
37    #region parameter names
38    private const string MinimumSchemaFrequencyParameterName = "MinimumSchemaFrequency";
39    private const string MinimumPhenotypicSimilarityParameterName = "MinimumPhenotypicSimilarity";
40    private const string ReplacementRatioParameterName = "ReplacementRatio";
41    private const string SchemaParameterName = "Schema";
42    private const string PopulationSizeParameterName = "PopulationSize";
43    private const string RandomParameterName = "Random";
44    private const string EvaluatorParameterName = "Evaluator";
45    private const string ProblemDataParameterName = "ProblemData";
46    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
47    private const string EstimationLimitsParameterName = "EstimationLimits";
48    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
49    private const string MutatorParameterName = "Mutator";
50    private const string RandomReplacementParameterName = "RandomReplacement";
51    private const string ChangedTreesParameterName = "ChangedTrees";
52    #endregion
53
54    #region parameters
55    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>> EvaluatorParameter {
56      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>)Parameters[EvaluatorParameterName]; }
57    }
58    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
59      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
60    }
61    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
62      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
63    }
64    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
65      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
66    }
67    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
68      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
69    }
70    public ILookupParameter<BoolValue> RandomReplacementParameter {
71      get { return (ILookupParameter<BoolValue>)Parameters[RandomReplacementParameterName]; }
72    }
73    public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter {
74      get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; }
75    }
76    public ILookupParameter<IRandom> RandomParameter {
77      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
78    }
79    public ILookupParameter<IntValue> PopulationSizeParameter {
80      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
81    }
82    public ILookupParameter<ISymbolicExpressionTree> SchemaParameter {
83      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SchemaParameterName]; }
84    }
85    public ILookupParameter<PercentValue> MinimumSchemaFrequencyParameter {
86      get { return (ILookupParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; }
87    }
88    public ILookupParameter<PercentValue> ReplacementRatioParameter {
89      get { return (ILookupParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; }
90    }
91    public ILookupParameter<PercentValue> MinimumPhenotypicSimilarityParameter {
92      get { return (ILookupParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; }
93    }
94    public LookupParameter<IntValue> ChangedTreesParameter {
95      get { return (LookupParameter<IntValue>)Parameters[ChangedTreesParameterName]; }
96    }
97
98    #endregion
99
100    #region parameter properties
101    public PercentValue MinimumSchemaFrequency {
102      get { return MinimumSchemaFrequencyParameter.ActualValue; }
103    }
104
105    public PercentValue ReplacementRatio {
106      get { return ReplacementRatioParameter.ActualValue; }
107    }
108
109    public PercentValue MinimumPhenotypicSimilarity {
110      get { return MinimumPhenotypicSimilarityParameter.ActualValue; }
111    }
112
113    public BoolValue RandomReplacement {
114      get { return RandomReplacementParameter.ActualValue; }
115    }
116    #endregion
117
118    private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
119    private readonly QueryMatch qm;
120
121    private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer {
122      MatchConstantValues = false,
123      MatchVariableWeights = false,
124      MatchVariableNames = true
125    };
126
127
128    [StorableHook(HookType.AfterDeserialization)]
129    private void AfterDeserialization() {
130      if (!Parameters.ContainsKey(ChangedTreesParameterName))
131        Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName));
132    }
133
134    public SchemaEvaluator() {
135      qm = new QueryMatch(comp) { MatchParents = true };
136
137      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SchemaParameterName, "The current schema to be evaluated"));
138      Parameters.Add(new LookupParameter<PercentValue>(MinimumSchemaFrequencyParameterName));
139      Parameters.Add(new LookupParameter<PercentValue>(ReplacementRatioParameterName));
140      Parameters.Add(new LookupParameter<PercentValue>(MinimumPhenotypicSimilarityParameterName));
141      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(PopulationSizeParameterName));
142      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName));
143      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>(EvaluatorParameterName));
144      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
145      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
146      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
147      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
148      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName));
149      Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName));
150      Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName));
151    }
152
153    protected SchemaEvaluator(SchemaEvaluator original, Cloner cloner) : base(original, cloner) {
154      this.comp = original.comp;
155      this.qm = original.qm;
156    }
157
158    public override IDeepCloneable Clone(Cloner cloner) {
159      return new SchemaEvaluator(this, cloner);
160    }
161
162    private static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator) {
163      double similarity = 0;
164      int count = individuals.Count;
165      for (int i = 0; i < count - 1; ++i) {
166        for (int j = i + 1; j < count; ++j) {
167          similarity += calculator.CalculateSolutionSimilarity(individuals[i], individuals[j]);
168        }
169      }
170      return similarity / (count * (count - 1) / 2.0);
171    }
172
173    public override IOperation Apply() {
174      var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals
175
176      var random = RandomParameter.ActualValue;
177      var mutator = MutatorParameter.ActualValue;
178      var evaluator = EvaluatorParameter.ActualValue;
179      var updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
180
181      var s = SchemaParameter.ActualValue;
182      var matchingIndividuals = new ScopeList(from ind in individuals
183                                              let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
184                                              where qm.Match(t, s)
185                                              select ind);
186
187      if (matchingIndividuals.Count < MinimumSchemaFrequency.Value * individuals.Count) {
188        ChangedTreesParameter.ActualValue = new IntValue(0);
189        return base.Apply();
190      }
191
192      var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator);
193      if (similarity < MinimumPhenotypicSimilarity.Value) {
194        ChangedTreesParameter.ActualValue = new IntValue(0);
195        return base.Apply();
196      }
197
198      var oc = new OperationCollection();
199      int n = (int)Math.Round(matchingIndividuals.Count * ReplacementRatio.Value);
200      var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList()
201                                                         : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList();
202      foreach (var ind in individualsToReplace) {
203        var mutatorOp = ExecutionContext.CreateChildOperation(mutator, ind);
204        var evaluatorOp = ExecutionContext.CreateChildOperation(evaluator, ind);
205        var updateEstimatedValuesOp = ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, ind);
206        oc.Add(mutatorOp);
207        oc.Add(evaluatorOp);
208        oc.Add(updateEstimatedValuesOp);
209      }
210      ChangedTreesParameter.ActualValue = new IntValue(individualsToReplace.Count);
211      return new OperationCollection(oc, base.Apply());
212    }
213  }
214}
Note: See TracBrowser for help on using the repository browser.