Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs @ 3251

Last change on this file since 3251 was 3239, checked in by gkronber, 14 years ago

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

File size: 5.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
24using HeuristicLab.Data;
25using System.Collections.Generic;
26
27namespace HeuristicLab.Problems.ArtificialAnt {
28  public class AntInterpreter {
29    public int MaxTimeSteps { get; set; }
30    public int FoodEaten { get; set; }
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();
38      }
39    }
40
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();
84      if (currentNode.Symbol is Left) {
85        currentDirection = (currentDirection + 3) % 4;
86        ElapsedTime++;
87      } else if (currentNode.Symbol is Right) {
88        currentDirection = (currentDirection + 1) % 4;
89        ElapsedTime++;
90      } else if (currentNode.Symbol is Move) {
91        MoveAntForward();
92        if (World[currentAntLocationRow, currentAntLocationColumn])
93          FoodEaten++;
94        World[currentAntLocationRow, currentAntLocationColumn] = false;
95        ElapsedTime++;
96      } else if (currentNode.Symbol is IfFoodAhead) {
97        int nextAntLocationRow;
98        int nextAntLocationColumn;
99        NextField(out nextAntLocationRow, out nextAntLocationColumn);
100        if (World[nextAntLocationRow, nextAntLocationColumn]) {
101          nodeStack.Push(currentNode.SubTrees[0]);
102        } else {
103          nodeStack.Push(currentNode.SubTrees[1]);
104        }
105      } else if (currentNode.Symbol is Prog2) {
106        nodeStack.Push(currentNode.SubTrees[1]);
107        nodeStack.Push(currentNode.SubTrees[0]);
108        return;
109      } else if (currentNode.Symbol is Prog3) {
110        nodeStack.Push(currentNode.SubTrees[2]);
111        nodeStack.Push(currentNode.SubTrees[1]);
112        nodeStack.Push(currentNode.SubTrees[0]);
113        return;
114      } else {
115        throw new InvalidOperationException(currentNode.Symbol.ToString());
116      }
117    }
118
119    private void MoveAntForward() {
120      NextField(out currentAntLocationRow, out currentAntLocationColumn);
121    }
122
123    private void NextField(out int nextAntLocationRow, out int nextAntLocationColumn) {
124      switch (currentDirection) {
125        case 0:
126          nextAntLocationColumn = (currentAntLocationColumn + 1) % World.Columns; // EAST
127          nextAntLocationRow = currentAntLocationRow;
128          break;
129        case 1:
130          nextAntLocationRow = (currentAntLocationRow + 1) % World.Rows; // SOUTH
131          nextAntLocationColumn = currentAntLocationColumn;
132          break;
133        case 2:
134          nextAntLocationColumn = (currentAntLocationColumn + World.Columns - 1) % World.Columns; // WEST
135          nextAntLocationRow = currentAntLocationRow;
136          break;
137        case 3:
138          nextAntLocationRow = (currentAntLocationRow + World.Rows - 1) % World.Rows; // NORTH
139          nextAntLocationColumn = currentAntLocationColumn;
140          break;
141        default:
142          throw new InvalidOperationException();
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.