Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/examples/Artificial Ant.txt @ 9430

Last change on this file since 9430 was 9430, checked in by gkronber, 11 years ago

initial import of GPDL parser plugin

File size: 7.9 KB
Line 
1PROBLEM ArtificialAnt
2
3CODE <<
4public struct Point {
5  public int x; public int y;
6  public Point(int x, int y) {
7    this.x = x;
8  this.y = y;
9  }
10};
11
12class World {
13
14private static string[] LosAltosTrail = new string[] {
15" ###                                          ",
16"   #                                  .###..  ",
17"   #                                  #    #  ",
18"   #                                  #    #  ",
19"   ####.#####           .############..    .  ",
20"            #           .                  #  ",
21"            #           #                  .  ",
22"            #           #                  .  ",
23"            #           #                  #  ",
24"            .           #                  .  ",
25"            #           .                  .  ",
26"            #           .                  #  ",
27"            #           #                  .  ",
28"            #           #            ...###.  ",
29"            .       .#...            #        ",
30"            .       .                .        ",
31"            #       .                .        ",
32"            #       #                .#...    ",
33"            #       #                    #    ",
34"            #       #                    .    ",
35"            #       #                    .    ",
36"            #       .                ...#.    ",
37"            #       .                #        ",
38" ..##..#####.       #                #        ",
39" #                  #                #        ",
40" #                  #                #        ",
41" #                  #                #        ",
42" #                  #                #        ",
43" #                  #                #        ",
44" #     .#####....##..                #        ",
45" #     #                             #        ",
46" .     #                             #        ",
47" .####..                             #        ",
48"                                   #.#        ",
49"                                   #          ",
50"                                   #          ",
51"                                   #          ",
52"                                   #          ",
53"                                   #          ",
54"                                   #          ",
55"                                   #          ",
56"                              ######          ",
57"                              .               ",
58"                              #               ",
59"                              #               ",
60"                              #               ",
61"                              #               ",
62"                              #               ",
63"                              #               ",
64"                            #..               ",
65"                            #                 ",
66"                            #                 ",
67"                            #                 ",
68"                            #                 ",
69"                            #                 ",
70"                      .######                 ",
71"                      .                       ",
72"                      #                       ",
73"                      #                       ",
74"                      #                       ",
75"                      #                       ",
76"                      #                       ",
77"                      #                       ",
78"                      #                       ",
79"                      #                       ",
80"                      #                       "
81};
82  private char[][] world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
83  public int TotalFood = LosAltosTrail.Select(l=>l.Where(c=>c=='#').Count()).Sum();
84  public int Width { get { return 100; } }
85  public int Height { get { return 100; } }
86 
87  public void Reset() {
88    world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
89  }
90 
91  public bool FoodAt(Point p) {
92    if(p.x < 0 || p.y < 0) return false;
93    if(world.Length <= p.y) return false;
94  if(world[p.y].Length <= p.x) return false;
95    return world[p.y][p.x] == '#';
96  }
97  public void RemoveFoodAt(Point p) {
98    if(world.Length < p.y) return;
99  if(world[p.y].Length < p.x) return;
100    world[p.y][p.x] = ' ';
101  }
102}
103
104enum Direction {West, East, North, South};
105
106class AntState {
107  public Point Position { get; set;}
108  public int MaxSteps { get; set;}
109  public Direction Direction { get; set; }
110  public World World { get; set; }
111  public int Steps { get; set; }
112  public int FoodEaten { get; set; }
113 
114  public AntState() {
115    Steps = 0; 
116  FoodEaten = 0;
117  }
118 
119  public void TurnLeft() {
120    Steps++;
121    switch(Direction) {
122    case Direction.West: Direction = Direction.South; break;
123    case Direction.South: Direction = Direction.East; break;
124    case Direction.East: Direction = Direction.North; break;
125    case Direction.North: Direction = Direction.West; break;
126  }
127  }
128  public void TurnRight() {
129    Steps++;
130    switch(Direction) {
131    case Direction.West: Direction = Direction.North; break;
132    case Direction.South: Direction = Direction.West; break;
133    case Direction.East: Direction = Direction.South; break;
134    case Direction.North: Direction = Direction.East; break;
135  }
136  }
137  public void MoveForward() {
138    Position = NextPosition;
139  Steps++;
140  if(World.FoodAt(Position)) {
141      World.RemoveFoodAt(Position);
142    FoodEaten++;
143    }
144  }
145  public Point NextPosition {
146    get {
147      int x, y;
148        switch(Direction) {
149        case Direction.West:
150        x = (Position.x + World.Width - 1) % World.Width;
151      y = Position.y;
152      return new Point(x, y);
153        case Direction.South:
154          x = Position.x;
155      y = (Position.y + World.Height + 1) % World.Height;
156      return new Point(x, y);
157        case Direction.East:
158          x = (Position.x + World.Width + 1) % World.Width;
159      y = Position.y;
160      return new Point(x, y);
161        case Direction.North:
162          x = Position.x;
163      y = (Position.y + World.Height - 1) % World.Height;
164      return new Point(x, y);
165      }
166    return new Point(-1,-1);
167    }
168  } 
169}
170
171World CreateWorld() {   
172  var w = new World();
173  Console.WriteLine("Total food: "+w.TotalFood);
174  return w;
175}
176
177World world;
178>>
179INIT <<
180  world = CreateWorld();
181>>
182
183NONTERMINALS
184  Ant<<World world, AntState state>>.
185  IfFoodAhead<<World world, AntState state>>.
186  Prog2<<World world, AntState state>>.
187  Prog3<<World world, AntState state>>.
188
189TERMINALS
190  Left<<>> CONSTRAINTS .
191  Right<<>> CONSTRAINTS .
192  Move<<>> CONSTRAINTS .
193 
194RULES
195  Ant<<World world, AntState state>> =
196    IfFoodAhead<<world, state>>
197    | Prog2<<world, state>>
198    | Prog3<<world, state>>
199    | Left<<>>                                         SEM << state.TurnLeft(); >>
200    | Right<<>>                                        SEM << state.TurnRight(); >>
201    | Move<<>>                                         SEM << state.MoveForward(); >>
202    .
203   
204  IfFoodAhead<<World world, AntState state>> =     
205                                                   SEM <<
206                                                      if(world.FoodAt(state.NextPosition)) {
207                                                   >>
208                                                   
209    Ant<<world, state>>                            SEM << } else { >>
210    Ant<<world, state>>                            SEM << } >>
211  .
212
213  Prog2<<World world, AntState state>> =
214    Ant<<world, state>> Ant<<world, state>>
215  .
216 
217  Prog3<<World world, AntState state>> =
218    Ant<<world, state>> Ant<<world, state>> Ant<<world, state>>
219  .
220
221
222MAXIMIZE                                                   /* could also use the keyword MINIMIZE here */
223  <<   
224    world.Reset();
225    var state = new AntState() {Position = new Point(0,0), MaxSteps=400, Direction=Direction.East, World = world};
226    while(state.Steps < state.MaxSteps) {
227      Ant(world, state);
228    }
229    return (double)state.FoodEaten;
230  >> 
231END ArtitificialAnt.
Note: See TracBrowser for help on using the repository browser.