Added scoring system to algorithm

still need testing and optimisations
not norm-compliant
This commit is contained in:
Tanguy MAZE
2019-04-13 14:56:39 +02:00
parent d85e0873cb
commit 9b75432c2e
6 changed files with 100 additions and 39 deletions

View File

@@ -6,11 +6,11 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/04/10 14:47:07 by tmaze ### ########.fr # # Updated: 2019/04/12 11:44:15 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
NAME = lem_in NAME = lem-in
# Make options # Make options
MAKEFLAGS += --no-print-directory MAKEFLAGS += --no-print-directory

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/11 17:31:09 by tmaze ### ########.fr */ /* Updated: 2019/04/13 14:33:18 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -135,8 +135,8 @@ 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 t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_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/05 17:43:11 by tmaze ### ########.fr */ /* Updated: 2019/04/13 12:48:35 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -37,19 +37,21 @@ static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i)
{ {
if (tab[it->index].visited == 0 && it->weight == 2) if (tab[it->index].visited == 0 && it->weight == 2)
used = 1; used = 1;
if (tab[it->index].visited == 0 && it->weight == 2) if (used)
break ; break ;
it = it->next; it = it->next;
} }
it = data->adj[tab[i].queue]; it = data->adj[tab[i].queue];
while (it != NULL) while (it != NULL)
{ {
if (tab[it->index].visited == 0 && ((!used && it->weight > 0) if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|| (used && it->weight == 2))) || (used && it->weight == 2)))
bfs_addtoqueue(tab, it->index, data->nb_nodes); bfs_addtoqueue(tab, it->index, data->nb_nodes);
if (tab[it->index].visited == 0 && ((!used && it->weight > 0) if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|| (used && it->weight == 2))) || (used && it->weight == 2)))
tab[it->index].parent = tab[i].queue; tab[it->index].parent = tab[i].queue;
if (it->weight == 2)
break ;
it = it->next; it = it->next;
} }
} }

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/10 15:00:16 by tmaze ### ########.fr */ /* Updated: 2019/04/13 14:55:19 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -37,72 +37,113 @@ static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
} }
} }
static int reset_weights(t_lmdata *data, int s_ind, int node) static int get_score(t_lmdata *data, t_ind **ret)
{ {
int tot;
int score;
int nb_paths;
int i;
t_ind *it; t_ind *it;
t_ind *it2;
it = data->adj[node]; i = 0;
while (it && it->weight != 2) tot = 0;
it = it->next; score = 0;
if (it && it->weight == 2) while (ret[i])
it->weight--; i++;
it2 = data->adj[it->index]; nb_paths = i;
while (it2 && it2->index != node) i = 0;
it2 = it2->next; while (ret[i] && (it = ret[i]))
if (it2 && it2->index == node) {
it2->weight++; while (it && tot++)
return ((node == s_ind) ? -1 : it->index); it = it->next;
tot *= (data->nbants / nb_paths);
if (score == 0 || tot > score)
score = tot;
i++;
}
return (score);
} }
/* static int reset_weights(t_lmdata *data, int s_ind, int node) */
/* { */
/* t_ind *it; */
/* t_ind *it2; */
/* it = data->adj[node]; */
/* while (it && it->weight != 2) */
/* it = it->next; */
/* if (it && it->weight == 2) */
/* it->weight = 1; */
/* 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); */
/* } */
static t_ind **clean_ret(t_ind **ret) static t_ind **clean_ret(t_ind **ret)
{ {
tablst_inddel(ret); tablst_inddel(ret);
return (NULL); return (NULL);
} }
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind static t_ind **resolve_path(int s_ind, int e_ind
, int nb_path) , int nb_path, t_bfs *tab)
{ {
t_ind **ret; t_ind **ret;
int i; int i;
int j; int j;
ret = NULL;
i = 0; i = 0;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*) ret = NULL;
* (nb_path + 1))) != NULL) if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL)
{
while (i < nb_path && (j = e_ind) == e_ind) while (i < nb_path && (j = e_ind) == e_ind)
{ {
while (j != s_ind) while (j != s_ind)
{ {
if (lst_indadd(&(ret[i]), j) == NULL) if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret)); return (clean_ret(ret));
j = reset_weights(data, s_ind, j); j = tab[j].parent;
} }
i++; i++;
} }
}
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];
int nb_path; t_ind **ret[2];
int score[2];
nb_path = 0; ret[0] = NULL;
ret[1] = NULL;
score[0] = 0;
score[1] = 0;
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)
{ {
nb_path++;
update_weights(data, tab, end_ind); update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, 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])
{
score[0] = score[1];
if (ret[0] != NULL)
clean_ret(ret[0]);
ret[0] = ret[1];
}
else if (score[1] >= score[0])
{
clean_ret(ret[1]);
break;
}
} }
return (resolve_path(data, start_ind, end_ind, nb_path)); return (ret[0]);
} }
return (NULL); return (NULL);
} }

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/11 12:48:07 by mndhlovu ### ########.fr */ /* Updated: 2019/04/13 14:52:37 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -36,6 +36,23 @@ 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)
{
int i;
int j;
t_ind *it;
i = 0;
j = 0;
it = data->adj[start];
while (it && i++)
it= it->next;
it = data->adj[end];
while (it && j++)
it= it->next;
return ((i > j) ? j : i);
}
static int lem_in(t_syntax *synt, t_holder *holder, static int lem_in(t_syntax *synt, t_holder *holder,
t_lmdata *lmdata, t_ind ***ret) t_lmdata *lmdata, t_ind ***ret)
{ {
@@ -49,7 +66,8 @@ static int lem_in(t_syntax *synt, t_holder *holder,
return (0); return (0);
lm_clear_unv(holder); lm_clear_unv(holder);
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)) == NULL) , get_node_role(lmdata, 'e')->ind, get_nb_paths_max(lmdata, get_node_role(lmdata, 's')->ind
, 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);

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */ /* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
/* Updated: 2019/04/11 09:14:45 by tmaze ### ########.fr */ /* Updated: 2019/04/12 12:06:51 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */