Example #1
0
        public void drawMaze(Maze maz)
        {
            System.Drawing.Graphics graphicsObj;
            graphicsObj = Graphics.FromImage(pictureBox1.Image);

            // first clear the picture box
            Brush myBrush = new SolidBrush(System.Drawing.Color.Black);
            Rectangle myRectangle = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
            graphicsObj.FillRectangle(myBrush, myRectangle);

            // now draw the maze
            Brush myBrush2 = new SolidBrush(System.Drawing.Color.Black);
            Rectangle myRectangle2 = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height);
            for (int y=0; y<maz.ySize; y++)
                for (int x = 0; x < maz.xSize; x++)
                {
                    System.Drawing.Color c = maz.getColor(x, y);
                    ((SolidBrush)myBrush2).Color = c;
                    myRectangle2.X = x * maz.drawSizeX;
                    myRectangle2.Y = y * maz.drawSizeY;
                    myRectangle2.Width = maz.drawSizeX;
                    myRectangle2.Height = maz.drawSizeY;
                    graphicsObj.FillRectangle(myBrush2, myRectangle2);
                }
            pictureBox1.Refresh();
        }
Example #2
0
        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;
        }
Example #3
0
 public PathInMaze(Maze m)
 {
     mazz = m;
     maxLengthOfpath = mazz.xSize * mazz.ySize; // that should be enough room
     pathx = new int[maxLengthOfpath]; // that should be enough room
     pathy = new int[maxLengthOfpath]; // that should be enough room
     curDirection = 0;
     curX = mazz.startPosx;
     curY = mazz.startPosy;
     lengthOfpath = 1;
     pathx[0] = curX;
     pathy[0] = curY;
 }