- Timestamp:
- 04/13/10 20:44:31 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs
r3294 r3338 29 29 namespace HeuristicLab.Problems.ArtificialAnt { 30 30 public class AntInterpreter { 31 31 32 public int MaxTimeSteps { get; set; } 32 33 public int FoodEaten { get; set; } … … 48 49 } 49 50 } 51 52 private SymbolicExpressionTreeNode FindMatchingFunction(string name) { 53 foreach (var defunBranch in expression.Root.SubTrees.OfType<DefunTreeNode>()) { 54 if (defunBranch.FunctionName == name) return defunBranch; 55 } 56 throw new ArgumentException("Function definition for " + name + " not found."); 57 } 58 59 60 50 61 public int ElapsedTime { get; set; } 51 62 private int currentDirection; … … 81 92 public void Step() { 82 93 // expression evaluated completly => start at root again 83 if (nodeStack.Count == 0) 94 if (nodeStack.Count == 0) { 84 95 nodeStack.Push(Expression.ResultProducingExpression); 96 } 97 85 98 var currentNode = nodeStack.Pop(); 86 99 if (currentNode.Symbol is Left) { … … 116 129 } else if (currentNode.Symbol is InvokeFunction) { 117 130 var invokeNode = currentNode as InvokeFunctionTreeNode; 118 var fun Branch = (from node in expression.Root.SubTrees119 let funNode = node as DefunTreeNode120 where funNode != null121 where funNode.Name == invokeNode.InvokedFunctionName122 select funNode).FirstOrDefault();123 if (funBranch == null) throw new InvalidOperationException("Can't find definition of function " + invokeNode.InvokedFunctionName);124 nodeStack.Push(funBranch.SubTrees[0]);125 foreach (var subTree in invokeNode.SubTrees)126 nodeStack.Push(subTree);127 } else if(currentNode.Symbol is Argument) {128 // do nothing131 var functionDefinition = (SymbolicExpressionTreeNode)FindMatchingFunction(invokeNode.InvokedFunctionName).Clone(); 132 var argumentCutPoints = (from node in functionDefinition.IterateNodesPrefix() 133 where node.SubTrees.Count > 0 134 from subtree in node.SubTrees 135 where subtree is ArgumentTreeNode 136 select new { Parent = node, Argument = subtree.Symbol as Argument, ChildIndex = node.SubTrees.IndexOf(subtree) }).ToList(); 137 foreach (var cutPoint in argumentCutPoints) { 138 cutPoint.Parent.RemoveSubTree(cutPoint.ChildIndex); 139 cutPoint.Parent.InsertSubTree(cutPoint.ChildIndex, (SymbolicExpressionTreeNode)invokeNode.SubTrees[cutPoint.Argument.ArgumentIndex].Clone()); 140 } 141 nodeStack.Push(functionDefinition.SubTrees[0]); 129 142 } else { 130 143 throw new InvalidOperationException(currentNode.Symbol.ToString());
Note: See TracChangeset
for help on using the changeset viewer.