@@ -211,10 +211,130 @@ Séance 7
211211
212212* :ref: `Classe et héritage <nbl-practice-py-base-classe_user_p >`
213213
214- Convertir une expression mathématique comme :math: `((34 + 6 ) - 2 ) / (7 - 4 )`
215- en `notation polonaise inverse <https://fr.wikipedia.org/wiki/Notation_polonaise_inverse >`_.
216- Voir aussi `Algorithme Shunting-yard
217- <https://fr.wikipedia.org/wiki/Algorithme_Shunting-yard> `_.
214+ Bouts de code, un peu d'optimization.
215+
216+ .. runpython ::
217+ :showcode:
218+
219+ import numpy as np
220+
221+ def calcul(h1,h2,v1,v2,x):
222+ t1 = np.sqrt(x ** 2 + h1**2) / v1
223+ t2 = np.sqrt((1-x) ** 2 + h2**2) / v2
224+ return t1 + t2
225+
226+ h1, h2, v1, v2 = 1, 0.5, 1, 0.8
227+ p = np.arange(6) / 5
228+ print(p)
229+ print(calcul(1,1,1,1,p))
230+ print(calcul(h1,h2,v1,v2,p))
231+
232+ def calcul_entrepot(v1,v2,A, B, Es):
233+ # A: [[0, 0]], B: [[1, 1]], Es: [[0.3, 0.4], [...]]
234+ t1 = np.sqrt(((A - Es) ** 2).sum(axis=1)) / v1
235+ t2 = np.sqrt(((B - Es) ** 2).sum(axis=1)) / v2
236+ return t1 + t2
237+
238+ A = np.array([[0,0]])
239+ B = np.array([[1,1]])
240+ Es = np.array([[0.5, 0.5], [0.1, 0.1], [0, 0.1]])
241+ print("---------")
242+ print(calcul_entrepot(v1,v2,A, B, Es))
243+
244+ Jeu de la vie:
245+
246+ .. runpython ::
247+ :showcode:
248+
249+ import os
250+ import numpy as np
251+ import matplotlib.pyplot as plt
252+ import tqdm
253+
254+
255+ def plateau(n, p=0.5):
256+ return (np.random.rand(n, n) < p).astype(int)
257+
258+
259+ def dessin(plat, next_plat):
260+ fig, ax = plt.subplots(1, 2)
261+ ax[0].imshow(plat.astype(float))
262+ ax[0].get_xaxis().set_visible(False)
263+ ax[0].get_yaxis().set_visible(False)
264+ ax[1].imshow(next_plat.astype(float))
265+ ax[1].get_xaxis().set_visible(False)
266+ ax[1].get_yaxis().set_visible(False)
267+ return fig, ax
268+
269+
270+ def iteration(plat):
271+ voisin = np.zeros(plat.shape, dtype=int)
272+ i, j = plat.shape
273+ # voisin gauche, droite
274+ voisin[:-1, :] += plat[1:, :]
275+ voisin[1:, :] += plat[:-1, :]
276+ # voisin haut,bas
277+ voisin[:, :-1] += plat[:, 1:]
278+ voisin[:, 1:] += plat[:, :-1]
279+ # voisin diagonal
280+ voisin[:-1, :-1] += plat[1:, 1:]
281+ voisin[1:, 1:] += plat[:-1, :-1]
282+ # voisin autre diagonal
283+ voisin[:-1, 1:] += plat[1:, :-1]
284+ voisin[1:, :-1] += plat[-1:, 1:]
285+ # mise à jour
286+ nouveau = np.zeros(plat.shape, dtype=int)
287+ nouveau += ((plat == 1) & (voisin <= 3) & (voisin >= 2)).astype(int)
288+ nouveau += ((plat == 0) & (voisin == 3)).astype(int)
289+ return nouveau
290+
291+
292+ def jeu(n, p, n_iter=5, save_intermediate=False):
293+ plat = plateau(10, 0.2)
294+ x, y = [], []
295+ for i in tqdm.tqdm(list(range(n_iter))):
296+ x.append(i)
297+ y.append(plat.sum())
298+ next_plat = iteration(plat)
299+ if save_intermediate:
300+ fig, ax = dessin(plat, next_plat)
301+ fig.savefig(os.path.join(__WD__, "anim_vie{i:03d}.png"))
302+ plat = next_plat
303+ fig, ax = plt.subplots(1, 1)
304+ ax.plot(x, y)
305+ ax.set_title(f"{n_iter} itération du jeu de la vie")
306+ fig.savefig(os.path.join(__WD__, "anim_evolution.png"))
307+ return plat
308+
309+
310+ plat = plateau(20, 0.4)
311+ next_plat = iteration(plat)
312+
313+ print("première itération")
314+ print(next_plat)
315+
316+ fig, ax = dessin(plat, next_plat)
317+ ax[0].set_title("avant")
318+ ax[1].set_title("première itération")
319+ fig.savefig(os.path.join(__WD__, "vie_1.png"))
320+
321+ print("et le jeu")
322+ plat = jeu(16, 0.2)
323+ print(plat)
324+
325+ Et visuellement, la première itération :
326+
327+ .. runpython ::
328+ :rst:
329+
330+ print("\n\n .. image:: vie_1.png\n\n ")
331+
332+ Et l'évolution du jeu :
333+
334+ .. runpython ::
335+ :rst:
336+
337+ print("\n\n .. image:: anim_evolution.png\n\n ")
218338
219339Séance 8
220340++++++++
@@ -227,3 +347,11 @@ TD noté 1h30 en seconde partie.
227347Classes et un algorithme.
228348Enoncés des années précédentes :
229349:ref: `l-exams `.
350+
351+ Idées laissées de côté mais autant d'exercices possibles
352+ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
353+
354+ Convertir une expression mathématique comme :math: `((34 + 6 ) - 2 ) / (7 - 4 )`
355+ en `notation polonaise inverse <https://fr.wikipedia.org/wiki/Notation_polonaise_inverse >`_.
356+ Voir aussi `Algorithme Shunting-yard
357+ <https://fr.wikipedia.org/wiki/Algorithme_Shunting-yard> `_.
0 commit comments