Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SymbolicDataAnalysisExpressionAfterManipulatorOperator.cs @ 11233

Last change on this file since 11233 was 11233, checked in by bburlacu, 10 years ago

#1772: Worked towards integrating the new graph api with the tracking operators.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.EvolutionTracking;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
30  public class SymbolicDataAnalysisExpressionAfterManipulatorOperator : AfterManipulatorOperator<ISymbolicExpressionTree> {
31    private readonly SymbolicExpressionTreeNodeSimilarityComparer comparer;
32
33    public SymbolicDataAnalysisExpressionAfterManipulatorOperator() {
34      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
35        MatchVariableNames = true,
36        MatchVariableWeights = true,
37        MatchConstantValues = true
38      };
39    }
40
41    public override IOperation Apply() {
42      var vChild = (IGenealogyGraphNode<ISymbolicExpressionTree>)GenealogyGraph.GetByContent(ChildParameter.ActualValue);
43      var nodesBefore = (List<ISymbolicExpressionTreeNode>)vChild.InArcs.First().Data;
44      var nodesAfter = ChildParameter.ActualValue.IterateNodesPrefix().ToList();
45      IFragment<ISymbolicExpressionTreeNode> fragment = null;
46
47      for (int i = 0; i < Math.Min(nodesAfter.Count, nodesBefore.Count); ++i) {
48        var a = nodesAfter[i];
49        var b = nodesBefore[i];
50
51        bool found = false;
52        if (comparer.Equals(a, b)) {
53          int m = 0;
54          for (int j = 0; j < a.SubtreeCount; ++j) {
55            if (!AreSimilar(a.GetSubtree(j), b.GetSubtree(j), comparer)) { ++m; }
56            if (m > 1) {
57              found = true;
58              break;
59            }
60          }
61          if (m == 0) {
62            // everything is similar so we skip the whole subtree
63            i += a.GetLength();
64          }
65          if (!found) continue;
66        }
67        fragment = new Fragment<ISymbolicExpressionTreeNode> {
68          Root = a,
69          Index1 = i,
70          Index2 = i
71        };
72        break;
73      }
74
75      vChild.InArcs.First().Data = fragment;
76      return base.Apply();
77    }
78
79    private bool AreSimilar(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, SymbolicExpressionTreeNodeSimilarityComparer comp) {
80      int length = a.GetLength();
81      return comparer.Equals(a, b) && length.Equals(b.GetLength()) && SymbolicExpressionTreeMatching.Match(a, b, comp) == length;
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.