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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
28 | using HeuristicLab.EvolutionTracking;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Random;
|
---|
32 |
|
---|
33 | namespace 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 | protected SchemaEvaluator(SchemaEvaluator original, Cloner cloner) : base(original, cloner) {
|
---|
153 | this.comp = original.comp;
|
---|
154 | this.qm = original.qm;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
158 | return new SchemaEvaluator(this, cloner);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator) {
|
---|
162 | double similarity = 0;
|
---|
163 | int count = individuals.Count;
|
---|
164 | for (int i = 0; i < count - 1; ++i) {
|
---|
165 | for (int j = i + 1; j < count; ++j) {
|
---|
166 | similarity += calculator.CalculateSolutionSimilarity(individuals[i], individuals[j]);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | return similarity / (count * (count - 1) / 2.0);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public override IOperation Apply() {
|
---|
173 | var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals
|
---|
174 |
|
---|
175 | var random = RandomParameter.ActualValue;
|
---|
176 | var mutator = MutatorParameter.ActualValue;
|
---|
177 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
178 | var updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
|
---|
179 |
|
---|
180 | var s = SchemaParameter.ActualValue;
|
---|
181 | var matchingIndividuals = new ScopeList();
|
---|
182 | foreach (var ind in individuals) {
|
---|
183 | var t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value;
|
---|
184 | if (t.Length >= s.Length && qm.Match(t, s))
|
---|
185 | matchingIndividuals.Add(ind);
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (matchingIndividuals.Count < MinimumSchemaFrequency.Value * individuals.Count) {
|
---|
189 | ChangedTreesParameter.ActualValue = new IntValue(0);
|
---|
190 | return base.Apply();
|
---|
191 | }
|
---|
192 |
|
---|
193 | var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator);
|
---|
194 | if (similarity < MinimumPhenotypicSimilarity.Value) {
|
---|
195 | ChangedTreesParameter.ActualValue = new IntValue(0);
|
---|
196 | return base.Apply();
|
---|
197 | }
|
---|
198 |
|
---|
199 | var oc = new OperationCollection();
|
---|
200 | int n = (int)Math.Round(matchingIndividuals.Count * ReplacementRatio.Value);
|
---|
201 | var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList()
|
---|
202 | : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList();
|
---|
203 | foreach (var ind in individualsToReplace) {
|
---|
204 | var mutatorOp = ExecutionContext.CreateChildOperation(mutator, ind);
|
---|
205 | var evaluatorOp = ExecutionContext.CreateChildOperation(evaluator, ind);
|
---|
206 | var updateEstimatedValuesOp = ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, ind);
|
---|
207 | oc.Add(mutatorOp);
|
---|
208 | oc.Add(evaluatorOp);
|
---|
209 | oc.Add(updateEstimatedValuesOp);
|
---|
210 | }
|
---|
211 | ChangedTreesParameter.ActualValue = new IntValue(individualsToReplace.Count);
|
---|
212 | return new OperationCollection(oc, base.Apply());
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|