Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.ArtificialAnt.Views/3.3/AntTrailView.cs @ 8581

Last change on this file since 8581 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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