Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/Examples/Artificial Ant.txt @ 17834

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

#2026 minor corrections in GPDL example files

File size: 8.2 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    for(int row = 0; row<world.Length;row++)
89      for(int col = 0; col < world[row].Length; col++) {
90        world[row][col] = LosAltosTrail[row][col];
91      }
92  }
93 
94  public bool FoodAt(Point p) {
95    if(p.x < 0 || p.y < 0) return false;
96    if(world.Length <= p.y) return false;
97    if(world[p.y].Length <= p.x) return false;
98    return world[p.y][p.x] == '#';
99  }
100  public void RemoveFoodAt(Point p) {
101    if(world.Length < p.y) return;
102    if(world[p.y].Length < p.x) return;
103    world[p.y][p.x] = ' ';
104  }
105}
106
107enum Direction {West, East, North, South};
108
109class AntState {
110  public Point Position { get; set;}
111  public int MaxSteps { get; set;}
112  public Direction Direction { get; set; }
113  public World World { get; set; }
114  public int Steps { get; set; }
115  public int FoodEaten { get; set; }
116 
117  public AntState() {
118    Steps = 0;   
119    FoodEaten = 0;
120  }
121 
122  public void TurnLeft() {
123    if(Steps++ > MaxSteps) return;
124    switch(Direction) {
125      case Direction.West: Direction = Direction.South; break;
126      case Direction.South: Direction = Direction.East; break;
127      case Direction.East: Direction = Direction.North; break;
128      case Direction.North: Direction = Direction.West; break;
129    }
130  }
131  public void TurnRight() {
132    if(Steps++ > MaxSteps) return;
133    switch(Direction) {
134      case Direction.West: Direction = Direction.North; break;
135      case Direction.South: Direction = Direction.West; break;
136      case Direction.East: Direction = Direction.South; break;
137      case Direction.North: Direction = Direction.East; break;
138    }
139  }
140  public void MoveForward() {
141    if(Steps++ > MaxSteps) return;
142    Position = NextPosition;
143    if(World.FoodAt(Position)) {
144        World.RemoveFoodAt(Position);
145        FoodEaten++;
146    }
147  }
148  public Point NextPosition {
149    get {
150        int x, y;
151        switch(Direction) {
152          case Direction.West:
153            x = (Position.x + World.Width - 1) % World.Width;
154            y = Position.y;
155            return new Point(x, y);
156          case Direction.South:
157              x = Position.x;
158            y = (Position.y + World.Height + 1) % World.Height;
159            return new Point(x, y);
160          case Direction.East:
161              x = (Position.x + World.Width + 1) % World.Width;
162            y = Position.y;
163            return new Point(x, y);
164          case Direction.North:
165              x = Position.x;
166            y = (Position.y + World.Height - 1) % World.Height;
167            return new Point(x, y);
168        }
169         return new Point(-1,-1);
170    }
171  } 
172}
173
174World CreateWorld() {   
175  var w = new World();
176  Console.WriteLine("Total food: "+w.TotalFood);
177  return w;
178}
179
180World world;
181>>
182INIT <<
183  world = CreateWorld();
184>>
185
186NONTERMINALS
187  Ant<<World world, AntState state>>.
188  IfFoodAhead<<World world, AntState state>>.
189  Prog2<<World world, AntState state>>.
190  Prog3<<World world, AntState state>>.
191
192TERMINALS
193  Left. Right. Move.
194 
195RULES
196  Ant<<World world, AntState state>> =
197    IfFoodAhead<<world, state>>
198    | Prog2<<world, state>>
199    | Prog3<<world, state>>
200    | Left                                             SEM << state.TurnLeft(); >>
201    | Right                                            SEM << state.TurnRight(); >>
202    | Move                                             SEM << state.MoveForward(); >>
203    .
204   
205  IfFoodAhead<<World world, AntState state>> =     
206                                                   SEM <<
207                                                      if(world.FoodAt(state.NextPosition)) {
208                                                   >>
209                                                   
210    Ant<<world, state>>                            SEM << } else { >>
211    Ant<<world, state>>                            SEM << } >>
212  .
213
214  Prog2<<World world, AntState state>> =
215    Ant<<world, state>> Ant<<world, state>>
216  .
217 
218  Prog3<<World world, AntState state>> =
219    Ant<<world, state>> Ant<<world, state>> Ant<<world, state>>
220  .
221
222
223MAXIMIZE                                                   /* could also use the keyword MINIMIZE here */
224  <<   
225    world.Reset();
226    var state = new AntState() {Position = new Point(0,0), MaxSteps=400, Direction=Direction.East, World = world};
227    while(state.Steps < state.MaxSteps) {
228      Ant(world, state);
229    }
230    return (double)state.FoodEaten;
231  >> 
232END ArtificialAnt.
Note: See TracBrowser for help on using the repository browser.