rework on score system

need to score with correct amount of ants on each paths
also secured ft_atois
This commit is contained in:
Tanguy MAZE 2019-04-14 14:45:38 +02:00
parent 9b75432c2e
commit 4a5c798b25
4 changed files with 52 additions and 34 deletions

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 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 nbants;
int nb_nodes; int nb_nodes;
int nb_paths_max;
t_node *nodes_data; t_node *nodes_data;
t_ind **adj; t_ind **adj;
} t_lmdata; } 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 void bfs(t_lmdata *data, t_bfs *tab, int start_ind
, int end_ind); , int end_ind);
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind);
, int nb_paths);
int push_ants(t_lmdata *data, t_ind **paths int push_ants(t_lmdata *data, t_ind **paths
, int nb_paths); , int nb_paths);

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/27 14:41:49 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; int i;
ft_printf("New bfs\n");
bfs_init(tab, data); bfs_init(tab, data);
bfs_addtoqueue(tab, start_ind, data->nb_nodes); bfs_addtoqueue(tab, start_ind, data->nb_nodes);
i = 0; i = 0;

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 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; i = 0;
tot = 0; tot = 0;
score = 0; score = 0;
while (ret[i]) while (ret && ret[i])
i++; i++;
nb_paths = i; nb_paths = i;
ft_printf("nb_paths: %d\n", nb_paths);
i = 0; i = 0;
while (ret[i] && (it = ret[i])) while (ret && ret[i] && (it = ret[i]))
{ {
while (it && tot++) while (it && ++tot)
it = it->next; it = it->next;
tot *= (data->nbants / nb_paths); tot = tot + (data->nbants / nb_paths) - 1;
if (score == 0 || tot > score) if (score == 0 || tot > score)
score = tot; score = tot;
i++; i++;
@ -64,22 +65,24 @@ static int get_score(t_lmdata *data, t_ind **ret)
return (score); 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 *it; */
/* t_ind *it2; */ /* t_ind *it2; */
/* it = data->adj[node]; */ /* it = data->adj[node]; */
/* it2 = NULL; */
/* while (it && it->weight != 2) */ /* while (it && it->weight != 2) */
/* it = it->next; */ /* it = it->next; */
/* if (it && it->weight == 2) */ /* if (it && it->weight == 2) */
/* it->weight = 1; */ /* it->weight = 1; */
/* it2 = data->adj[it->index]; */ /* if (it && it->index) */
/* it2 = data->adj[it->index]; */
/* while (it2 && it2->index != node) */ /* while (it2 && it2->index != node) */
/* it2 = it2->next; */ /* it2 = it2->next; */
/* if (it2 && it2->index == node) */ /* if (it2 && it2->index == node) */
/* it2->weight = 1; */ /* 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) static t_ind **clean_ret(t_ind **ret)
@ -88,31 +91,39 @@ static t_ind **clean_ret(t_ind **ret)
return (NULL); return (NULL);
} }
static t_ind **resolve_path(int s_ind, int e_ind static t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab)
, int nb_path, t_bfs *tab)
{ {
t_ind **ret; t_ind **ret;
int i; int i;
int j; int j;
t_ind *it;
i = 0; i = 0;
ret = NULL; ret = NULL;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL) if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (data->nb_paths_max + 1))) != NULL)
while (i < nb_path && (j = e_ind) == e_ind) {
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)); 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); return (ret);
} }
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
, int nb_paths)
{ {
t_bfs tab[data->nb_nodes]; t_bfs tab[data->nb_nodes];
t_ind **ret[2]; t_ind **ret[2];
@ -122,22 +133,27 @@ t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind
ret[1] = NULL; ret[1] = NULL;
score[0] = 0; score[0] = 0;
score[1] = 0; score[1] = 0;
ft_printf("nb paths max: %d\n", data->nb_paths_max);
if (data && data->adj) if (data && data->adj)
{ {
bfs(data, tab, start_ind, end_ind); bfs(data, tab, start_ind, end_ind);
while (tab[end_ind].parent != -1) while (tab[end_ind].parent != -1)
{ {
update_weights(data, tab, end_ind); update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, end_ind); if (tab[end_ind].parent != -1)
ret[1] = resolve_path(start_ind, end_ind, nb_paths, tab); ret[1] = resolve_path(data, start_ind, end_ind, tab);
if (ret[0] == NULL || (score[1] = get_score(data, ret[1])) < score[0]) 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]; score[0] = score[1];
if (ret[0] != NULL) if (ret[0] != NULL)
clean_ret(ret[0]); clean_ret(ret[0]);
ret[0] = ret[1]; 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]); clean_ret(ret[1]);
break; break;

View File

@ -6,7 +6,7 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */ /* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 06:31:05 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); 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 i;
int j; int j;
@ -45,12 +45,12 @@ static int get_nb_paths_max(t_lmdata *data, int start, int end)
i = 0; i = 0;
j = 0; j = 0;
it = data->adj[start]; it = data->adj[start];
while (it && i++) while (it && ++i)
it= it->next; it = it->next;
it = data->adj[end]; it = data->adj[end];
while (it && j++) while (it && ++j)
it= it->next; 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, 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))) if (!(lm_adj_parser(lmdata, holder)))
return (0); return (0);
lm_clear_unv(holder); 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 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); return (0);
if (!push_ants(lmdata, *ret, get_nb_paths(*ret))) if (!push_ants(lmdata, *ret, get_nb_paths(*ret)))
return (0); return (0);