Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12951 was 12951, checked in by bburlacu, 8 years ago

#1772:

  • Slight refactor in QueryMatch.cs
  • Added a parameter to the genealogy analyzer for removing older generations from the graph (useful to conserve memory in experiments)
  • Updated wildcard nodes (added persistence & cloning)
  • Implemented diversification strategy based on schema frequencies & phenotypic similarity as a separate operator (for now keep also the analyzer)
  • Updated license headers
  • Added QueryMatch performance test (to be expanded)
File size: 9.8 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    private const string MinimumSchemaFrequencyParameterName = "MinimumSchemaFrequency";
38    private const string MinimumPhenotypicSimilarityParameterName = "MinimumPhenotypicSimilarity";
39    private const string ReplacementRatioParameterName = "ReplacementRatio";
40    private const string SchemaParameterName = "Schema";
41    private const string PopulationSizeParameterName = "PopulationSize";
42    private const string RandomParameterName = "Random";
43    private const string EvaluatorParameterName = "Evaluator";
44    private const string ProblemDataParameterName = "ProblemData";
45    private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
46    private const string EstimationLimitsParameterName = "EstimationLimits";
47    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
48    private const string MutatorParameterName = "Mutator";
49    private const string RandomReplacementParameterName = "RandomReplacement";
50
51    private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
52
53    private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer {
54      MatchConstantValues = false,
55      MatchVariableWeights = false,
56      MatchVariableNames = true
57    };
58
59    private readonly QueryMatch qm;
60
61    #region parameters
62    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>> EvaluatorParameter {
63      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>)Parameters[EvaluatorParameterName]; }
64    }
65    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
66      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
67    }
68    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> InterpreterParameter {
69      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[InterpreterParameterName]; }
70    }
71    public ILookupParameter<DoubleLimit> EstimationLimitsParameter {
72      get { return (ILookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
73    }
74    public ILookupParameter<BoolValue> ApplyLinearScalingParameter {
75      get { return (ILookupParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
76    }
77    public ILookupParameter<BoolValue> RandomReplacementParameter {
78      get { return (ILookupParameter<BoolValue>)Parameters[RandomReplacementParameterName]; }
79    }
80    public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter {
81      get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; }
82    }
83    public ILookupParameter<IRandom> RandomParameter {
84      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
85    }
86    public ILookupParameter<IntValue> PopulationSizeParameter {
87      get { return (ILookupParameter<IntValue>)Parameters[PopulationSizeParameterName]; }
88    }
89    public ILookupParameter<ISymbolicExpressionTree> SchemaParameter {
90      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SchemaParameterName]; }
91    }
92    public ILookupParameter<PercentValue> MinimumSchemaFrequencyParameter {
93      get { return (ILookupParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; }
94    }
95    public ILookupParameter<PercentValue> ReplacementRatioParameter {
96      get { return (ILookupParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; }
97    }
98    public ILookupParameter<PercentValue> MinimumPhenotypicSimilarityParameter {
99      get { return (ILookupParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; }
100    }
101    #endregion
102
103    #region parameter properties
104    public PercentValue MinimumSchemaFrequency {
105      get { return MinimumSchemaFrequencyParameter.ActualValue; }
106    }
107
108    public PercentValue ReplacementRatio {
109      get { return ReplacementRatioParameter.ActualValue; }
110    }
111
112    public PercentValue MinimumPhenotypicSimilarity {
113      get { return MinimumPhenotypicSimilarityParameter.ActualValue; }
114    }
115
116    public BoolValue RandomReplacement {
117      get { return RandomReplacementParameter.ActualValue; }
118    }
119    #endregion
120
121    public SchemaEvaluator() {
122      qm = new QueryMatch(comp) { MatchParents = true };
123
124      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SchemaParameterName, "The current schema to be evaluated"));
125      Parameters.Add(new LookupParameter<PercentValue>(MinimumSchemaFrequencyParameterName));
126      Parameters.Add(new LookupParameter<PercentValue>(ReplacementRatioParameterName));
127      Parameters.Add(new LookupParameter<PercentValue>(MinimumPhenotypicSimilarityParameterName));
128      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(PopulationSizeParameterName));
129      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName));
130      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>(EvaluatorParameterName));
131      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
132      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
133      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
134      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
135      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName));
136      Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName));
137    }
138
139    protected SchemaEvaluator(SchemaEvaluator original, Cloner cloner) : base(original, cloner) {
140      this.comp = original.comp;
141      this.qm = original.qm;
142    }
143
144    public override IDeepCloneable Clone(Cloner cloner) {
145      return new SchemaEvaluator(this, cloner);
146    }
147
148    private static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator) {
149      double similarity = 0;
150      int count = individuals.Count;
151      for (int i = 0; i < count - 1; ++i) {
152        for (int j = i + 1; j < count; ++j) {
153          similarity += calculator.CalculateSolutionSimilarity(individuals[i], individuals[j]);
154        }
155      }
156      return similarity / (count * (count - 1) / 2.0);
157    }
158
159    public override IOperation Apply() {
160      var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals
161
162      var random = RandomParameter.ActualValue;
163      var mutator = MutatorParameter.ActualValue;
164      var evaluator = EvaluatorParameter.ActualValue;
165      var updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
166
167      var s = SchemaParameter.ActualValue;
168      var matchingIndividuals = new ScopeList(from ind in individuals
169                                              let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
170                                              where qm.Match(t, s)
171                                              select ind);
172
173      if (matchingIndividuals.Count < MinimumSchemaFrequency.Value * individuals.Count)
174        return base.Apply();
175
176      var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator);
177      if (similarity < MinimumPhenotypicSimilarity.Value)
178        return base.Apply();
179
180      var oc = new OperationCollection();
181
182      int n = (int)Math.Round(matchingIndividuals.Count * ReplacementRatio.Value);
183      var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n)
184                                                         : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n);
185      foreach (var ind in individualsToReplace) {
186        var mutatorOp = ExecutionContext.CreateChildOperation(mutator, ind);
187        var evaluatorOp = ExecutionContext.CreateChildOperation(evaluator, ind);
188        var updateEstimatedValuesOp = ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, ind);
189        oc.Add(mutatorOp);
190        oc.Add(evaluatorOp);
191        oc.Add(updateEstimatedValuesOp);
192      }
193
194      return new OperationCollection(oc, base.Apply());
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.