Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/SantaFeAntProblem.cs @ 11770

Last change on this file since 11770 was 11770, checked in by gkronber, 9 years ago

#2283: worked on generic sequential search alg with bandit policy as parameter

File size: 7.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Runtime.Remoting.Messaging;
5using System.Text;
6
7namespace HeuristicLab.Problems.GrammaticalOptimization {
8  public class SantaFeAntProblem : IProblem {
9    private const string grammarString = @"
10G(A):
11A -> l | r | m | ?(A)(A) | lA | rA | mA
12";
13
14
15    // original koza grammar
16    // Ant -> left | right | move | if-food-ahead Ant Ant | Ant Ant | Ant Ant Ant
17    //
18    // here we use a modified grammar which has only one syntax tree for each sentence
19
20
21    private readonly IGrammar grammar;
22
23    public SantaFeAntProblem() {
24      this.grammar = new Grammar(grammarString);
25    }
26
27    public double BestKnownQuality(int maxLen) {
28      // for now only an upper bound is returned, ideally all food pellets are discovered
29      return 89;
30    }
31
32    public IGrammar Grammar {
33      get { return grammar; }
34    }
35
36    public double Evaluate(string sentence) {
37      var ant = new Ant();
38      int p = 0;
39      Run(ant, sentence, ref p);
40      return ant.FoodEaten;
41    }
42
43    private void Run(Ant ant, string sentence, ref int p) {
44      while (!ant.Done()) {
45        if (p >= sentence.Length) p = 0; // restart
46        switch (sentence[p]) {
47          case 'r': {
48              ant.TurnRight();
49              p++;
50              break;
51            }
52          case 'l': {
53              ant.TurnLeft();
54              p++;
55              break;
56            }
57          case 'm': {
58              ant.Move();
59              p++;
60              break;
61            }
62          case '?': {
63              p++; // skip p
64              if (ant.IsFoodAhead()) {
65                if (sentence[p] != '(') throw new ArgumentException();
66                p++;
67                Run(ant, sentence, ref p);      // it cannot happen that we run over the the end of the sentence here
68                if (ant.Done()) return;
69                if (sentence[p] != '(') throw new ArgumentException();
70                p++;
71                Skip(sentence, ref p);
72              } else {
73                if (sentence[p] != '(') throw new ArgumentException();
74                p++;
75                Skip(sentence, ref p);
76                if (sentence[p] != '(') throw new ArgumentException();
77                p++;
78                Run(ant, sentence, ref p);    // it cannot happen that we run over the the end of the sentence here
79              }
80              break;
81            }
82          case ')': {
83              p++; // skip
84              return;     // caller must continue processing
85            }
86          default: {
87              throw new ArgumentException();
88            }
89        }
90      }
91    }
92
93    private void Skip(string sentence, ref int p) {
94      int openCount = 1;
95      while (openCount > 0) {
96        if (sentence[p] == '(') openCount++;
97        else if (sentence[p] == ')') openCount--;
98        p++;
99      }
100    }
101
102    public string CanonicalRepresentation(string terminalPhrase) {
103      //return terminalPhrase;
104      string oldPhrase;
105      do {
106        oldPhrase = terminalPhrase;
107        terminalPhrase = terminalPhrase.Replace("ll", "rr").Replace("rl", "lr").Replace("lr", "").Replace("lll", "r").Replace("rrr", "l");
108      } while (terminalPhrase != oldPhrase);
109      return terminalPhrase;
110    }
111  }
112
113  public class Ant {
114    private const int maxSteps = 600;
115    public enum HeadingEnum { North, East, South, West };
116    public int FoodEaten { get; private set; }
117    private readonly char[][] world = new char[32][];
118    private int posX;
119    private int posY;
120    private int steps;
121    private HeadingEnum heading;
122
123
124
125    public Ant() {
126      world[00] = " ###                            ".ToCharArray();
127      world[01] = "   #                            ".ToCharArray();
128      world[02] = "   #                    .###..  ".ToCharArray();
129      world[03] = "   #                    #    #  ".ToCharArray();
130      world[04] = "   #                    #    #  ".ToCharArray();
131      world[05] = "   ####.#####       .##..    .  ".ToCharArray();
132      world[06] = "            #       .        #  ".ToCharArray();
133      world[07] = "            #       #        .  ".ToCharArray();
134      world[08] = "            #       #        .  ".ToCharArray();
135      world[09] = "            #       #        #  ".ToCharArray();
136      world[10] = "            .       #        .  ".ToCharArray();
137      world[11] = "            #       .        .  ".ToCharArray();
138      world[12] = "            #       .        #  ".ToCharArray();
139      world[13] = "            #       #        .  ".ToCharArray();
140      world[14] = "            #       #  ...###.  ".ToCharArray();
141      world[15] = "            .   .#...  #        ".ToCharArray();
142      world[16] = "            .   .      .        ".ToCharArray();
143      world[17] = "            #   .      .        ".ToCharArray();
144      world[18] = "            #   #      .#...    ".ToCharArray();
145      world[19] = "            #   #          #    ".ToCharArray();
146      world[20] = "            #   #          .    ".ToCharArray();
147      world[21] = "            #   #          .    ".ToCharArray();
148      world[22] = "            #   .      ...#.    ".ToCharArray();
149      world[23] = "            #   .      #        ".ToCharArray();
150      world[24] = " ..##..#####.   #               ".ToCharArray();
151      world[25] = " #              #               ".ToCharArray();
152      world[26] = " #              #               ".ToCharArray();
153      world[27] = " #     .#######..               ".ToCharArray();
154      world[28] = " #     #                        ".ToCharArray();
155      world[29] = " .     #                        ".ToCharArray();
156      world[30] = " .####..                        ".ToCharArray();
157      world[31] = "                                ".ToCharArray();
158
159      posX = 0;
160      posY = 0;
161      heading = HeadingEnum.East;
162      FoodEaten = 0;
163      steps = 0;
164    }
165
166    public bool Done() {
167      return steps > maxSteps;
168    }
169
170    public void TurnRight() {
171      if (steps <= maxSteps) {
172        steps++;
173        if (heading == HeadingEnum.West) heading = HeadingEnum.North;
174        else heading++;
175      }
176    }
177
178    public void TurnLeft() {
179      if (steps <= maxSteps) {
180        steps++;
181        if (heading == HeadingEnum.North) heading = HeadingEnum.West;
182        else heading--;
183      }
184    }
185
186    public void Move() {
187      if (steps <= maxSteps) {
188        steps++;
189        MoveInternal(ref posX, ref posY);
190        if (world[posY][posX] == '#') {
191          FoodEaten++;
192          world[posY][posX] = '.';
193        }
194      }
195    }
196
197    private void MoveInternal(ref int posX, ref int posY) {
198      switch (heading) {
199        case HeadingEnum.North:
200          posY = (posY + 31) % 32;
201          break;
202        case HeadingEnum.East:
203          posX = (posX + 1) % 32;
204          break;
205        case HeadingEnum.South:
206          posY = (posY + 1) % 32;
207          break;
208        case HeadingEnum.West:
209          posX = (posX + 31) % 32;
210          break;
211      }
212    }
213
214    public bool IsFoodAhead() {
215      int nextPosX = posX;
216      int nextPosY = posY;
217      MoveInternal(ref nextPosX, ref nextPosY);
218      return world[nextPosY][nextPosX] == '#';
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.