IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Programmation multimédia/Jeux Python Discussion :

2048 Intelligence Artificielle


Sujet :

Programmation multimédia/Jeux Python

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Étudiant
    Inscrit en
    Mars 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hautes Alpes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Mars 2015
    Messages : 1
    Points : 1
    Points
    1
    Par défaut 2048 Intelligence Artificielle
    Bonjour,

    Je viens vers vous car j'ai besoin d'aide sur un projet à rendre sous peu en intelligence artificielle (IA) : https://intelligence-artificielle.developpez.com
    Je doit coder sous Python un algorithme intelligent qui résout le jeu 2048 à 100%.

    Pour ce faire, j'ai tout d'abord codé les déplacements du jeu.
    Il a fallu ensuite concevoir une fonction d'évaluation (c'est cette partie qui est à ameliorer) basée sur divers arguments afin de choisir la matrice (gauche, droite, haut ou bas) pour l'état suivant.

    Un algorithme minmax doit aussi être établi mais ma fonction d'évaluation est trop faible pour y penser :p (il y en a quand même un début !)
    Voici le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
     
     
    #=============================================================================#
     
                                       # CODE JEU
    import random, sys, copy
     
    matrice = [[2,0,0,0],
               [0,0,0,0],
               [0,0,0,0],
               [0,0,0,2]]
     
     
    def tourner_matrice(matrice):
        n = []   
        for i in range(4):
            n.append([matrice[3][i], matrice[2][i], matrice[1][i], matrice[0][i]])
        return n
     
    def glisser_droite(matrice): 
        nouvelle_matrice = [] 
        for ligne in matrice : 
            ligne = [elem for elem in ligne if elem!=0] 
            for i in range(len(ligne)-1,0,-1): 
                if ligne[i]==ligne[i-1]: 
                    ligne[i] = ligne[i]+ligne[i-1] 
                    ligne[i-1] = 0 
            ligne = [elem for elem in ligne if elem!=0] 
            ligne = [0]*(4-len(ligne))+ligne 
            nouvelle_matrice.append(ligne) 
        return nouvelle_matrice     
     
    def glisser_gauche(matrice): 
        nouvelle_matrice = [] 
        for ligne in matrice : 
            ligne = [elem for elem in ligne if elem!=0] 
            for i in range(0,len(ligne)-1,1): 
                if ligne[i]==ligne[i+1]: 
                    ligne[i] = ligne[i]+ligne[i+1] 
                    ligne[i+1] = 0 
            ligne = [elem for elem in ligne if elem!=0] 
            ligne = ligne+[0]*(4-len(ligne))
            nouvelle_matrice.append(ligne) 
        return nouvelle_matrice
     
     
    def glisser_haut(matrice):
        matrice = tourner_matrice(matrice)
        nouvelle_matrice = glisser_droite(matrice)
        for i in range(3):
            nouvelle_matrice= tourner_matrice(nouvelle_matrice)
        return nouvelle_matrice
     
    def glisser_bas(matrice):
        for i in range(3):
            matrice = tourner_matrice(matrice)
        nouvelle_matrice = glisser_droite(matrice)
        nouvelle_matrice = tourner_matrice(nouvelle_matrice)
        return nouvelle_matrice
     
    def aleatoire(nouvelle_matrice):
        liste = []
        for i in range(4):
            for j in range(4):
                if nouvelle_matrice[i][j] == 0:
                    liste.append((i,j))
     
        if liste== []:
            return nouvelle_matrice
        bite = random.choice(liste)    
        nouvelle_matrice[bite[0]][bite[1]] = random.choice([2,4])
        return nouvelle_matrice
     
    #=============================================================================#
     
                                  # EVALUATION    
     
    def maximum(m):
        h=2
        for i in range (4):
            for j in range (4):
                if m[i][j]>h:
                    h=m[i][j]
        return h
     
    def nbre_zero(m): 
        k=0
        for i in range (4):
            for j in range (4):
                if m[i][j]==0:
                    k=k+1
        return k
     
    def nombre_addition_ligne (m):
        f=0
        for ligne in m : 
            ligne = [elem for elem in ligne if elem!=0] 
            for i in range(len(ligne)-1,0,-1): 
                ligne[i-1]==ligne[i]
                f=f+1
            return f
     
    def nombre_addition_colonne(m):
        g=0
        m=tourner_matrice(m)
        for ligne in m : 
            ligne = [elem for elem in ligne if elem!=0] 
            for i in range(0,len(ligne)-1,1): 
                ligne[i]==ligne[i+1]
                g=g+1
            return g
     
    def sousmax(m):          
        h=2
        for i in range (4):
            for j in range (4):
                if m[i][j]>h and m[i][j]< maximum(m):
                    h=m[i][j]
        return h
     
    def sous_sousmax(m):
        h=2
        for i in range (4):
            for j in range (4):
                if m[i][j]>h and m[i][j]<sousmax(m):
                    h=m[i][j]
        return h         
     
    def sous_sous_sousmax(m):
        h=2 
        for i in range (4):
            for j in range(4):
                if m[i][j]>h and m[i][j]<sous_sousmax(m):
                    h=m[i][j]
        return h
     
     
     
    def c_pleine (m):
    	if m[0][0]!=0 and m[1][0]!=0 and m[2][0]!=0 and m[3][0 ]!=0: 
    		return True
    	else:
    		return False    
     
     
    def evaluation (matrice, etat_actuel):
     
        if matrice==etat_actuel:
            return -sys.maxsize
        score=0
     
        if maximum(matrice)==matrice[0][0]:
            score += pow(matrice[0][0],5)
     
        else:
            score += 0
     
        score += pow(maximum(matrice),4)        
     
        o = nbre_zero(matrice)  
        score += pow(o,3)
     
        for i in range (4):
            for j in range (4):        
                if matrice[0][0]==maximum(matrice) and matrice[0][1]==sousmax(matrice):
                    score += pow(sousmax(matrice),2.5)
                if matrice[0][1]==sousmax(matrice) and matrice[0][2]==sous_sousmax(matrice):
                    score += pow(sous_sousmax(matrice),2.5)
                if matrice[0][2]==sous_sousmax(matrice) and matrice[0][3]==sous_sous_sousmax(matrice):
                    score+= pow(sous_sous_sousmax(matrice),2.5)
     
        return score   
     
        if c_pleine (matrice) == True :
            score+=pow(matrice[1][0],2)
        else :
            score+=0
     
        if matrice[1][0]>matrice[2][0] and matrice[2][0]>matrice[3][0] and matrice[3][0]>0:
            score +=pow(matrice[1][0],2.5)
        else:
            score +=0        
     
    ''' 
      a = nombre_addition_ligne(matrice)
        if a==0:
            score += 0
        else:     
            score += pow(a,10)
        
        e = nombre_addition_colonne(matrice)
        if e==0:
            score += 0  
        else:
            score += pow(e,10)
    '''
     
     
     
    ###############################################################################
     
                                    #JEU#   
     
    def jouer(matrice):
     
        matrice_gauche = glisser_gauche(matrice)
        matrice_droite = glisser_droite(matrice)
        matrice_haut = glisser_haut(matrice)
        matrice_bas = glisser_bas(matrice)
        if matrice_gauche==matrice_droite==matrice_haut==matrice_bas:
            print "************************************************************************************"
            return "aucune", None
     
        score_gauche = evaluation(matrice_gauche, matrice)
        score_droite = evaluation(matrice_droite, matrice)
        score_haut = evaluation(matrice_haut, matrice)
        score_bas = evaluation(matrice_bas, matrice)
     
        print score_gauche
        print score_droite
        print score_haut
        print score_bas
     
        score_max = max(score_gauche, score_droite, score_haut, score_bas)
     
        if(score_max == score_gauche):
            return "gauche", matrice_gauche
        elif(score_max == score_droite):
            return "droite", matrice_droite
        elif(score_max == score_haut):
            return "haut", matrice_haut
        else:
            return "bas", matrice_bas
     
     
     
    for i in range(1000):
        print("tour",i)
        nouvelle_direction, nouvelle_matrice = jouer(matrice)
        if nouvelle_direction=="aucune":
            print "                                       You failed - Try again (Gautier suce)"
        matrice = aleatoire(nouvelle_matrice)
        print(nouvelle_direction,nouvelle_matrice)
     
     
     
    ###############################################################################
     
                                        #MINMAX#
    '''
    def minmax(etat_jeu):
        matrice_gauche = glisser_gauche(etat_jeu)
        matrice_droite = glisser_droite(etat_jeu)
        matrice_haut = glisser_haut(etat_jeu)
        matrice_bas = glisser_bas(etat_jeu)
        mon_tour=[matrice_gauche, matrice_droite, matrice_haut,matrice_bas]
        tour_ordinateur=[]
        for tour in mon_tour:
            for i in range (4):
                for j in range (4):
                    if matrice[i][j]==0:
                        matrice[i][j]=2
                        nouvelle_matrice = copy.deepcopy(matrice)
                        tour_ordinateur.append(nouvelle_matrice)
            mon_tour[i]=(tour, tour_ordinateur)
            i= i+1
    Voilà voilà, il me faudrait donc de l'aide pour aboutir ! Merci d'avance !!

  2. #2
    Expert éminent sénior
    Homme Profil pro
    Architecte technique retraité
    Inscrit en
    Juin 2008
    Messages
    21 283
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Manche (Basse Normandie)

    Informations professionnelles :
    Activité : Architecte technique retraité
    Secteur : Industrie

    Informations forums :
    Inscription : Juin 2008
    Messages : 21 283
    Points : 36 770
    Points
    36 770
    Par défaut
    Salut,

    Si vous voulez qu'on vous aide, il va falloir apprendre à utiliser la balise code en lisant ce tuto.

    - W
    PS: Vous avez remarqué que les scripts Python étaient une suite de blocks délimités par une indentation. Sans indentation, votre code devient illisible Normalement, si vous l'avez inséré par un simple copie&coller, l'indentation n'est pas perdue et je peux ajouter la balise code (et vous expédier un MP pour vous dire que...). Dans votre cas, hélas, je ne sais pas comment vous avez recopié votre code dans le message mais les indentations ne sont plus là et çà, pas facile à inventer.
    Architectures post-modernes.
    Python sur DVP c'est aussi des FAQs, des cours et tutoriels

Discussions similaires

  1. Débat sur l'I.A (Intelligence Artificielle)
    Par Anonymous dans le forum Intelligence artificielle
    Réponses: 457
    Dernier message: 05/07/2016, 01h11
  2. [Lisp][CLIPS] Intelligence artificielle
    Par hanane93 dans le forum Lisp
    Réponses: 21
    Dernier message: 15/12/2005, 13h15
  3. [ETUDES] Traitement images ou intelligence artificielle ?
    Par green_castor dans le forum Etudes
    Réponses: 9
    Dernier message: 29/11/2005, 13h01
  4. Intelligence Artificielle?
    Par pedrolan dans le forum C++
    Réponses: 16
    Dernier message: 26/08/2005, 20h20
  5. Intelligence artificielle
    Par pekka77 dans le forum Intelligence artificielle
    Réponses: 7
    Dernier message: 18/03/2005, 12h37

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo