Example #1
0
        public void adicionaVizinhos(Labirinto l, Estado atual, Estado final, Agente agent)
        {
            Boolean achouVizinhoAberta  = false;
            Boolean achouVizinhoFechada = false;
            Estado  auxiliar            = new Estado();

            for (int x = agent.posX - 1; x <= agent.posX + 1; x++)
            {
                for (int y = agent.posY - 1; y <= agent.posY + 1; y++)
                {
                    if (x > 0 && x < l.linhas && y > 0 && y < l.colunas && l.m[x, y] != "X" && (x != agent.posX || y != agent.posY))
                    {
                        foreach (Estado e in Aberta) // verifica se já não esta na lista aberta
                        {
                            if (e.posX == x && e.posY == y)
                            {
                                achouVizinhoAberta = true;
                                auxiliar           = e;
                                break;
                            }
                        }
                        if (Fechada.Contains(auxiliar))
                        {
                            break;
                        }
                        if (!achouVizinhoAberta) // Nao achou vizinho na lista Aberta
                        {
                            Estado estate = new Estado();
                            estate.posX = x;
                            estate.posY = y;
                            estate.pai  = atual;
                            estate.g    = distEuclidiana(estate, atual) + atual.g;
                            estate.h    = distEuclidiana(estate, final);
                            estate.f    = estate.g + estate.h;
                            Aberta.Add(estate);
                        }
                        else
                        {
                            int k           = distEuclidiana(auxiliar, atual);
                            int melhorCusto = k + atual.g;

                            if (melhorCusto < auxiliar.g)
                            {
                                auxiliar.g  += melhorCusto;
                                auxiliar.f   = auxiliar.g + auxiliar.h;
                                auxiliar.pai = atual;
                            }
                        }
                    }
                    achouVizinhoAberta  = false;
                    achouVizinhoFechada = false;
                }
            }
        }
Example #2
0
 public void ir(Labirinto l, Estado atual, Estado antigo, int Animacao)
 {
     if (Animacao == 1)
     {
         l.m[atual.posX, atual.posY]   = "A";
         l.m[antigo.posX, antigo.posY] = cont.ToString();
     }
     this.posX = atual.posX;
     this.posY = atual.posY;
     cont++;
 }
Example #3
0
        public void Pathfind(Agente agent, Estado inicial, Estado final, Labirinto maze)
        {
            Console.WriteLine("Você quer ver a animação? 0 pra não E 1 pra sim");
            mostrarAnimacao = Convert.ToInt32(Console.ReadLine());
            if (mostrarAnimacao == 1)
            {
                maze.mostraLabirinto();
            }

            inicial.h   = distEuclidiana(inicial, final);
            inicial.g   = 0;
            inicial.f   = inicial.g + inicial.h;
            inicial.pai = null;
            Estado atual;
            Estado antigo;
            Estado auxiliar;

            atual  = inicial;
            antigo = atual;

            while (atual.posX != final.posX || atual.posY != final.posY)
            {
                Console.Clear();


                agent.ir(maze, atual, antigo, mostrarAnimacao);
                if (mostrarAnimacao == 1)
                {
                    maze.mostraLabirinto();
                    System.Threading.Thread.Sleep(150);
                    Console.Clear();
                }
                if (atual.posX == final.posX && atual.posY == final.posY)
                {
                    break;
                }
                Vizinhos = olhaVizinhos(maze, atual, final, agent);
                Vizinhos.Sort((x, y) => x.f.CompareTo(y.f)); // sort

                auxiliar = Vizinhos[0];
                if (auxiliar.f <= atual.f)
                {
                    antigo = atual; // pega a posição anterior
                    atual  = Vizinhos[0];
                }
                Vizinhos.Clear();
            }
            maze.mostraCaminho(atual, maze);
            //Console.Clear();
            maze.mostraLabirinto();
        }
Example #4
0
 public Boolean entraLabirinto(Labirinto l, int x, int y)
 {
     if (l.m[x, y] == "X" || x <0 || x> l.linhas || y <0 || y> l.colunas)
     {
         Console.WriteLine("Posição fora do labirinto\n");
         return(false);
     }
     else
     {
         posX      = x;
         posY      = y;
         l.m[x, y] = "A";
         return(true);
     }
 }
Example #5
0
 public int chegou(Labirinto l)
 {
     if (posX + 1 >= l.linhas || posY + 1 >= l.colunas)
     {
         return(0);
     }
     else if (l.m[posX + 1, posY] == "$" || l.m[posX - 1, posY] == "$" || l.m[posX, posY + 1] == "$" || l.m[posX, posY - 1] == "$" ||
              l.m[posX + 1, posY + 1] == "$" || l.m[posX + 1, posY - 1] == "$" || l.m[posX - 1, posY - 1] == "$" || l.m[posX - 1, posY + 1] == "$") // diagonais
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }
Example #6
0
        public void mostraCaminho(Estado state, Labirinto maze)
        {
            List <Estado> Invertida;

            Invertida = new List <Estado>();
            Estado atual, ondeEstou;

            while (state.pai != null)
            {
                Invertida.Add(state);
                //m[state.posX, state.posY] = "@";
                state = state.pai;
            }
            Invertida.Add(state);
            //m[state.posX, state.posY] = "@";
            Invertida.Reverse();
            while (Invertida.Any())
            {
                Console.WriteLine("Deseja andar ou ver posição? 0 - Andar  1 - Ver Posição");
                acao = Convert.ToInt32(Console.ReadLine());
                if (acao == 0)
                {
                    Console.Clear();
                    atual = Invertida[0];
                    Invertida.RemoveAt(0);
                    if (Invertida.Any())
                    {
                        ondeEstou = Invertida[0];
                    }
                    else
                    {
                        break;
                    }
                    m[ondeEstou.posX, ondeEstou.posY] = "A";
                    m[atual.posX, atual.posY]         = "@";
                    maze.mostraLabirinto();
                }
                else
                {
                    Console.Clear();
                    atual = Invertida[0];
                    m[atual.posX, atual.posY] = "A";
                    Console.WriteLine("Posição: X: {0}, Y: {1}", atual.posX, atual.posY);
                    maze.mostraLabirinto();
                }
            }
        }
Example #7
0
        public void Pathfind(Agente agent, Estado inicial, Estado final, Labirinto maze)
        {
            Console.WriteLine("Você quer ver a animação? 0 pra não E 1 pra sim");
            mostrarAnimacao = Convert.ToInt32(Console.ReadLine());
            if (mostrarAnimacao == 1)
            {
                maze.mostraLabirinto();
            }

            inicial.h   = distEuclidiana(inicial, final);
            inicial.g   = 0;
            inicial.pai = null;
            Aberta.Add(inicial);
            Estado atual;
            Estado antigo;

            atual = inicial;

            while (Aberta.Any())
            {
                Console.Clear();
                Aberta.Sort((x, y) => x.f.CompareTo(y.f)); // sort
                antigo = atual;
                atual  = Aberta[0];
                Aberta.RemoveAt(0);
                Fechada.Add(atual);
                agent.ir(maze, atual, antigo, mostrarAnimacao);
                if (mostrarAnimacao == 1)
                {
                    maze.mostraLabirinto();
                    System.Threading.Thread.Sleep(150);
                    Console.Clear();
                }
                if (atual.posX == final.posX && atual.posY == final.posY)
                {
                    break;
                }
                adicionaVizinhos(maze, atual, final, agent);
            }
            maze.mostraCaminho(atual, maze);
            //Console.Clear();
            maze.mostraLabirinto();
        }
Example #8
0
        public List <Estado> olhaVizinhos(Labirinto l, Estado atual, Estado final, Agente agent)
        {
            List <Estado> vizinhos;

            vizinhos = new List <Estado>();
            for (int x = agent.posX - 1; x <= agent.posX + 1; x++)
            {
                for (int y = agent.posY - 1; y <= agent.posY + 1; y++)
                {
                    if (x > 0 && x < l.linhas && y > 0 && y < l.colunas && l.m[x, y] != "X" && (x != agent.posX || y != agent.posY))
                    {
                        Estado estate = new Estado();
                        estate.posX = x;
                        estate.posY = y;
                        estate.pai  = atual;
                        estate.g    = distEuclidiana(estate, atual);
                        estate.h    = distEuclidiana(estate, final);
                        estate.f    = estate.g + estate.h;
                        vizinhos.Add(estate);
                    }
                }
            }
            return(vizinhos);
        }
Example #9
0
        static void Main(string[] args)
        {
            int          linhas, colunas, x1, y1, x2, y2, posxChegada, posyChegada, posxAgente, posyAgente, obstaculo, algoritmo;
            bool         fechou1      = false;
            bool         fechou2      = false;
            Agente       agent        = new Agente();
            Estado       estadoFinal  = new Estado();
            Estado       inicial      = new Estado();
            AEstrela     aestrela     = new AEstrela();
            HillClimbing hillclimbing = new HillClimbing();

            Console.WriteLine("Numero de Linhas: ");
            linhas = Convert.ToInt32(Console.ReadLine()) + 1;
            Console.WriteLine("Numero de Colunas: ");
            colunas = Convert.ToInt32(Console.ReadLine()) + 1;

            Labirinto maze = new Labirinto(linhas, colunas);

            maze.preencheLabirinto();

            while (!fechou1)
            {
                Console.Clear();
                maze.mostraLabirinto();
                Console.WriteLine("\nDeseja inserir um obstáculo na horizontal ou na vertical? horizontal(1) vertical(0)\n");
                Console.WriteLine("\n(1) Horizontal\n");
                Console.WriteLine("\n(2) Vertical\n");
                obstaculo = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("\nInsira as coordenadas do obstaculo, uma de cada vez, no formato (x1,y1), (x2,y2)\n");
                Console.WriteLine("X1: ");
                x1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Y1: ");
                y1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("X2: ");
                x2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Y2: ");
                y2 = Convert.ToInt32(Console.ReadLine());

                if (obstaculo == 1)
                {
                    if (maze.geraObstatuloHoriz(x1, y1, x2, y2) == 1)
                    {
                        Console.Clear();
                        maze.mostraLabirinto();
                        Console.WriteLine("Deseja inserir outro obstaculo? sim(1) não(0)\n");
                        if (Convert.ToInt32(Console.ReadLine()) == 0)
                        {
                            fechou1 = true;
                        }
                    }
                }
                else if (obstaculo == 2)
                {
                    if (maze.geraObstatuloVertical(x1, y1, x2, y2) == 1)
                    {
                        Console.Clear();
                        maze.mostraLabirinto();
                        Console.WriteLine("Deseja inserir outro obstaculo? sim(1) não(0)\n");
                        if (Convert.ToInt32(Console.ReadLine()) == 0)
                        {
                            fechou1 = true;
                        }
                    }
                }
            }
            Console.WriteLine();

            while (!fechou2)
            {
                Console.WriteLine("Posição de chegada (x,y): ");
                Console.WriteLine("PosX: ");
                posxChegada = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("PosY: ");
                posyChegada = Convert.ToInt32(Console.ReadLine());


                Console.WriteLine("Posição do Agente (x,y): ");
                Console.WriteLine("PosX: ");
                posxAgente = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("PosY: ");
                posyAgente = Convert.ToInt32(Console.ReadLine());

                if (maze.posicionaChegada(posxChegada, posyChegada) && agent.entraLabirinto(maze, posxAgente, posyAgente))
                {
                    inicial.posX = posxAgente;
                    inicial.posY = posyAgente;
                    inicial.f    = 0;
                    inicial.g    = 0;
                    inicial.pai  = null;

                    estadoFinal.posX = posxChegada;
                    estadoFinal.posY = posyChegada;
                    estadoFinal.g    = 0;
                    estadoFinal.f    = 0;
                    estadoFinal.pai  = null;

                    fechou2 = true;
                }
            }

            maze.mostraLabirinto();
            Console.Clear();
            Console.WriteLine("Qual algoritmo irá rodar? 0 - A* 1 - Hill Climbing ");
            algoritmo = Convert.ToInt32(Console.ReadLine());
            if (algoritmo == 0)
            {
                aestrela.Pathfind(agent, inicial, estadoFinal, maze);
            }
            else if (algoritmo == 1)
            {
                hillclimbing.Pathfind(agent, inicial, estadoFinal, maze);
            }

            /*bool achou = false;
             * int k;
             *
             * while (!achou)
             * {
             *  k = agent.deliberar(maze);
             *  if (k == 0)
             *  {
             *      achou = true;
             *
             *  }
             *  else
             *  {
             *      System.Threading.Thread.Sleep(150);
             *      Console.Clear();
             *  }
             *
             * }*/
            System.Threading.Thread.Sleep(200);
            Console.WriteLine("\n Achou\n");
            System.Threading.Thread.Sleep(10000);
        }