Changeset 7089 for branches/gp-crossover
- Timestamp:
- 11/28/11 17:18:49 (13 years ago)
- 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 33 33 34 34 [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 { 37 36 [StorableConstructor] 38 37 private SymbolicDataAnalysisExpressionContextAwareCrossover(bool deserializing) : base(deserializing) { } … … 45 44 public override IDeepCloneable Clone(Cloner cloner) { 46 45 return new SymbolicDataAnalysisExpressionContextAwareCrossover<T>(this, cloner); 47 } 46 } 48 47 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 49 48 if (this.ExecutionContext == null) … … 63 62 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); 64 63 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) 67 66 crossoverPoints0.Add(new CutPoint(n, child)); 68 67 }); … … 73 72 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 74 73 parent1.Root.ForEachNodePostfix((n) => { 75 if (n.Subtrees.Any() && n != parent1.Root) 74 if (n.Subtrees.Any() && n != parent1.Root) 76 75 foreach (var child in n.Subtrees) 77 76 if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength)) 78 allowedBranches.Add( n);77 allowedBranches.Add(child); 79 78 }); 80 79 … … 82 81 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 83 82 84 if (allowedBranches.Count == 0) 83 if (allowedBranches.Count == 0) 85 84 return parent0; 86 85 … … 90 89 ISymbolicExpressionTreeNode selectedBranch = null; 91 90 92 var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>();91 var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>(); 93 92 94 93 foreach (var node in allowedBranches) { -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionCrossover.cs
r7075 r7089 54 54 } 55 55 public ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>> EvaluatorParameter { 56 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>) 56 get { return (ILookupParameter<ISymbolicDataAnalysisSingleObjectiveEvaluator<T>>)Parameters[EvaluatorParameterName]; } 57 57 } 58 58 public IValueLookupParameter<IntRange> SymbolicDataAnalysisEvaluationPartitionParameter { … … 107 107 /// <param name="startSymbol"></param> 108 108 /// <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); 119 126 rootNode.AddSubtree(startNode); 120 127 121 rootNode.AddSubtree(startNode);122 if (startNode.Grammar.IsAllowedChildSymbol(startNode.Symbol, node.Symbol))123 startNode.AddSubtree(node);124 else125 throw new InvalidOperationException("The given node cannot be a child of the start symbol.");126 128 return new SymbolicExpressionTree(rootNode); 127 129 } -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDepthConstrainedCrossover.cs
r7072 r7089 33 33 34 34 [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 { 37 36 [StorableConstructor] 38 37 private SymbolicDataAnalysisExpressionDepthConstrainedCrossover(bool deserializing) : base(deserializing) { } … … 41 40 public SymbolicDataAnalysisExpressionDepthConstrainedCrossover() : base() { } 42 41 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionDepthConstrainedCrossover<T>(this, cloner); } 43 42 44 43 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 45 44 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; … … 56 55 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); 57 56 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) 60 59 crossoverPoints0.Add(new CutPoint(n, child)); 61 60 }); … … 66 65 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 67 66 parent1.Root.ForEachNodePostfix((n) => { 68 if (n.Subtrees.Any() && n != parent1.Root) 67 if (n.Subtrees.Any() && n != parent1.Root) 69 68 foreach (var child in n.Subtrees) 70 69 if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength)) 71 allowedBranches.Add( n);70 allowedBranches.Add(child); 72 71 }); 73 72 … … 75 74 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 76 75 77 if (allowedBranches.Count == 0) 76 if (allowedBranches.Count == 0) 78 77 return parent0; 79 78 -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover.cs
r7075 r7089 33 33 34 34 [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 { 37 36 [StorableConstructor] 38 37 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(bool deserializing) : base(deserializing) { } 39 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner) 38 private SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover(SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T> original, Cloner cloner) 40 39 : base(original, cloner) { } 41 40 public SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover() : base() { } 42 41 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionProbabilisticFunctionalCrossover<T>(this, cloner); } 43 42 44 43 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 45 44 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; … … 47 46 T problemData = ProblemDataParameter.ActualValue; 48 47 var grammar = parent0.Root.Grammar; 49 return Cross(random, parent0, parent1, interpreter, problemData, 48 return Cross(random, parent0, parent1, interpreter, problemData, 50 49 rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value); 51 50 } … … 58 57 /// choose the second crosspoint via a random weighted selection procedure. 59 58 /// </summary> 60 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 59 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 61 60 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) { 62 61 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); 63 62 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) 66 65 crossoverPoints0.Add(new CutPoint(n, child)); 67 66 }); … … 72 71 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 73 72 parent1.Root.ForEachNodePostfix((n) => { 74 if (n.Subtrees.Any() && n != parent1.Root) 73 if (n.Subtrees.Any() && n != parent1.Root) 75 74 foreach (var child in n.Subtrees) 76 75 if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength)) 77 allowedBranches.Add( n);76 allowedBranches.Add(child); 78 77 }); 79 78 … … 81 80 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 82 81 83 if (allowedBranches.Count == 0) 82 if (allowedBranches.Count == 0) 84 83 return parent0; 85 84 … … 87 86 88 87 // 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); 90 91 IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows); 91 92 double min0 = estimatedValues0.Min(); … … 94 95 List<double> weights = new List<double>(); 95 96 foreach (var node in allowedBranches) { 96 var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);97 var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol); 97 98 IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows); 98 99 double min1 = estimatedValues1.Min(); … … 133 134 double r = weights.Sum() * random.NextDouble(); 134 135 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]; 137 138 r -= weights[i]; 138 139 } -
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
r7075 r7089 33 33 34 34 [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 { 37 36 [StorableConstructor] 38 37 private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { } … … 40 39 public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { } 41 40 public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); } 42 41 43 42 protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) { 44 43 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue; … … 51 50 /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria. 52 51 /// </summary> 53 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 52 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, 54 53 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) { 55 54 List<CutPoint> crossoverPoints0 = new List<CutPoint>(); 56 55 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) 59 58 crossoverPoints0.Add(new CutPoint(n, child)); 60 59 }); … … 65 64 List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>(); 66 65 parent1.Root.ForEachNodePostfix((n) => { 67 if (n.Subtrees.Any() && n != parent1.Root) 66 if (n.Subtrees.Any() && n != parent1.Root) 68 67 foreach (var child in n.Subtrees) 69 68 if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength)) 70 allowedBranches.Add( n);69 allowedBranches.Add(child); 71 70 }); 72 71 … … 74 73 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null); 75 74 76 if (allowedBranches.Count == 0) 75 if (allowedBranches.Count == 0) 77 76 return parent0; 78 77 … … 80 79 81 80 // 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); 85 84 IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows); 86 85 … … 89 88 // pick the first node that fulfills the semantic similarity conditions 90 89 foreach (var node in allowedBranches) { 91 var tree1 = CreateTreeFromNode(random, node, (ISymbolicExpressionGrammar)parent0.Root.Grammar);90 var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); 92 91 IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows); 93 92
Note: See TracChangeset
for help on using the changeset viewer.