Changeset 7193 for branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
- Timestamp:
- 12/16/11 09:43:02 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
r7119 r7193 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 30 using HeuristicLab.Data;31 29 32 30 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { … … 36 34 [StorableConstructor] 37 35 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(bool deserializing) : base(deserializing) { } 38 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpression DeterministicBestCrossover<T> original, Cloner cloner)36 private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpressionCrossover<T> original, Cloner cloner) 39 37 : base(original, cloner) { 40 38 } … … 48 46 if (this.ExecutionContext == null) 49 47 throw new InvalidOperationException("ExecutionContext not set."); 50 IEnumerable<int> rows = GenerateRowsToEvaluate();48 List<int> rows = GenerateRowsToEvaluate().ToList(); 51 49 T problemData = ProblemDataParameter.ActualValue; 52 50 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue; … … 63 61 /// </summary> 64 62 public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, IExecutionContext context, 65 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {63 ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, List<int> rows, int maxDepth, int maxLength) { 66 64 var crossoverPoints0 = new List<CutPoint>(); 67 65 parent0.Root.ForEachNodePostfix((n) => { 68 66 if (n.Subtrees.Any() && n != parent0.Root) 69 foreach (var child in n.Subtrees) 70 crossoverPoints0.Add(new CutPoint(n, child)); 67 crossoverPoints0.AddRange(from s in n.Subtrees select new CutPoint(n, s)); 71 68 }); 72 CutPoint crossoverPoint0 = crossoverPoints0 [random.Next(crossoverPoints0.Count)];69 CutPoint crossoverPoint0 = crossoverPoints0.SelectRandom(random); 73 70 int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child); 74 71 int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength(); … … 77 74 parent1.Root.ForEachNodePostfix((n) => { 78 75 if (n.Subtrees.Any() && n != parent1.Root) 79 foreach (var child in n.Subtrees)80 if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))81 allowedBranches.Add(child);76 allowedBranches.AddRange(from s in n.Subtrees 77 where crossoverPoint0.IsMatchingPointType(s) && s.GetDepth() + level <= maxDepth && s.GetLength() + length <= maxLength 78 select s); 82 79 }); 83 84 // check if empty branch is allowed85 if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);86 80 87 81 if (allowedBranches.Count == 0) … … 93 87 var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>(); 94 88 89 var originalChild = crossoverPoint0.Child; 90 95 91 foreach (var node in allowedBranches) { 96 92 var parent = node.Parent; 97 93 swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent 98 94 double quality = evaluator.Evaluate(context, parent0, problemData, rows); 95 swap(crossoverPoint0, originalChild); // swap the child back (so that the next swap will not affect the currently swapped node from parent1) 99 96 nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality)); 100 97 node.Parent = parent; // restore correct parent … … 103 100 nodeQualities.Sort((a, b) => a.Item2.CompareTo(b.Item2)); // assuming this sorts the list in ascending order 104 101 selectedBranch = evaluator.Maximization ? nodeQualities.Last().Item1 : nodeQualities.First().Item1; 102 103 104 if (selectedBranch == null) 105 throw new Exception("Selected branch is null"); 106 107 if (selectedBranch.Parent == null) 108 throw new Exception("Parent is null"); 109 110 105 111 // swap the node that would create the best offspring 106 112 swap(crossoverPoint0, selectedBranch);
Note: See TracChangeset
for help on using the changeset viewer.