| 12
 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
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 
 |  
#Condition simulation
 
    #Foret à simuler
 
Nombre_ligne=100
Nombre_colonne=100
Densite=95
Vent=[100,100]
intensite=0
 
    #Chance de mise à feux
 
chance_buis=30
chance_chêne=30
chance_cyprès=30
 
     #Durée de vie
 
durée_de_vie_buis=3
durée_de_vie_chêne=3
durée_de_vie_cyprès=3
 
 #taille fenetre simulation
 
largeur=700
hauteur=700
 
 
 
#bibliothéque utile
 
from random import *
from copy import *
import pygame
from math import *
from numpy import *
 
#def des vecteurs
 
def vecteur(A,B):
    '''Crée un vecteur entre le point A et le point B'''
    y=B[0]-A[0]
    x=B[1]-A[1]
    v=[x,-y]
    return v
 
def norme(A):
    x=A[0]
    y=A[1]
    n = (x**2+y**2)**0.5
    return n
 
def produitscalaire(A,B):
    '''Produit scalaire canonique'''
    xA = A[0]
    yA = A[1]
    xB = B[0]
    yB = B[1]
    Ps = (xA*xB)+(yA*yB)
    return Ps
 
def normalisation(Vecteur):
    '''Rend un vecteur unitaire'''
    normeV=norme(Vecteur)
    X=Vecteur[0]/normeV
    Y=Vecteur[1]/normeV
    Vn=[X,Y]
    return Vn
 
#Définition des classes
 
class arbre:
 
                            ##Constructeur
    def __init__(self,t,e,s):
        self.etat=e
        self.status=s
        self.type=t
 
 
                            ##Afficheur
    def __repr__(self):
        E=self.etat
        S=self.status
        T=self.type
        if T==0:
            if E==0 and S==0:
                return 'Buis vivant'
            elif E==0 and S>=1:
                return 'Buis mort'
            else:
                return 'Buis en feu'
        if T==1:
            if E==0 and S==0:
                return 'Chêne vivant'
            elif E==0 and S>=1:
                return 'Chêne mort'
            else:
                return 'Chêne en feu'
        if T==2:
            if E==0 and S==0:
                return 'Cyprès vivant'
            elif E==0 and S>=1:
                return 'Cyprès mort'
            else:
                return 'Cyprès en feu'
        if T==3:
            if E==0 and S==0:
                return 'Terre'
            elif E==0 and S>=1:
                return 'Terre'
            else:
                return 'Terre'
        if T==4:
            if E==0 and S==0:
                return 'Terre(Buis)'
            elif E==0 and S>=1:
                return 'Terre(Buis)'
            else:
                return 'Terre(Buis)'
        if T==5:
            if E==0 and S==0:
                return 'Terre(Chêne)'
            elif E==0 and S>=1:
                return 'Terre(Chêne)'
            else:
                return 'Terre(Chêne)'
        if T==6:
            if E==0 and S==0:
                return 'Terre(Cyprès)'
            elif E==0 and S>=1:
                return 'Terre(Cyprès)'
            else:
                return 'Terre(Cyprès)'
 
 
#Initialisation
 
 
def Foret_vierge(n,p,x):
    '''création Foret de taille n*p de densité x'''
    F=[]
    buis=arbre(0,0,0)
    chene=arbre(1,0,0)
    cypres=arbre(2,0,0)
    terre=arbre(3,0,0)
    for i in range(n):
        F.append([])
        for j in range(p):
            k=randint(0,100)
            if k<=x:
                r=randint(0,2)
                if r==0:
                    F[i].append(deepcopy(buis))
                elif r==1:
                    F[i].append(deepcopy(chene))
                elif r==2:
                    F[i].append(deepcopy(cypres))
            else:
                F[i].append(deepcopy(terre))
    return array(F)
 
#def Foret_vierge_plus(n,p,.....)
 
def Foret_vierge_test(n,p):
    '''création Foret simple(sans terre, un seul type d'arbre) pour tester le programme ( utile pour identifier les défauts)'''
    F=[]
    chene=arbre(1,0,0)
    for i in range(n):
        F.append([])
        for j in range(p):
                F[i].append(deepcopy(chene))
    return array(F)
 
 
def Foret_ignit(F):
    '''Mise à feux en un point aléatoire de la Foret'''
    j=randint(0,len(F)-1)
    i=randint(0,len(F[0])-1)
    if F[j][i].etat == 0 and F[j][i].status== 0 and F[j][i].type<3:
        F[j][i].etat=1
        return F
    else:
        return Foret_ignit(F)
 
 
#Moteur simulation
 
 
def TreesWillBeOnFire(L,Vent,Intensite):
    '''Fait passer la Foret d'une étape n à l'étape n+1'''
    N=deepcopy(L) #notre nouvelle Foret
    nl=len(L) # nombre ligne+1
    nc=len(L[0]) # nombre colone+1
    Vent=normalisation(Vent)
    p=portee(Intensite)
 
    for ligne in range(nl):
 
        for colonne in range(nc):
 
            if L[ligne][colonne].etat > 0 : #ne prend que les arbres en feu
 
                N[ligne][colonne]=MAJ(L[ligne][colonne]) #met à jour la case en feux
                for i in range(-1-p,2+p):
                    for j in range(-1-p,2+p):
                            if (i!=0 or j!=0) and existence(L,ligne+i,colonne+j,nl,nc) and N[ligne+i][colonne+j].etat==0 and N[ligne+i][colonne+j].status==0:
                                case = deepcopy(L[ligne+i][colonne+j])
                                Vecteur=[i,j]
                                N[ligne+i][colonne+j]=régle_vent(case,Vecteur,Vent,Intensite,p)
    return N
 
    #Annexe Moteur
 
def portee(Intensite):
    '''Fait le lien entre l'intensité du vent et la portée maximale d'envoie de braise'''
    if Intensite>=100:
        return 10
    else:
        return int(Intensite*10/100)
 
 
 
def angle_vent(Intensite):
    """Fait le lien entre l'intensité du vent et le cones de propagation du feu"""
    if Intensite>=100:
        return 7*pi/8
    else:
        return Intensite*7*pi/(8*100)
 
def régle_vent(ab,Vecteur,Vent,Intensite,p):
    '''régle de mise à feu d'un arbre vivant'''
    if ab.etat == 0 and ab.status== 0 and ab.type < 3:
        if Vent == [0,0] or Intensite==0:
            return régle(ab,1)
        else:
            costheta=produitscalaire(normalisation(Vecteur),[-Vent[0],-Vent[1]])
            alpha=angle_vent(Intensite)
 
            if costheta<cos(alpha):
                coeff=(1-(max(abs(Vecteur[0]),abs(Vecteur[1]))-1)/(p+1))**10
                return régle(ab,coeff)
            else:
                return ab
 
    return ab
 
 
 
def régle(ab,coeff):
    '''chance qu'un arbre vivant prenne feu'''
 
    if ab.etat == 0 and ab.status== 0 and ab.type < 3:
        if ab.type == 0:
            if randint(1,100)<=chance_buis*coeff:
                ab.etat=1
        elif ab.type == 1:
            if randint(1,100)<=chance_chêne*coeff:
                ab.etat=1
        else:
            if randint(1,100)<=chance_cyprès*coeff:
                ab.etat=1
 
    return ab
 
 
def MAJ(a):
    '''met à jours le statut des arbres en feu'''
    a.status=1+a.status
    if a.status>durée_de_vie_buis and a.type==0:
        a.etat=0
        return a
    elif a.status>durée_de_vie_chêne and a.type==1:
        a.etat=0
        return a
    elif a.status>durée_de_vie_cyprès and a.type==2:
        a.etat=0
        return a
    else:
        return a
 
 
def existence(L,n,p,nc,nl):
    '''étudie l'existence de la case étudié'''
 
    if n>=0 and n<nl and p>=0 and p<nc:
        return True
    return False
 
def arbre_en_feu(L):
    '''permet de savoir si la foret brule encore'''
    for i in range(len(L)):
        for j in range(len(L[0])):
            if str(L[i][j])=='Buis en feu' or str(L[i][j])=='Chêne en feu' or str(L[i][j])=='Cyprès en feu':
                return 1
    return 0
 
#Simulation et statistique
 
def simulation(n,F,Vent,intensite):
    ''' Renvoie la foret aprés n étape'''
    for i in range(n):
        F=TreesWillBeOnFire(F,Vent,intensite)
    return F
 
def simulation_total(F,Vent,intensite):
    '''Renvoie la foret quand celle-ci ne brule plus'''
    while arbre_en_feu(F) == 1:
        F=TreesWillBeOnFire(F,Vent,intensite)
    return F
 
def comptage(F,Vent,intensite):
    '''Permet de connaitre le nombre d'arbre en vie ( de  chaque type) à la fin d'une simulation Totale'''
    L=[0,0,0,0]
    F=simulation_total(F,Vent,intensite)
    for i in range(len(F)):
        for j in range(len(F[0])):
            if F[i][j].etat == 0 and F[i][j].status== 0 and F[i][j].type<3 :
                L[0]=L[0]+1
                if F[i][j].type==0:
                    L[1]=L[1]+1
 
                if F[i][j].type==1:
                    L[2]=L[2]+1
 
                if F[i][j].type==2:
                    L[3]=L[3]+1
 
    return L
 
def comptage_plus(F,n,Vent,intensite):
    '''Permet de connaitre le % de survie des arbres'''
    L=[]
    C=[]
    for i in range(n):
        L.append(comptage(Foret_ignit(deepcopy(F)),Vent,intensite))
    for i in range(len(L[0])):
        T=0
        for j in range(n):
            T=T+L[j][i]
        Pourcent=(T/n)/(len(F)*len(F[0]))
        C.append(Pourcent)
    return C
 
#Affichage
 
    # Les couleurs.
NOIR=(0,0,0)
ROUGE=(189,35,13)
VERT_CLAIR=(51,84,11)
VERT=(35,57,7)
VERT_SAPIN=(18,31,4)
MARRON=(74,53,38)
 
    # notre tableau entre 10 et 180
grille=Foret_vierge(Nombre_ligne,Nombre_colonne,Densite)
 
z=len(grille)
y=len(grille[0])
 
 
    # Taille de la grille.
LARGEUR=int(largeur/z)
HAUTEUR=int(hauteur/z)
 
    #Afficheur
 
def afficher(L):
    grille=L
    c=0
    z=len(grille)
 
     # Initialisation de pygame.
    pygame.init()
 
    # Taille de notre écran
    Taille_fenetre=[largeur,hauteur]
    screen=pygame.display.set_mode(Taille_fenetre)
 
    # Titre de la fenêtre.
    pygame.display.set_caption("Modélisation d'un feu de forêt")
 
    # Fait des tour jusqu'à ce que l'on clique sur fermer.
    done=False
 
    # Vitesse d'affichage.
    clock=pygame.time.Clock()
 
    while not done:
        for event in pygame.event.get():  # On fait quelque chose.
            if event.type==pygame.QUIT:  # Si on quitte.
                done=True  #Etape indiquant la fin, sortie de cette boucle.
        if c==0:
            while arbre_en_feu(grille)==1:
                grille=TreesWillBeOnFire(grille,[-Vent[0],Vent[1]],intensite)
 
                # -------- Programme Principal Loop -----------
                screen.fill(NOIR)
                    # La grille
 
                for ligne in range(z):
                    for colonne in range(y):
                            if str(grille[ligne][colonne])=='Buis vivant':
                                color=VERT_CLAIR
                            if str(grille[ligne][colonne])=='Chêne vivant':
                                color=VERT
                            if str(grille[ligne][colonne]) =='Cyprès vivant':
                                color=VERT_SAPIN
                            if str(grille[ligne][colonne])=='Buis en feu' or str(grille[ligne][colonne])=='Chêne en feu' or str(grille[ligne][colonne])=='Cyprès en feu':
                                color=ROUGE
                            if str(grille[ligne][colonne])=='Buis mort' or str(grille[ligne][colonne])=='Chêne mort' or str(grille[ligne][colonne])=='Cyprès mort':
                                color=NOIR
                            if str(grille[ligne][colonne])=='Terre':
                                color=MARRON
                            if str(grille[ligne][colonne])=='Terre(Buis)':
                                color=MARRON
                            if str(grille[ligne][colonne])=='Terre(Chêne)':
                                color=MARRON
                            if str(grille[ligne][colonne])=='Terre(Cyprès)':
                                color=MARRON
                            pygame.draw.rect(screen,color,[(LARGEUR)*colonne,(HAUTEUR)*ligne,LARGEUR,HAUTEUR])
 
                # 60 fps max.
                clock.tick(1)
 
                # Mettre à jour l'écran avec ce que nous avons dessiné.
                pygame.display.flip()
 
        else:
            # -------- Program Princip Loop -----------
            screen.fill(NOIR)
                # La grille
            for ligne in range(z):
                for colonne in range(y):
                    if str(grille[ligne][colonne])=='Buis vivant':
                        color=VERT_CLAIR
                    if str(grille[ligne][colonne])=='Chêne vivant':
                        color=VERT
                    if str(grille[ligne][colonne]) =='Cyprès vivant':
                        color=VERT_SAPIN
                    if str(grille[ligne][colonne])=='Buis en feu' or str(grille[ligne][colonne])=='Chêne en feu' or str(grille[ligne][colonne])=='Cyprès en feu':
                        color=ROUGE
                    if str(grille[ligne][colonne])=='Buis mort' or str(grille[ligne][colonne])=='Chêne mort' or str(grille[ligne][colonne])=='Cyprès mort':
                        color=NOIR
                    if str(grille[ligne][colonne])=='Terre':
                        color=MARRON
                    if str(grille[ligne][colonne])=='Terre(Buis)':
                        color=MARRON
                    if str(grille[ligne][colonne])=='Terre(Chêne)':
                        color=MARRON
                    if str(grille[ligne][colonne])=='Terre(Cyprès)':
                        color=MARRON
                    pygame.draw.rect(screen,color,[(LARGEUR)*colonne,(HAUTEUR)*ligne,LARGEUR,HAUTEUR])
 
            # 60 fps max.
            clock.tick(3)
 
            # Mettre à jour l'écran avec ce que nous avons dessiné.
            pygame.display.flip()
        c=1
 
 
 
# initialisation de pygame.
pygame.init()
 
# Taille de notre écran.
Taille_fenetre=[largeur,hauteur]
screen=pygame.display.set_mode(Taille_fenetre)
 
# Titre de la fenêtre.
pygame.display.set_caption("Modélisation d'un feu de forêt")
 
# Fait des tour jusqu'à ce que l'on clique sur fermer.
done=False
 
# Vitesse d'affichage.
clock=pygame.time.Clock()
 
# -------- Program Principal Loop -----------
while not done:
    for event in pygame.event.get():  # On fait quelque chose.
        if event.type==pygame.QUIT:  # Si on quitte.
            done=True  #Etape indiquant la fin, sortie de cette boucle.
            afficher(grille)
        elif event.type==pygame.MOUSEBUTTONDOWN:
            # Position du clic
            pos = pygame.mouse.get_pos()
            # Conversion coordonnées x/y à celle de la grille.
            colonne=pos[0]//(LARGEUR)
            ligne=pos[1]//(HAUTEUR)
            # Emplacement égal à un.
            if str(grille[ligne][colonne])=='Buis vivant':
                grille[ligne][colonne]=arbre(0,1,1)
            elif str(grille[ligne][colonne])=='Chêne vivant':
                grille[ligne][colonne]=arbre(1,1,1)
            elif str(grille[ligne][colonne])=='Cyprès vivant':
                grille[ligne][colonne]=arbre(2,1,1)
            elif str(grille[ligne][colonne])=='Buis en feu':
                grille[ligne][colonne]=arbre(0,0,1)
            elif str(grille[ligne][colonne])=='Chêne en feu':
                grille[ligne][colonne]=arbre(1,0,1)
            elif str(grille[ligne][colonne])=='Cyprès en feu':
                grille[ligne][colonne]=arbre(2,0,1)
            elif str(grille[ligne][colonne])=='Buis mort':
                grille[ligne][colonne]=arbre(4,0,0)
            elif str(grille[ligne][colonne])=='Chêne mort':
                grille[ligne][colonne]=arbre(5,0,0)
            elif str(grille[ligne][colonne])=='Cyprès mort':
                grille[ligne][colonne]=arbre(6,0,0)
            elif str(grille[ligne][colonne])=='Terre(Buis)':
                grille[ligne][colonne]=arbre(0,0,0)
            elif str(grille[ligne][colonne])=='Terre(Chêne)':
                grille[ligne][colonne]=arbre(1,0,0)
            elif str(grille[ligne][colonne])=='Terre(Cyprès)':
                grille[ligne][colonne]=arbre(2,0,0)
    # Fond de l'écran.
    screen.fill(NOIR)
 
    # La grille.
    for ligne in range(z):
        for colonne in range(y):
            if str(grille[ligne][colonne])=='Buis vivant':
                color=VERT_CLAIR
            if str(grille[ligne][colonne])=='Chêne vivant':
                color=VERT
            if str(grille[ligne][colonne])=='Cyprès vivant':
                color=VERT_SAPIN
            if str(grille[ligne][colonne])=='Buis en feu' or str(grille[ligne][colonne])=='Chêne en feu' or str(grille[ligne][colonne])=='Cyprès en feu':
                color=ROUGE
            if str(grille[ligne][colonne])=='Buis mort' or str(grille[ligne][colonne])=='Chêne mort' or str(grille[ligne][colonne])=='Cyprès mort':
                color=NOIR
            if str(grille[ligne][colonne])=='Terre':
                color=MARRON
            if str(grille[ligne][colonne])=='Terre(Buis)':
                color=MARRON
            if str(grille[ligne][colonne])=='Terre(Chêne)':
                color=MARRON
            if str(grille[ligne][colonne])=='Terre(Cyprès)':
                color=MARRON
            pygame.draw.rect(screen,color,[(LARGEUR)*colonne,(HAUTEUR)*ligne,LARGEUR,HAUTEUR])
 
    # 60 fps max.
    clock.tick(60)
 
    # Mettre à jour l'écran avec ce que nous avons dessiné.
    pygame.display.flip()
 
# Ralenti obligatoire, sans cette ligne le programme sera 'suspendu'.
# Fin.
pygame.quit() | 
Partager