Compare commits

3 Commits

Author SHA1 Message Date
Tanguy MAZE
dd255666f1 started work to redo algorithm
branch out to redo algorithm
2019-04-15 18:05:25 +02:00
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
Tanguy MAZE
9b75432c2e Added scoring system to algorithm
still need testing and optimisations
not norm-compliant
2019-04-13 14:56:39 +02:00
9 changed files with 215 additions and 91 deletions

View File

@@ -6,11 +6,11 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/04/10 14:47:07 by tmaze ### ########.fr #
# Updated: 2019/04/15 17:03:17 by tmaze ### ########.fr #
# #
#******************************************************************************#
NAME = lem_in
NAME = lem-in
# Make options
MAKEFLAGS += --no-print-directory
@@ -50,6 +50,7 @@ SRC = lm_parser.c \
edmunds_karp.c \
push_ants.c \
push_ants_utils.c \
score_utils.c \
lem_in.c

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
/* Updated: 2019/04/11 17:31:09 by tmaze ### ########.fr */
/* Updated: 2019/04/15 17:44:39 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,10 +32,18 @@ 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;
int nb_nodes;
int nb_paths_max;
t_node *nodes_data;
t_ind **adj;
} t_lmdata;
@@ -125,19 +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

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/14 11:39:52 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;
}
}
@@ -72,6 +74,7 @@ void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
{
int i;
ft_printf("New bfs\n");
bfs_init(tab, data);
bfs_addtoqueue(tab, start_ind, data->nb_nodes);
i = 0;

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/15 17:37:09 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,72 +37,105 @@ 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)
{
t_ind *it;
t_ind *it2;
/* static int reset_weights(t_lmdata *data, 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--;
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);
}
/* 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)
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);
}
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
, int nb_path)
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;
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_path*)ft_memalloc(sizeof(t_path) * (data->nb_paths_max))) != 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].nodes), e_ind) == NULL)
return (clean_ret(ret));
j = reset_weights(data, s_ind, j);
while (j != s_ind)
{
if (lst_indadd(&(ret[i].nodes), j) == NULL)
return (clean_ret(ret));
j = tab[j].parent;
}
i++;
}
i++;
it = it->next;
}
}
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];
int nb_path;
t_path *ret[2];
int score[2];
nb_path = 0;
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)
{
nb_path++;
update_weights(data, tab, end_ind);
bfs(data, tab, start_ind, 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 (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/15 17:55:43 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@@ -26,18 +26,25 @@ static t_node *get_node_role(t_lmdata *data, char role)
return (NULL);
}
static int get_nb_paths(t_ind **ret)
static void get_nb_paths_max(t_lmdata *data, int start, int end)
{
int i;
int i;
int j;
t_ind *it;
i = 0;
while (ret[i])
i++;
return (i);
j = 0;
it = data->adj[start];
while (it && ++i)
it = it->next;
it = data->adj[end];
while (it && ++j)
it= it->next;
data->nb_paths_max = (i > j) ? j : i;
}
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);
@@ -48,12 +55,14 @@ static int lem_in(t_syntax *synt, t_holder *holder,
if (!(lm_adj_parser(lmdata, holder)))
return (0);
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
, get_node_role(lmdata, 'e')->ind)) == NULL)
, 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);
}
@@ -68,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)

View File

@@ -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);
}

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/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);
}

View File

@@ -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
View 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);
}