1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.DataAnalysis;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using System.Xml;
|
---|
29 | using System.Diagnostics;
|
---|
30 | using HeuristicLab.Functions;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.GP.SantaFe {
|
---|
33 | internal class AntInterpreter {
|
---|
34 | private const int N_FOOD_ITEMS = 89;
|
---|
35 | private const int WORLD_WIDTH = 32;
|
---|
36 | private const int WORLD_HEIGHT = 32;
|
---|
37 | private const int FOOD = 1;
|
---|
38 | private const int EMPTY = 0;
|
---|
39 | private int[] SANTA_FE_TRAIL = new int[] {
|
---|
40 | 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,
|
---|
41 | 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0, 0, 0,
|
---|
71 | 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,
|
---|
72 | };
|
---|
73 |
|
---|
74 | private int[] trail;
|
---|
75 |
|
---|
76 | public int ElapsedTime { get; set; }
|
---|
77 | public int MaxTimeSteps { get; set; }
|
---|
78 | public int FoodEaten { get; set; }
|
---|
79 | private int currentDirection;
|
---|
80 | private int currentLocation;
|
---|
81 |
|
---|
82 | public AntInterpreter() {
|
---|
83 | currentLocation = 0;
|
---|
84 | currentDirection = 0;
|
---|
85 | FoodEaten = 0;
|
---|
86 | trail = new int[WORLD_HEIGHT * WORLD_WIDTH];
|
---|
87 | Array.Copy(SANTA_FE_TRAIL, trail, SANTA_FE_TRAIL.Length);
|
---|
88 | }
|
---|
89 |
|
---|
90 | internal void Run(IFunctionTree tree) {
|
---|
91 | while(FoodEaten < N_FOOD_ITEMS && ElapsedTime < MaxTimeSteps) {
|
---|
92 | Step(tree);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | internal void Step(IFunctionTree tree) {
|
---|
97 | int symbol = SymbolTable.MapFunction(tree.Function);
|
---|
98 | switch(symbol) {
|
---|
99 | case SymbolTable.LEFT:
|
---|
100 | if(ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
|
---|
101 | currentDirection = (currentDirection + 3) % 4;
|
---|
102 | ElapsedTime++;
|
---|
103 | return;
|
---|
104 | case SymbolTable.RIGHT:
|
---|
105 | if(ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
|
---|
106 | currentDirection = (currentDirection + 1) % 4;
|
---|
107 | ElapsedTime++;
|
---|
108 | return;
|
---|
109 | case SymbolTable.MOVE:
|
---|
110 | if(ElapsedTime >= MaxTimeSteps || FoodEaten >= N_FOOD_ITEMS) return;
|
---|
111 | currentLocation = NextField();
|
---|
112 | FoodEaten += trail[currentLocation];
|
---|
113 | trail[currentLocation] = EMPTY;
|
---|
114 | ElapsedTime++;
|
---|
115 | return;
|
---|
116 | case SymbolTable.IF_FOOD_AHEAD:
|
---|
117 | if(trail[NextField()] == FOOD) {
|
---|
118 | Step(tree.SubTrees[0]);
|
---|
119 | } else {
|
---|
120 | Step(tree.SubTrees[1]);
|
---|
121 | }
|
---|
122 | return;
|
---|
123 | case SymbolTable.PROG2:
|
---|
124 | Step(tree.SubTrees[0]);
|
---|
125 | Step(tree.SubTrees[1]);
|
---|
126 | return;
|
---|
127 | case SymbolTable.PROG3:
|
---|
128 | Step(tree.SubTrees[0]);
|
---|
129 | Step(tree.SubTrees[1]);
|
---|
130 | Step(tree.SubTrees[2]);
|
---|
131 | return;
|
---|
132 | case SymbolTable.UNKNOWN:
|
---|
133 | throw new InvalidOperationException(tree.Function.ToString());
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private int NextField() {
|
---|
138 | int currentLocationX = currentLocation % WORLD_WIDTH;
|
---|
139 | int currentLocationY = currentLocation / WORLD_HEIGHT;
|
---|
140 |
|
---|
141 | switch(currentDirection) {
|
---|
142 | case 0:
|
---|
143 | currentLocationX = (currentLocationX + 1) % WORLD_WIDTH; // EAST
|
---|
144 | break;
|
---|
145 | case 1:
|
---|
146 | currentLocationY = (currentLocationY + 1) % WORLD_HEIGHT; // SOUTH
|
---|
147 | break;
|
---|
148 | case 2:
|
---|
149 | currentLocationX = (currentLocationX + WORLD_WIDTH - 1) % WORLD_WIDTH; // WEST
|
---|
150 | break;
|
---|
151 | case 3:
|
---|
152 | currentLocationY = (currentLocationY + WORLD_HEIGHT - 1) % WORLD_HEIGHT; // NORTH
|
---|
153 | break;
|
---|
154 | default:
|
---|
155 | throw new InvalidOperationException();
|
---|
156 | }
|
---|
157 | return currentLocationY * WORLD_WIDTH + currentLocationX;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|