- Timestamp:
- 07/16/12 09:06:27 (12 years ago)
- Location:
- branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMove.cs
r8206 r8292 29 29 [StorableClass] 30 30 public class ReplaceBranchMove : Item { 31 31 [Storable] 32 public int FragmentIndex { get; set; } 32 33 [Storable] 33 34 public ISymbolicExpressionTree Tree { get; set; } … … 59 60 protected ReplaceBranchMove(ReplaceBranchMove original, Cloner cloner) 60 61 : base(original, cloner) { 62 this.FragmentIndex = original.FragmentIndex; 61 63 this.Tree = cloner.Clone(original.Tree); 62 64 this.SubtreeIndex = original.SubtreeIndex; … … 68 70 this.Beta = original.Beta; 69 71 } 70 public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild, double[] originalOutput, double[] newOutput )72 public ReplaceBranchMove(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent, int subtreeIndex, ISymbolicExpressionTreeNode newChild, double[] originalOutput, double[] newOutput, int fragmentIndex) 71 73 : base() { 72 74 this.Tree = tree; … … 76 78 this.OriginalOutput = originalOutput; 77 79 this.NewOutput = newOutput; 80 this.FragmentIndex = fragmentIndex; 78 81 } 79 82 -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveEvaluator.cs
r8286 r8292 87 87 88 88 // prevent scaling to a constant value 89 if (beta.IsAlmost(0.0)) beta = 1 0E-5;89 if (beta.IsAlmost(0.0)) beta = 1.0; 90 90 91 91 move.Alpha = alpha; -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveMaker.cs
r8214 r8292 48 48 get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; } 49 49 } 50 public ILookupParameter<ItemList<ISymbolicExpressionTree>> FragmentsParameter { 51 get { return (ILookupParameter<ItemList<ISymbolicExpressionTree>>)Parameters["Fragments"]; } 52 } 53 public ILookupParameter<ItemList<DoubleArray>> FragmentOutputsParameter { 54 get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters["FragmentOutputs"]; } 55 } 50 56 51 57 [StorableConstructor] … … 58 64 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The relative quality of the move.")); 59 65 Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree on which the move should be applied.")); 66 Parameters.Add(new LookupParameter<ItemList<ISymbolicExpressionTree>>("Fragments")); 67 Parameters.Add(new LookupParameter<ItemList<DoubleArray>>("FragmentOutputs")); 60 68 } 61 69 … … 65 73 66 74 public override IOperation Apply() { 75 var fragments = FragmentsParameter.ActualValue; 76 var fragmentOutputs = FragmentOutputsParameter.ActualValue; 77 67 78 var move = ReplaceBranchMoveParameter.ActualValue; 68 79 DoubleValue moveQuality = MoveQualityParameter.ActualValue; … … 70 81 71 82 var newNode = (ISymbolicExpressionTreeNode)move.NewBranch.Clone(); 83 var oldBranch = move.Parent.GetSubtree(move.SubtreeIndex); 84 72 85 move.Parent.RemoveSubtree(move.SubtreeIndex); 73 86 move.Parent.InsertSubtree(move.SubtreeIndex, Scale(newNode, move.Alpha, move.Beta)); 74 87 88 // move oldBranch into pool 89 var fragmentStart = fragments[move.FragmentIndex].Root.GetSubtree(0); 90 fragmentStart.RemoveSubtree(0); 91 fragmentStart.AddSubtree(oldBranch); 92 fragmentOutputs[move.FragmentIndex] = new DoubleArray(move.NewOutput); 75 93 quality.Value = moveQuality.Value; 76 94 // update solution -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMoveSoftTabuCriterion.cs
r8286 r8292 101 101 ItemList<IItem> tabuList = TabuListParameter.ActualValue; 102 102 var move = ReplaceBranchMoveParameter.ActualValue; 103 var tree = SymbolicExpressionTreeParameter.ActualValue;104 103 bool isTabu = true; 105 104 bool useQualityAspiration = UseQualityAspirationCriterion.Value; -
branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMultiMoveGenerator.cs
r8286 r8292 94 94 get { return (IValueLookupParameter<BoolValue>)Parameters["Semantic"]; } 95 95 } 96 97 private IList<ISymbolicExpressionTree> fragments; 98 private IList<double[]> fragmentOutput; 96 public ILookupParameter<ItemList<ISymbolicExpressionTree>> FragmentsParameter { 97 get { return (ILookupParameter<ItemList<ISymbolicExpressionTree>>)Parameters["Fragments"]; } 98 } 99 public ILookupParameter<ItemList<DoubleArray>> FragmentOutputsParameter { 100 get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters["FragmentOutputs"]; } 101 } 99 102 100 103 [StorableConstructor] … … 119 122 Parameters.Add(new ValueLookupParameter<IntValue>("NeighbourhoodSize", new IntValue(5))); 120 123 Parameters.Add(new ValueLookupParameter<BoolValue>("Semantic", new BoolValue())); 124 Parameters.Add(new LookupParameter<ItemList<ISymbolicExpressionTree>>("Fragments")); 125 Parameters.Add(new LookupParameter<ItemList<DoubleArray>>("FragmentOutputs")); 121 126 } 122 127 … … 139 144 } 140 145 141 public override void ClearState() {142 fragments = null;143 fragmentOutput = null;144 base.ClearState();145 }146 public override void InitializeState() {147 fragments = null;148 fragmentOutput = null;149 base.InitializeState();150 }151 152 146 public override IOperation Apply() { 153 147 var random = RandomParameter.ActualValue; 154 if ( fragments == null || fragmentOutput== null) {148 if (FragmentsParameter.ActualValue == null || FragmentOutputsParameter.ActualValue == null) { 155 149 InitializeOperator(); 156 150 } … … 176 170 int maxDepth = MaximumSymbolicExpressionTreeDepthParameter.ActualValue.Value; 177 171 int maxLength = MaximumSymbolicExpressionTreeLengthParameter.ActualValue.Value; 178 var possibleInternalChildren = (from parent in tree.Root.GetSubtree(0). GetSubtree(0).IterateNodesPrefix()172 var possibleInternalChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() 179 173 from i in Enumerable.Range(0, parent.SubtreeCount) 180 174 let currentChild = parent.GetSubtree(i) 181 175 where currentChild.SubtreeCount > 0 182 where tree.Root.GetBranchLevel(currentChild) < =maxDepth + 2176 where tree.Root.GetBranchLevel(currentChild) < maxDepth + 2 183 177 where tree.Length - currentChild.GetLength() < maxLength 184 178 select new CutPoint(parent, i)).ToArray(); 185 179 186 var possibleLeaveChildren = (from parent in tree.Root.GetSubtree(0). GetSubtree(0).IterateNodesPrefix()180 var possibleLeaveChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() 187 181 from i in Enumerable.Range(0, parent.SubtreeCount) 188 182 let currentChild = parent.GetSubtree(i) 189 183 where currentChild.SubtreeCount == 0 190 where tree.Root.GetBranchLevel(currentChild) < =maxDepth + 2184 where tree.Root.GetBranchLevel(currentChild) < maxDepth + 2 191 185 where tree.Length - 1 < maxLength 192 186 select new CutPoint(parent, i)).ToArray(); 193 187 if (possibleInternalChildren.Length == 0) possibleInternalChildren = possibleLeaveChildren; 188 if (possibleLeaveChildren.Length == 0) possibleLeaveChildren = possibleInternalChildren; 194 189 195 190 var root = (new ProgramRootSymbol()).CreateTreeNode(); … … 202 197 203 198 bool semantic = SemanticParameter.ActualValue.Value; 204 205 // select a random replacement point 206 CutPoint[] possibleChildren; 207 if (random.NextDouble() < 0.9) 208 possibleChildren = possibleInternalChildren; 209 else possibleChildren = possibleLeaveChildren; 210 var selected = possibleChildren[random.Next(possibleChildren.Length)]; 211 // evaluate 212 start.AddSubtree(selected.Parent.GetSubtree(selected.ChildIndex)); 213 var output = interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray(); 214 start.RemoveSubtree(0); 215 216 if (semantic) { 217 return FindMostSimilarFragments(tree, maxLength, maxDepth, selected, random, n, output); 218 } else { 219 return FindRandomFragments(tree, maxLength, maxDepth, selected, random, n, output); 199 int maxNeighbours = NeighbourhoodSizeParameter.ActualValue.Value; 200 var count = 0; 201 while (count < n) { 202 // select a random replacement point 203 CutPoint[] possibleChildren; 204 if (random.NextDouble() < 0.9) 205 possibleChildren = possibleInternalChildren; 206 else possibleChildren = possibleLeaveChildren; 207 var selected = possibleChildren[random.Next(possibleChildren.Length)]; 208 // evaluate 209 start.AddSubtree(selected.Parent.GetSubtree(selected.ChildIndex)); 210 var output = interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray(); 211 start.RemoveSubtree(0); 212 213 if (semantic) { 214 foreach (var m in FindMostSimilarFragments(tree, maxLength, maxDepth, selected, random, maxNeighbours, output)) { 215 yield return m; 216 count++; 217 } 218 } else { 219 foreach (var m in FindRandomFragments(tree, maxLength, maxDepth, selected, random, maxNeighbours, output)) { 220 yield return m; 221 count++; 222 } 223 } 220 224 } 221 225 } … … 223 227 private IEnumerable<ReplaceBranchMove> FindRandomFragments(ISymbolicExpressionTree tree, int maxLength, int maxDepth, CutPoint selected, 224 228 IRandom random, int maxNeighbours, double[] output) { 225 var selectedFragments = new List< Tuple<ISymbolicExpressionTree, double[]>>(maxNeighbours);229 var selectedFragments = new List<int>(maxNeighbours); 226 230 int treeLength = tree.Length; 227 231 int removedFragementLength = selected.Parent.GetSubtree(selected.ChildIndex).GetLength(); … … 229 233 int iterations = 0; 230 234 int maxIterations = maxNeighbours + 100; 235 var fragments = FragmentsParameter.ActualValue; 236 var fragmentOutput = FragmentOutputsParameter.ActualValue; 231 237 // select random fragments 232 238 while (selectedFragments.Count < maxNeighbours && iterations++ < maxIterations) { … … 238 244 parentBranchLevel + selectedFragment.Depth - 2 <= maxDepth + 2 && 239 245 tree.Root.Grammar.IsAllowedChildSymbol(selected.Parent.Symbol, selectedFragment.Root.GetSubtree(0).GetSubtree(0).Symbol, selected.ChildIndex)) { 240 selectedFragments.Add( Tuple.Create(selectedFragment, selectedFragmentOutput));246 selectedFragments.Add(r); 241 247 } 242 248 } 243 249 // yield moves (we need to add linear scaling parameters for the inserted tree) 244 250 return selectedFragments 245 .Select( pair => new ReplaceBranchMove(tree, selected.Parent, selected.ChildIndex, pair.Item1.Root.GetSubtree(0).GetSubtree(0), output, pair.Item2));251 .Select(i => new ReplaceBranchMove(tree, selected.Parent, selected.ChildIndex, fragments[i].Root.GetSubtree(0).GetSubtree(0), output, fragmentOutput[i].ToArray(), i)); 246 252 } 247 253 248 254 private IEnumerable<ReplaceBranchMove> FindMostSimilarFragments(ISymbolicExpressionTree tree, int maxLength, int maxDepth, CutPoint selected, 249 255 IRandom random, int maxNeighbours, double[] output) { 250 var bestTrees = new SortedList<double, List<Tuple<ISymbolicExpressionTree, double[]>>>(fragments.Count); 256 var fragments = FragmentsParameter.ActualValue; 257 var fragmentOutput = FragmentOutputsParameter.ActualValue; 258 var bestTrees = new SortedList<double, List<int>>(fragments.Count); 251 259 int treeLength = tree.Length; 252 260 int removedFragementLength = selected.Parent.GetSubtree(selected.ChildIndex).GetLength(); … … 266 274 if (similarity < 1 && ((bestTrees.Count < maxNeighbours) || similarity > bestTrees.ElementAt(0).Key)) { 267 275 if (!bestTrees.ContainsKey(similarity)) { 268 var l = new List< Tuple<ISymbolicExpressionTree, double[]>>();276 var l = new List<int>(); 269 277 bestTrees.Add(similarity, l); 270 278 } 271 bestTrees[similarity].Add( Tuple.Create(fragments[i], fragmentOutput[i]));279 bestTrees[similarity].Add(i); 272 280 if (bestTrees.Count > maxNeighbours) bestTrees.RemoveAt(0); 273 281 } … … 278 286 while (c < maxNeighbours) { 279 287 var l = bestTrees.ElementAt(c % bestTrees.Count).Value; 280 var pair= l[random.Next(l.Count)];288 var index = l[random.Next(l.Count)]; 281 289 yield return 282 new ReplaceBranchMove(tree, selected.Parent, selected.ChildIndex, pair.Item1.Root.GetSubtree(0).GetSubtree(0),283 output, pair.Item2);290 new ReplaceBranchMove(tree, selected.Parent, selected.ChildIndex, fragments[index].Root.GetSubtree(0).GetSubtree(0), 291 output, fragmentOutput[index].ToArray(), index); 284 292 c++; 285 293 } … … 313 321 // enable constants again 314 322 constSym.InitialFrequency = oldConstFreq; 315 this.fragments = trees; 316 this.fragmentOutput = treeOutput; 323 // set parameters 324 FragmentsParameter.ActualValue = new ItemList<ISymbolicExpressionTree>(trees); 325 FragmentOutputsParameter.ActualValue = new ItemList<DoubleArray>(treeOutput.Select(a => new DoubleArray(a))); 317 326 } 318 327 }
Note: See TracChangeset
for help on using the changeset viewer.