1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.MainForm;
|
---|
29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Views;
|
---|
32 | using HeuristicLab.Optimizer;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.GPDL.Views {
|
---|
35 | internal class CreateProblemMenuItem : HeuristicLab.MainForm.WindowsForms.MenuItem, IOptimizerUserInterfaceItemProvider {
|
---|
36 | private const string gpdlString = @"
|
---|
37 | PROBLEM ArtificialAnt
|
---|
38 |
|
---|
39 | CODE <<
|
---|
40 | public struct Point {
|
---|
41 | public int x; public int y;
|
---|
42 | public Point(int x, int y) {
|
---|
43 | this.x = x;
|
---|
44 | this.y = y;
|
---|
45 | }
|
---|
46 | };
|
---|
47 |
|
---|
48 | class World {
|
---|
49 |
|
---|
50 | private static string[] LosAltosTrail = new string[] {
|
---|
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 | "" . # # "",
|
---|
83 | "" .####.. # "",
|
---|
84 | "" #.# "",
|
---|
85 | "" # "",
|
---|
86 | "" # "",
|
---|
87 | "" # "",
|
---|
88 | "" # "",
|
---|
89 | "" # "",
|
---|
90 | "" # "",
|
---|
91 | "" # "",
|
---|
92 | "" ###### "",
|
---|
93 | "" . "",
|
---|
94 | "" # "",
|
---|
95 | "" # "",
|
---|
96 | "" # "",
|
---|
97 | "" # "",
|
---|
98 | "" # "",
|
---|
99 | "" # "",
|
---|
100 | "" #.. "",
|
---|
101 | "" # "",
|
---|
102 | "" # "",
|
---|
103 | "" # "",
|
---|
104 | "" # "",
|
---|
105 | "" # "",
|
---|
106 | "" .###### "",
|
---|
107 | "" . "",
|
---|
108 | "" # "",
|
---|
109 | "" # "",
|
---|
110 | "" # "",
|
---|
111 | "" # "",
|
---|
112 | "" # "",
|
---|
113 | "" # "",
|
---|
114 | "" # "",
|
---|
115 | "" # "",
|
---|
116 | "" # ""
|
---|
117 | };
|
---|
118 | private char[][] world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
|
---|
119 | public int TotalFood = LosAltosTrail.Select(l=>l.Where(c=>c=='#').Count()).Sum();
|
---|
120 | public int Width { get { return 100; } }
|
---|
121 | public int Height { get { return 100; } }
|
---|
122 |
|
---|
123 | public void Reset() {
|
---|
124 | world = LosAltosTrail.Select(l=>l.ToArray()).ToArray();
|
---|
125 | }
|
---|
126 |
|
---|
127 | public bool FoodAt(Point p) {
|
---|
128 | if(p.x < 0 || p.y < 0) return false;
|
---|
129 | if(world.Length <= p.y) return false;
|
---|
130 | if(world[p.y].Length <= p.x) return false;
|
---|
131 | return world[p.y][p.x] == '#';
|
---|
132 | }
|
---|
133 | public void RemoveFoodAt(Point p) {
|
---|
134 | if(world.Length < p.y) return;
|
---|
135 | if(world[p.y].Length < p.x) return;
|
---|
136 | world[p.y][p.x] = ' ';
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | enum Direction {West, East, North, South};
|
---|
141 |
|
---|
142 | class AntState {
|
---|
143 | public Point Position { get; set;}
|
---|
144 | public int MaxSteps { get; set;}
|
---|
145 | public Direction Direction { get; set; }
|
---|
146 | public World World { get; set; }
|
---|
147 | public int Steps { get; set; }
|
---|
148 | public int FoodEaten { get; set; }
|
---|
149 |
|
---|
150 | public AntState() {
|
---|
151 | Steps = 0;
|
---|
152 | FoodEaten = 0;
|
---|
153 | }
|
---|
154 |
|
---|
155 | public void TurnLeft() {
|
---|
156 | Steps++;
|
---|
157 | switch(Direction) {
|
---|
158 | case Direction.West: Direction = Direction.South; break;
|
---|
159 | case Direction.South: Direction = Direction.East; break;
|
---|
160 | case Direction.East: Direction = Direction.North; break;
|
---|
161 | case Direction.North: Direction = Direction.West; break;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | public void TurnRight() {
|
---|
165 | Steps++;
|
---|
166 | switch(Direction) {
|
---|
167 | case Direction.West: Direction = Direction.North; break;
|
---|
168 | case Direction.South: Direction = Direction.West; break;
|
---|
169 | case Direction.East: Direction = Direction.South; break;
|
---|
170 | case Direction.North: Direction = Direction.East; break;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | public void MoveForward() {
|
---|
174 | Position = NextPosition;
|
---|
175 | Steps++;
|
---|
176 | if(World.FoodAt(Position)) {
|
---|
177 | World.RemoveFoodAt(Position);
|
---|
178 | FoodEaten++;
|
---|
179 | }
|
---|
180 | }
|
---|
181 | public Point NextPosition {
|
---|
182 | get {
|
---|
183 | int x, y;
|
---|
184 | switch(Direction) {
|
---|
185 | case Direction.West:
|
---|
186 | x = (Position.x + World.Width - 1) % World.Width;
|
---|
187 | y = Position.y;
|
---|
188 | return new Point(x, y);
|
---|
189 | case Direction.South:
|
---|
190 | x = Position.x;
|
---|
191 | y = (Position.y + World.Height + 1) % World.Height;
|
---|
192 | return new Point(x, y);
|
---|
193 | case Direction.East:
|
---|
194 | x = (Position.x + World.Width + 1) % World.Width;
|
---|
195 | y = Position.y;
|
---|
196 | return new Point(x, y);
|
---|
197 | case Direction.North:
|
---|
198 | x = Position.x;
|
---|
199 | y = (Position.y + World.Height - 1) % World.Height;
|
---|
200 | return new Point(x, y);
|
---|
201 | }
|
---|
202 | return new Point(-1,-1);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | World CreateWorld() {
|
---|
208 | var w = new World();
|
---|
209 | Console.WriteLine(""Total food: ""+w.TotalFood);
|
---|
210 | return w;
|
---|
211 | }
|
---|
212 |
|
---|
213 | World world;
|
---|
214 | >>
|
---|
215 | INIT <<
|
---|
216 | world = CreateWorld();
|
---|
217 | >>
|
---|
218 |
|
---|
219 | NONTERMINALS
|
---|
220 | Ant<<World world, AntState state>>.
|
---|
221 | IfFoodAhead<<World world, AntState state>>.
|
---|
222 | Prog2<<World world, AntState state>>.
|
---|
223 | Prog3<<World world, AntState state>>.
|
---|
224 |
|
---|
225 | TERMINALS
|
---|
226 | Left<<>> CONSTRAINTS .
|
---|
227 | Right<<>> CONSTRAINTS .
|
---|
228 | Move<<>> CONSTRAINTS .
|
---|
229 |
|
---|
230 | RULES
|
---|
231 | Ant<<World world, AntState state>> =
|
---|
232 | IfFoodAhead<<world, state>>
|
---|
233 | | Prog2<<world, state>>
|
---|
234 | | Prog3<<world, state>>
|
---|
235 | | Left<<>> SEM << state.TurnLeft(); >>
|
---|
236 | | Right<<>> SEM << state.TurnRight(); >>
|
---|
237 | | Move<<>> SEM << state.MoveForward(); >>
|
---|
238 | .
|
---|
239 |
|
---|
240 | IfFoodAhead<<World world, AntState state>> =
|
---|
241 | SEM <<
|
---|
242 | if(world.FoodAt(state.NextPosition)) {
|
---|
243 | >>
|
---|
244 |
|
---|
245 | Ant<<world, state>> SEM << } else { >>
|
---|
246 | Ant<<world, state>> SEM << } >>
|
---|
247 | .
|
---|
248 |
|
---|
249 | Prog2<<World world, AntState state>> =
|
---|
250 | Ant<<world, state>> Ant<<world, state>>
|
---|
251 | .
|
---|
252 |
|
---|
253 | Prog3<<World world, AntState state>> =
|
---|
254 | Ant<<world, state>> Ant<<world, state>> Ant<<world, state>>
|
---|
255 | .
|
---|
256 |
|
---|
257 |
|
---|
258 | MAXIMIZE /* could also use the keyword MINIMIZE here */
|
---|
259 | <<
|
---|
260 | world.Reset();
|
---|
261 | var state = new AntState() {Position = new Point(0,0), MaxSteps=400, Direction=Direction.East, World = world};
|
---|
262 | while(state.Steps < state.MaxSteps) {
|
---|
263 | Ant(world, state);
|
---|
264 | }
|
---|
265 | return (double)state.FoodEaten;
|
---|
266 | >>
|
---|
267 | END ArtitificialAnt.
|
---|
268 | ";
|
---|
269 |
|
---|
270 |
|
---|
271 | public override string Name {
|
---|
272 | get { return "Create &Problem"; }
|
---|
273 | }
|
---|
274 | public override IEnumerable<string> Structure {
|
---|
275 | get { return new string[] { "&Edit" }; }
|
---|
276 | }
|
---|
277 | public override int Position {
|
---|
278 | get { return 3000; }
|
---|
279 | }
|
---|
280 | public override string ToolTipText {
|
---|
281 | get { return "Create a genetic programming problem from a GPDL definition."; }
|
---|
282 | }
|
---|
283 |
|
---|
284 | protected override void OnToolStripItemSet(EventArgs e) {
|
---|
285 | ToolStripItem.Enabled = true;
|
---|
286 | }
|
---|
287 | protected override void OnActiveViewChanged(object sender, EventArgs e) {
|
---|
288 | ToolStripItem.Enabled = true;
|
---|
289 | }
|
---|
290 | protected override void OnViewChanged(object sender, EventArgs e) {
|
---|
291 | base.OnViewChanged(sender, e);
|
---|
292 | ToolStripItem.Enabled = true;
|
---|
293 | }
|
---|
294 |
|
---|
295 | public override void Execute() {
|
---|
296 | Utils.InstallModule("Utils", new Utils.ModuleMethodDelegate(Utils.UtilsMethod));
|
---|
297 | Utils.InstallModule("Sets", new Utils.ModuleMethodDelegate(Sets.SetsMethod));
|
---|
298 | Utils.InstallModule("Errors", new Utils.ModuleMethodDelegate(Errors.ErrorsMethod));
|
---|
299 |
|
---|
300 | Utils.InstallModule("GPDefLex", new Utils.ModuleMethodDelegate(GPDefLex.GPDefLexMethod));
|
---|
301 | Utils.InstallModule("GPDefSem", new Utils.ModuleMethodDelegate(GPDefSem.GPDefSemMethod));
|
---|
302 | Utils.InstallModule("GPDefSyn", new Utils.ModuleMethodDelegate(GPDefSyn.GPDefSynMethod));
|
---|
303 |
|
---|
304 | // --- initialize modules ---
|
---|
305 | Utils.Modules(Utils.ModuleAction.initModule);
|
---|
306 |
|
---|
307 | Errors.PushAbortMethod(new Errors.AbortMethod(Abort));
|
---|
308 |
|
---|
309 | using (var src = new StringReader(gpdlString)) {
|
---|
310 | GPDefLex.src = src;
|
---|
311 | GPDefSyn.Parse();
|
---|
312 | }
|
---|
313 |
|
---|
314 | GPDefLex.InitLex();
|
---|
315 | GPDefSyn.Interpret();
|
---|
316 | MainFormManager.MainForm.ShowContent(GPDefSem.problem);
|
---|
317 | }
|
---|
318 |
|
---|
319 | private void Abort(string abortKind, string moduleName, string methodName, string descr) {
|
---|
320 |
|
---|
321 |
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|