diff --git a/antes_colony/Makefile b/antes_colony/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..61f9b97308503244735bf058a83a1fafa0bfd249 --- /dev/null +++ b/antes_colony/Makefile @@ -0,0 +1,46 @@ +CC=gcc + +CFLAGS=-Wall -Wextra -std=c11 -g -fsanitize=leak -fsanitize=address -fsanitize=address + +LDFLAGS=-lSDL2 -lm -O3 + +VPATH:=fourmi/ graphe/ matrix/ stack/ + +all: tp_fourmi + +tp_fourmi: fourmi.o graphe.o stack.o matrix.o main.o + $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) + + + +matrix.o: matrix.h + +stack.o: stack.h + +fourmi.o: fourmi.h + +graphe.o: graphe.h + + +main.o: graphe.h + +execute: + + ./tp_fourmi + +cls: + + clear + +clean: + + rm -rf *.o tp_fourmi + + $(MAKE) cls + + + +rebuild: + + $(MAKE) clean + $(MAKE) tp_fourmi \ No newline at end of file diff --git a/antes_colony/fourmi.o b/antes_colony/fourmi.o new file mode 100644 index 0000000000000000000000000000000000000000..3fd1b78186ea6b3e8c0614be7162b1ffd9db06f0 Binary files /dev/null and b/antes_colony/fourmi.o differ diff --git a/antes_colony/fourmi/fourmi.c b/antes_colony/fourmi/fourmi.c new file mode 100644 index 0000000000000000000000000000000000000000..aeec21544e53a3df471042dab62366cf4e3db3e8 --- /dev/null +++ b/antes_colony/fourmi/fourmi.c @@ -0,0 +1,19 @@ +#include "fourmi.h" + + +frmi create_fourmi() +{ + frmi fr = malloc(sizeof(f)); + fr->path = malloc(sizeof(stack)); + stack_init(fr->path, 100); + fr->distance = 0; + fr->ttl = 7; + return fr; +} + +void destroy_fourmi(frmi f) +{ + stack_destroy(f->path); + free(f->path); + free(f); +} diff --git a/antes_colony/fourmi/fourmi.h b/antes_colony/fourmi/fourmi.h new file mode 100644 index 0000000000000000000000000000000000000000..886cbb0150b8e960b47901d2d4879df93f40391c --- /dev/null +++ b/antes_colony/fourmi/fourmi.h @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> +#include "../matrix/matrix.h" +#include "../stack/stack.h" + +#ifndef _FOURMI_ +#define _FOURMI_ +#define food_address 3 + +typedef struct _fourmi +{ + stack *path; + int distance; + int ttl; + int depose_pheromone; +}f; + +typedef f *frmi; + +frmi create_fourmi(); + +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/antes_colony/graphe.o b/antes_colony/graphe.o new file mode 100644 index 0000000000000000000000000000000000000000..509ee7aa530f3df2435dac25674d2834fd532455 Binary files /dev/null and b/antes_colony/graphe.o differ diff --git a/antes_colony/graphe/graphe.c b/antes_colony/graphe/graphe.c new file mode 100644 index 0000000000000000000000000000000000000000..6e23c042bcf97eebfcd6f578ad90af13c593cbfb --- /dev/null +++ b/antes_colony/graphe/graphe.c @@ -0,0 +1,285 @@ +#include "graphe.h" +#define RHO 0.5 +graph create_graph(int num) +{ + graph gr = malloc(sizeof(g)); + if (gr == NULL) + { + return NULL; + } + gr->num = num; + // init edges (links) matrix + matrix *mat = malloc(sizeof(matrix)); + matrix_init(mat, num, num, 0); + gr->edges = mat; + + // init pheromon matrix + matrix *mat2 = malloc(sizeof(matrix)); + matrix_init(mat2, num, num, 1); + gr->pheromone = mat2; + printf("rand %d, \n", rand()); + return gr; +} + +int has_edge(graph g, int from_node, int to_node) +{ + assert(g != NULL); + assert(from_node < g->num); + assert(to_node < g->num); + return g->edges->data[from_node][to_node]; +} + +void add_edge(graph g, int from_node, int to_node, double distance) +{ + assert(g != NULL); + assert(from_node < g->num); + assert(to_node < g->num); + if (has_edge(g, from_node, to_node)) + { + return; + } + + g->edges->data[from_node][to_node] = distance; +} + +int nb_voisin(graph g, int noeud) +{ + int nb_noeud = 0; + for (int i = 0; i < g->num; i++) + { + if (has_edge(g, noeud, i)) + { + nb_noeud++; + } + } + return nb_noeud; +} + +double ph_x_dist(graph g, int from, int to) +{ + + double ph_x_dst = g->pheromone->data[from][to] * (1 / g->edges->data[from][to]); + // printf("phx : %f", g->pheromone->data[1][0]); + + return ph_x_dst; +} + +stack ants_path(stack frmi, int path) +{ + stack_push(&frmi, path); + return frmi; +} + +void pheromone_dissipation(graph g) +{ + for (int from = 0; from < g->num; from++) + { + for (int to = 0; to < g->num; to++) + { + + g->pheromone->data[from][to] = (1-RHO)*g->pheromone->data[from][to]; + + } + } +} + +int path_probability_taken(graph g, int noeud) +{ + printf("rentré par : %d\n", noeud); + double tab[g->num]; + double tab_cumul[g->num]; + // initialisation des tableaux à 0 + for (int k = 0; k < g->num; k++) + { + tab[k] = 0; + tab_cumul[k] = 0; + } + // mettre les phéromones aux places du tableau pour le choix de l'indicee + double somme_pour_proba = 0; + for (int r = 0; r < g->num; r++) + { + if (has_edge(g, noeud, r)) + { + somme_pour_proba += ph_x_dist(g, noeud, r); + } + } + printf("dénominateur : %f \n", somme_pour_proba); + // printf("%d somme proab\n", (int)somme_pour_proba); + for (int i = 0; i < g->num; i++) + { + if (has_edge(g, noeud, i)) + { + tab[i] = (g->pheromone->data[noeud][i] * (1.0 / g->edges->data[noeud][i])) / somme_pour_proba; + printf("valeur dans tab[%d]: %f\n", i, tab[i]); + } + } + // Calcul nombre total de phéromone + double sum_phero = 0; + for (int j = 0; j < g->num; j++) + { + sum_phero += tab[j]; + } + printf("somme total phéromone : %f\n", sum_phero); + + // somme cumulative du tableau + for (int x = g->num - 1; x >= 0; x--) + { + for (int y = 0; y <= x; y++) + { + tab_cumul[x] += tab[y]; + } + printf("valeur dans tabcumul[%d]: %f\n", x, tab[x]); + } + + // print tableau + // for (int a = 0; a < g->num; a++) + // { + // printf("[%f]", tab[a]); + // } + printf("cumul : "); + for (int b = 0; b < g->num; b++) + { + printf("[%f]", tab_cumul[b]); + } + printf("\n"); + // printf("\n"); + + // calcul de la probabilité + + double chemin_probabiliste = (double)rand() / (double)RAND_MAX; // nb entre 0 et 1 + printf("%f, \n", chemin_probabiliste); + printf("nombre aléatoire tiré : %f\n", chemin_probabiliste); + // if(chemin_probabiliste == 0) + // { + // chemin_probabiliste = 1; + // } + for (int z = 0; z < g->num; z++) + { + if (chemin_probabiliste <= tab_cumul[z]) + { + printf("passe par %d -> %d\n", noeud, z); + // printf("le nombre de phéromone %d et la probabilité est %d et z est %d et tab_cumul est %d\n", sum_phero, chemin_probabiliste, z, tab_cumul[z]); + return z; + } + } + // chemin_probabiliste = chemin_probabiliste % (sum_phero +1); + printf("\n"); + // printf("le nombre de phéromone %d et la probabilité est %d\n", sum_phero, chemin_probabiliste); + return 0; +} + +double pheromone_depose(double distance) +{ + return 1.0000 / distance; +} + +void random_path(graph g, int noeud, frmi f) +{ + // f->ttl--; + // condition d'arret si on arrive au noeud final ou si le ttl est à 0 + if (noeud == 6 || f->ttl <= 0) + { + return; + } + //////////////////////////////////////////////////////////////////////// + // calcul chemin aléatoire et renvoi l'indice du chemin à prendre + int chemin_aleatoire; + // do + // { + chemin_aleatoire = path_probability_taken(g, noeud); + // } while (!has_edge(g, noeud, chemin_aleatoire)); + //----------------------------------------------------------------- + + // 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) + random_path(g, chemin_aleatoire, f); + printf("phero depose %f\n", pheromone_depose(f->distance)); + // if(f->ttl == 0) + // { + // return; + // } + g->pheromone->data[noeud][chemin_aleatoire] = ((1-RHO)*g->pheromone->data[noeud][chemin_aleatoire])+ pheromone_depose(f->distance); + printf("déposé comme phéromone : %f\n", g->pheromone->data[noeud][chemin_aleatoire]); + printf("f->distance = %d\n", f->distance); + // print_stack(*f->path); + // if(noeud == 0) + // { + // pheromone_dissipation(g); + // } +} + +void create_graph_vis(graph g) +{ + FILE *file = fopen("test2.dot", "w"); + int affiche = 0; + fprintf(file, "digraph Test {\n"); + for (int from = 0; from < g->num; from++) + { + for (int to = 0; to < g->num; to++) + { + if (g->edges->data[from][to]) + { + fprintf(file, "%d->%d[penwidth=2, label= %f]\n", from, to, g->edges->data[from][to]); + } + } + affiche++; + } + + fprintf(file, "}"); + + fclose(file); +} + +void shortest_path(graph g, frmi f, int noeud) +{ + printf("%d noeud", noeud); + if (noeud == 6) + { + return; + } + double bigger = 0; + 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) + { + bigger = g->pheromone->data[noeud][i]; + index = i; + } + if (i == g->num - 1) + { + printf("s\n\n"); + + shortest_path(g, f, index); + stack_push(f->path, noeud); + stack_push(f->path, index); + } + } +} + +void display_graph_vis() +{ + + system("dot -Tpng -O test2.dot"); + system("xdg-open test2.dot.png"); +} + +void destroy_graph(graph g) +{ + printf("distance_pheromone : %.0f\n", ph_x_dist(g, 0, 1)); + printf("phero : %f\n", g->pheromone->data[0][1]); + if (g->edges->data == NULL) + { + return; + } + + matrix_destroy(g->edges); + + matrix_destroy(g->pheromone); + free(g); +} \ No newline at end of file diff --git a/antes_colony/graphe/graphe.h b/antes_colony/graphe/graphe.h new file mode 100644 index 0000000000000000000000000000000000000000..e0d3b1c2d70956b531f44751a2f0b31f6581accb --- /dev/null +++ b/antes_colony/graphe/graphe.h @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <stdbool.h> +#include <time.h> +#include "../matrix/matrix.h" +#include "../stack/stack.h" +#include "../fourmi/fourmi.h" +#ifndef _GRAPHE_ +#define _GRAPHE_ + +typedef struct _graph +{ + int num; + matrix *edges; + matrix *pheromone; +} g; + +typedef g *graph; + +graph create_graph(int num); + +int has_edge(graph g, int from_node, int to_node); + +void add_edge(graph g, int from_node, int to_node, double distance); + +int nb_voisin(graph g, int noeud); + +void random_path(graph g, int noeud, frmi f); + +void shortest_path(graph g, frmi f, int noeud); + +// void first_walk(graph g, int index, stack *s); +// void walk(graph g, int index); +void create_graph_vis(graph g); + +void display_graph_vis(); + +void destroy_graph(graph g); + +#endif \ No newline at end of file diff --git a/antes_colony/main.c b/antes_colony/main.c new file mode 100644 index 0000000000000000000000000000000000000000..30b8c673750d2f3bf8451aec264d64c730b10ca3 --- /dev/null +++ b/antes_colony/main.c @@ -0,0 +1,66 @@ +#include "graphe/graphe.h" +// #include "stack/stack.h" + +int main() +{ + double testestset = 0; + testestset = ((0.5)*1.0)+(45.0/434.0); + printf("test calcul %f\n",testestset); + srand(time(NULL)); + graph g1 = create_graph(7); + stack stack; + stack_init(&stack, 50); + + // printf("\n"); + // matrix_print(*g1->pheromone); + // printf("\nadded edge\n\n"); + add_edge(g1, 4, 3, 1.0); + add_edge(g1, 1, 3, 2.0); + add_edge(g1, 2, 3, 2.0); + add_edge(g1, 4, 1, 3.0); + add_edge(g1, 0, 1, 5.0); + add_edge(g1, 0, 4, 1.0); + add_edge(g1, 0, 2, 1.0); + add_edge(g1, 0, 3, 8.0); + add_edge(g1, 3, 5, 1.0); + add_edge(g1, 5, 6, 1.0); + add_edge(g1, 2, 5, 2.0); + add_edge(g1, 1, 6, 4.0); + add_edge(g1, 0, 5, 5.0); + matrix_print(*g1->pheromone); + for (int i = 0; i < 10000; i++) + { + frmi f = create_fourmi(); + random_path(g1, 0, f); + destroy_fourmi(f); + } + + printf("\n length path matrix \n"); + matrix_print(*g1->edges); + printf("\npheromone matrix \n"); + matrix_print(*g1->pheromone); + // printf("\npheromone changed\n\n"); + // pheromone_dissipation(g1); + // matrix_print(*g1->pheromone); + // algorithme_des_fourmils(1, 3, g1); + + printf("===================================================="); + // first_walk(g1, 0, &stack); + printf("\n%d a %d voisins \n", 0, nb_voisin(g1, 0)); + + printf("===================================================="); + + create_graph_vis(g1); + display_graph_vis(); + frmi eclaireuse = create_fourmi(); + // shortest_path(g1, eclaireuse, 0); + // printf("shortest path is : "); + // print_stack(*eclaireuse->path); + // printf("\n"); + // destroy_fourmi(eclaireuse); + + destroy_graph(g1); + stack_destroy(&stack); + + return 0; +} \ No newline at end of file diff --git a/antes_colony/main.o b/antes_colony/main.o new file mode 100644 index 0000000000000000000000000000000000000000..de911c75734588e69513b3659949212736815200 Binary files /dev/null and b/antes_colony/main.o differ diff --git a/antes_colony/matrix.o b/antes_colony/matrix.o new file mode 100644 index 0000000000000000000000000000000000000000..7a3327178aab672328497ed3064c12f0d0d84257 Binary files /dev/null and b/antes_colony/matrix.o differ diff --git a/antes_colony/matrix/matrix.c b/antes_colony/matrix/matrix.c new file mode 100644 index 0000000000000000000000000000000000000000..9e42e0bd3cf44d1dc75cf94e72c77b519b8ba902 --- /dev/null +++ b/antes_colony/matrix/matrix.c @@ -0,0 +1,183 @@ +#include "matrix.h" + +error_code matrix_alloc(matrix *mat, int li, int co) +{ + mat->data = malloc(li * sizeof(double*)); + if(mat->data == NULL) + { + return err; + } + for(int row = 0; row < li; row++) + { + mat->data[row] = malloc(co * sizeof(double)); + if(mat->data[row] == NULL) + { + return err; + } + } + return ok; +} + +error_code matrix_init(matrix *mat, int li, int co, double val) +{ + if( matrix_alloc(mat, li, co) == err) return err; + mat->rows = li; + mat->cols = co; + for(int row = 0; row < li; row++) + { + for(int col = 0; col < co; col++) + { + mat->data[row][col] = val; + } + } + return ok; +} + +error_code matrix_print(const matrix mat) +{ + printf(" 0 1 2 3 4 5 6\n"); + printf(" - - - - -\n"); + for(int row = 0; row < mat.rows; row++) + { + printf("%d | ", row); + for(int col = 0; col < mat.cols; col++) + { + printf("%f ", mat.data[row][col]); + } + printf("\n"); + } + return ok; +} + +error_code matrix_destroy(matrix *mat) +{ + for(int row = 0; row < mat->rows; row++) + { + free(mat->data[row]); + } + free(mat->data); + mat->rows = -1; + mat->cols = -1; + mat->data = NULL; + free(mat); + return ok; +} + +// error_code matrix_init_from_array(matrix *mat, int li, int co, int data[], int s) //data [0] = mat->data[0][0] data[3] = mat->data[1][1] +// { +// if(s != li*co) +// { +// return err; +// } +// for(int row = 0; row < li; row++) +// { +// for(int col = 0; col < co; col++) +// { +// mat->data[row][col] = data[col + (row * li)]; +// } +// } +// return ok; +// } + +// error_code matrix_clone(matrix *cloned, const matrix mat) +// { +// for(int row = 0; row < mat.rows; row++) +// { +// for(int col = 0; col < mat.cols; col++) +// { +// cloned->data[row][col] = mat.data[row][col]; +// } +// } +// return ok; +// } + +// error_code matrix_transpose(matrix *transposed, const matrix mat) +// { +// for(int row = 0; row < mat.rows; row++) +// { +// for(int col = 0; col < mat.cols; col++) +// { +// transposed->data[col][row] = mat.data[row][col]; +// } +// } +// return ok; +// } +// error_code matrix_extract_submatrix(matrix *sub, const matrix mat,int li0, int li1, int co0, int co1) +// { +// int row = li1 - li0; +// int col = co1 - co0; +// if(row < sub->rows || col < sub->cols || row > sub->rows || col > sub->cols) +// { +// return err; +// } +// for(int i = li0; i < li1; i++) +// { +// for(int j = co0; j < co1; j++) +// { +// sub->data[i-li0][j-co0] = mat.data[i][j]; +// } +// } +// return ok; +// } + +// bool matrix_is_equal(matrix mat1, matrix mat2) +// { +// int estVrai = 1; +// assert(estVrai == 1); +// if(mat1.rows != mat2.rows) +// { +// return false; +// } +// else if(mat1.cols != mat2.cols) +// { +// return false; +// } +// else +// { +// for(int row = 0; row < mat1.rows; row++) +// { +// for(int col = 0; col < mat1.cols; col++) +// { +// if(mat1.data[row][col] != mat2.data[row][col]) +// { +// estVrai = 0; +// } + +// } +// } +// if(estVrai == 1) +// { +// return true; +// } +// else +// { +// return false; +// } +// } +// } + +// error_code matrix_get(int *elem, const matrix mat, int ix, int iy) +// { +// if(ix >= mat.rows || iy >= mat.cols) +// { +// return err; +// } +// else +// { +// *elem = mat.data[ix][iy]; +// return ok; +// } +// } + +// error_code matrix_set(matrix *mat, int ix, int iy, int elem) +// { +// if(ix >= mat->cols || iy >= mat->cols) +// { +// return err; +// } +// else +// { +// mat->data[ix][iy] = elem; +// return ok; +// } +// } \ No newline at end of file diff --git a/antes_colony/matrix/matrix.h b/antes_colony/matrix/matrix.h new file mode 100644 index 0000000000000000000000000000000000000000..4a1675aa9d253fe23cb0a958200e070c7eaf6fbb --- /dev/null +++ b/antes_colony/matrix/matrix.h @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +#ifndef _MATRIX_ +#define _MATRIX_ + +typedef enum _error_code { + ok, err +} error_code; + +typedef struct _matrix { + int rows; // number of rows + int cols; // number of columns + double** data; // un pointeur sur un tableau de ligne n +} matrix; + +error_code matrix_alloc(matrix *mat, int li, int co); + +error_code matrix_init(matrix *mat, int m, int n, double val); + +error_code matrix_print(const matrix mat); + +error_code matrix_destroy(matrix *mat); + +error_code matrix_init_from_array(matrix *mat, int li, int co, int data[], int s); + +error_code matrix_clone(matrix *cloned, const matrix mat); + +error_code matrix_transpose(matrix *transposed, const matrix mat); + +error_code matrix_extract_submatrix(matrix *sub, const matrix mat,int li0, int li1, int co0, int co1); + +bool matrix_is_equal(matrix mat1, matrix mat2); + +error_code matrix_get(int *elem, const matrix mat, int ix, int iy); + +error_code matrix_set(matrix *mat, int ix, int iy, int elem); + +#endif \ No newline at end of file diff --git a/antes_colony/stack.o b/antes_colony/stack.o new file mode 100644 index 0000000000000000000000000000000000000000..242931fa3944b295d33589a679d003ad1de2adf3 Binary files /dev/null and b/antes_colony/stack.o differ diff --git a/antes_colony/stack/stack.c b/antes_colony/stack/stack.c new file mode 100644 index 0000000000000000000000000000000000000000..0b5e8be4863ca13553fe567732b0595b151d904c --- /dev/null +++ b/antes_colony/stack/stack.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include "stack.h" + +void stack_init(stack *s, int max_capacity) +{ + s->size = max_capacity; + s->data = malloc(max_capacity * sizeof(int)); + s->top = -1; +} + +void print_stack(stack s) +{ + if(stack_is_empty(s)){ + printf("stack is empty\n"); + }else { + + for(int i = 0; i < s.top+1; i++){ + printf("[%d] ", s.data[i]); + } + printf("\n"); + } + +} + +void stack_push(stack *s, int value) +{ + if(s->top == s->size-1) + { + printf("stack full\n"); + return; + } + else + { + s->top++; + s->data[s->top] = value; + } + +} + +void stack_pop(stack *s, int *val) //échoue si vide +{ + if(stack_is_empty(*s)) + { + printf("empty stack\n"); + return; + } + else + { + *val = s->data[s->top]; + s->top--; + } + +} + +void stack_peek(stack s, int *val)//return the top of the stack without pooping +{ // error if stakc is empty or NULL + *val = s.data[s.top]; +} + +bool stack_is_empty(stack s)//check if stack is empty +{ + return s.top < 0; +} + +void stack_destroy(stack *s) +{ + s->top = -1; + free(s->data); +} + +void stack_to_stack(stack *stack_emet, stack *stack_dest) +{ + if (stack_is_empty(*stack_emet)){ + printf("stack empty"); + return; + } + int valeur; + stack_pop(stack_emet, &valeur); + stack_push(stack_dest, valeur); +} \ No newline at end of file diff --git a/antes_colony/stack/stack.h b/antes_colony/stack/stack.h new file mode 100644 index 0000000000000000000000000000000000000000..e22fbd77007907e3417b4d2c49864f1e415476d6 --- /dev/null +++ b/antes_colony/stack/stack.h @@ -0,0 +1,32 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> + +#ifndef _STACK_ +#define _STACK_ + +typedef struct _stack { + int size; + int top; + int *data; +} stack; + +void stack_init(stack *s, int max_apacity); + +void print_stack(stack s); + +void stack_push(stack *s, int value); + +void stack_pop(stack *s, int *val); + +void stack_peek(stack s, int *val); + +bool stack_is_empty(stack s); + +void stack_to_stack(stack *stack_emet, stack *stack_dest); + +void stack_destroy(stack *s); + +#endif + diff --git a/antes_colony/stack_stack/stack_stack.c b/antes_colony/stack_stack/stack_stack.c new file mode 100644 index 0000000000000000000000000000000000000000..9669645ebce1b1ef2716f3deb9d6bae2e3fd20ac --- /dev/null +++ b/antes_colony/stack_stack/stack_stack.c @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <assert.h> +#include "stack.h" + +typedef struct _stack_stack { + int size; + int top; + stack *data; +} stack_stack; \ No newline at end of file diff --git a/antes_colony/stack_stack/stack_stack.h b/antes_colony/stack_stack/stack_stack.h new file mode 100644 index 0000000000000000000000000000000000000000..0b5e8be4863ca13553fe567732b0595b151d904c --- /dev/null +++ b/antes_colony/stack_stack/stack_stack.h @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include "stack.h" + +void stack_init(stack *s, int max_capacity) +{ + s->size = max_capacity; + s->data = malloc(max_capacity * sizeof(int)); + s->top = -1; +} + +void print_stack(stack s) +{ + if(stack_is_empty(s)){ + printf("stack is empty\n"); + }else { + + for(int i = 0; i < s.top+1; i++){ + printf("[%d] ", s.data[i]); + } + printf("\n"); + } + +} + +void stack_push(stack *s, int value) +{ + if(s->top == s->size-1) + { + printf("stack full\n"); + return; + } + else + { + s->top++; + s->data[s->top] = value; + } + +} + +void stack_pop(stack *s, int *val) //échoue si vide +{ + if(stack_is_empty(*s)) + { + printf("empty stack\n"); + return; + } + else + { + *val = s->data[s->top]; + s->top--; + } + +} + +void stack_peek(stack s, int *val)//return the top of the stack without pooping +{ // error if stakc is empty or NULL + *val = s.data[s.top]; +} + +bool stack_is_empty(stack s)//check if stack is empty +{ + return s.top < 0; +} + +void stack_destroy(stack *s) +{ + s->top = -1; + free(s->data); +} + +void stack_to_stack(stack *stack_emet, stack *stack_dest) +{ + if (stack_is_empty(*stack_emet)){ + printf("stack empty"); + return; + } + int valeur; + stack_pop(stack_emet, &valeur); + stack_push(stack_dest, valeur); +} \ No newline at end of file diff --git a/antes_colony/test.dot b/antes_colony/test.dot new file mode 100644 index 0000000000000000000000000000000000000000..acc96be732bbc583278fd3906b89c3a830e4ef04 --- /dev/null +++ b/antes_colony/test.dot @@ -0,0 +1,13 @@ +digraph Test { +0->1->3->1->6->25; +1->1->3->1->6->25; +2->1->3->1->6->25; +3->1->3->1->6->25; +4->1->3->1->6->25; +5->1->3->1->6->25; +6->1->3->1->6->25; +7->1->3->1->6->25; +8->1->3->1->6->25; +4->3->1->3->9; +1->3->1->6->25; +} diff --git a/antes_colony/test2.dot b/antes_colony/test2.dot new file mode 100644 index 0000000000000000000000000000000000000000..c1f6d86103eda9cb47dc6d36637534670f351fbd --- /dev/null +++ b/antes_colony/test2.dot @@ -0,0 +1,15 @@ +digraph Test { +0->1[penwidth=2, label= 5.000000] +0->2[penwidth=2, label= 1.000000] +0->3[penwidth=2, label= 8.000000] +0->4[penwidth=2, label= 1.000000] +0->5[penwidth=2, label= 5.000000] +1->3[penwidth=2, label= 2.000000] +1->6[penwidth=2, label= 4.000000] +2->3[penwidth=2, label= 2.000000] +2->5[penwidth=2, label= 2.000000] +3->5[penwidth=2, label= 1.000000] +4->1[penwidth=2, label= 3.000000] +4->3[penwidth=2, label= 1.000000] +5->6[penwidth=2, label= 1.000000] +} \ No newline at end of file diff --git a/antes_colony/test2.dot.png b/antes_colony/test2.dot.png new file mode 100644 index 0000000000000000000000000000000000000000..7d7a38cb9bb7b96a1d33a999ce120720ba19b9d8 Binary files /dev/null and b/antes_colony/test2.dot.png differ diff --git a/antes_colony/tp_fourmi b/antes_colony/tp_fourmi new file mode 100755 index 0000000000000000000000000000000000000000..34468f5c7974b76da0dcd664823c59434f4e9227 Binary files /dev/null and b/antes_colony/tp_fourmi differ