Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views/3.3/AntTrailView.cs @ 3566

Last change on this file since 3566 was 3566, checked in by mkommend, 14 years ago

removed ctors with contents in all views (ticket #972)

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