diff --git a/Matrice.png b/Matrice.png deleted file mode 100644 index 948b64531373130ca8bc60b07cd5dae142e070e8..0000000000000000000000000000000000000000 Binary files a/Matrice.png and /dev/null differ diff --git a/chemin_plus_court.txt b/chemin_plus_court.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/fourmi/fourmi.c b/fourmi/fourmi.c index 462f51984d647bdbfdb523de9e436a8066962a95..2189dc721b3f8046aba5632828a2470582ab9768 100644 --- a/fourmi/fourmi.c +++ b/fourmi/fourmi.c @@ -7,7 +7,6 @@ frmi create_fourmi(int nb_sommet) fr->path = malloc(sizeof(stack)); stack_init(fr->path, nb_sommet); fr->distance = 0; - fr->ttl = nb_sommet; return fr; } diff --git a/fourmi/fourmi.h b/fourmi/fourmi.h index 5f1a362953e27ef009df6cebd5c0f5103b2d875a..140b2d301fb483ba0083fe1450927c06163b03cc 100644 --- a/fourmi/fourmi.h +++ b/fourmi/fourmi.h @@ -12,9 +12,8 @@ typedef struct _fourmi { stack *path; - int distance; - int ttl; - int depose_pheromone; + int distance; //stocke la distane parcourue par la fourmi + int depose_pheromone; //permet d'activer le dépôt de phéromones }f; typedef f *frmi; @@ -23,40 +22,4 @@ frmi create_fourmi(int nb_sommet); void destroy_fourmi(frmi f); -// // typedef struct _graph{ -// // int numnodes; -// // matrix *edges; -// // matrix *pheromone; -// // } g; -// // //pile deux à deux pour une coordonnée - -// // typedef g *graph; - -// // typedef struct _fourmi -// // { -// // stack *chemin; -// // }f; - -// // typedef f *fourmi; - -// // graph create_graph(int numnodes); - -// // int has_edge(graph g, int from_node, int to_node); - -// // void add_edge(graph g, int from_node, int to_node, int distance); - -// // void pheromone_depose(fourmi f); - -// // void pheromone_dissipation(graph g); - -// // void algorithme_des_fourmils(int fourmiliere, int nourriture, graph g); - -// // void create_graph_vis(graph g); - -// // void destroy_graph(graph g); - -// // void display_graph_vis(); - -// // void create_graph_vis(graph g); - #endif \ No newline at end of file diff --git a/graphe.csv b/graphe.csv index 3e4e0d2ce360dff13cebe646b1bc739bf05c53a8..3278bc97f86fd0756a6db763f3c80f5273be278a 100644 --- a/graphe.csv +++ b/graphe.csv @@ -1,15 +1,15 @@ 8 0 7 0 1 2.0 1 2 5.0 -2 1 5.0 +2 1 1.0 1 3 2.0 3 1 1.0 1 5 2.0 2 3 1.0 -2 6 4.0 +2 6 1.0 3 4 4.0 3 5 2.0 3 6 3.0 -3 7 4.0 +3 7 1.0 4 7 2.0 6 7 2.0 diff --git a/graphe/graphe.c b/graphe/graphe.c index 26d731d2d10cafdf02cb103c2cb1b3d4b72e2fd9..04506eb151de44af3fb917350210401bf8d69625 100644 --- a/graphe/graphe.c +++ b/graphe/graphe.c @@ -124,7 +124,7 @@ int path_probability_taken(graph g, int noeud) { //calcul qui fait [(phéromone * (1/arête))] // [-----------------------] //fraction - // [ 2 ] + // [ somme_proba ] tab[i] = (g->pheromone->data[noeud][i] * (1.0 / g->edges->data[noeud][i])) / somme_pour_proba; } } @@ -175,7 +175,7 @@ int launch_colonization(graph g, int nb_fourmi, double rho) int random_path(graph g, int noeud, double rho, frmi f) { - // condition d'arret si on arrive au noeud final ou si le ttl est à 0 + // condition d'arret si on arrive au noeud final if (noeud == g->nourriture) { f->depose_pheromone = 1; @@ -189,13 +189,10 @@ int random_path(graph g, int noeud, double rho, frmi f) if (chemin_aleatoire == noeud) { f->depose_pheromone = 0; - return 1; + return 1;//retourne 1 pour compter les fourmis perdues } // stocke la distance parcourue dans la fourmi f->distance += g->edges->data[noeud][chemin_aleatoire]; - // push le chemin parcouru dans la stack de la fourmi - stack_push(f->path, noeud); - stack_push(f->path, chemin_aleatoire); // la récursivité de l'algorithme (parcours graphe en profondeur) //récursivité de l'algorithme int fourmi_perdu; @@ -203,6 +200,8 @@ int random_path(graph g, int noeud, double rho, frmi f) if (f->depose_pheromone) { + //calcul qui dépose les phéromone en fonction de la dissipation et des phéromones à déposer + // (1-rho)*phéromone_precedente + pheromone_a_déposer(en fonction de la distance) g->pheromone->data[noeud][chemin_aleatoire] = ((1 - rho) * g->pheromone->data[noeud][chemin_aleatoire]) + pheromone_depose(f->distance); } //permet le calcul du nombre de fourmi perdu récursivement @@ -238,8 +237,7 @@ void create_graph_vis(graph g) void shortest_path(graph g, frmi f, int noeud) { - f->ttl--; - if (noeud == g->nourriture || f->ttl == 0) + if (noeud == g->nourriture) { stack_push(f->path, noeud); return; @@ -248,10 +246,12 @@ void shortest_path(graph g, frmi f, int noeud) int index = 0; for (int i = 0; i < g->num; i++) { - if (g->pheromone->data[noeud][i] > bigger && g->pheromone->data[noeud][i] != 1.0) + //on teste pour récupérer l'index des phéromones les plus grandes en excluant les phéromones à 1 où il n'y a pas de chemin + if (g->pheromone->data[noeud][i] > bigger && g->pheromone->data[noeud][i] != 1.0) { - bigger = g->pheromone->data[noeud][i]; - index = i; + bigger = g->pheromone->data[noeud][i]; //récupère la valeur plus grande + index = i; //on récupère l'index du sommet de destination + } if (i == g->num - 1) { @@ -259,24 +259,28 @@ void shortest_path(graph g, frmi f, int noeud) shortest_path(g, f, index); } } + f->distance+=g->edges->data[noeud][index]; //calcul la longueur du chemin parcouru } void export_shortest_path(graph g1, char *filename) { + //on crée un fourmi éclaireuse qui ira sur les noeuds avec le plus de phéromone à la suite int ecrit_chemin = 0; int trash; FILE *file = fopen(filename, "w"); - fprintf(file, "the shortest path is \n"); + fprintf(file, "the shortest path is "); frmi eclaireuse = create_fourmi(g1->num); shortest_path(g1, eclaireuse, 0); + //part du principe que le chemin le plus court ne sera jamais un chemin qui passe par tous les noeuds if (eclaireuse->path->top == g1->num - 1) { destroy_fourmi(eclaireuse); printf("Aucun chemin ne peut être défini\n"); return; } - printf("shortest path is : "); + printf("shortest path is "); print_stack(*eclaireuse->path); + printf("with a cost of %d\n", eclaireuse->distance); while (!stack_is_empty(*eclaireuse->path)) { @@ -284,7 +288,7 @@ void export_shortest_path(graph g1, char *filename) fprintf(file, "[%d]", eclaireuse->path->data[ecrit_chemin]); ecrit_chemin++; } - + fprintf(file, " with a cost of %d\n", eclaireuse->distance); printf("\n"); destroy_fourmi(eclaireuse); diff --git a/le_plus_court_chemin.txt b/le_plus_court_chemin.txt index c799b6b62927dc681c635a8a9e8622ded8f4a5c9..14ae4e3d4fbf1eb2e6325f0419ba2000de7b8c68 100644 --- a/le_plus_court_chemin.txt +++ b/le_plus_court_chemin.txt @@ -1,2 +1 @@ -the shortest path is -[0][1][3][7] \ No newline at end of file +the shortest path is [0][1][3][7] with a cost of 5 diff --git a/liste_chaine.jpg b/liste_chaine.jpg deleted file mode 100644 index babba3de97046232da31d836913a8a97608ffa67..0000000000000000000000000000000000000000 Binary files a/liste_chaine.jpg and /dev/null differ diff --git a/main.c b/main.c index c9a88df4502159cad90661d3835d42133e06c762..2a18b6aba132f1e326e867c985aed86f30e64ba3 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ int main() //création du graphe à partir d'un csv graph g1 = main_create_graph("graphe.csv"); //lancement de la colonisation des fourmis - nb_fourmi_perdu = launch_colonization(g1, 100000, 0.0001); + nb_fourmi_perdu = launch_colonization(g1, 100000, 0.001); //impression de la matrice des phéromones et des arêtes printf("\n length path matrix \n"); diff --git a/test2.dot b/test2.dot index b55299990ce95960973609700cb1cdf4be876ac9..3883cbf071052c8cf549746a72c2ba563692263f 100644 --- a/test2.dot +++ b/test2.dot @@ -3,14 +3,14 @@ digraph Test { 1->2[penwidth=2, label= 5.000000] 1->3[penwidth=2, label= 2.000000] 1->5[penwidth=2, label= 2.000000] -2->1[penwidth=2, label= 5.000000] +2->1[penwidth=2, label= 1.000000] 2->3[penwidth=2, label= 1.000000] -2->6[penwidth=2, label= 4.000000] +2->6[penwidth=2, label= 1.000000] 3->1[penwidth=2, label= 1.000000] 3->4[penwidth=2, label= 4.000000] 3->5[penwidth=2, label= 2.000000] 3->6[penwidth=2, label= 3.000000] -3->7[penwidth=2, label= 4.000000] +3->7[penwidth=2, label= 1.000000] 4->7[penwidth=2, label= 2.000000] 6->7[penwidth=2, label= 2.000000] } \ No newline at end of file diff --git a/test2.dot.png b/test2.dot.png index 0f3bd97af945318467c84b10d6e3a863bf6f41b2..b5281d2b0711f689167e95ace32c8effbb814986 100644 Binary files a/test2.dot.png and b/test2.dot.png differ diff --git a/tp_fourmi b/tp_fourmi index d594449a5ff2623bfa3c7c14208f8eb65c05a803..a256ec5d48575123f4602c362955ff8dcf26c6f7 100755 Binary files a/tp_fourmi and b/tp_fourmi differ