public void drawHighScore() { drawMaze(G.maze); PathInMaze p = new PathInMaze(G.maze); Genome g = G.pop.pp[G.pop.highScoreIndex]; G.path = g.calcScore(G.maze); drawPathInMaze(G.path); }
public void drawPathInMaze(PathInMaze path, int offsetX, int offsetY, int lineWidth, bool showCount) { if (path.lengthOfpath == 0) return; System.Drawing.Graphics graphicsObj; graphicsObj = Graphics.FromImage(pictureBox1.Image); Pen myPen = new Pen(Color.DarkBlue, lineWidth); for (int i = 0; i < path.lengthOfpath-1; i++) { Point p1 = new Point(path.pathx[i] * path.mazz.drawSizeX + path.mazz.drawSizeX / path.divi, path.pathy[i] * path.mazz.drawSizeY + path.mazz.drawSizeY / path.divi); Point p2 = new Point(path.pathx[i + 1] * path.mazz.drawSizeX + path.mazz.drawSizeX / path.divi, path.pathy[i + 1] * path.mazz.drawSizeY + path.mazz.drawSizeY / path.divi); p1.X = p1.X + offsetX; p2.X = p2.X + offsetX; p1.Y = p1.Y + offsetY; p2.Y = p2.Y + offsetY; // draw one line graphicsObj.DrawLine(myPen,p1,p2); if (showCount) { // now draw overcount int x = path.pathx[i + 1]; int y = path.pathy[i + 1]; int cnt = path.countOnPath(x, y); if (cnt > 0) { string s = cnt.ToString(); Point p3 = new Point(); p3.X = p2.X - 5; p3.Y = p2.Y - 5; graphicsObj.DrawString(s, new Font("Courier New", 9), Brushes.DarkGoldenrod, p2); } } } //draw final place Point p1a = new Point(); Point p2a = new Point(path.pathx[path.lengthOfpath - 1] * path.mazz.drawSizeX + path.mazz.drawSizeX / path.divi, path.pathy[path.lengthOfpath-1] * path.mazz.drawSizeY + path.mazz.drawSizeY / path.divi); p2a.X = p2a.X + 3; p2a.Y = p2a.Y + 3; p1a.X = p2a.X - 6; p1a.Y = p2a.Y - 6; Pen zPen = new Pen(Color.Purple, 6); graphicsObj.DrawLine(zPen, p1a, p2a); pictureBox1.Refresh(); }
// quick way to draw lines public void drawPathInMaze(PathInMaze path) { drawPathInMaze(path, 2 , 2, 2, true); }
public PathInMaze calcScore(Maze m) { PathInMaze p = new PathInMaze(m); score = 500; // manhattan dis calc on move & end point if (G.fitFunc == "FitFunc1") { #region FitFunc1 if(firstrun2) manhattanDisPoi = calcDistance(p); manhattanDisMov = calcDistance(p); for (int i = 0; i < G.genomeLen; i++) { int mi = dna[i].gene; // 0= Move Right(x+) // 1= Move Down(y+) // 2= Move Left(x+) // 3= Move Up(y-) // 4= move forward // 5= turn right // 6= turn left // 7= about face (turn right twice) //if (mi == -1) break; // perhaps use -1 to indicate at end int rc = p.movement(mi); // resSucessEnd = Success got to end // resSucessTurn = move was successfull but did not move it was a turn command // resSucess = successfull and moved 1 square // resFailWall = fail hit a wall (did not move) // resFailOut = fail moved out of maze (did not move) // resFailOverPath = fail moved over previous path (but it did move) // resInvalid = not a valid action - fail int newmanhattanDisMov = calcDistance(p); if (rc == PathInMaze.resSucessEnd) { foundEnd = true; score = score * 50; manhattanDisMov = newmanhattanDisMov; break; } if (rc == PathInMaze.resFailOverPath) { score = score - 5; if (newmanhattanDisMov < manhattanDisMov) score -= 1; else if (newmanhattanDisMov > manhattanDisMov) score -= 2; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 1; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resSucess) { score = score + 2; if (newmanhattanDisMov < manhattanDisMov) score += 5; else if (newmanhattanDisMov > manhattanDisMov) score -= 5; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 1; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resFailWall) { score = score - 5; if (newmanhattanDisMov < manhattanDisMov) score -= 2; else if (newmanhattanDisMov > manhattanDisMov) score -= 2; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 5; if (i == 29) score -= 15; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resFailOut) { score = score - 2; if (newmanhattanDisMov < manhattanDisMov) score -= 2; else if (newmanhattanDisMov > manhattanDisMov) score -= 2; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 1; manhattanDisMov = newmanhattanDisMov; continue; } #region not used /*if (rc == PathInMaze.resSucessTurn) { score = score + 3; if (newmanhattanDisMov < manhattanDisMov) score += 20; else if (newmanhattanDisMov > manhattanDisMov || newmanhattanDisMov == manhattanDisMov) score -= 20; manhattanDisMov = newmanhattanDisMov; continue; #endregion if (rc == PathInMaze.resInvalid) { System.Windows.Forms.MessageBox.Show("Invalid Movement"); continue; } firstrun = false; } // manhattan dis calc on end point if (G.fitFunc == "FitFunc2") { #region FitFunc2 if (firstrun2) manhattanDisPoi = calcDistance(p); int prevmi = 0; for (int i = 0; i < G.genomeLen; i++) { int mi = dna[i].gene; prevmi = mi; // 0= Move Right(x+) // 1= Move Down(y+) // 2= Move Left(x+) // 3= Move Up(y-) // 4= move forward // 5= turn right // 6= turn left // 7= about face (turn right twice) //if (mi == -1) break; // perhaps use -1 to indicate at end int rc = p.movement(mi); // resSucessEnd = Success got to end // resSucessTurn = move was successfull but did not move it was a turn command // resSucess = successfull and moved 1 square // resFailWall = fail hit a wall (did not move) // resFailOut = fail moved out of maze (did not move) // resFailOverPath = fail moved over previous path (but it did move) // resInvalid = not a valid action - fail if (rc == PathInMaze.resSucessEnd) { foundEnd = true; score = score * 50; break; } if (rc == PathInMaze.resFailOverPath) { score = score - 50; continue; } if (rc == PathInMaze.resSucess) { score = score + 5; continue; } if (rc == PathInMaze.resFailWall) { score = score - 10; continue; } if (rc == PathInMaze.resFailOut) { score = score - 10; continue; } #region not used /*if (rc == PathInMaze.resSucessTurn) { score = score + 3; if (newmanhattanDisMov < manhattanDisMov) score += 20; else if (newmanhattanDisMov > manhattanDisMov || newmanhattanDisMov == manhattanDisMov) score -= 20; manhattanDisMov = newmanhattanDisMov; continue; }*/ #endregion if (rc == PathInMaze.resInvalid) { System.Windows.Forms.MessageBox.Show("Invalid Movement"); continue; } } int newmanhattanDisPoi = calcDistance(p); if (newmanhattanDisPoi < manhattanDisPoi) { int manhatdif = -newmanhattanDisPoi - -manhattanDisPoi; if (manhatdif > 4) { score += 100; } else score += 40; } else if (newmanhattanDisPoi > manhattanDisPoi) { score -= 40; } else if (newmanhattanDisPoi == manhattanDisPoi && !firstrun2) score -= 10; //System.Diagnostics.Debug.WriteLine("Prev: " + manhattanDisPoi.ToString() + "\n Cur: " + newmanhattanDisPoi.ToString() + "\n"); manhattanDisPoi = newmanhattanDisPoi; firstrun2 = false; #endregion } // manhattan dis calc on end point if (G.fitFunc == "FitFunc2") { #region FitFunc2 if (firstrun2) manhattanDisPoi = calcDistance(p); int prevmi = 0; for (int i = 0; i < G.genomeLen; i++) { int mi = dna[i].gene; prevmi = mi; // 0= Move Right(x+) // 1= Move Down(y+) // 2= Move Left(x+) // 3= Move Up(y-) // 4= move forward // 5= turn right // 6= turn left // 7= about face (turn right twice) //if (mi == -1) break; // perhaps use -1 to indicate at end int rc = p.movement(mi); // resSucessEnd = Success got to end // resSucessTurn = move was successfull but did not move it was a turn command // resSucess = successfull and moved 1 square // resFailWall = fail hit a wall (did not move) // resFailOut = fail moved out of maze (did not move) // resFailOverPath = fail moved over previous path (but it did move) // resInvalid = not a valid action - fail if (rc == PathInMaze.resSucessEnd) { foundEnd = true; score = score * 50; break; } if (rc == PathInMaze.resFailOverPath) { score = score - 5; continue; } if (rc == PathInMaze.resSucess) { score = score + 5; continue; } if (rc == PathInMaze.resFailWall) { score = score - 5; if (i == 29) score -= 15; continue; } if (rc == PathInMaze.resFailOut) { score = score - 2; continue; } #region not used /*if (rc == PathInMaze.resSucessTurn) { score = score + 3; if (newmanhattanDisMov < manhattanDisMov) score += 20; else if (newmanhattanDisMov > manhattanDisMov || newmanhattanDisMov == manhattanDisMov) score -= 20; manhattanDisMov = newmanhattanDisMov; continue; }*/ #endregion if (rc == PathInMaze.resInvalid) { System.Windows.Forms.MessageBox.Show("Invalid Movement"); continue; } } int newmanhattanDisPoi = calcDistance(p); if (newmanhattanDisPoi < manhattanDisPoi) { int manhatdif = -newmanhattanDisPoi - -manhattanDisPoi; if (manhatdif > 4) { score += 100; } else score += 40; } else if (newmanhattanDisPoi > manhattanDisPoi) score -= 40; else if (newmanhattanDisPoi == manhattanDisPoi && !firstrun2) score -= 15; //System.Diagnostics.Debug.WriteLine("Prev: " + manhattanDisPoi.ToString() + "\n Cur: " + newmanhattanDisPoi.ToString() + "\n"); manhattanDisPoi = newmanhattanDisPoi; firstrun2 = false; #endregion } // manhattan dis calc on move if (G.fitFunc == "FitFunc3") { #region FitFunc3 manhattanDisMov = calcDistance(p); int prevmi = 0; for (int i = 0; i < G.genomeLen; i++) { int mi = dna[i].gene; prevmi = mi; // 0= Move Right(x+) // 1= Move Down(y+) // 2= Move Left(x+) // 3= Move Up(y-) // 4= move forward // 5= turn right // 6= turn left // 7= about face (turn right twice) //if (mi == -1) break; // perhaps use -1 to indicate at end int rc = p.movement(mi); // resSucessEnd = Success got to end // resSucessTurn = move was successfull but did not move it was a turn command // resSucess = successfull and moved 1 square // resFailWall = fail hit a wall (did not move) // resFailOut = fail moved out of maze (did not move) // resFailOverPath = fail moved over previous path (but it did move) // resInvalid = not a valid action - fail int newmanhattanDisMov = calcDistance(p); if (rc == PathInMaze.resSucessEnd) { foundEnd = true; score = score * 50; //manhattanDisMov = newmanhattanDisMov; break; } if (rc == PathInMaze.resFailOverPath) { score = score - 50; if (newmanhattanDisMov < manhattanDisMov) score += 10; else if (newmanhattanDisMov > manhattanDisMov) score -= 20; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 10; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resSucess) { score = score + 30; if (newmanhattanDisMov < manhattanDisMov) score += 50; else if (newmanhattanDisMov > manhattanDisMov) score -= 50; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 15; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resFailWall) { score = score - 20; if (newmanhattanDisMov < manhattanDisMov) score += 5; else if (newmanhattanDisMov > manhattanDisMov) score -= 20; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 30; manhattanDisMov = newmanhattanDisMov; continue; } if (rc == PathInMaze.resFailOut) { score = score - 15; if (newmanhattanDisMov < manhattanDisMov) score -= 10; else if (newmanhattanDisMov > manhattanDisMov) score -= 20; else if (newmanhattanDisMov == manhattanDisMov && !firstrun) score -= 15; manhattanDisMov = newmanhattanDisMov; continue; } #region not used /*if (rc == PathInMaze.resSucessTurn) { score = score + 3; if (newmanhattanDisMov < manhattanDisMov) score += 20; else if (newmanhattanDisMov > manhattanDisMov || newmanhattanDisMov == manhattanDisMov) score -= 20; manhattanDisMov = newmanhattanDisMov; continue; }*/ #endregion if (rc == PathInMaze.resInvalid) { System.Windows.Forms.MessageBox.Show("Invalid Movement"); continue; } firstrun = false; } #endregion } return p; }
private int calcDistance(PathInMaze p) { int dx = Math.Abs(p.curX - p.mazz.endPosx); int dy = Math.Abs(p.curY - p.mazz.endPosy); return (dx + dy); }