Changeset 8292 for branches/GP-MoveOperators/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/ReplaceBranchMultiMoveGenerator.cs
- Timestamp:
- 07/16/12 09:06:27 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.