Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/31/10 14:26:45 (14 years ago)
Author:
gkronber
Message:

Extracted view for artificial ant problem into a separate plugin/project. #952 (Artificial Ant Problem for 3.3)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs

    r3238 r3239  
    2222using System;
    2323using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
     24using HeuristicLab.Data;
     25using System.Collections.Generic;
    2426
    2527namespace 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, 0, 0, 1, 0, 0,
    40       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    41       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    42       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
    43       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, 0, 0, 0, 0, 0, 0, 0,
    44       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, 0, 0, 0, 0, 0,
    45       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, 0, 0, 1, 0, 0,
    46       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    47       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
    48       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
    49       0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    60       0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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 {
    7029    public int MaxTimeSteps { get; set; }
    7130    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();
    8638      }
    8739    }
    8840
    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();
    9084      if (currentNode.Symbol is Left) {
    91         if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
    9285        currentDirection = (currentDirection + 3) % 4;
    9386        ElapsedTime++;
    9487      } else if (currentNode.Symbol is Right) {
    95         if (ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
    9688        currentDirection = (currentDirection + 1) % 4;
    9789        ElapsedTime++;
    9890      } 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;
    10395        ElapsedTime++;
    10496      } 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]);
    107102        } else {
    108           Step(currentNode.SubTrees[1]);
     103          nodeStack.Push(currentNode.SubTrees[1]);
    109104        }
    110105      } 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]);
    113108        return;
    114109      } 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]);
    118113        return;
    119       } else  {
     114      } else {
    120115        throw new InvalidOperationException(currentNode.Symbol.ToString());
    121116      }
    122117    }
    123118
    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    }
    127122
     123    private void NextField(out int nextAntLocationRow, out int nextAntLocationColumn) {
    128124      switch (currentDirection) {
    129125        case 0:
    130           currentLocationX = (currentLocationX + 1) % WORLD_WIDTH; // EAST
     126          nextAntLocationColumn = (currentAntLocationColumn + 1) % World.Columns; // EAST
     127          nextAntLocationRow = currentAntLocationRow;
    131128          break;
    132129        case 1:
    133           currentLocationY = (currentLocationY + 1) % WORLD_HEIGHT; // SOUTH
     130          nextAntLocationRow = (currentAntLocationRow + 1) % World.Rows; // SOUTH
     131          nextAntLocationColumn = currentAntLocationColumn;
    134132          break;
    135133        case 2:
    136           currentLocationX = (currentLocationX + WORLD_WIDTH - 1) % WORLD_WIDTH; // WEST
     134          nextAntLocationColumn = (currentAntLocationColumn + World.Columns - 1) % World.Columns; // WEST
     135          nextAntLocationRow = currentAntLocationRow;
    137136          break;
    138137        case 3:
    139           currentLocationY = (currentLocationY + WORLD_HEIGHT - 1) % WORLD_HEIGHT; // NORTH
     138          nextAntLocationRow = (currentAntLocationRow + World.Rows - 1) % World.Rows; // NORTH
     139          nextAntLocationColumn = currentAntLocationColumn;
    140140          break;
    141141        default:
    142142          throw new InvalidOperationException();
    143143      }
    144       return currentLocationY * WORLD_WIDTH + currentLocationX;
    145144    }
    146145  }
Note: See TracChangeset for help on using the changeset viewer.