- Timestamp:
- 03/31/10 14:26:45 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs
r3238 r3239 22 22 using System; 23 23 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 24 using HeuristicLab.Data; 25 using System.Collections.Generic; 24 26 25 27 namespace HeuristicLab.Problems.ArtificialAnt { 26 internal class AntInterpreter { 27 private const int N_FOOD_ITEMS = 89; 28 private const int WORLD_WIDTH = 32; 29 private const int WORLD_HEIGHT = 32; 30 private const int FOOD = 1; 31 private const int EMPTY = 0; 32 private int[] SANTA_FE_TRAIL = new int[] { 33 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 34 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 36 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 37 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 38 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0, 0, 0, 0, 0, 0, 61 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 63 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 65 }; 66 67 private int[] trail; 68 69 public int ElapsedTime { get; set; } 28 public class AntInterpreter { 70 29 public int MaxTimeSteps { get; set; } 71 30 public int FoodEaten { get; set; } 72 private int currentDirection; 73 private int currentLocation; 74 75 public AntInterpreter() { 76 currentLocation = 0; 77 currentDirection = 0; 78 FoodEaten = 0; 79 trail = new int[WORLD_HEIGHT * WORLD_WIDTH]; 80 Array.Copy(SANTA_FE_TRAIL, trail, SANTA_FE_TRAIL.Length); 81 } 82 83 internal void Run(SymbolicExpressionTree tree) { 84 while (FoodEaten < N_FOOD_ITEMS && ElapsedTime < MaxTimeSteps) { 85 Step(tree.Root.SubTrees[0]); 31 private BoolMatrix world; 32 public BoolMatrix World { 33 get { return world; } 34 set { 35 // create a clone of the world because the ant will remove the food items it can find. 36 world = (BoolMatrix)value.Clone(); 37 CountFoodItems(); 86 38 } 87 39 } 88 40 89 internal void Step(SymbolicExpressionTreeNode currentNode) { 41 private SymbolicExpressionTree expression; 42 public SymbolicExpressionTree Expression { 43 get { return expression; } 44 set { 45 expression = value; 46 } 47 } 48 public int ElapsedTime { get; set; } 49 private int currentDirection; 50 private int currentAntLocationRow; 51 private int currentAntLocationColumn; 52 private int nFoodItems; 53 private Stack<SymbolicExpressionTreeNode> nodeStack = new Stack<SymbolicExpressionTreeNode>(); 54 55 private void CountFoodItems() { 56 nFoodItems = 0; 57 for (int i = 0; i < World.Rows; i++) { 58 for (int j = 0; j < World.Columns; j++) { 59 if (World[i, j]) nFoodItems++; 60 } 61 } 62 } 63 64 public void AntLocation(out int row, out int column) { 65 row = currentAntLocationRow; 66 column = currentAntLocationColumn; 67 } 68 69 public int AntDirection { 70 get { return currentDirection; } 71 } 72 73 public void Run() { 74 while (ElapsedTime < MaxTimeSteps && FoodEaten < nFoodItems) { 75 Step(); 76 } 77 } 78 79 public void Step() { 80 // expression evaluated completly => start at root again 81 if (nodeStack.Count == 0) 82 nodeStack.Push(Expression.Root.SubTrees[0]); 83 var currentNode = nodeStack.Pop(); 90 84 if (currentNode.Symbol is Left) { 91 if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;92 85 currentDirection = (currentDirection + 3) % 4; 93 86 ElapsedTime++; 94 87 } else if (currentNode.Symbol is Right) { 95 if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;96 88 currentDirection = (currentDirection + 1) % 4; 97 89 ElapsedTime++; 98 90 } else if (currentNode.Symbol is Move) { 99 if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;100 currentLocation = NextField();101 FoodEaten += trail[currentLocation];102 trail[currentLocation] = EMPTY;91 MoveAntForward(); 92 if (World[currentAntLocationRow, currentAntLocationColumn]) 93 FoodEaten++; 94 World[currentAntLocationRow, currentAntLocationColumn] = false; 103 95 ElapsedTime++; 104 96 } else if (currentNode.Symbol is IfFoodAhead) { 105 if (trail[NextField()] == FOOD) { 106 Step(currentNode.SubTrees[0]); 97 int nextAntLocationRow; 98 int nextAntLocationColumn; 99 NextField(out nextAntLocationRow, out nextAntLocationColumn); 100 if (World[nextAntLocationRow, nextAntLocationColumn]) { 101 nodeStack.Push(currentNode.SubTrees[0]); 107 102 } else { 108 Step(currentNode.SubTrees[1]);103 nodeStack.Push(currentNode.SubTrees[1]); 109 104 } 110 105 } else if (currentNode.Symbol is Prog2) { 111 Step(currentNode.SubTrees[0]);112 Step(currentNode.SubTrees[1]);106 nodeStack.Push(currentNode.SubTrees[1]); 107 nodeStack.Push(currentNode.SubTrees[0]); 113 108 return; 114 109 } else if (currentNode.Symbol is Prog3) { 115 Step(currentNode.SubTrees[0]);116 Step(currentNode.SubTrees[1]);117 Step(currentNode.SubTrees[2]);110 nodeStack.Push(currentNode.SubTrees[2]); 111 nodeStack.Push(currentNode.SubTrees[1]); 112 nodeStack.Push(currentNode.SubTrees[0]); 118 113 return; 119 } else 114 } else { 120 115 throw new InvalidOperationException(currentNode.Symbol.ToString()); 121 116 } 122 117 } 123 118 124 private int NextField() {125 int currentLocationX = currentLocation % WORLD_WIDTH;126 int currentLocationY = currentLocation / WORLD_HEIGHT;119 private void MoveAntForward() { 120 NextField(out currentAntLocationRow, out currentAntLocationColumn); 121 } 127 122 123 private void NextField(out int nextAntLocationRow, out int nextAntLocationColumn) { 128 124 switch (currentDirection) { 129 125 case 0: 130 currentLocationX = (currentLocationX + 1) % WORLD_WIDTH; // EAST 126 nextAntLocationColumn = (currentAntLocationColumn + 1) % World.Columns; // EAST 127 nextAntLocationRow = currentAntLocationRow; 131 128 break; 132 129 case 1: 133 currentLocationY = (currentLocationY + 1) % WORLD_HEIGHT; // SOUTH 130 nextAntLocationRow = (currentAntLocationRow + 1) % World.Rows; // SOUTH 131 nextAntLocationColumn = currentAntLocationColumn; 134 132 break; 135 133 case 2: 136 currentLocationX = (currentLocationX + WORLD_WIDTH - 1) % WORLD_WIDTH; // WEST 134 nextAntLocationColumn = (currentAntLocationColumn + World.Columns - 1) % World.Columns; // WEST 135 nextAntLocationRow = currentAntLocationRow; 137 136 break; 138 137 case 3: 139 currentLocationY = (currentLocationY + WORLD_HEIGHT - 1) % WORLD_HEIGHT; // NORTH 138 nextAntLocationRow = (currentAntLocationRow + World.Rows - 1) % World.Rows; // NORTH 139 nextAntLocationColumn = currentAntLocationColumn; 140 140 break; 141 141 default: 142 142 throw new InvalidOperationException(); 143 143 } 144 return currentLocationY * WORLD_WIDTH + currentLocationX;145 144 } 146 145 }
Note: See TracChangeset
for help on using the changeset viewer.