Exemplo n.º 1
0
        void SortBasedDistance()
        {
            int tracker = 0;

            while (tracker <= nodes.Count)
            {
                for (int i = 0; i < nodeCount - 1; i++)
                {
                    DNANode temp = nodes[i];

                    float comparative1 = nodes[i].functionalCost;
                    float comparative2 = nodes[i + 1].functionalCost;

                    if (comparative1 > comparative2)
                    {
                        nodes[i]     = nodes[i + 1];
                        nodes[i + 1] = temp;
                        tracker      = 0;
                    }
                    else
                    {
                        tracker++;
                    }
                }
            }

            //nodes.Reverse();
            nodes[0].fittest = true;
        }
Exemplo n.º 2
0
        void GenerateInitialPaths(DNANode n)
        {
            int totalMoves = 0;

            while (!(n.x <= 0 || n.y <= 0 || n.x >= boardSize - 1 || n.y >= boardSize - 1))
            {
                if (n.isStuck)
                {
                    break;
                }

                n.AddNewPath(rand.Next(availableMoves));
                totalMoves++;

                //Obstacle Logic
                foreach (Obstacle ob in obstacles)
                {
                    for (int k = 0; k < nodes.Count; k++)
                    {
                        if (!nodes[k].isStuck)
                        {
                            if (NeighboringObstacle(nodes[k], ob))
                            {
                                nodes[k].isStuck = true;
                            }
                        }
                    }
                }

                //this.Invalidate();
                //this.Update();
                //this.Refresh();

                //System.Threading.Thread.Sleep(20);

                //if (totalMoves >= boardSize * 5)
                //    break;
            }

            nodes[0].fittest = false;

            for (int i = 0; i < nodeCount; i++)
            {
                nodes[i].distance       = Distance(nodes[i].x, objX, nodes[i].y, objY);
                nodes[i].functionalCost = nodes[i].distance / nodes[i].posPath.Count;
                nodes[i].isStuck        = false;
            }
        }
Exemplo n.º 3
0
        bool NeighboringObstacle(DNANode node, Obstacle obst)
        {
            if (obst != null)
            {
                for (int x = -1; x <= 1; x += 1)
                {
                    for (int y = -1; y <= 1; y += 1)
                    {
                        if (obst.x == x + node.x && obst.y == y + node.y)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemplo n.º 4
0
        void SelectParentsActivateBreed()
        {
            SortBasedDistance();

            List <DNANode> newNodes = new List <DNANode>();

            for (int i = 0; i < nodeCount; i++)
            {
                newNodes.Add(Breed(nodes[0], nodes[rand.Next((nodes.Count / 3) - 1)]));

                nodes[i].x = resetX;
                nodes[i].y = resetY;
            }

            //nodes.Clear();
            //nodes = newNodes;

            //this.Invalidate();
            //this.Update();
            //this.Refresh();

            //MessageBox.Show(nodes.Count.ToString());

            for (int i = 0; i < nodes.Count; i++)
            {
                DNANode n         = nodes[i];
                int     pathCount = newNodes[i].path.Count;
                nodes[i].path.Clear();
                nodes[i].posPath.Clear();

                //MessageBox.Show("Node " + i);
                //MessageBox.Show(pathCount.ToString());
                //MessageBox.Show(nodes[i].x + ", " + nodes[i].y);

                for (int p = 0; p < pathCount; p++)
                {
                    //MessageBox.Show(nodes[i].x + "," + nodes[i].y);

                    //this.Invalidate();
                    //this.Update();
                    //this.Refresh();

                    if (!(n.x <= 0 || n.y <= 0 || n.x >= boardSize - 1 || n.y >= boardSize - 1) &&
                        !nodes[i].isStuck)
                    {
                        //MessageBox.Show(newNodes[i].path[p].ToString());
                        nodes[i].AddNewPath(newNodes[i].path[p]);

                        //Obstacle Logic
                        foreach (Obstacle ob in obstacles)
                        {
                            for (int k = 0; k < nodes.Count; k++)
                            {
                                if (!nodes[k].isStuck)
                                {
                                    if (NeighboringObstacle(nodes[k], ob))
                                    {
                                        nodes[k].isStuck = true;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                this.Invalidate();
                this.Update();
                this.Refresh();
            }

            for (int i = 0; i < nodes.Count; i++)
            {
                nodes[i].distance       = Distance(nodes[i].x, objX, nodes[i].y, objY);
                nodes[i].functionalCost = nodes[i].distance / nodes[i].posPath.Count;
                nodes[i].isStuck        = false;
            }
        }
Exemplo n.º 5
0
        DNANode Breed(DNANode p1, DNANode p2)
        {
            List <int> newPath = new List <int>();

            for (int i = 0; i < p1.path.Count / 2; i++)
            {
                newPath.Add(p1.path[i]);
            }

            for (int i = p2.path.Count / 2; i < p2.path.Count; i++)
            {
                newPath.Add(p2.path[i]);
            }

            /*
             * Potential Breeding methodology *
             */
            //List<int> newPath = new List<int>();
            //int pathSize = 0;

            //DNANode shortest;
            //DNANode longest;

            //if (p1.path.Count > p2.path.Count)
            //{
            //    shortest = p2;
            //    longest = p1;
            //}
            //else
            //{
            //    shortest = p1;
            //    longest = p2;
            //}

            //pathSize = shortest.path.Count;

            //for(int i = 0; i < pathSize; i++)
            //{
            //    if (i % 2 == 0)
            //    {
            //        newPath.Add(shortest.path[i]);
            //    }
            //    else
            //        newPath.Add(longest.path[i]);
            //}

            //for(int i = pathSize; i < longest.path.Count; i++)
            //{
            //    newPath.Add(longest.path[i]);
            //}


            //List<int> newPath2 = new List<int>();

            //for (int i = 0; i < p2.path.Count / 2; i++)
            //{
            //    newPath2.Add(p1.path[i]);
            //}

            //for (int i = p1.path.Count / 2; i < p1.path.Count; i++)
            //{
            //    newPath2.Add(p2.path[i]);
            //}

            //List<int> newPath = new List<int>();

            //for(int i = 0; i < newPath1.Count; i++)
            //{
            //    newPath.Add((newPath1[i] + newPath2[i]) / 2);
            //}


            //Mutation
            if (generation % 2 == 0 && rand.Next(100) >= 25)
            {
                if (newPath.Count > 0)
                {
                    if (rand.Next(2) == 1)
                    {
                        newPath[rand.Next(newPath.Count - 1)] = rand.Next(availableMoves);
                    }
                    else
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            newPath.Add(rand.Next(availableMoves));
                        }
                    }
                }
                mutations++;
            }

            DNANode child = new DNANode(resetX, resetY, p1.brushColor);

            child.path = newPath;

            return(child);
        }