- Timestamp:
- 08/25/15 10:38:20 (9 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.5/AntInterpreter.cs
r12012 r12895 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Problems.ArtificialAnt.Symbols;28 27 29 28 namespace HeuristicLab.Problems.ArtificialAnt { 30 29 public class AntInterpreter { 31 30 32 public int MaxTimeSteps { get; set; } 33 public int FoodEaten { get; set; } 34 private BoolMatrix world; 35 public BoolMatrix World { 36 get { return world; } 37 set { 38 // create a clone of the world because the ant will remove the food items it can find. 39 world = (BoolMatrix)value.Clone(); 40 CountFoodItems(); 41 } 42 } 31 public int MaxTimeSteps { get; private set; } 32 public int FoodEaten { get; private set; } 33 public BoolMatrix World { get; private set; } 43 34 44 private SymbolicExpressionTree expression; 45 public SymbolicExpressionTree Expression { 46 get { return expression; } 47 set { 48 expression = value; 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 35 public ISymbolicExpressionTree Expression { get; private set; } 59 36 60 37 … … 65 42 private int nFoodItems; 66 43 private Stack<ISymbolicExpressionTreeNode> nodeStack = new Stack<ISymbolicExpressionTreeNode>(); 44 45 public AntInterpreter(ISymbolicExpressionTree tree, BoolMatrix world, int maxTimeSteps) { 46 this.Expression = tree; 47 this.MaxTimeSteps = maxTimeSteps; 48 // create a clone of the world because the ant will remove the food items it can find. 49 World = (BoolMatrix)world.Clone(); 50 CountFoodItems(); 51 } 67 52 68 53 private void CountFoodItems() { … … 97 82 98 83 var currentNode = nodeStack.Pop(); 99 if (currentNode.Symbol is Left) {84 if (currentNode.Symbol.Name == "Left") { 100 85 currentDirection = (currentDirection + 3) % 4; 101 86 ElapsedTime++; 102 } else if (currentNode.Symbol is Right) {87 } else if (currentNode.Symbol.Name == "Right") { 103 88 currentDirection = (currentDirection + 1) % 4; 104 89 ElapsedTime++; 105 } else if (currentNode.Symbol is Move) {90 } else if (currentNode.Symbol.Name == "Move") { 106 91 MoveAntForward(); 107 if (World[currentAntLocationRow, currentAntLocationColumn]) 92 if (World[currentAntLocationRow, currentAntLocationColumn]) { 108 93 FoodEaten++; 109 World[currentAntLocationRow, currentAntLocationColumn] = false; 94 World[currentAntLocationRow, currentAntLocationColumn] = false; 95 } 110 96 ElapsedTime++; 111 } else if (currentNode.Symbol is IfFoodAhead) {97 } else if (currentNode.Symbol.Name == "IfFoodAhead") { 112 98 int nextAntLocationRow; 113 99 int nextAntLocationColumn; … … 118 104 nodeStack.Push(currentNode.GetSubtree(1)); 119 105 } 120 } else if (currentNode.Symbol is Prog2) {106 } else if (currentNode.Symbol.Name == "Prog2") { 121 107 nodeStack.Push(currentNode.GetSubtree(1)); 122 108 nodeStack.Push(currentNode.GetSubtree(0)); 123 return; 124 } else if (currentNode.Symbol is Prog3) { 109 } else if (currentNode.Symbol.Name == "Prog3") { 125 110 nodeStack.Push(currentNode.GetSubtree(2)); 126 111 nodeStack.Push(currentNode.GetSubtree(1)); 127 112 nodeStack.Push(currentNode.GetSubtree(0)); 128 return;129 113 } else if (currentNode.Symbol is InvokeFunction) { 130 114 var invokeNode = currentNode as InvokeFunctionTreeNode; … … 171 155 } 172 156 } 157 158 159 private SymbolicExpressionTreeNode FindMatchingFunction(string name) { 160 foreach (var defunBranch in Expression.Root.Subtrees.OfType<DefunTreeNode>()) { 161 if (defunBranch.FunctionName == name) return defunBranch; 162 } 163 throw new ArgumentException("Function definition for " + name + " not found."); 164 } 173 165 } 174 166 }
Note: See TracChangeset
for help on using the changeset viewer.