Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1772: Fix plugin dependencies. Small changes to the SchemaEvaluator: require a minimum of 2 matches for each schema (regardless of the MinimumSchemaFrequency) and use Math.Floor instead of Math.Round when determining the number of individuals to be replaced according to the ReplacementRatio.

File size: 10.9 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 {
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    #endregion
98
99    #region parameter properties
100    public PercentValue MinimumSchemaFrequency {
101      get { return MinimumSchemaFrequencyParameter.ActualValue; }
102    }
103
104    public PercentValue ReplacementRatio {
105      get { return ReplacementRatioParameter.ActualValue; }
106    }
107
108    public PercentValue MinimumPhenotypicSimilarity {
109      get { return MinimumPhenotypicSimilarityParameter.ActualValue; }
110    }
111
112    public BoolValue RandomReplacement {
113      get { return RandomReplacementParameter.ActualValue; }
114    }
115    #endregion
116
117    private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
118    private readonly QueryMatch qm;
119
120    private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer {
121      MatchConstantValues = false,
122      MatchVariableWeights = false,
123      MatchVariableNames = true
124    };
125
126
127    [StorableHook(HookType.AfterDeserialization)]
128    private void AfterDeserialization() {
129      if (!Parameters.ContainsKey(ChangedTreesParameterName))
130        Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName));
131    }
132
133    public SchemaEvaluator() {
134      qm = new QueryMatch(comp) { MatchParents = true };
135
136      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SchemaParameterName, "The current schema to be evaluated"));
137      Parameters.Add(new LookupParameter<PercentValue>(MinimumSchemaFrequencyParameterName));
138      Parameters.Add(new LookupParameter<PercentValue>(ReplacementRatioParameterName));
139      Parameters.Add(new LookupParameter<PercentValue>(MinimumPhenotypicSimilarityParameterName));
140      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(PopulationSizeParameterName));
141      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName));
142      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<IRegressionProblemData>>(EvaluatorParameterName));
143      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
144      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
145      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
146      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
147      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName));
148      Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName));
149      Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName));
150    }
151
152
153    [StorableConstructor]
154    protected SchemaEvaluator(bool deserializing) : base(deserializing) { }
155
156    protected SchemaEvaluator(SchemaEvaluator original, Cloner cloner) : base(original, cloner) {
157      this.comp = (ISymbolicExpressionTreeNodeEqualityComparer)original.comp.Clone();
158      this.qm = new QueryMatch(comp) { MatchParents = original.qm.MatchParents };
159    }
160
161    public override IDeepCloneable Clone(Cloner cloner) {
162      return new SchemaEvaluator(this, cloner);
163    }
164
165    private static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator) {
166      double similarity = 0;
167      int count = individuals.Count;
168      for (int i = 0; i < count - 1; ++i) {
169        for (int j = i + 1; j < count; ++j) {
170          similarity += calculator.CalculateSolutionSimilarity(individuals[i], individuals[j]);
171        }
172      }
173      return similarity / (count * (count - 1) / 2.0);
174    }
175
176    public override IOperation Apply() {
177      var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals
178
179      var random = RandomParameter.ActualValue;
180      var mutator = MutatorParameter.ActualValue;
181      var evaluator = EvaluatorParameter.ActualValue;
182      var updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
183
184      var s = SchemaParameter.ActualValue;
185      var matchingIndividuals = new ScopeList();
186      foreach (var ind in individuals) {
187        var t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value;
188        if (t.Length >= s.Length && qm.Match(t, s))
189          matchingIndividuals.Add(ind);
190      }
191
192      if (matchingIndividuals.Count < (int)Math.Max(2, Math.Round(MinimumSchemaFrequency.Value * individuals.Count))) {
193        ChangedTreesParameter.ActualValue = new IntValue(0);
194        return base.Apply();
195      }
196
197      var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator);
198      if (similarity < MinimumPhenotypicSimilarity.Value) {
199        ChangedTreesParameter.ActualValue = new IntValue(0);
200        return base.Apply();
201      }
202
203      var oc = new OperationCollection();
204      int n = (int)Math.Floor(matchingIndividuals.Count * ReplacementRatio.Value);
205      var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList()
206                                                         : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList();
207      foreach (var ind in individualsToReplace) {
208        var mutatorOp = ExecutionContext.CreateChildOperation(mutator, ind);
209        var evaluatorOp = ExecutionContext.CreateChildOperation(evaluator, ind);
210        var updateEstimatedValuesOp = ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, ind);
211        oc.Add(mutatorOp);
212        oc.Add(evaluatorOp);
213        oc.Add(updateEstimatedValuesOp);
214      }
215      ChangedTreesParameter.ActualValue = new IntValue(individualsToReplace.Count);
216      return new OperationCollection(oc, base.Apply());
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.