-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV11 Recuit simule.py
More file actions
299 lines (249 loc) · 9.02 KB
/
V11 Recuit simule.py
File metadata and controls
299 lines (249 loc) · 9.02 KB
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
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
# coding: utf8
# Tipe MP 2016 #
# Capdevila - Germa #
# Version 3-27.09.15#
from __future__ import unicode_literals
import sys as Sys
from pylab import *
from tkinter import *
from random import *
from math import *
## Declatations
# Matrice
dimx=50
dimy=50
bords=4
#Types
Vide=0
Mur=1
Occupee=2
Sortie=3
tours=50
# # # # # # # # # # # # # Paniq mode #
PM=False
# # # # # # # # # # # # #
sorties=[[1,25]] #Position des sorties
global nbsorties
nbsorties=0
mouvements=[
[0,0], #Ne pas bouger 0
[0,1], #Haut 1
[1,0], #Gauche 2
[-1,0], #Droite 3
[0,-1], #Bas 4
]
deplacements=[
[0,0], #Ne pas bouger 0
[0,1], #Haut 1
[1,0], #Gauche 2
[-1,0], #Droite 3
[0,-1], #Bas 4
[0,2], #courir haut 5
[2,0], #courir Gauche 6
[-2,0], #courir Droite 7
[0,-2], #courir Bas 8
[1,1], #Haut gauche 9
[1,-1], #Gauche bas 10
[-1,1], #Droite haut 11
[-1,-1], #Droite bas 12
]
bords=3
def ArchiParam(n,i,j):
if (i-bords<0 or i+bords>dimy or j-bords<0 or j+bords>dimx or (j<dimy/2 and abs(i-dimx)<3) or j<6) and i!=25 and i!=24 and i!=26 and n==0 and not (abs(sorties[0][0]+7-j)<4 and abs(sorties[0][1]-i)<5):
return(Mur) # 3 par 2
if (abs(sorties[0][0]+7-j)<4 and abs(sorties[0][1]-i)<5) and n==0 and randint(0,3)==0 :
return(Mur) # 3 par 2
else : return(Vide)
def placage(densite):
n=0
densite_actuelle=0
global Cellules
while densite_actuelle<densite:
aleai=randint(bords,dimx-bords+1)
aleaj=randint(bords,dimy-bords+1)
if Cellules[0][aleai][aleaj]==Vide:
Cellules[0][aleai][aleaj]=Occupee
n+=1
densite_actuelle=n/((dimx-bords)*(dimy-bords))
return(True)
## Outils
def SC(cp1,cp2): # Sommes de couples
return([cp1[0]+cp2[0],cp1[1]+cp2[1]])
## Mouvements
distance=[dimx,dimy]
##### Comportement ####
#### v29.01.16 ########
######### 1 ###########
##### 3 # 2 # 6 #######
# 4 # 5 #%%%###########
def Comportement(n,x,y):
if not PM:
distance=[dimx,dimy]
for couple in sorties:
if abs(distance[0])**2+abs(distance[1])**2>abs(x-couple[0])**2+abs(y-couple[1])**2 :
distance[0]=x-couple[0]
distance[1]=y-couple[1]
#Ordre des mouvements ## a compacter. Place pour lisibilite
if distance[0]>0 : # si vers la droite
if abs(distance[0])>abs(distance[1]) : # -> Sortie est a droite et plus longue dist
if distance[1]>=0 : ordre=[7,12,3,8,4,11,0,1,2] #Vers le bas ensuite
else : ordre=[7,11,3,5,1,12,0,4,2]
else :
if distance[1]>=0 : ordre=[8,12,4,7,3,10,0,2,1]
else : ordre=[5,11,1,7,3,9,0,2,4]
else :
if abs(distance[0])>abs(distance[1]) : # -> Sortie est a droite et plus longue dist
if distance[1]>=0 : ordre=[6,10,2,8,4,9,0,1,3] #Vers le bas ensuite
else : ordre=[6,9,2,5,1,10,0,4,3]
else :
if distance[1]>=0 : ordre=[8,10,4,6,2,12,0,3,1]
else : ordre=[5,9,1,6,2,11,0,3,4]
global nbsorties
possibledeplacement=False
k=0
out=False
while not possibledeplacement and k<len(ordre):
if verif(n,x,y,deplacements[ordre[k]]) :
possibledeplacement=True
if Cellules[n+1][x+deplacements[ordre[k]][0]][y+deplacements[ordre[k]][1]]==Sortie:
out=True
nbsorties=nbsorties+1
else :
Cellules[n+1][x][y]=Vide # On vide l'ancienne case
Cellules[n+1][x+deplacements[ordre[k]][0]][y+deplacements[ordre[k]][1]]=Occupee # On occupe la nouvelle (qui peut etre identique)
k+=1
def verif(n,x,y,traj):
#traj=[[+-1,+-1]]
verific=SC([x,y],traj)
verific1=SC([x,y],[sign(traj[0]),sign(traj[1])])
verific2=SC([x,y],[traj[0]+sign(traj[0]),traj[1]+sign(traj[1])]) # Si plus loin, il y a un mur, on y va pas. Si on cours,
# alors on ne sera pas pousse par ceux de derriere, alors on peut eviter l obstacle de plus loin
if verific!=[x,y] and (Cellules[n+1][verific[0]][verific[1]]==Occupee or Cellules[n][verific[0]]
[verific[1]]==Occupee or Cellules[n][x+traj[0]][y+traj[1]]==Mur or Cellules[n][verific1[0]][verific1[1]]==Mur or Cellules[n][verific2[0]][verific2[1]]==Mur) : return(False) # Si il y a quelque chose : C'est impossible
return True
global ite
ite=0
global Cellules
def defcell(densite,boule):
#init
ite=0
global Cellules
global nbsorties
nbsorties=0
Cellules=[#n
[#x
[ArchiParam(k,i,j) for i in range(dimy)]#y
for j in range(dimx)]
for k in range(tours+1)]
for couple in sorties:
for mouve in mouvements:
Cellules[0][couple[0]+mouve[0]][couple[1]]=Sortie
Cellules[0][couple[0]+mouve[0]][couple[1]+1]=Sortie
Cellules[0][couple[0]+mouve[0]][couple[1]-1]=Sortie
placage(densite)
return Cellules
def value(Cellulles):
for instance in range(tours): # On lance le calcul
# Recherche de personne
startx=randint(0,dimx) # Choix aleatoire a chaque tour le du debut de la recherche
starty=randint(0,dimy)
for x in range(dimx):
for y in range(dimy):
if Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]==Occupee :
Comportement(instance,(x+startx)%dimx,(y+starty)%dimy)
elif Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]!=Occupee and Cellules[instance][((x+startx)%dimx)%dimx][(y+starty)%dimy]!=Vide :
Cellules[instance+1][(x+startx)%dimx][(y+starty)%dimy]=Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]
# ###########
return(nbsorties)
def barreprogression(ite,tot):
long=100
decimales=2
longcomplet = int(round(long*ite/float(tot)))
percents=round(100.0 * ite/float(tot),decimales)
prefix='Progression : '
suffix=' Terminé'
bar= '#' * longcomplet + '-'*(long-longcomplet)
Sys.stdout.write('%s [%s] %s%s %s\r' % (prefix, bar, percents, '%', suffix)),
Sys.stdout.flush()
if ite==tot:
print('\n')
def opensave(ind):# Verifiee. ok saufe [-1][-1] np
file=open('recherche/sauvegarde','r')
Arch=file.read()
file.close()
Architec=[[0 for i in range(dimy)]for j in range(dimx)]
Arch2=Arch.split('[')
for indice in range(2,len(Arch2)):
obj=Arch2[indice].split(',')
for ind2 in range(0,len(obj)-1):
i=0
while obj[ind2][i]==' ':
i+=1
Architec[indice-2][ind2]=int(obj[ind2][i])
return(Architec)
densite=0.3
def value2(Archit):
#init
ite=0
global Cellules
global nbsorties
nbsorties=0
Cellules=[Archit for k in range(tours+1)]
placage(densite)
for instance in range(tours): # On lance le calcul
# Recherche de personne
startx=randint(0,dimx) # Choix aleatoire a chaque tour du debut de la recherche
starty=randint(0,dimy)
for x in range(dimx):
for y in range(dimy):
if Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]==Occupee :
Comportement(instance,(x+startx)%dimx,(y+starty)%dimy)
elif Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]!=Occupee and Cellules[instance][((x+startx)%dimx)%dimx][(y+starty)%dimy]!=Vide :
Cellules[instance+1][(x+startx)%dimx][(y+starty)%dimy]=Cellules[instance][(x+startx)%dimx][(y+starty)%dimy]
# ###########
return(nbsorties)
def sauvegarde(cellule):
chemin='recherche/sauvegarde'
sauvegarde=open(chemin,'w')
if type(cellule[0][0])!=list:
sauvegarde.write(str(cellule))
else:
sauvegarde.write(str(cellule[0]))
sauvegarde.close()
T=1000
new=input('Nouveau aletatoire ou charger architecture ? 0 aleat / ind != 0 pour charger ind')
if new==0:
defcell(0.3,0)
else :
value2(opensave(''))
def resetsave():
for i in range(len(Cellules[0])) :
for j in range(len(Cellules[0][i])):
Cellules[0][i][j]=int(Cellules[0][i][j]==1)+3*int(Cellules[0][i][j]==3)
sauvegarde(Cellules)
resetsave()
perc=0.01
# Pour barre progres
T1=T
k=0
while T1>=1:
T1=T1-T1*perc
k+=1
iprog=0
# Fin barre progres
while T>=1:
i=randint(5,11)
j=randint(20,30)
Archirecuit=opensave('')
Archirecuit[i][j]=1-Archirecuit[i][j]
a=value2(opensave(''))
resetsave()
de=value2(Archirecuit)-a
resetsave()
if de>5 :
sauvegarde(Archirecuit)
elif rand()<exp(de-5/T):
sauvegarde(Archirecuit)
T=T-T*perc
iprog+=1
barreprogression(iprog,k)