Changeset 3239 for trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views
- Timestamp:
- 03/31/10 14:26:45 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views
- Files:
-
- 7 added
- 4 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views/3.3/AntTrailView.Designer.cs
r3238 r3239 20 20 #endregion 21 21 22 namespace HeuristicLab.Problems.ArtificialAnt {22 namespace HeuristicLab.Problems.ArtificialAnt.Views { 23 23 partial class AntTrailView { 24 24 /// <summary> … … 45 45 /// </summary> 46 46 private void InitializeComponent() { 47 this.components = new System.ComponentModel.Container(); 47 48 this.pictureBox = new System.Windows.Forms.PictureBox(); 49 this.playButton = new System.Windows.Forms.Button(); 50 this.animationTimer = new System.Windows.Forms.Timer(this.components); 48 51 ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit(); 49 52 this.SuspendLayout(); … … 58 61 this.pictureBox.Location = new System.Drawing.Point(0, 0); 59 62 this.pictureBox.Name = "pictureBox"; 60 this.pictureBox.Size = new System.Drawing.Size(392, 310);63 this.pictureBox.Size = new System.Drawing.Size(392, 275); 61 64 this.pictureBox.TabIndex = 0; 62 65 this.pictureBox.TabStop = false; 63 66 this.pictureBox.SizeChanged += new System.EventHandler(this.pictureBox_SizeChanged); 64 67 // 65 // PathTSPTourView 68 // playButton 69 // 70 this.playButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 71 this.playButton.Location = new System.Drawing.Point(4, 281); 72 this.playButton.Name = "playButton"; 73 this.playButton.Size = new System.Drawing.Size(75, 23); 74 this.playButton.TabIndex = 1; 75 this.playButton.Text = "Play"; 76 this.playButton.UseVisualStyleBackColor = true; 77 this.playButton.Click += new System.EventHandler(this.playButton_Click); 78 // 79 // animationTimer 80 // 81 this.animationTimer.Interval = 15; 82 this.animationTimer.Tick += new System.EventHandler(this.animationTimer_Tick); 83 // 84 // AntTrailView 66 85 // 67 86 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 68 87 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 88 this.Controls.Add(this.playButton); 69 89 this.Controls.Add(this.pictureBox); 70 this.Name = " PathTSPTourView";90 this.Name = "AntTrailView"; 71 91 this.Size = new System.Drawing.Size(392, 310); 92 this.EnabledChanged += new System.EventHandler(this.AntTrailView_EnabledChanged); 72 93 ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit(); 73 94 this.ResumeLayout(false); … … 78 99 79 100 private System.Windows.Forms.PictureBox pictureBox; 101 private System.Windows.Forms.Button playButton; 102 private System.Windows.Forms.Timer animationTimer; 80 103 81 104 -
trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views/3.3/AntTrailView.cs
r3238 r3239 29 29 using System.Collections.Generic; 30 30 31 namespace HeuristicLab.Problems.ArtificialAnt {31 namespace HeuristicLab.Problems.ArtificialAnt.Views { 32 32 [View("AntTrail View")] 33 33 [Content(typeof(AntTrail), true)] 34 34 public sealed partial class AntTrailView : ItemView { 35 private const int N_FOOD_ITEMS = 89;36 private const int WORLD_WIDTH = 32;37 private const int WORLD_HEIGHT = 32;38 private const int FOOD = 1;39 private const int EMPTY = 0;40 private int[] SANTA_FE_TRAIL = new int[] {41 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,42 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,43 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,44 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,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, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,66 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,67 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,68 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,69 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,70 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,71 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,73 };74 75 private const int MAX_TIME_STEPS = 600;76 77 private int[] trail;78 private int currentLocation;79 private int currentDirection;80 Stack<SymbolicExpressionTreeNode> nodeStack;81 82 35 public new AntTrail Content { 83 36 get { return (AntTrail)base.Content; } … … 87 40 public AntTrailView() { 88 41 InitializeComponent(); 89 ResetTrailInterpreter();90 42 } 91 43 … … 115 67 } 116 68 117 private void ResetTrailInterpreter() {118 nodeStack = new Stack<SymbolicExpressionTreeNode>();119 currentLocation = 0;120 currentDirection = 0;121 trail = new int[WORLD_HEIGHT * WORLD_WIDTH];122 Array.Copy(SANTA_FE_TRAIL, trail, SANTA_FE_TRAIL.Length);123 }124 125 126 69 private void GenerateImage() { 70 playButton.Enabled = this.Enabled; 71 animationTimer.Stop(); 127 72 if ((pictureBox.Width > 0) && (pictureBox.Height > 0)) { 128 73 if (Content == null) { 129 74 pictureBox.Image = null; 75 playButton.Enabled = false; 130 76 } else { 131 ResetTrailInterpreter(); 77 var nodeStack = new Stack<SymbolicExpressionTreeNode>(); 78 int rows = Content.World.Rows; 79 int columns = Content.World.Columns; 132 80 SymbolicExpressionTree expression = Content.SymbolicExpressionTree; 133 81 134 Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); 135 using (Graphics graphics = Graphics.FromImage(bitmap)) { 136 int cellHeight = pictureBox.Height / WORLD_HEIGHT; 137 int cellWidth = pictureBox.Width / WORLD_WIDTH; 138 // draw world 139 for (int i = 0; i < WORLD_HEIGHT; i++) { 140 graphics.DrawLine(Pens.Black, 0, i * cellHeight, pictureBox.Width, i * cellHeight); 82 DrawWorld(); 83 using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) { 84 float cellHeight = pictureBox.Height / (float)rows; 85 float cellWidth = pictureBox.Width / (float)columns; 86 87 AntInterpreter interpreter = new AntInterpreter(); 88 interpreter.MaxTimeSteps = Content.MaxTimeSteps.Value; 89 interpreter.Expression = Content.SymbolicExpressionTree; 90 interpreter.World = Content.World; 91 int currentAntLocationColumn; 92 int currentAntLocationRow; 93 // draw initial ant 94 interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn); 95 DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight); 96 // interpret ant code and draw trail 97 while (interpreter.ElapsedTime < interpreter.MaxTimeSteps) { 98 interpreter.Step(); 99 interpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn); 100 DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, interpreter.AntDirection, cellWidth, cellHeight); 141 101 } 142 for (int j = 0; j < WORLD_WIDTH; j++) { 143 graphics.DrawLine(Pens.Black, j * cellWidth, 0, j * cellWidth, pictureBox.Height); 144 } 145 for (int i = 0; i < WORLD_HEIGHT; i++) { 146 for (int j = 0; j < WORLD_WIDTH; j++) { 147 if (trail[i * WORLD_HEIGHT + j] == FOOD) graphics.FillEllipse(Brushes.LightBlue, j * cellWidth, i * cellHeight, cellWidth, cellHeight); 148 } 149 } 150 // step ant and draw trail 151 nodeStack.Clear(); 152 int step = 0; 153 while (step < MAX_TIME_STEPS) { 154 // expression evaluated completly => start at root again 155 if (nodeStack.Count == 0) 156 nodeStack.Push(expression.Root.SubTrees[0]); 157 158 var currentNode = nodeStack.Pop(); 159 if (currentNode.Symbol is Left) { 160 currentDirection = (currentDirection + 3) % 4; 161 step++; 162 } else if (currentNode.Symbol is Right) { 163 currentDirection = (currentDirection + 1) % 4; 164 step++; 165 } else if (currentNode.Symbol is Move) { 166 currentLocation = NextField(); 167 trail[currentLocation] = EMPTY; 168 float currentCellX = currentLocation % WORLD_WIDTH; 169 float currentCellY = currentLocation / WORLD_HEIGHT; 170 graphics.FillRectangle(Brushes.Brown, 171 currentCellX * cellWidth + cellWidth * 0.25f, currentCellY * cellHeight + cellHeight * 0.25f, 172 cellWidth * 0.5f, cellHeight * 0.5f); 173 step++; 174 } else if (currentNode.Symbol is IfFoodAhead) { 175 if (trail[NextField()] == FOOD) { 176 nodeStack.Push(currentNode.SubTrees[0]); 177 } else { 178 nodeStack.Push(currentNode.SubTrees[1]); 179 } 180 } else if (currentNode.Symbol is Prog2) { 181 nodeStack.Push(currentNode.SubTrees[1]); 182 nodeStack.Push(currentNode.SubTrees[0]); 183 } else if (currentNode.Symbol is Prog3) { 184 nodeStack.Push(currentNode.SubTrees[2]); 185 nodeStack.Push(currentNode.SubTrees[1]); 186 nodeStack.Push(currentNode.SubTrees[0]); 187 } else { 188 throw new InvalidOperationException(currentNode.Symbol.ToString()); 189 } 190 191 192 } 193 } 194 pictureBox.Image = bitmap; 195 } 196 } 197 } 198 199 private int NextField() { 200 int currentLocationX = currentLocation % WORLD_WIDTH; 201 int currentLocationY = currentLocation / WORLD_HEIGHT; 202 203 switch (currentDirection) { 204 case 0: 205 currentLocationX = (currentLocationX + 1) % WORLD_WIDTH; // EAST 206 break; 207 case 1: 208 currentLocationY = (currentLocationY + 1) % WORLD_HEIGHT; // SOUTH 209 break; 210 case 2: 211 currentLocationX = (currentLocationX + WORLD_WIDTH - 1) % WORLD_WIDTH; // WEST 212 break; 213 case 3: 214 currentLocationY = (currentLocationY + WORLD_HEIGHT - 1) % WORLD_HEIGHT; // NORTH 215 break; 216 default: 217 throw new InvalidOperationException(); 218 } 219 return currentLocationY * WORLD_WIDTH + currentLocationX; 102 } 103 pictureBox.Refresh(); 104 } 105 } 106 } 107 108 private void DrawWorld() { 109 int rows = Content.World.Rows; 110 int columns = Content.World.Columns; 111 Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); 112 using (Graphics graphics = Graphics.FromImage(bitmap)) { 113 float cellHeight = pictureBox.Height / (float)rows; 114 float cellWidth = pictureBox.Width / (float)columns; 115 // draw world 116 for (int i = 0; i < rows; i++) { 117 graphics.DrawLine(Pens.Black, 0, i * cellHeight, pictureBox.Width, i * cellHeight); 118 } 119 for (int j = 0; j < columns; j++) { 120 graphics.DrawLine(Pens.Black, j * cellWidth, 0, j * cellWidth, pictureBox.Height); 121 } 122 for (int i = 0; i < rows; i++) { 123 for (int j = 0; j < columns; j++) { 124 if (Content.World[i, j]) 125 graphics.FillEllipse(Brushes.LightBlue, j * cellWidth, i * cellHeight, cellWidth, cellHeight); 126 } 127 } 128 pictureBox.Image = bitmap; 129 } 130 } 131 132 private void DrawAnt(Graphics g, int row, int column, int direction, float cellWidth, float cellHeight) { 133 //g.FillRectangle(Brushes.White, column * cellWidth, row * cellHeight, 134 // cellWidth, cellHeight); 135 // draw ant body 136 g.FillRectangle(Brushes.Brown, 137 column * cellWidth + cellWidth * 0.25f, row * cellHeight + cellHeight * 0.25f, 138 cellWidth * 0.5f, cellHeight * 0.5f); 139 // show ant direction 140 float centerX = column * cellWidth + cellWidth * 0.5f; 141 float centerY = row * cellHeight + cellHeight * 0.5f; 142 float directionX = centerX; 143 float directionY = centerY; 144 switch (direction) { 145 case 0: { // EAST 146 directionX = centerX + cellWidth * 0.5f; 147 break; 148 } 149 case 1: { // SOUTH 150 directionY = directionY + cellHeight * 0.5f; 151 break; 152 } 153 case 2: { // WEST 154 directionX = centerX - cellWidth * 0.5f; 155 break; 156 } 157 case 3: { // NORTH 158 directionY = directionY - cellHeight * 0.5f; 159 break; 160 } 161 default: throw new InvalidOperationException(); 162 } 163 g.DrawLine(Pens.Brown, centerX, centerY, directionX, directionY); 220 164 } 221 165 … … 230 174 GenerateImage(); 231 175 } 176 177 #region animation 178 private AntInterpreter animationInterpreter; 179 private void playButton_Click(object sender, EventArgs e) { 180 playButton.Enabled = false; 181 int rows = Content.World.Rows; 182 int columns = Content.World.Columns; 183 SymbolicExpressionTree expression = Content.SymbolicExpressionTree; 184 var nodeStack = new Stack<SymbolicExpressionTreeNode>(); 185 186 animationInterpreter = new AntInterpreter(); 187 animationInterpreter.MaxTimeSteps = Content.MaxTimeSteps.Value; 188 animationInterpreter.Expression = Content.SymbolicExpressionTree; 189 animationInterpreter.World = Content.World; 190 191 DrawWorld(); 192 using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) { 193 float cellHeight = pictureBox.Height / (float)Content.World.Rows; 194 float cellWidth = pictureBox.Width / (float)Content.World.Columns; 195 // draw initial ant 196 int currentAntLocationColumn; 197 int currentAntLocationRow; 198 animationInterpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn); 199 DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, animationInterpreter.AntDirection, cellWidth, cellHeight); 200 pictureBox.Refresh(); 201 } 202 203 animationTimer.Start(); 204 } 205 206 private void animationTimer_Tick(object sender, EventArgs e) { 207 using (Graphics graphics = Graphics.FromImage(pictureBox.Image)) { 208 float cellHeight = pictureBox.Height / (float)Content.World.Rows; 209 float cellWidth = pictureBox.Width / (float)Content.World.Columns; 210 int currentAntLocationColumn; 211 int currentAntLocationRow; 212 // interpret ant code and draw trail 213 animationInterpreter.Step(); 214 animationInterpreter.AntLocation(out currentAntLocationRow, out currentAntLocationColumn); 215 DrawAnt(graphics, currentAntLocationRow, currentAntLocationColumn, animationInterpreter.AntDirection, cellWidth, cellHeight); 216 pictureBox.Refresh(); 217 if (animationInterpreter.ElapsedTime < animationInterpreter.MaxTimeSteps) { 218 animationTimer.Start(); 219 } else { 220 animationTimer.Stop(); 221 playButton.Enabled = this.Enabled; 222 } 223 } 224 } 225 #endregion 226 227 private void AntTrailView_EnabledChanged(object sender, EventArgs e) { 228 if (this.Enabled) playButton.Enabled = true; 229 else playButton.Enabled = false; 230 } 232 231 } 233 232 } -
trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views/3.3/ArtificialAntProblemView.Designer.cs
r3238 r3239 1 namespace HeuristicLab.Problems.ArtificialAnt {1 namespace HeuristicLab.Problems.ArtificialAnt.Views { 2 2 partial class ArtificialAntProblemView { 3 3 /// <summary> -
trunk/sources/HeuristicLab.Problems.ArtificialAnt.Views/3.3/ArtificialAntProblemView.cs
r3238 r3239 9 9 using HeuristicLab.MainForm; 10 10 11 namespace HeuristicLab.Problems.ArtificialAnt {11 namespace HeuristicLab.Problems.ArtificialAnt.Views { 12 12 [View("ArtificialAntProblem View")] 13 13 [Content(typeof(ArtificialAntProblem), true)]
Note: See TracChangeset
for help on using the changeset viewer.