Ejemplo n.º 1
0
class SimulatedAnnealing(object):
    # maxDisc -> Numero maximo de perturbacoes na temperatura
    # maxSuc -> Numero maximo de sucessos por iteracao
    # alpha -> fator de reducao da temperatura
    def __init__(self, iterate, maxDis, maxSuc, alpha, startTemp, **kwargs):
        self.iterate = iterate
        self.maxDis = maxDis
        self.maxSuc = maxSuc
        self.alpha = alpha
        self.startState = file.read()
        self.neighbor = Neighbor(self.startState)
        self.startTemp = startTemp
        self.state_update = None
        if 'state_update' in kwargs:
            self.state_update = True

    def simulate(self):
        i = 0
        success = sys.maxsize
        currentState = self.startState
        t = self.startTemp

        solutions = []

        while not (success == 0) and i < self.iterate:
            j = 0
            success = 0
            while success <= self.maxSuc and j < self.maxDis:
                f1 = Heuristic(currentState).attacks()
                newState = self.neighbor.generateState()

                if Heuristic(newState).attacks() == 0:
                    if not Heuristic(newState).queensPosition() in solutions:
                        solutions.append(Heuristic(newState).queensPosition())

                if self.state_update:
                    print Heuristic(currentState).queensPosition(
                    ), " -> ", Heuristic(newState).queensPosition()

                f2 = Heuristic(newState).attacks()
                deltaF = f2 - f1
                if not t == 0.0:
                    if (deltaF <= 0) or (exp(-deltaF / t) > random.random()):
                        currentState = newState
                        success += 1

                j += 1
                self.neighbor = Neighbor(currentState)

            t = self.alpha * t
            i += 1

        file.write(Heuristic(currentState).queensPosition(),
                   self.neighbor.createBoard(),
                   url='./resource/newBoard.txt')
        print "Contagem final de sucessos : ", success
        print "Temperatura final : ", t
        print "Numero de iteracoes : ", i
        print "Posicao Inicial das ", len(
            self.startState), " rainhas : ", Heuristic(
                self.startState).queensPosition()
        print "Posicao Final das ", len(
            self.startState), " rainhas : ", Heuristic(
                currentState).queensPosition()
        print "\tNumero de rainhas atacando : ", Heuristic(
            currentState).attacks()

        print "Solucoes encontradas: "

        for solution in solutions:
            print solution

        return Heuristic(currentState).attacks()
Ejemplo n.º 2
0
class HillClimbing(object):
    def __init__(self, iterate, **kwargs):
        self.iterate = iterate
        self.startState = file.read()
        self.neighbor = Neighbor(self.startState)
        self.update_states = None
        if "update_states" in kwargs:
            self.update_states = True

    def hill(self):
        currentState = self.startState

        nextEval = Heuristic(currentState).attacks()

        i = 0
        while i < self.iterate and nextEval != 0:
            newState = self.neighbor.generateState()
            currentEval = Heuristic(newState).attacks()
            if self.update_states:
                print Heuristic(currentState).queensPosition(
                ), " -> ", Heuristic(newState).queensPosition()
            if currentEval <= nextEval:
                currentState = newState
                nextEval = Heuristic(currentState).attacks()

            i += 1
            self.neighbor = Neighbor(currentState)

        file.write(Heuristic(currentState).queensPosition(),
                   self.neighbor.createBoard(),
                   url="./resource/newBoard.txt")

        print "Hill Comum > Iteracao  : ", i
        print "Posicao Inicial das ", len(
            self.startState), " rainhas : ", Heuristic(
                self.startState).queensPosition()
        print "Posicao Final das ", len(
            self.startState), " rainhas : ", Heuristic(
                currentState).queensPosition()
        print "\tNumero de rainhas atacando : ", Heuristic(
            currentState).attacks()
        self.startState = currentState
        return Heuristic(currentState).attacks()

    def hill_random(self):
        colision = sys.maxsize
        count = 0
        stagnate = sys.maxsize
        old_col = -1
        while colision != 0 and not stagnate == 100:
            print "Hill Random > Iteracao: ", count + 1
            print "------------------------------------------\n"
            start = time.time()
            colision = self.hill()
            end = time.time() - start
            print "\tTempo de execucao : ", end, " segundos"
            self.neighbor = Neighbor(self.startState)
            count += 1
            print "\n------------------------------------------\n"
            if not old_col == colision:
                old_col = colision
                stagnate = 0
            else:
                stagnate += 1