lem_in/srcs/edmunds_karp.c
Tanguy MAZE 4a5c798b25 rework on score system
need to score with correct amount of ants on each paths
also secured ft_atois
2019-04-14 14:45:38 +02:00

166 lines
3.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* edmunds_karp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
/* Updated: 2019/04/14 14:03:45 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "lem_in.h"
static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
{
t_ind *it;
int i;
i = end_ind;
while (i != -1)
{
if (tab[i].parent != -1)
{
it = data->adj[tab[i].parent];
while (it && it->index != i)
it = it->next;
if (it && it->index == i)
it->weight--;
it = data->adj[i];
while (it && it->index != tab[i].parent)
it = it->next;
if (it && it->index == tab[i].parent)
it->weight++;
}
i = tab[i].parent;
}
}
static int get_score(t_lmdata *data, t_ind **ret)
{
int tot;
int score;
int nb_paths;
int i;
t_ind *it;
i = 0;
tot = 0;
score = 0;
while (ret && ret[i])
i++;
nb_paths = i;
ft_printf("nb_paths: %d\n", nb_paths);
i = 0;
while (ret && ret[i] && (it = ret[i]))
{
while (it && ++tot)
it = it->next;
tot = tot + (data->nbants / nb_paths) - 1;
if (score == 0 || tot > score)
score = tot;
i++;
}
return (score);
}
/* 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; */
/* 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 != NULL) ? it->index : -1); */
/* } */
static t_ind **clean_ret(t_ind **ret)
{
tablst_inddel(ret);
return (NULL);
}
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*) * (data->nb_paths_max + 1))) != NULL)
{
it = data->adj[e_ind];
while (it && i < data->nb_paths_max && (j = e_ind) == e_ind)
{
if (it->weight == 2 && (j = it->index))
{
if (lst_indadd(&(ret[i]), e_ind) == NULL)
return (clean_ret(ret));
while (j != s_ind)
{
if (lst_indadd(&(ret[i]), j) == NULL)
return (clean_ret(ret));
j = tab[j].parent;
}
i++;
}
it = it->next;
}
}
return (ret);
}
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
{
t_bfs tab[data->nb_nodes];
t_ind **ret[2];
int score[2];
ret[0] = NULL;
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);
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[0] <= score[1])
{
clean_ret(ret[1]);
break;
}
}
return (ret[0]);
}
return (NULL);
}