def busca_em_profundidade_limitada(self, problema, limite): borda = [] #Lista de Nos Nos = No() Nos._init_(problema.estado_inicial, None, 0, 0) #No inicial borda.insert(0, Nos) #Inserindo while (True): if (borda == []): print("Não foi atingido neste limite") return 0, False #Boolean usado na busca interativa Auxiliar.matrizprint(self, borda[0].estado) if (problema.teste_objetivo(borda[0], "cega") == True): #Atingir o objetivo print("Profundidade Total:", borda[0].profundidade) return Auxiliar.caminhos( self, borda[0]), True #Boolean usado na busca interativa if (borda[0].profundidade < limite ): #So ira adicionar novos filhos caso chegue no limite filhos = Auxiliar.expande(self, borda[0], problema, "cega") #Expandir random.shuffle(filhos) #Bagunçar a lista de filhos borda.pop(0) #remover da borda for acoes in range(len(filhos)): borda.insert(0, filhos[acoes]) else: borda.pop(0)
def busca_em_profundidade_com_lista_de_visitados(self, problema): borda = [] #Lista de Nos visitado = [] Nos = No() Nos._init_(problema.estado_inicial, None, 0, 0) #No inicial borda.insert(0, Nos) #Inserindo while (True): Auxiliar.matrizprint(self, borda[0].estado) if (borda == []): print("Profundidade Total:", borda[0].profundidade) return Auxiliar.caminhos(self, borda[0]) if (problema.teste_objetivo(borda[0], "cega") == True): #Atingir o objetivo print("Profundidade Total:", borda[0].profundidade) return Auxiliar.caminhos(self, borda[0]) while (True): #Fica em um laço eliminado os visitados if not borda[0] in visitado: visitado.append(borda[0]) break borda.pop(0) filhos = Auxiliar.expande(self, borda[0], problema, "cega") #Expandir random.shuffle(filhos) #Bagunçar a lista de filhos borda.pop(0) for acoes in range(len(filhos)): borda.insert(0, filhos[acoes]) #Inserir no começo
def Hill_Climbing(self): #Subida da encosta matriz = [["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'Q'", "'x'", "'x'", "'x'", "'x'"], ["'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'", "'x'", "'x'"], ["'x'", "'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'", "'Q'"], ["'x'", "'x'", "'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"]] # matriz = problema.estado_inicial # Gerar matriz automatica # for c in range(len(matriz)): # n= randrange(0,7) # matriz[n][c] = "'Q'" Auxiliar.Batidas = Auxiliar.conflitos(self, matriz) Nos = No() Nos._init_(matriz, None, Auxiliar.gerarCusto(self, Auxiliar.Batidas, matriz), 0) #No inicial for i in range(1000): if (Nos.custo <= 0): break Nos = Auxiliar.expandelocal(self, Nos) Auxiliar.matrizprint(self, Nos.estado) print("Profundidade Total:", Nos.profundidade) return Auxiliar.caminhos(self, Nos)
def expande(self, lista): matriz = copy.deepcopy(lista) nofilho = No() nofilho._init_( matriz, None, Auxiliar.gerarCusto(self, Auxiliar.conflitos(self, matriz), matriz), None) return nofilho
def busca_em_profundidade(self, problema): borda = [] #Lista de Nos Nos = No() Nos._init_(problema.estado_inicial, None, 0, 0) #No inicial borda.insert(0, Nos) #Inserindo while (True): Auxiliar.matrizprint(self, borda[0].estado) if (problema.teste_objetivo(borda[0], "cega") == True): #Atingir o objetivo print("Profundidade Total:", borda[0].profundidade) return Auxiliar.caminhos(self, borda[0]) filhos = Auxiliar.expande(self, borda[0], problema, "cega") #Expandir random.shuffle(filhos) #Bagunçar a lista de filhos borda.pop(0) #remover da borda for acoes in range(len(filhos)): borda.insert(0, filhos[acoes]) #Inserir no começo
def busca_com_custo_uniforme(self, problema): borda = [] #Lista de Nos Nos = No() Nos._init_(problema.estado_inicial, None, 0, 0) #No inicial borda.insert(0, Nos) #Inserindo while (True): Auxiliar.matrizprint(self, borda[0].estado) if (problema.teste_objetivo(borda[0], "cega") == True): #Atingir o objetivo print("Profundidade Total:", borda[0].profundidade) return Auxiliar.caminhos(self, borda[0]) filhos = Auxiliar.expande(self, borda[0], problema, "cega") #Expandir borda.sort(key=attrgetter("custo_do_caminho")) borda.pop(0) for acoes in range(len(filhos)): borda.append(filhos[acoes]) #Inserir no final
def Simulated_Annealing(self, T, interacoes, pulos, coef): #Recozimento Simulado matriz = [["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"], ["'x'", "'x'", "'x'", "'Q'", "'x'", "'x'", "'x'", "'x'"], ["'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'", "'x'", "'x'"], ["'x'", "'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'", "'Q'"], ["'x'", "'x'", "'Q'", "'x'", "'x'", "'x'", "'Q'", "'x'"], ["'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'", "'x'"]] # matriz = problema.estado_inicial # Gerar matriz automatica # for c in range(len(matriz)): # n= randrange(0,7) # matriz[n][c] = "'Q'" Auxiliar.Batidas = Auxiliar.conflitos(self, matriz) Nos = No() Nos._init_(matriz, None, Auxiliar.gerarCusto(self, Auxiliar.Batidas, matriz), 0) #No inicial Auxiliar.matrizprint(self, Nos.estado) for i in range(1000): nSucesso = 0 for i in range(interacoes): if (len(Auxiliar.visitados) == 56): break Proximo = Auxiliar.Peturbar(self, Nos) Delta = Proximo.custo - Nos.custo if (Delta <= 0 or numpy.exp(-Delta / T) > 1): Nos = Proximo Auxiliar.visitados = [] nSucesso += 1 if (nSucesso >= pulos): break T = T * coef if (nSucesso == 0 or Nos.custo <= 0 or T < 1): break Auxiliar.matrizprint(self, Nos.estado) print("Profundidade Total:", Nos.profundidade) print("Custo:", Nos.custo) return Nos