From 4a5c798b25f1aff94c3b6f72347bf244190b2339 Mon Sep 17 00:00:00 2001 From: Tanguy MAZE Date: Sun, 14 Apr 2019 14:45:38 +0200 Subject: [PATCH] rework on score system need to score with correct amount of ants on each paths also secured ft_atois --- includes/lem_in.h | 6 ++--- srcs/bfs.c | 3 ++- srcs/edmunds_karp.c | 60 ++++++++++++++++++++++++++++----------------- srcs/lem_in.c | 17 +++++++------ 4 files changed, 52 insertions(+), 34 deletions(-) diff --git a/includes/lem_in.h b/includes/lem_in.h index bcc2031..459643d 100644 --- a/includes/lem_in.h +++ b/includes/lem_in.h @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */ -/* Updated: 2019/04/13 14:33:18 by tmaze ### ########.fr */ +/* Updated: 2019/04/14 12:18:02 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ typedef struct s_lmdata { int nbants; int nb_nodes; + int nb_paths_max; t_node *nodes_data; t_ind **adj; } t_lmdata; @@ -135,8 +136,7 @@ t_ind *get_node_path(t_ind *lst, int index); void bfs(t_lmdata *data, t_bfs *tab, int start_ind , int end_ind); -t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind - , int nb_paths); +t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind); int push_ants(t_lmdata *data, t_ind **paths , int nb_paths); diff --git a/srcs/bfs.c b/srcs/bfs.c index 3244e91..caabb44 100644 --- a/srcs/bfs.c +++ b/srcs/bfs.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ -/* Updated: 2019/04/13 12:48:35 by tmaze ### ########.fr */ +/* Updated: 2019/04/14 11:39:52 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,6 +74,7 @@ void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind) { int i; + ft_printf("New bfs\n"); bfs_init(tab, data); bfs_addtoqueue(tab, start_ind, data->nb_nodes); i = 0; diff --git a/srcs/edmunds_karp.c b/srcs/edmunds_karp.c index d35f93e..85d1d09 100644 --- a/srcs/edmunds_karp.c +++ b/srcs/edmunds_karp.c @@ -6,7 +6,7 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */ -/* Updated: 2019/04/13 14:55:19 by tmaze ### ########.fr */ +/* Updated: 2019/04/14 14:03:45 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,15 +48,16 @@ static int get_score(t_lmdata *data, t_ind **ret) i = 0; tot = 0; score = 0; - while (ret[i]) + while (ret && ret[i]) i++; nb_paths = i; + ft_printf("nb_paths: %d\n", nb_paths); i = 0; - while (ret[i] && (it = ret[i])) + while (ret && ret[i] && (it = ret[i])) { - while (it && tot++) + while (it && ++tot) it = it->next; - tot *= (data->nbants / nb_paths); + tot = tot + (data->nbants / nb_paths) - 1; if (score == 0 || tot > score) score = tot; i++; @@ -64,22 +65,24 @@ static int get_score(t_lmdata *data, t_ind **ret) return (score); } -/* static int reset_weights(t_lmdata *data, int s_ind, int node) */ +/* static int reset_weights(t_lmdata *data, int node) */ /* { */ /* t_ind *it; */ /* t_ind *it2; */ /* it = data->adj[node]; */ +/* it2 = NULL; */ /* while (it && it->weight != 2) */ /* it = it->next; */ /* if (it && it->weight == 2) */ /* it->weight = 1; */ -/* it2 = data->adj[it->index]; */ +/* if (it && it->index) */ +/* it2 = data->adj[it->index]; */ /* while (it2 && it2->index != node) */ /* it2 = it2->next; */ /* if (it2 && it2->index == node) */ /* it2->weight = 1; */ -/* return ((!it && node == s_ind) ? -1 : it->index); */ +/* return ((it != NULL) ? it->index : -1); */ /* } */ static t_ind **clean_ret(t_ind **ret) @@ -88,31 +91,39 @@ static t_ind **clean_ret(t_ind **ret) return (NULL); } -static t_ind **resolve_path(int s_ind, int e_ind - , int nb_path, t_bfs *tab) +static t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab) { t_ind **ret; int i; int j; + t_ind *it; i = 0; ret = NULL; - if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL) - while (i < nb_path && (j = e_ind) == e_ind) + if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (data->nb_paths_max + 1))) != NULL) + { + it = data->adj[e_ind]; + while (it && i < data->nb_paths_max && (j = e_ind) == e_ind) { - while (j != s_ind) + if (it->weight == 2 && (j = it->index)) { - if (lst_indadd(&(ret[i]), j) == NULL) + if (lst_indadd(&(ret[i]), e_ind) == NULL) return (clean_ret(ret)); - j = tab[j].parent; + while (j != s_ind) + { + if (lst_indadd(&(ret[i]), j) == NULL) + return (clean_ret(ret)); + j = tab[j].parent; + } + i++; } - i++; + it = it->next; } + } return (ret); } -t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind - , int nb_paths) +t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind) { t_bfs tab[data->nb_nodes]; t_ind **ret[2]; @@ -122,22 +133,27 @@ t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind ret[1] = NULL; score[0] = 0; score[1] = 0; + ft_printf("nb paths max: %d\n", data->nb_paths_max); if (data && data->adj) { bfs(data, tab, start_ind, end_ind); while (tab[end_ind].parent != -1) { update_weights(data, tab, end_ind); - bfs(data, tab, start_ind, end_ind); - ret[1] = resolve_path(start_ind, end_ind, nb_paths, tab); - if (ret[0] == NULL || (score[1] = get_score(data, ret[1])) < score[0]) + if (tab[end_ind].parent != -1) + ret[1] = resolve_path(data, start_ind, end_ind, tab); + ft_printf("ret 0: %p\n", ret[0]); + ft_printf("score 0: %d\nscore 1: %d\n", score[0], get_score(data, ret[1])); + score[1] = get_score(data, ret[1]); + if (ret[0] == NULL || score[0] > score[1]) { score[0] = score[1]; if (ret[0] != NULL) clean_ret(ret[0]); ret[0] = ret[1]; + bfs(data, tab, start_ind, end_ind); } - else if (score[1] >= score[0]) + else if (score[0] <= score[1]) { clean_ret(ret[1]); break; diff --git a/srcs/lem_in.c b/srcs/lem_in.c index 7913c22..9863927 100644 --- a/srcs/lem_in.c +++ b/srcs/lem_in.c @@ -6,7 +6,7 @@ /* By: mndhlovu +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */ -/* Updated: 2019/04/13 14:52:37 by tmaze ### ########.fr */ +/* Updated: 2019/04/14 12:22:28 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ static int get_nb_paths(t_ind **ret) return (i); } -static int get_nb_paths_max(t_lmdata *data, int start, int end) +static void get_nb_paths_max(t_lmdata *data, int start, int end) { int i; int j; @@ -45,12 +45,12 @@ static int get_nb_paths_max(t_lmdata *data, int start, int end) i = 0; j = 0; it = data->adj[start]; - while (it && i++) - it= it->next; + while (it && ++i) + it = it->next; it = data->adj[end]; - while (it && j++) + while (it && ++j) it= it->next; - return ((i > j) ? j : i); + data->nb_paths_max = (i > j) ? j : i; } static int lem_in(t_syntax *synt, t_holder *holder, @@ -65,9 +65,10 @@ static int lem_in(t_syntax *synt, t_holder *holder, if (!(lm_adj_parser(lmdata, holder))) return (0); lm_clear_unv(holder); + get_nb_paths_max(lmdata, get_node_role(lmdata, 's')->ind + , get_node_role(lmdata, 'e')->ind); if ((*ret = edmunds_karp(lmdata, get_node_role(lmdata, 's')->ind - , get_node_role(lmdata, 'e')->ind, get_nb_paths_max(lmdata, get_node_role(lmdata, 's')->ind - , get_node_role(lmdata, 'e')->ind))) == NULL) + , get_node_role(lmdata, 'e')->ind)) == NULL) return (0); if (!push_ants(lmdata, *ret, get_nb_paths(*ret))) return (0);