From 313fdeff36951f1cd5d04c350cccb877de2dff6c Mon Sep 17 00:00:00 2001 From: xadupre Date: Mon, 21 Oct 2024 00:49:08 +0200 Subject: [PATCH 1/3] space --- _doc/articles/2024/2024-11-31-route2024.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/_doc/articles/2024/2024-11-31-route2024.rst b/_doc/articles/2024/2024-11-31-route2024.rst index 5966e45..b006319 100644 --- a/_doc/articles/2024/2024-11-31-route2024.rst +++ b/_doc/articles/2024/2024-11-31-route2024.rst @@ -193,6 +193,7 @@ Ensuite le voyageur de commerce. optim = solution_permutations(villes) print("-- optimisation gourmande:", distance(optim)) + print() print("-- optimisation plus rapide mais approchée...") optim = solution_croisement(villes) print("-- optimisation plus rapide mais approchée", distance(optim)) @@ -202,6 +203,7 @@ Ensuite le voyageur de commerce. fig.savefig(os.path.join(__WD__, "tsp_simple.png")) text = ".. image:: tsp_simple.png" + print("\n\n") print(text) Séance 7 From 2f7f317401f08f3bd0c29a5af269d006f9920f4a Mon Sep 17 00:00:00 2001 From: xadupre Date: Sat, 2 Nov 2024 10:40:09 +0100 Subject: [PATCH 2/3] add session 7 --- .gitignore | 1 + _doc/articles/2024/2024-11-31-route2024.rst | 123 +++++++++++++++++++- 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9bfcbde..581c686 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ *.pickle *.shp *.shx +*.png .coverage data.* paris*.* diff --git a/_doc/articles/2024/2024-11-31-route2024.rst b/_doc/articles/2024/2024-11-31-route2024.rst index b006319..0b94d34 100644 --- a/_doc/articles/2024/2024-11-31-route2024.rst +++ b/_doc/articles/2024/2024-11-31-route2024.rst @@ -211,10 +211,117 @@ Séance 7 * :ref:`Classe et héritage ` -Convertir une expression mathématique comme :math:`((34 + 6) - 2) / (7 - 4)` -en `notation polonaise inverse `_. -Voir aussi `Algorithme Shunting-yard -`_. +Bouts de code, un peu d'optimization. + +.. runpython:: + :showcode: + + import numpy as np + + def calcul(h1,h2,v1,v2,x): + t1 = np.sqrt(x ** 2 + h1**2) / v1 + t2 = np.sqrt((1-x) ** 2 + h2**2) / v2 + return t1 + t2 + + h1, h2, v1, v2 = 1, 0.5, 1, 0.8 + p = np.arange(6) / 5 + print(p) + print(calcul(1,1,1,1,p)) + print(calcul(h1,h2,v1,v2,p)) + + def calcul_entrepot(v1,v2,A, B, Es): + # A: [[0, 0]], B: [[1, 1]], Es: [[0.3, 0.4], [...]] + t1 = np.sqrt(((A - Es) ** 2).sum(axis=1)) / v1 + t2 = np.sqrt(((B - Es) ** 2).sum(axis=1)) / v2 + return t1 + t2 + + A = np.array([[0,0]]) + B = np.array([[1,1]]) + Es = np.array([[0.5, 0.5], [0.1, 0.1], [0, 0.1]]) + print("---------") + print(calcul_entrepot(v1,v2,A, B, Es)) + +Jeu de la vie: + +.. runpython:: + :showcode: + :rst: + + import os + import numpy as np + import matplotlib.pyplot as plt + import tqdm + + + def plateau(n, p=0.5): + return (np.random.rand(n, n) < p).astype(int) + + + def dessin(plat, next_plat): + fig, ax = plt.subplots(1, 2) + ax[0].imshow(plat.astype(float)) + ax[0].get_xaxis().set_visible(False) + ax[0].get_yaxis().set_visible(False) + ax[1].imshow(next_plat.astype(float)) + ax[1].get_xaxis().set_visible(False) + ax[1].get_yaxis().set_visible(False) + return fig, ax + + + def iteration(plat): + voisin = np.zeros(plat.shape, dtype=int) + i, j = plat.shape + # voisin gauche, droite + voisin[:-1, :] += plat[1:, :] + voisin[1:, :] += plat[:-1, :] + # voisin haut,bas + voisin[:, :-1] += plat[:, 1:] + voisin[:, 1:] += plat[:, :-1] + # voisin diagonal + voisin[:-1, :-1] += plat[1:, 1:] + voisin[1:, 1:] += plat[:-1, :-1] + # voisin autre diagonal + voisin[:-1, 1:] += plat[1:, :-1] + voisin[1:, :-1] += plat[-1:, 1:] + # mise à jour + nouveau = np.zeros(plat.shape, dtype=int) + nouveau += ((plat == 1) & (voisin <= 3) & (voisin >= 2)).astype(int) + nouveau += ((plat == 0) & (voisin == 3)).astype(int) + return nouveau + + + def jeu(n, p, n_iter=5, save_intermediate=False): + plat = plateau(10, 0.2) + x, y = [], [] + for i in tqdm.tqdm(list(range(n_iter))): + x.append(i) + y.append(plat.sum()) + next_plat = iteration(plat) + if save_intermediate: + fig, ax = dessin(plat, next_plat) + fig.savefig(os.path.join(__WD__, "anim_vie{i:03d}.png")) + plat = next_plat + fig, ax = plt.subplots(1, 1) + ax.plot(x, y) + ax.set_title(f"{n_iter} itération du jeu de la vie") + fig.savefig(os.path.join(__WD__, "anim_evolution.png")) + + + plat = plateau(20, 0.4) + next_plat = iteration(plat) + + print("``première itération``") + print(next_plat) + + fig, ax = dessin(plat, next_plat) + ax[0].set_title("avant") + ax[1].set_title("première itération") + fig.savefig(os.path.join(__WD__, "vie_1.png")) + + print("\n\n.. image:: vie_1.png\n\n") + + jeu(16, 0.2) + print("\n\n.. image:: anim_evolution.png\n\n") Séance 8 ++++++++ @@ -227,3 +334,11 @@ TD noté 1h30 en seconde partie. Classes et un algorithme. Enoncés des années précédentes : :ref:`l-exams`. + +Idées laissées de côté mais autant d'exercices possibles +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +Convertir une expression mathématique comme :math:`((34 + 6) - 2) / (7 - 4)` +en `notation polonaise inverse `_. +Voir aussi `Algorithme Shunting-yard +`_. From 03b2061eddad6009826ebf53ce5c8702be604874 Mon Sep 17 00:00:00 2001 From: xadupre Date: Sat, 2 Nov 2024 13:39:05 +0100 Subject: [PATCH 3/3] push --- _doc/articles/2024/2024-11-31-route2024.rst | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/_doc/articles/2024/2024-11-31-route2024.rst b/_doc/articles/2024/2024-11-31-route2024.rst index 0b94d34..68e37df 100644 --- a/_doc/articles/2024/2024-11-31-route2024.rst +++ b/_doc/articles/2024/2024-11-31-route2024.rst @@ -245,7 +245,6 @@ Jeu de la vie: .. runpython:: :showcode: - :rst: import os import numpy as np @@ -305,12 +304,13 @@ Jeu de la vie: ax.plot(x, y) ax.set_title(f"{n_iter} itération du jeu de la vie") fig.savefig(os.path.join(__WD__, "anim_evolution.png")) + return plat plat = plateau(20, 0.4) next_plat = iteration(plat) - print("``première itération``") + print("première itération") print(next_plat) fig, ax = dessin(plat, next_plat) @@ -318,9 +318,22 @@ Jeu de la vie: ax[1].set_title("première itération") fig.savefig(os.path.join(__WD__, "vie_1.png")) + print("et le jeu") + plat = jeu(16, 0.2) + print(plat) + +Et visuellement, la première itération : + +.. runpython:: + :rst: + print("\n\n.. image:: vie_1.png\n\n") - jeu(16, 0.2) +Et l'évolution du jeu : + +.. runpython:: + :rst: + print("\n\n.. image:: anim_evolution.png\n\n") Séance 8