Logo Pastebin.fr
Pastebin

Retrouvez, créez et partagez vos snippets en temps réel.

bouffon

import random
import math


def etatInitial(N):
    etat =[]
    for i in range(N) :
        etat.append(random.randint(0, N - 1))
    return etat

def afficher(etat):
    N = len(etat)
    for i in range(N):
        for j in range(N):
            if(etat[j]==i):
                print ("|Q", end="")
            else:
                print("| ", end="")
            print("|")

def evaluer(etat):
    eval = 0
    N = len(etat)
    for i in range(N):
        for j in range(i+1, N):
            if etat[i] == etat[j] or abs(i - j) == abs(etat[i] - etat[j]):
                eval += 1
    return eval

def RecuitSimule(etat_initial,T_0, taux, iter_max):
    n = etat_initial.copy()
    f_n = evaluer(n)
    n_best = n.copy()
    f_n_best = f_n
    t= T_0
    for i in range(iter_max):
        n_voisin = n.copy()
        index= random.randint(0,N-1)
        n_voisin[index] = random.randint(0,N-1) 
        f_voisin = evaluer(n_voisin)
        if f_voisin < f_n:
            n = n_voisin
        else : 
            delta = abs(f_voisin, f_n)
            proba = 1/(np.exp(delta/t))
            r = random.random()
            if(r < proba):
                n = n_voisin
                f_n = f_voisin

    return meilleur_etat, meilleur_evaluation


e = etatInitial(N=8)
afficher(e)
print(evaluer(e))
etat, eval = RecuitSimule(etat_initial=e, T_0=10000, taux=0.9, iter_max=10000)
afficher(etat)
print(eval)

Créé il y a 3 mois.

Rechercher un Pastebin

Aucun paste trouvé.