Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.ArtificialAnt.Views/3.5/AntTrailView.cs @ 13278

Last change on this file since 13278 was 12895, checked in by gkronber, 9 years ago

#2421 new version of artificial ant problem that uses SymbolicExpressionTreeEncoding

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