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