1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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.Drawing;
|
---|
24 | using System.Windows.Forms;
|
---|
25 | using HeuristicLab.Core.Views;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.MainForm;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using System.Collections.Generic;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.ArtificialAnt.Views {
|
---|
32 | [View("AntTrail View")]
|
---|
33 | [Content(typeof(AntTrail), true)]
|
---|
34 | public sealed partial class AntTrailView : ItemView {
|
---|
35 | public new AntTrail Content {
|
---|
36 | get { return (AntTrail)base.Content; }
|
---|
37 | set { base.Content = value; }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public AntTrailView() {
|
---|
41 | InitializeComponent();
|
---|
42 | }
|
---|
43 |
|
---|
44 | public AntTrailView(AntTrail content)
|
---|
45 | : this() {
|
---|
46 | Content = content;
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override void DeregisterContentEvents() {
|
---|
50 | Content.SymbolicExpressionTreeChanged -= new EventHandler(Content_SymbolicExpressionTreeChanged);
|
---|
51 | base.DeregisterContentEvents();
|
---|
52 | }
|
---|
53 | protected override void RegisterContentEvents() {
|
---|
54 | base.RegisterContentEvents();
|
---|
55 | Content.SymbolicExpressionTreeChanged += new EventHandler(Content_SymbolicExpressionTreeChanged);
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override void OnContentChanged() {
|
---|
59 | base.OnContentChanged();
|
---|
60 | if (Content == null) {
|
---|
61 | pictureBox.Image = null;
|
---|
62 | } else {
|
---|
63 | GenerateImage();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected override void OnLockedChanged() {
|
---|
68 | this.playButton.Enabled = !Locked;
|
---|
69 | base.OnLockedChanged();
|
---|
70 | }
|
---|
71 |
|
---|
72 | private void GenerateImage() {
|
---|
73 | animationTimer.Stop();
|
---|
74 | if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) {
|
---|
75 | if (Content == null) {
|
---|
76 | pictureBox.Image = null;
|
---|
77 | playButton.Enabled = false;
|
---|
78 | } else {
|
---|
79 | var nodeStack = new Stack<SymbolicExpressionTreeNode>();
|
---|
80 | int rows = Content.World.Rows;
|
---|
81 | int columns = Content.World.Columns;
|
---|
82 | SymbolicExpressionTree expression = Content.SymbolicExpressionTree;
|
---|
83 |
|
---|
84 | DrawWorld();
|
---|
85 | using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
|
---|
86 | float cellHeight = pictureBox.Height / (float)rows;
|
---|
87 | float cellWidth = pictureBox.Width / (float)columns;
|
---|
88 |
|
---|
89 | AntInterpreter interpreter = new AntInterpreter();
|
---|
90 | interpreter.MaxTimeSteps = Content.MaxTimeSteps.Value;
|
---|
91 | interpreter.Expression = Content.SymbolicExpressionTree;
|
---|
92 | interpreter.World = Content.World;
|
---|
93 | int currentAntLocationColumn;
|
---|
94 | int currentAntLocationRow;
|
---|
95 | // draw initial ant
|
---|
96 | interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
|
---|
97 | DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight);
|
---|
98 | // interpret ant code and draw trail
|
---|
99 | while (interpreter.ElapsedTime < interpreter.MaxTimeSteps) {
|
---|
100 | interpreter.Step();
|
---|
101 | interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
|
---|
102 | DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | pictureBox.Refresh();
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void DrawWorld() {
|
---|
111 | int rows = Content.World.Rows;
|
---|
112 | int columns = Content.World.Columns;
|
---|
113 | Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
|
---|
114 | using (Graphics graphics = Graphics.FromImage(bitmap)) {
|
---|
115 | float cellHeight = pictureBox.Height / (float)rows;
|
---|
116 | float cellWidth = pictureBox.Width / (float)columns;
|
---|
117 | // draw world
|
---|
118 | for (int i = 0; i < rows; i++) {
|
---|
119 | graphics.DrawLine(Pens.Black, 0, i * cellHeight, pictureBox.Width, i * cellHeight);
|
---|
120 | }
|
---|
121 | for (int j = 0; j < columns; j++) {
|
---|
122 | graphics.DrawLine(Pens.Black, j * cellWidth, 0, j * cellWidth, pictureBox.Height);
|
---|
123 | }
|
---|
124 | for (int i = 0; i < rows; i++) {
|
---|
125 | for (int j = 0; j < columns; j++) {
|
---|
126 | if (Content.World[i, j])
|
---|
127 | graphics.FillEllipse(Brushes.LightBlue, j * cellWidth, i * cellHeight, cellWidth, cellHeight);
|
---|
128 | }
|
---|
129 | }
|
---|
130 | pictureBox.Image = bitmap;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | private void DrawAnt(Graphics g, int row, int column, int direction, float cellWidth, float cellHeight) {
|
---|
135 | //g.FillRectangle(Brushes.White, column * cellWidth, row * cellHeight,
|
---|
136 | // cellWidth, cellHeight);
|
---|
137 | // draw ant body
|
---|
138 | g.FillRectangle(Brushes.Brown,
|
---|
139 | column * cellWidth + cellWidth * 0.25f, row * cellHeight + cellHeight * 0.25f,
|
---|
140 | cellWidth * 0.5f, cellHeight * 0.5f);
|
---|
141 | // show ant direction
|
---|
142 | float centerX = column * cellWidth + cellWidth * 0.5f;
|
---|
143 | float centerY = row * cellHeight + cellHeight * 0.5f;
|
---|
144 | float directionX = centerX;
|
---|
145 | float directionY = centerY;
|
---|
146 | switch (direction) {
|
---|
147 | case 0: { // EAST
|
---|
148 | directionX = centerX + cellWidth * 0.5f;
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | case 1: { // SOUTH
|
---|
152 | directionY = directionY + cellHeight * 0.5f;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | case 2: { // WEST
|
---|
156 | directionX = centerX - cellWidth * 0.5f;
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | case 3: { // NORTH
|
---|
160 | directionY = directionY - cellHeight * 0.5f;
|
---|
161 | break;
|
---|
162 | }
|
---|
163 | default: throw new InvalidOperationException();
|
---|
164 | }
|
---|
165 | g.DrawLine(Pens.Brown, centerX, centerY, directionX, directionY);
|
---|
166 | }
|
---|
167 |
|
---|
168 | void Content_SymbolicExpressionTreeChanged(object sender, EventArgs e) {
|
---|
169 | if (InvokeRequired)
|
---|
170 | Invoke(new EventHandler(Content_SymbolicExpressionTreeChanged), sender, e);
|
---|
171 | else
|
---|
172 | GenerateImage();
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void pictureBox_SizeChanged(object sender, EventArgs e) {
|
---|
176 | GenerateImage();
|
---|
177 | }
|
---|
178 |
|
---|
179 | #region animation
|
---|
180 | private AntInterpreter animationInterpreter;
|
---|
181 | private void playButton_Click(object sender, EventArgs e) {
|
---|
182 | playButton.Enabled = false;
|
---|
183 | int rows = Content.World.Rows;
|
---|
184 | int columns = Content.World.Columns;
|
---|
185 | SymbolicExpressionTree expression = Content.SymbolicExpressionTree;
|
---|
186 | var nodeStack = new Stack<SymbolicExpressionTreeNode>();
|
---|
187 |
|
---|
188 | animationInterpreter = new AntInterpreter();
|
---|
189 | animationInterpreter.MaxTimeSteps = Content.MaxTimeSteps.Value;
|
---|
190 | animationInterpreter.Expression = Content.SymbolicExpressionTree;
|
---|
191 | animationInterpreter.World = Content.World;
|
---|
192 |
|
---|
193 | DrawWorld();
|
---|
194 | using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
|
---|
195 | float cellHeight = pictureBox.Height / (float)Content.World.Rows;
|
---|
196 | float cellWidth = pictureBox.Width / (float)Content.World.Columns;
|
---|
197 | // draw initial ant
|
---|
198 | int currentAntLocationColumn;
|
---|
199 | int currentAntLocationRow;
|
---|
200 | animationInterpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
|
---|
201 | DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, animationInterpreter.AntDirection, cellWidth, cellHeight);
|
---|
202 | pictureBox.Refresh();
|
---|
203 | }
|
---|
204 |
|
---|
205 | animationTimer.Start();
|
---|
206 | }
|
---|
207 |
|
---|
208 | private void animationTimer_Tick(object sender, EventArgs e) {
|
---|
209 | using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) {
|
---|
210 | float cellHeight = pictureBox.Height / (float)Content.World.Rows;
|
---|
211 | float cellWidth = pictureBox.Width / (float)Content.World.Columns;
|
---|
212 | int currentAntLocationColumn;
|
---|
213 | int currentAntLocationRow;
|
---|
214 | // interpret ant code and draw trail
|
---|
215 | animationInterpreter.Step();
|
---|
216 | animationInterpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn);
|
---|
217 | DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, animationInterpreter.AntDirection, cellWidth, cellHeight);
|
---|
218 | pictureBox.Refresh();
|
---|
219 | if (animationInterpreter.ElapsedTime < animationInterpreter.MaxTimeSteps) {
|
---|
220 | animationTimer.Start();
|
---|
221 | } else {
|
---|
222 | animationTimer.Stop();
|
---|
223 | playButton.Enabled = true;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 | #endregion
|
---|
228 | }
|
---|
229 | }
|
---|