Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/25/15 10:38:20 (9 years ago)
Author:
gkronber
Message:

#2421 new version of artificial ant problem that uses SymbolicExpressionTreeEncoding

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  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    27 using HeuristicLab.Problems.ArtificialAnt.Symbols;
    2827
    2928namespace HeuristicLab.Problems.ArtificialAnt {
    3029  public class AntInterpreter {
    3130
    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; }
    4334
    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; }
    5936
    6037
     
    6542    private int nFoodItems;
    6643    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    }
    6752
    6853    private void CountFoodItems() {
     
    9782
    9883      var currentNode = nodeStack.Pop();
    99       if (currentNode.Symbol is Left) {
     84      if (currentNode.Symbol.Name == "Left") {
    10085        currentDirection = (currentDirection + 3) % 4;
    10186        ElapsedTime++;
    102       } else if (currentNode.Symbol is Right) {
     87      } else if (currentNode.Symbol.Name == "Right") {
    10388        currentDirection = (currentDirection + 1) % 4;
    10489        ElapsedTime++;
    105       } else if (currentNode.Symbol is Move) {
     90      } else if (currentNode.Symbol.Name == "Move") {
    10691        MoveAntForward();
    107         if (World[currentAntLocationRow, currentAntLocationColumn])
     92        if (World[currentAntLocationRow, currentAntLocationColumn]) {
    10893          FoodEaten++;
    109         World[currentAntLocationRow, currentAntLocationColumn] = false;
     94          World[currentAntLocationRow, currentAntLocationColumn] = false;
     95        }
    11096        ElapsedTime++;
    111       } else if (currentNode.Symbol is IfFoodAhead) {
     97      } else if (currentNode.Symbol.Name == "IfFoodAhead") {
    11298        int nextAntLocationRow;
    11399        int nextAntLocationColumn;
     
    118104          nodeStack.Push(currentNode.GetSubtree(1));
    119105        }
    120       } else if (currentNode.Symbol is Prog2) {
     106      } else if (currentNode.Symbol.Name == "Prog2") {
    121107        nodeStack.Push(currentNode.GetSubtree(1));
    122108        nodeStack.Push(currentNode.GetSubtree(0));
    123         return;
    124       } else if (currentNode.Symbol is Prog3) {
     109      } else if (currentNode.Symbol.Name == "Prog3") {
    125110        nodeStack.Push(currentNode.GetSubtree(2));
    126111        nodeStack.Push(currentNode.GetSubtree(1));
    127112        nodeStack.Push(currentNode.GetSubtree(0));
    128         return;
    129113      } else if (currentNode.Symbol is InvokeFunction) {
    130114        var invokeNode = currentNode as InvokeFunctionTreeNode;
     
    171155      }
    172156    }
     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    }
    173165  }
    174166}
Note: See TracChangeset for help on using the changeset viewer.