clear; close all; taille_ecran = get(0, 'ScreenSize'); L = taille_ecran(3); H = taille_ecran(4); delete("saves/exercice2.gif"); % Paramètres : N = 0; % Nombre de disques d'une configuration R = 10; % Rayon des disques nb_points_affichage_disque = 30; increment_angulaire = 2 * pi / nb_points_affichage_disque; theta = 0:increment_angulaire:2 * pi; rose = [253 108 158] / 255; q_max = 1000; nb_affichages = 20; pas_entre_affichages = floor(q_max / nb_affichages); temps_pause = 0.0005; lambda0 = 100; S = 130; gamma = 5; beta = 1; T0 = 0.1; alpha = 0.99; liste_u = 0; liste_N = 0; % Lecture et affichage de l'image : I = imread('colonie.png'); I = rgb2gray(I); I = double(I); I = I(1:400, 100:450); [nb_lignes, nb_colonnes] = size(I); poog = figure('Name', ['Detection de ' num2str(N) ' flamants roses'], 'Position', [0.25 * L, 0, 0.75 * L, 0.5 * H]); c = zeros(N, 2); I_moyen = zeros(N, 1); liste_q = 0; I_moyen_config = mean(I_moyen); liste_I_moyen_config = I_moyen_config; % Affichage de la configuration initiale : subplot(2, 2, 1); imagesc(I); axis image; axis off; colormap gray; hold on; for i = 1:N x_affich = c(i, 1) + R * cos(theta); y_affich = c(i, 2) + R * sin(theta); indices = find(x_affich > 0 & x_affich < nb_colonnes & y_affich > 0 & y_affich < nb_lignes); plot(x_affich(indices), y_affich(indices), 'Color', rose, 'LineWidth', 3); end pause(temps_pause); % Courbe d'évolution du niveau de gris moyen : subplot(2, 2, 2); plot(liste_q, liste_I_moyen_config, '.', 'Color', rose); axis([0 q_max 0 255]); xlabel('Nombre d''iterations'); ylabel('Niveau de gris moyen'); T = T0; lambda = lambda0; pause % Recherche de la configuration optimale : for q = 1:q_max % Tirage aléatoire des nouveaux disques et calcul des niveaux de gris moyen : new_n = poissrnd(lambda); c_alea = [nb_colonnes * rand(new_n, 1) nb_lignes * rand(new_n, 1)]; I_alea = zeros(new_n, 1); for i = 1:new_n I_alea(i) = calcul_I_moyen(I, c_alea(i, :), R); end c_old = c; N = N + new_n; c = [c; c_alea]; I_moyen = [I_moyen; I_alea]; % Calcul de l'énergie et trie energie = 1 - 2 ./ (1 + exp(- gamma * (I_moyen / S - 1))); [energie, indexs] = sort(energie, "descend"); c = c(indexs, :); I_moyen = I_moyen(indexs); % dégager les overlapp a_suppr = []; for i = 1:N for j = i + 1:N if norm((c(i, :) - c(j, :))) <= sqrt(2) * R a_suppr = [a_suppr i]; break end end end c(a_suppr, :) = []; I_moyen(a_suppr) = []; N = size(c, 1); % random death U_total = U(c, I_moyen, beta, gamma, S, R); probas = zeros(N, 1); for i = 1:N set_diff = setdiff(1:N, i); U_sans_i = U(c(set_diff, :), I_moyen(set_diff), beta, gamma, S, R); probas(i) = lambda / (lambda + exp((U_sans_i - U_total) / T)); end a_suppr = rand(length(probas), 1) < probas; c(a_suppr, :) = []; I_moyen(a_suppr) = []; N = size(c, 1); % if size(c_old, 1) == size(c, 1) && all(all(c_old == c)) % break % end % mises à jour : T = alpha * T; lambda = alpha * lambda; hold off; subplot(2, 2, 1); imagesc(I); axis image; axis off; colormap gray; hold on; for j = 1:N x_affich = c(j, 1) + R * cos(theta); y_affich = c(j, 2) + R * sin(theta); indices = find(x_affich > 0 & x_affich < nb_colonnes & y_affich > 0 & y_affich < nb_lignes); plot(x_affich(indices), y_affich(indices), 'Color', rose, 'LineWidth', 3); end pause(temps_pause); % évolution des courbes if rem(q, pas_entre_affichages) == 0 liste_q = [liste_q q]; I_moyen_config = mean(I_moyen); liste_I_moyen_config = [liste_I_moyen_config I_moyen_config]; subplot(2, 2, 2); plot(liste_q, liste_I_moyen_config, '.-', 'Color', rose, 'LineWidth', 3); axis([0 q_max 0 255]); xlabel('Nombre d''iterations'); ylabel('Niveau de gris moyen'); boop = U(c, I_moyen, beta, gamma, S, R); liste_u = [liste_u boop]; subplot(2, 2, 3); plot(liste_q, liste_u, '.-', 'Color', rose, 'LineWidth', 3); xlim([0, q_max]); xlabel('Nombre d''iterations'); ylabel('Énergie'); liste_N = [liste_N N]; subplot(2, 2, 4); plot(liste_q, liste_N, '.-', 'Color', rose, 'LineWidth', 3); xlim([0, q_max]); xlabel('Nombre d''iterations'); ylabel('Nombre de flammants roses'); export_fig(poog, "saves/exercice2.gif", '-png', '-painters', '-m2', '-append'); end end