Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7089 for branches/gp-crossover


Ignore:
Timestamp:
11/28/11 17:18:49 (13 years ago)
Author:
bburlacu
Message:

#1682: Fixed a bug affecting the new crossovers

Location:
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionContextAwareCrossover.cs

    r7075 r7089  
    3333
    3434  [Item("ContextAwareCrossover", "An operator which performs subtree swapping in a deterministic way: it checks all possible cross points in the second parent with the aim of creating the best possible offspring.")]
    35   public sealed class SymbolicDataAnalysisExpressionContextAwareCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
    36   {
     35  public sealed class SymbolicDataAnalysisExpressionContextAwareCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3736    [StorableConstructor]
    3837    private SymbolicDataAnalysisExpressionContextAwareCrossover(bool deserializing) : base(deserializing) { }
     
    4544    public override IDeepCloneable Clone(Cloner cloner) {
    4645      return new SymbolicDataAnalysisExpressionContextAwareCrossover<T>(this, cloner);
    47     } 
     46    }
    4847    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4948      if (this.ExecutionContext == null)
     
    6362      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
    6463      parent0.Root.ForEachNodePostfix((n) => {
    65         if (n.Subtrees.Any() && n != parent0.Root) 
    66           foreach (var child in n.Subtrees) 
     64        if (n.Subtrees.Any() && n != parent0.Root)
     65          foreach (var child in n.Subtrees)
    6766            crossoverPoints0.Add(new CutPoint(n, child));
    6867      });
     
    7372      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    7473      parent1.Root.ForEachNodePostfix((n) => {
    75         if (n.Subtrees.Any() && n != parent1.Root) 
     74        if (n.Subtrees.Any() && n != parent1.Root)
    7675          foreach (var child in n.Subtrees)
    7776            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    78               allowedBranches.Add(n);
     77              allowedBranches.Add(child);
    7978      });
    8079
     
    8281      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    8382
    84       if (allowedBranches.Count == 0) 
     83      if (allowedBranches.Count == 0)
    8584        return parent0;
    8685
     
    9089      ISymbolicExpressionTreeNode selectedBranch = null;
    9190
    92       var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode,double>>();
     91      var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>();
    9392
    9493      foreach (var node in allowedBranches) {
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs

    r7075 r7089  
    5454    }
    5555    public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter {
    56       get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>) Parameters[EvaluatorParameterName]; }
     56      get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; }
    5757    }
    5858    public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter {
     
    107107    /// <param name="startSymbol"></param>
    108108    /// <returns></returns>
    109     protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ISymbolicExpressionGrammar grammar) {
    110       SymbolicExpressionTree tree = new SymbolicExpressionTree();
    111       var rootNode = (SymbolicExpressionTreeTopLevelNode)grammar.ProgramRootSymbol.CreateTreeNode();
    112       if (rootNode.HasLocalParameters)
    113         rootNode.ResetLocalParameters(random);
    114       rootNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar);
    115       var startNode = (SymbolicExpressionTreeTopLevelNode)grammar.StartSymbol.CreateTreeNode();
    116       startNode.SetGrammar((ISymbolicExpressionTreeGrammar)grammar);
    117       if (startNode.HasLocalParameters)
    118         startNode.ResetLocalParameters(random);
     109    protected static ISymbolicExpressionTree CreateTreeFromNode(IRandom random, ISymbolicExpressionTreeNode node, ISymbol rootSymbol, ISymbol startSymbol) {
     110      var rootNode = new SymbolicExpressionTreeTopLevelNode(rootSymbol);
     111      if (rootNode.HasLocalParameters) rootNode.ResetLocalParameters(random);
     112
     113      //if (node.Symbol.Name == startSymbol.Name) {
     114      //  rootNode.AddSubtree(node);
     115      //} else {
     116      //  var startNode = new SymbolicExpressionTreeTopLevelNode(startSymbol);
     117      //  if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
     118
     119      //  startNode.AddSubtree(node);
     120      //  rootNode.AddSubtree(startNode);
     121      //}
     122      var startNode = new SymbolicExpressionTreeTopLevelNode(startSymbol);
     123      if (startNode.HasLocalParameters) startNode.ResetLocalParameters(random);
     124
     125      startNode.AddSubtree(node);
    119126      rootNode.AddSubtree(startNode);
    120127
    121       rootNode.AddSubtree(startNode);
    122       if (startNode.Grammar.IsAllowedChildSymbol(startNode.Symbol, node.Symbol))
    123         startNode.AddSubtree(node);
    124       else
    125         throw new InvalidOperationException("The given node cannot be a child of the start symbol.");
    126128      return new SymbolicExpressionTree(rootNode);
    127129    }
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs

    r7072 r7089  
    3333
    3434  [Item("DepthConstrainedCrossover", "An operator which performs subtree swapping only within a specific depth range.")]
    35   public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
    36   {
     35  public sealed class SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3736    [StorableConstructor]
    3837    private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { }
     
    4140    public SymbolicDataAnalysisExpressionDepthConstrainedCrossover() : base() { }
    4241    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); }
    43  
     42
    4443    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4544      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
     
    5655      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
    5756      parent0.Root.ForEachNodePostfix((n) => {
    58         if (n.Subtrees.Any() && n != parent0.Root) 
    59           foreach (var child in n.Subtrees) 
     57        if (n.Subtrees.Any() && n != parent0.Root)
     58          foreach (var child in n.Subtrees)
    6059            crossoverPoints0.Add(new CutPoint(n, child));
    6160      });
     
    6665      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    6766      parent1.Root.ForEachNodePostfix((n) => {
    68         if (n.Subtrees.Any() && n != parent1.Root) 
     67        if (n.Subtrees.Any() && n != parent1.Root)
    6968          foreach (var child in n.Subtrees)
    7069            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    71               allowedBranches.Add(n);
     70              allowedBranches.Add(child);
    7271      });
    7372
     
    7574      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    7675
    77       if (allowedBranches.Count == 0) 
     76      if (allowedBranches.Count == 0)
    7877        return parent0;
    7978
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs

    r7075 r7089  
    3333
    3434  [Item("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on behavioral similarity")]
    35   public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
    36   {
     35  public sealed class SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3736    [StorableConstructor]
    3837    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { }
    39     private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner) 
     38    private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner)
    4039      : base(original, cloner) { }
    4140    public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { }
    4241    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); }
    43  
     42
    4443    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4544      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
     
    4746      T problemData = ProblemDataParameter.ActualValue;
    4847      var grammar = parent0.Root.Grammar;
    49       return Cross(random, parent0, parent1, interpreter, problemData, 
     48      return Cross(random, parent0, parent1, interpreter, problemData,
    5049                   rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
    5150    }
     
    5857    /// choose the second crosspoint via a random weighted selection procedure.
    5958    /// </summary>
    60     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 
     59    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
    6160                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
    6261      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
    6362      parent0.Root.ForEachNodePostfix((n) => {
    64         if (n.Subtrees.Any() && n != parent0.Root) 
    65           foreach (var child in n.Subtrees) 
     63        if (n.Subtrees.Any() && n != parent0.Root)
     64          foreach (var child in n.Subtrees)
    6665            crossoverPoints0.Add(new CutPoint(n, child));
    6766      });
     
    7271      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    7372      parent1.Root.ForEachNodePostfix((n) => {
    74         if (n.Subtrees.Any() && n != parent1.Root) 
     73        if (n.Subtrees.Any() && n != parent1.Root)
    7574          foreach (var child in n.Subtrees)
    7675            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    77               allowedBranches.Add(n);
     76              allowedBranches.Add(child);
    7877      });
    7978
     
    8180      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    8281
    83       if (allowedBranches.Count == 0) 
     82      if (allowedBranches.Count == 0)
    8483        return parent0;
    8584
     
    8786
    8887      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
    89       var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
     88      var rootSymbol = new ProgramRootSymbol();
     89      var startSymbol = new StartSymbol();
     90      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
    9091      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
    9192      double min0 = estimatedValues0.Min();
     
    9495      List<double> weights = new List<double>();
    9596      foreach (var node in allowedBranches) {
    96         var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
     97        var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
    9798        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
    9899        double min1 = estimatedValues1.Min();
     
    133134      double r = weights.Sum() * random.NextDouble();
    134135      for (int i = 0; i != nodes.Count; ++i) {
    135         if (r < weights[i]) 
    136           return nodes[i];       
     136        if (r < weights[i])
     137          return nodes[i];
    137138        r -= weights[i];
    138139      }
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs

    r7075 r7089  
    3333
    3434  [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on semantic similarity.")]
    35   public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData
    36   {
     35  public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
    3736    [StorableConstructor]
    3837    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
     
    4039    public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { }
    4140    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); }
    42  
     41
    4342    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
    4443      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
     
    5150    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
    5251    /// </summary>
    53     public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 
     52    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
    5453                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
    5554      List<CutPoint> crossoverPoints0 = new List<CutPoint>();
    5655      parent0.Root.ForEachNodePostfix((n) => {
    57         if (n.Subtrees.Any() && n != parent0.Root) 
    58           foreach (var child in n.Subtrees) 
     56        if (n.Subtrees.Any() && n != parent0.Root)
     57          foreach (var child in n.Subtrees)
    5958            crossoverPoints0.Add(new CutPoint(n, child));
    6059      });
     
    6564      List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
    6665      parent1.Root.ForEachNodePostfix((n) => {
    67         if (n.Subtrees.Any() && n != parent1.Root) 
     66        if (n.Subtrees.Any() && n != parent1.Root)
    6867          foreach (var child in n.Subtrees)
    6968            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
    70               allowedBranches.Add(n);
     69              allowedBranches.Add(child);
    7170      });
    7271
     
    7473      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
    7574
    76       if (allowedBranches.Count == 0) 
     75      if (allowedBranches.Count == 0)
    7776        return parent0;
    7877
     
    8079
    8180      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
    82       //var rootSymbol = new ProgramRootSymbol();
    83       //var startSymbol = new StartSymbol();
    84       var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
     81      var rootSymbol = new ProgramRootSymbol();
     82      var startSymbol = new StartSymbol();
     83      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
    8584      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
    8685
     
    8988      // pick the first node that fulfills the semantic similarity conditions
    9089      foreach (var node in allowedBranches) {
    91         var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);
     90        var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol);
    9291        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
    9392
Note: See TracChangeset for help on using the changeset viewer.