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,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
used = 1;
if (tab[it->index].visited == 0 && it->weight == 2)
if (used)
break ;
it = it->next;
}
it = data->adj[tab[i].queue];
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)))
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)))
tab[it->index].parent = tab[i].queue;
if (it->weight == 2)
break ;
it = it->next;
}
}

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 *it2;
it = data->adj[node];
while (it && it->weight != 2)
it = it->next;
if (it && it->weight == 2)
it->weight--;
it2 = data->adj[it->index];
while (it2 && it2->index != node)
it2 = it2->next;
if (it2 && it2->index == node)
it2->weight++;
return ((node == s_ind) ? -1 : it->index);
i = 0;
tot = 0;
score = 0;
while (ret[i])
i++;
nb_paths = i;
i = 0;
while (ret[i] && (it = ret[i]))
{
while (it && tot++)
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)
{
tablst_inddel(ret);
return (NULL);
}
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
static t_ind **resolve_path(int s_ind, int e_ind
, int nb_path, t_bfs *tab)
{
t_ind **ret;
int i;
int j;
ret = NULL;
i = 0;
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*)
* (nb_path + 1))) != NULL)
{
ret = NULL;
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_path + 1))) != NULL)
while (i < nb_path && (j = e_ind) == e_ind)
{
while (j != s_ind)
{
if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret));
j = reset_weights(data, s_ind, j);
j = tab[j].parent;
}
i++;
}
}
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];
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)
{
bfs(data, tab, start_ind, end_ind);
while (tab[end_ind].parent != -1)
{
nb_path++;
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])
{
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);
}

View File

@@ -6,7 +6,7 @@
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
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,
t_lmdata *lmdata, t_ind ***ret)
{
@@ -49,7 +66,8 @@ static int lem_in(t_syntax *synt, t_holder *holder,
return (0);
lm_clear_unv(holder);
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);
if (!push_ants(lmdata, *ret, get_nb_paths(*ret)))
return (0);

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */