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