Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs @ 12958

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

#1772: Properly remove older generations in the genealogy graph. Fix namespaces in the schema diversification operators.

File size: 9.1 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.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.EvolutionTracking;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34
35namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
36  [Item("SchemaCreator", "An operator that builds schemas based on the heredity relationship in the genealogy graph.")]
37  [StorableClass]
38  public class SchemaCreator : EvolutionTrackingOperator<ISymbolicExpressionTree> {
39    private const string MinimumSchemaLengthParameterName = "MinimumSchemaLength";
40    private const string MinimumSchemaFrequencyParameterName = "MinimumSchemaFrequency";
41    private const string MinimumPhenotypicSimilarityParameterName = "MinimumPhenotypicSimilarity";
42    private const string ReplacementRatioParameterName = "ReplacementRatio";
43    private const string RandomReplacementParameterName = "RandomReplacement";
44
45    #region parameters
46    public IFixedValueParameter<IntValue> MinimumSchemaLengthParameter {
47      get { return (IFixedValueParameter<IntValue>)Parameters[MinimumSchemaLengthParameterName]; }
48    }
49
50    public IFixedValueParameter<PercentValue> MinimumSchemaFrequencyParameter {
51      get { return (IFixedValueParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; }
52    }
53
54    public IFixedValueParameter<PercentValue> MinimumPhenotypicSimilarityParameter {
55      get { return (IFixedValueParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; }
56    }
57
58    public IFixedValueParameter<PercentValue> ReplacementRatioParameter {
59      get { return (IFixedValueParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; }
60    }
61    #endregion
62
63    #region parameter properties
64    public int MinimumSchemaLength {
65      get { return MinimumSchemaLengthParameter.Value.Value; }
66    }
67    #endregion
68
69    private SchemaEvaluator schemaEvaluator;
70    private SchemaCleanupOperator schemaCleanupOperator;
71    private DataReducer changedTreesReducer;
72    private DataTableValuesCollector valuesCollector;
73    private ResultsCollector resultsCollector;
74
75    private void ParameterizeOperators() {
76      schemaEvaluator = new SchemaEvaluator();
77      schemaCleanupOperator = new SchemaCleanupOperator();
78
79      changedTreesReducer = new DataReducer();
80      changedTreesReducer.ParameterToReduce.ActualName = schemaEvaluator.ChangedTreesParameter.ActualName;
81      changedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
82      changedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
83      changedTreesReducer.TargetParameter.ActualName = "NumberOfChangedTrees";
84
85      valuesCollector = new DataTableValuesCollector();
86      valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfChangedTrees"));
87      valuesCollector.DataTableParameter.ActualName = "Diversification";
88
89      resultsCollector = new ResultsCollector();
90      resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Diversification"));
91    }
92
93    public SchemaCreator() {
94      Parameters.Add(new FixedValueParameter<IntValue>(MinimumSchemaLengthParameterName, new IntValue(10)));
95      Parameters.Add(new FixedValueParameter<PercentValue>(MinimumSchemaFrequencyParameterName, new PercentValue(0.05)));
96      Parameters.Add(new FixedValueParameter<PercentValue>(MinimumPhenotypicSimilarityParameterName, new PercentValue(0.8)));
97      Parameters.Add(new FixedValueParameter<PercentValue>(ReplacementRatioParameterName, new PercentValue(0.2)));
98      Parameters.Add(new FixedValueParameter<BoolValue>(RandomReplacementParameterName, new BoolValue(false)));
99
100      ParameterizeOperators();
101    }
102
103    protected SchemaCreator(SchemaCreator original, Cloner cloner) : base(original, cloner) { }
104
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new SchemaCreator(this, cloner);
107    }
108
109    [StorableConstructor]
110    protected SchemaCreator(bool deserializing) : base(deserializing) { }
111
112    public override IOperation Apply() {
113      // apply only when at least one generation has passed
114      var gen = Generations.Value;
115      if (gen < 1 || GenealogyGraph == null)
116        return base.Apply();
117      // for now, only consider crossover offspring
118      var vertices = GenealogyGraph.GetByRank(gen).Where(x => x.InDegree == 2).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
119      var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()).ToList();
120      var anySubtreeSymbol = new AnySubtreeSymbol();
121
122      var scopes = new ScopeList(this.ExecutionContext.Scope.SubScopes);
123
124      if (schemaEvaluator == null || schemaCleanupOperator == null || changedTreesReducer == null || valuesCollector == null || resultsCollector == null)
125        ParameterizeOperators();
126
127      var reduceChangedTrees = ExecutionContext.CreateChildOperation(changedTreesReducer);
128      var collectValues = ExecutionContext.CreateChildOperation(valuesCollector);
129      var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
130
131      var oc = new OperationCollection();
132
133      #region create schemas and add subscopes representing the individuals
134      foreach (var g in groups) {
135        var parent = g.Key;
136        if (parent.Data.Length < MinimumSchemaLength)
137          continue;
138        bool replaced = false;
139        var schema = (ISymbolicExpressionTree)parent.Data.Clone();
140        var nodes = schema.IterateNodesPrefix().ToList();
141        var arcs = g.Select(x => x.InArcs.Last()).Where(x => x.Data != null);
142        var indices = arcs.Select(x => ((IFragment<ISymbolicExpressionTreeNode>)x.Data).Index1).Distinct().ToArray();
143        Array.Sort(indices);
144        var nodesToReplace = indices.Select(x => nodes[x]).ToList();
145        for (int i = nodesToReplace.Count - 1; i >= 0; --i) {
146          var node = nodesToReplace[i];
147
148          // do not replace the node with a wildcard if it would result in a length < MinimumSchemaLength
149          if ((schema.Length - node.GetLength() + 1) < MinimumSchemaLength)
150            continue;
151
152          var replacement = anySubtreeSymbol.CreateTreeNode();
153          ReplaceSubtree(node, replacement, false);
154          replaced = true;
155        }
156        if (replaced) {
157          var scope = new Scope("Schema");
158          scope.Variables.Add(new Core.Variable("Schema", schema));
159          scope.SubScopes.AddRange(ShallowCopy(scopes));
160
161          oc.Add(ExecutionContext.CreateChildOperation(schemaEvaluator, scope));
162          ExecutionContext.Scope.SubScopes.Add(scope);
163        }
164      }
165      #endregion
166
167      var cleanup = ExecutionContext.CreateChildOperation(schemaCleanupOperator);
168      // return an operation collection containing all the scope operations + base.Apply()
169      return new OperationCollection { oc, reduceChangedTrees, collectValues, collectResults, cleanup, base.Apply() };
170    }
171
172    private static ScopeList ShallowCopy(ScopeList scopes) {
173      var scopeList = new ScopeList();
174      // shallow-copy means that we create a new scope but reuse the variables
175      foreach (var scope in scopes) {
176        var s = new Scope(scope.Name);
177        foreach (var v in scope.Variables)
178          s.Variables.Add(v);
179        scopeList.Add(s);
180      }
181      return scopeList;
182    }
183
184    private static void ReplaceSubtree(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement,
185      bool preserveChildren = true) {
186      var parent = original.Parent;
187      if (parent == null)
188        throw new ArgumentException("Parent cannot be null for node " + original);
189      var index = parent.IndexOfSubtree(original);
190      parent.RemoveSubtree(index);
191      parent.InsertSubtree(index, replacement);
192
193      if (preserveChildren) {
194        var subtrees = original.Subtrees.ToList();
195
196        for (int i = subtrees.Count - 1; i >= 0; --i)
197          original.RemoveSubtree(i);
198
199        for (int i = 0; i < subtrees.Count; ++i) {
200          replacement.AddSubtree(subtrees[i]);
201        }
202      }
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.