started work to redo algorithm
branch out to redo algorithm
This commit is contained in:
parent
4a5c798b25
commit
dd255666f1
3
Makefile
3
Makefile
@ -6,7 +6,7 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
|
||||
# Updated: 2019/04/12 11:44:15 by tmaze ### ########.fr #
|
||||
# Updated: 2019/04/15 17:03:17 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
@ -50,6 +50,7 @@ SRC = lm_parser.c \
|
||||
edmunds_karp.c \
|
||||
push_ants.c \
|
||||
push_ants_utils.c \
|
||||
score_utils.c \
|
||||
lem_in.c
|
||||
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/14 12:18:02 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:44:39 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -32,6 +32,13 @@ typedef struct s_ind
|
||||
struct s_ind *next;
|
||||
} t_ind;
|
||||
|
||||
typedef struct s_path
|
||||
{
|
||||
int nb_ants;
|
||||
int score;
|
||||
t_ind *nodes;
|
||||
} t_path;
|
||||
|
||||
typedef struct s_lmdata
|
||||
{
|
||||
int nbants;
|
||||
@ -126,18 +133,18 @@ t_ind **lst_indinit(t_lmdata *data);
|
||||
int lst_indadd_link(t_lmdata *data, int n1, int n2);
|
||||
t_ind *lst_indadd(t_ind **lst, int ind);
|
||||
void lst_inddel(t_ind **lst);
|
||||
void tablst_inddel(t_ind **tab);
|
||||
void clean_path(t_path *tab);
|
||||
|
||||
t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path
|
||||
, t_ind **paths);
|
||||
, t_path *paths);
|
||||
void del_ants(t_ants **ants);
|
||||
t_node *get_node(t_lmdata *data, int index);
|
||||
t_ind *get_node_path(t_ind *lst, int index);
|
||||
|
||||
int get_score(t_lmdata *data, t_path *ret);
|
||||
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 push_ants(t_lmdata *data, t_ind **paths
|
||||
, int nb_paths);
|
||||
t_path *edmunds_karp(t_lmdata *data, int start_ind, int end_ind);
|
||||
int push_ants(t_lmdata *data, t_path *paths);
|
||||
|
||||
#endif
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* 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 */
|
||||
/* Updated: 2019/04/15 17:37:09 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,34 +37,6 @@ static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
|
||||
}
|
||||
}
|
||||
|
||||
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; */
|
||||
@ -85,33 +57,37 @@ static int get_score(t_lmdata *data, t_ind **ret)
|
||||
/* return ((it != NULL) ? it->index : -1); */
|
||||
/* } */
|
||||
|
||||
static t_ind **clean_ret(t_ind **ret)
|
||||
static t_path *clean_ret(t_path *ret)
|
||||
{
|
||||
tablst_inddel(ret);
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret[i].nodes)
|
||||
lst_inddel(&(ret[i++].nodes));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab)
|
||||
static t_path *resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab)
|
||||
{
|
||||
t_ind **ret;
|
||||
t_path *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)
|
||||
if ((ret = (t_path*)ft_memalloc(sizeof(t_path) * (data->nb_paths_max))) != 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)
|
||||
if (lst_indadd(&(ret[i].nodes), e_ind) == NULL)
|
||||
return (clean_ret(ret));
|
||||
while (j != s_ind)
|
||||
{
|
||||
if (lst_indadd(&(ret[i]), j) == NULL)
|
||||
if (lst_indadd(&(ret[i].nodes), j) == NULL)
|
||||
return (clean_ret(ret));
|
||||
j = tab[j].parent;
|
||||
}
|
||||
@ -123,10 +99,10 @@ static t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab)
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
|
||||
t_path *edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
|
||||
{
|
||||
t_bfs tab[data->nb_nodes];
|
||||
t_ind **ret[2];
|
||||
t_path *ret[2];
|
||||
int score[2];
|
||||
|
||||
ret[0] = NULL;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/14 12:22:28 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:55:43 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -26,16 +26,6 @@ static t_node *get_node_role(t_lmdata *data, char role)
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
static int get_nb_paths(t_ind **ret)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
static void get_nb_paths_max(t_lmdata *data, int start, int end)
|
||||
{
|
||||
int i;
|
||||
@ -54,7 +44,7 @@ static void get_nb_paths_max(t_lmdata *data, int start, int end)
|
||||
}
|
||||
|
||||
static int lem_in(t_syntax *synt, t_holder *holder,
|
||||
t_lmdata *lmdata, t_ind ***ret)
|
||||
t_lmdata *lmdata, t_path **ret)
|
||||
{
|
||||
if (!(lm_parser(synt, lmdata, holder)))
|
||||
return (0);
|
||||
@ -70,9 +60,9 @@ static int lem_in(t_syntax *synt, t_holder *holder,
|
||||
if ((*ret = edmunds_karp(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)))
|
||||
if (!push_ants(lmdata, *ret))
|
||||
return (0);
|
||||
tablst_inddel(*ret);
|
||||
clean_path(*ret);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@ -87,7 +77,7 @@ int main(int ac, char **av)
|
||||
t_syntax synt;
|
||||
t_lmdata ldata;
|
||||
t_holder holder;
|
||||
t_ind **ret;
|
||||
t_path *ret;
|
||||
|
||||
(void)av;
|
||||
if (ac == 1)
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/09 19:04:33 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:45:54 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -50,12 +50,12 @@ void lst_inddel(t_ind **lst)
|
||||
}
|
||||
}
|
||||
|
||||
void tablst_inddel(t_ind **tab)
|
||||
void clean_path(t_path *tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
lst_inddel(&(tab[i++]));
|
||||
while (tab[i].nodes)
|
||||
lst_inddel(&(tab[i++].nodes));
|
||||
ft_memdel((void**)&tab);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/12 12:06:51 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:48:01 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -37,8 +37,7 @@ static void clean_ants(t_ants **ants)
|
||||
}
|
||||
}
|
||||
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, char *has_start
|
||||
, t_ind **paths)
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, t_path *paths)
|
||||
{
|
||||
t_ants *it;
|
||||
|
||||
@ -46,9 +45,9 @@ static void print_ants(t_lmdata *data, t_ants **ants, char *has_start
|
||||
while (it)
|
||||
{
|
||||
ft_printf("L%d-%s", it->nb_ant, get_node(data, it->nb_node)->name);
|
||||
if (get_node_path(paths[it->nb_path], it->nb_node)
|
||||
&& get_node_path(paths[it->nb_path], it->nb_node)->next)
|
||||
it->nb_node = get_node_path(paths[it->nb_path]
|
||||
if (get_node_path(paths[it->nb_path].nodes, it->nb_node)
|
||||
&& get_node_path(paths[it->nb_path].nodes, it->nb_node)->next)
|
||||
it->nb_node = get_node_path(paths[it->nb_path].nodes
|
||||
, it->nb_node)->next->index;
|
||||
else
|
||||
it->end = 1;
|
||||
@ -56,43 +55,42 @@ static void print_ants(t_lmdata *data, t_ants **ants, char *has_start
|
||||
ft_putchar(' ');
|
||||
else
|
||||
ft_putchar('\n');
|
||||
has_start[it->nb_path] = 0;
|
||||
it = it->next;
|
||||
}
|
||||
clean_ants(ants);
|
||||
}
|
||||
|
||||
static void init_ants(t_ants **ants, int *ant_c, char *has_start, int nb_paths)
|
||||
{
|
||||
*ants = NULL;
|
||||
ft_bzero(has_start, nb_paths);
|
||||
*ant_c = 2;
|
||||
has_start[0] = 1;
|
||||
}
|
||||
|
||||
int push_ants(t_lmdata *data, t_ind **paths, int nb_paths)
|
||||
int push_ants(t_lmdata *data, t_path *paths)
|
||||
{
|
||||
t_ants *ants;
|
||||
t_ants *it;
|
||||
t_node *node;
|
||||
int i;
|
||||
int ant_c;
|
||||
char has_start[nb_paths];
|
||||
|
||||
init_ants(&ants, &ant_c, has_start, nb_paths);
|
||||
if (add_ant(&ants, 1, 0, paths) == NULL)
|
||||
return (0);
|
||||
ant_c = 1;
|
||||
ants = NULL;
|
||||
while ((ants || ant_c <= data->nbants) && (i = -1))
|
||||
{
|
||||
while (++i < nb_paths)
|
||||
if (ant_c <= data->nbants && !has_start[i])
|
||||
it = ants;
|
||||
while (it)
|
||||
{
|
||||
node = get_node(data, it->nb_node);
|
||||
if (node && node->next)
|
||||
it->nb_node = node->next->ind;
|
||||
it = it->next;
|
||||
}
|
||||
while (paths[++i].nodes)
|
||||
if (ant_c <= data->nbants && paths[i].nb_ants)
|
||||
{
|
||||
if (!add_ant(&ants, ant_c, i, paths))
|
||||
del_ants(&ants);
|
||||
if (!ants)
|
||||
return (0);
|
||||
ant_c++;
|
||||
has_start[i] = 1;
|
||||
paths[i].nb_ants--;
|
||||
}
|
||||
print_ants(data, &ants, has_start, paths);
|
||||
print_ants(data, &ants, paths);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
@ -6,13 +6,13 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/10 11:23:36 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/10 11:24:01 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:44:11 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path, t_ind **paths)
|
||||
t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path, t_path *paths)
|
||||
{
|
||||
t_ants *new;
|
||||
t_ants *it;
|
||||
@ -21,7 +21,7 @@ t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path, t_ind **paths)
|
||||
{
|
||||
new->nb_ant = nb_ant;
|
||||
new->nb_path = nb_path;
|
||||
new->nb_node = paths[nb_path]->index;
|
||||
new->nb_node = paths[nb_path].nodes->index;
|
||||
new->end = 0;
|
||||
new->next = NULL;
|
||||
if (*ants == NULL)
|
||||
|
73
srcs/score_utils.c
Normal file
73
srcs/score_utils.c
Normal file
@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* score_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/15 14:24:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/15 17:50:56 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void init_paths(t_path *ret)
|
||||
{
|
||||
int nb_paths;
|
||||
int i;
|
||||
int nb_nodes;
|
||||
t_ind *it;
|
||||
|
||||
nb_paths = 0;
|
||||
while (ret[nb_paths].nodes)
|
||||
{
|
||||
ret[nb_paths].nb_ants = 0;
|
||||
ret[nb_paths].score = 0;
|
||||
nb_paths++;
|
||||
}
|
||||
i = 0;
|
||||
while (ret[i].nodes && !(nb_nodes = 0))
|
||||
{
|
||||
it = ret[i].nodes;
|
||||
while (it && ++nb_nodes)
|
||||
it = it->next;
|
||||
ret[i].score = nb_nodes;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int get_score(t_lmdata *data, t_path *ret)
|
||||
{
|
||||
int i;
|
||||
int ant_c;
|
||||
int max;
|
||||
|
||||
init_paths(ret);
|
||||
i = -1;
|
||||
max = 0;
|
||||
while (ret[++i].nodes)
|
||||
{
|
||||
if (ret[max].score < ret[i].score)
|
||||
max = i;
|
||||
}
|
||||
ant_c = 0;
|
||||
while (ant_c < data->nbants && !(i = 0))
|
||||
{
|
||||
while (ret[i].nodes && ((ret[i].nb_ants > 0) * (ret[i].score + ret[i].nb_ants - 1)) < ret[max].score && ant_c < data->nbants)
|
||||
{
|
||||
ret[i].nb_ants++;
|
||||
ant_c++;
|
||||
ret[i].score = (ret[i].nb_ants > 0) * (ret[i].score + ret[i].nb_ants - 1);
|
||||
i++;
|
||||
}
|
||||
i = -1;
|
||||
max = 0;
|
||||
while (ret[++i].nodes)
|
||||
{
|
||||
if (ret[max].score < ret[i].score)
|
||||
max = i;
|
||||
}
|
||||
}
|
||||
return (ret[max].score);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user