Skip to content
Snippets Groups Projects
Commit 53074936 authored by agnon.kurteshi's avatar agnon.kurteshi
Browse files

peu être le dernier, ou l'avant dernier push

parent a1a20e50
No related branches found
No related tags found
No related merge requests found
Matrice.png

18.2 KiB

......@@ -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;
}
......
......@@ -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
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
......@@ -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++)
{
//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);
......
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
liste_chaine.jpg

45 KiB

......@@ -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");
......
......@@ -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
test2.dot.png

49.8 KiB | W: | H:

test2.dot.png

49.9 KiB | W: | H:

test2.dot.png
test2.dot.png
test2.dot.png
test2.dot.png
  • 2-up
  • Swipe
  • Onion skin
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment