It finnaly kinda works
algorithm with no double nodes and nearly efficient enough still some optimizations to find for --big-superposition maps
This commit is contained in:
68
srcs/bfs.c
Normal file
68
srcs/bfs.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* bfs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/18 09:25:05 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/18 15:49:17 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void bfs_init(t_lmdata *data, t_bfs *tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < data->nb_nodes)
|
||||
{
|
||||
tab[i].queue = -1;
|
||||
tab[i].visited = 0;
|
||||
tab[i].parent = -1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void bfs(t_lmdata *data, t_bfs *tab, int s_ind, int e_ind)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int used;
|
||||
t_ind *it;
|
||||
|
||||
i = 0;
|
||||
bfs_init(data, tab);
|
||||
tab[i].queue = s_ind;
|
||||
used = 1;
|
||||
while (i < data->nb_nodes && tab[i].queue != -1 && tab[e_ind].parent == -1)
|
||||
{
|
||||
tab[tab[i].queue].visited = 1;
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it && used)
|
||||
{
|
||||
if (it->weight > 0 && !tab[it->index].visited && !tab[it->index].old_visited)
|
||||
used = 0;
|
||||
it = it->next;
|
||||
}
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it)
|
||||
{
|
||||
if (it->weight > 0 && !tab[it->index].visited && ((tab[tab[i].queue].old_visited && tab[it->index].old_visited) || (used && tab[it->index].old_visited) || (!used && !tab[it->index].old_visited)))
|
||||
{
|
||||
j = 0;
|
||||
while (j < data->nb_nodes && tab[j].queue != -1 && tab[j].queue != it->index)
|
||||
j++;
|
||||
if (j < data->nb_nodes && tab[j].queue == -1)
|
||||
{
|
||||
tab[j].queue = it->index;
|
||||
tab[it->index].parent = tab[i].queue;
|
||||
}
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
129
srcs/edmonds_karp.c
Normal file
129
srcs/edmonds_karp.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* edmonds_karp.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/18 09:59:11 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/18 19:39:47 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_ind **resolve_paths(t_lmdata *data, int nb_paths, int s_ind, int e_ind)
|
||||
{
|
||||
t_ind **ret;
|
||||
t_ind *it;
|
||||
t_ind *it2;
|
||||
int i;
|
||||
int j;
|
||||
int nb_nodes;
|
||||
|
||||
if ((ret = (t_ind**)ft_memalloc(sizeof(t_ind*) * (nb_paths + 1))) != NULL)
|
||||
{
|
||||
i = 0;
|
||||
it = data->adj[e_ind];
|
||||
j = e_ind;
|
||||
while (it && i < nb_paths)
|
||||
{
|
||||
if (it->weight == 2 && (nb_nodes = 1))
|
||||
{
|
||||
lst_indadd(&(ret[i]), e_ind);
|
||||
j = it->index;
|
||||
while (j != s_ind)
|
||||
{
|
||||
lst_indadd(&(ret[i]), j);
|
||||
nb_nodes++;
|
||||
it2 = data->adj[j];
|
||||
while (it2 && it2->weight != 2)
|
||||
it2 = it2->next;
|
||||
if (it2 && it2->weight == 2)
|
||||
j = it2->index;
|
||||
}
|
||||
ret[i]->weight = nb_nodes;
|
||||
i++;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
void print_paths(t_lmdata *data, t_ind **ret)
|
||||
{
|
||||
int i;
|
||||
t_ind *it;
|
||||
|
||||
i = 0;
|
||||
ft_putstr("=== all paths ===\n");
|
||||
while (ret[i])
|
||||
{
|
||||
it = ret[i];
|
||||
while (it)
|
||||
{
|
||||
ft_printf("%s-> ", get_node(data, it->index)->name);
|
||||
it = it->next;
|
||||
}
|
||||
ft_putstr("\n\n");
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
t_ind **edmonds_karp(t_lmdata *data, int s_ind, int e_ind)
|
||||
{
|
||||
t_bfs tab[data->nb_nodes];
|
||||
t_ind **ret[2];
|
||||
t_ind *it;
|
||||
int nb_paths;
|
||||
int scores[2];
|
||||
int i;
|
||||
|
||||
ret[0] = NULL;
|
||||
scores[0] = 0;
|
||||
i = 0;
|
||||
nb_paths = 0;
|
||||
while (i < data->nb_nodes)
|
||||
tab[i++].old_visited = 0;
|
||||
bfs(data, tab, s_ind, e_ind);
|
||||
while (tab[e_ind].parent != -1)
|
||||
{
|
||||
nb_paths++;
|
||||
i = e_ind;
|
||||
while (i != s_ind)
|
||||
{
|
||||
tab[i].old_visited = 0;
|
||||
it = data->adj[i];
|
||||
while (it && it->index != tab[i].parent)
|
||||
it = it->next;
|
||||
if (it->index == tab[i].parent)
|
||||
it->weight++;
|
||||
if (i != e_ind && it->weight == 2)
|
||||
tab[i].old_visited = 1;
|
||||
it = data->adj[tab[i].parent];
|
||||
while (it && it->index != i)
|
||||
it = it->next;
|
||||
if (it)
|
||||
it->weight--;
|
||||
i = tab[i].parent;
|
||||
}
|
||||
ret[1] = resolve_paths(data, nb_paths, s_ind, e_ind);
|
||||
scores[1] = get_score(data, ret[1], nb_paths);
|
||||
ft_printf("scores[0]: %d\nscores[1]: %d\n\n", scores[0], scores[1]);
|
||||
i = -1;
|
||||
while (ret[1][++i])
|
||||
ft_printf("ret[%d]->weight: %d\n\n", i, ret[1][i]->weight);
|
||||
if (ret[0] == NULL || scores[1] < scores[0])
|
||||
{
|
||||
if (ret[0] != NULL)
|
||||
tablst_inddel(ret[0]);
|
||||
ret[0] = ret[1];
|
||||
scores[0] = scores[1];
|
||||
}
|
||||
else
|
||||
tablst_inddel(ret[1]);
|
||||
bfs(data, tab, s_ind, e_ind);
|
||||
}
|
||||
return (ret[0]);
|
||||
}
|
103
srcs/lem_in.c
Normal file
103
srcs/lem_in.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lem_in.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/18 17:34:32 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static t_node *get_node_role(t_lmdata *data, char role)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = data->nodes_data;
|
||||
while (it)
|
||||
{
|
||||
if (it->role == role)
|
||||
return (it);
|
||||
it = it->next;
|
||||
}
|
||||
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;
|
||||
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;
|
||||
data->nb_paths_max = (i > j) ? j : i;
|
||||
}
|
||||
|
||||
static int lem_in(t_syntax *synt, t_holder *holder,
|
||||
t_lmdata *lmdata, t_ind ***ret)
|
||||
{
|
||||
if (!(lm_parser(synt, lmdata, holder)))
|
||||
return (0);
|
||||
if (!(lm_verify_cmd(synt, holder, lmdata)))
|
||||
return (0);
|
||||
if (!lst_indinit(lmdata))
|
||||
return (0);
|
||||
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 = edmonds_karp(lmdata, get_node_role(lmdata, 's')->ind
|
||||
, get_node_role(lmdata, 'e')->ind)) == NULL)
|
||||
return (0);
|
||||
// print_paths(lmdata, *ret);
|
||||
if (!push_ants(lmdata, *ret, get_nb_paths(*ret)))
|
||||
return (0);
|
||||
tablst_inddel(*ret);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int lm_error_exit(void)
|
||||
{
|
||||
ft_putendl_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_syntax synt;
|
||||
t_lmdata ldata;
|
||||
t_holder holder;
|
||||
t_ind **ret;
|
||||
|
||||
(void)av;
|
||||
if (ac == 1)
|
||||
{
|
||||
lm_init_data(&synt, &holder, &ldata);
|
||||
if (!(lem_in(&synt, &holder, &ldata, &ret)))
|
||||
return (lm_error_exit());
|
||||
lm_clean_data(&ldata);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
53
srcs/lm_check_errors.c
Normal file
53
srcs/lm_check_errors.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_check_errors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/02 08:39:09 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 12:48:13 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_check_room_before(char **tab, t_syntax *synt)
|
||||
{
|
||||
if (tab[0] != NULL && tab[1] != NULL && tab[2] != NULL)
|
||||
{
|
||||
if (!lm_validate_rooms(tab[0], tab[1], tab[2]))
|
||||
{
|
||||
synt->v_error = 1;
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void lm_clear_unv(t_holder *holder)
|
||||
{
|
||||
t_temp *data;
|
||||
t_temp *flush;
|
||||
|
||||
data = holder->data;
|
||||
while (data)
|
||||
{
|
||||
flush = data;
|
||||
data = data->next;
|
||||
free(flush);
|
||||
flush = NULL;
|
||||
}
|
||||
holder->data = NULL;
|
||||
}
|
||||
|
||||
int lm_verify_cmd(t_syntax *synt, t_holder *holder, t_lmdata *data)
|
||||
{
|
||||
if (synt->s_cmd && synt->e_cmd && !synt->v_error
|
||||
&& !synt->l_error && !synt->e_error && !synt->s_error
|
||||
&& synt->nb_state && (holder->count > 0)
|
||||
&& (data->nb_nodes > 0))
|
||||
return (1);
|
||||
lm_clear_unv(holder);
|
||||
return (0);
|
||||
}
|
58
srcs/lm_data_utils.c
Normal file
58
srcs/lm_data_utils.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_data_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/05 06:35:40 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 11:32:32 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_rdata *lm_new_raw_line(char *raw)
|
||||
{
|
||||
t_rdata *line;
|
||||
|
||||
if (!(line = (t_rdata *)malloc(sizeof(t_rdata))))
|
||||
return (NULL);
|
||||
line->line = ft_strdup(raw);
|
||||
line->next = NULL;
|
||||
return (line);
|
||||
}
|
||||
|
||||
void lm_append_line(t_rdata **line, char *raw)
|
||||
{
|
||||
if (*line)
|
||||
{
|
||||
if((*line)->next)
|
||||
lm_append_line(&(*line)->next, raw);
|
||||
else
|
||||
(*line)->next = lm_new_raw_line(raw);
|
||||
}
|
||||
else
|
||||
*line = lm_new_raw_line(raw);
|
||||
}
|
||||
|
||||
void lm_clean_data(t_lmdata *data)
|
||||
{
|
||||
t_node *tmp;
|
||||
int i;
|
||||
|
||||
while (data->nodes_data)
|
||||
{
|
||||
tmp = data->nodes_data;
|
||||
data->nodes_data = data->nodes_data->next;
|
||||
ft_strdel(&(tmp->name));
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
i = 0;
|
||||
while (i < data->nb_nodes)
|
||||
{
|
||||
lst_inddel(&(data->adj[i]));
|
||||
i++;
|
||||
}
|
||||
ft_memdel((void**)&(data->adj));
|
||||
}
|
50
srcs/lm_graph_utils.c
Normal file
50
srcs/lm_graph_utils.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_graph_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/29 07:17:06 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 20:34:58 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static int is_link_in(t_lmdata *data, int src, int dest)
|
||||
{
|
||||
t_ind *it;
|
||||
if (src < data->nb_nodes)
|
||||
{
|
||||
it = data->adj[src];
|
||||
while (it)
|
||||
{
|
||||
if (it->index == dest)
|
||||
return (1);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_adj_parser(t_lmdata *lmdata, t_holder *holder)
|
||||
{
|
||||
t_temp *data;
|
||||
|
||||
if (holder != NULL)
|
||||
{
|
||||
data = holder->data;
|
||||
if (data != NULL)
|
||||
{
|
||||
while (data)
|
||||
{
|
||||
if (!is_link_in(lmdata, data->src_ind, data->dest_ind))
|
||||
lst_indadd_link(lmdata, data->src_ind, data->dest_ind);
|
||||
data = data->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
20
srcs/lm_initdata.c
Normal file
20
srcs/lm_initdata.c
Normal file
@@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_initdata.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/23 17:43:34 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/08 18:26:13 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_initdata(t_lmdata *data)
|
||||
{
|
||||
data->nbants = 0;
|
||||
data->nodes_data = NULL;
|
||||
data->adj = NULL;
|
||||
}
|
126
srcs/lm_mem_utils.c
Normal file
126
srcs/lm_mem_utils.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_mem_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:37 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 11:39:06 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_init_data(t_syntax *synt, t_holder *holder, t_lmdata *ldata)
|
||||
{
|
||||
synt->nb_state = 0;
|
||||
synt->s_cmd = 0;
|
||||
synt->e_cmd = 0;
|
||||
synt->s_error = 0;
|
||||
synt->e_error = 0;
|
||||
synt->v_error = 0;
|
||||
synt->l_error = 0;
|
||||
synt->s_vert = 0;
|
||||
synt->e_vert = 0;
|
||||
synt->gr_status = 0;
|
||||
synt->s_pos = 0;
|
||||
synt->e_pos = 0;
|
||||
synt->v_flag = 0;
|
||||
holder->count = 0;
|
||||
holder->data = NULL;
|
||||
ldata->nbants = 0;
|
||||
ldata->nb_nodes = 0;
|
||||
ldata->nodes_data = NULL;
|
||||
ldata->adj = NULL;
|
||||
}
|
||||
|
||||
static void lm_add_vertex_sub(t_lmdata *ldata, t_node *new)
|
||||
{
|
||||
t_node *tmp;
|
||||
|
||||
if (ldata->nodes_data == NULL)
|
||||
ldata->nodes_data = new;
|
||||
else
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = new;
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
|
||||
int lm_add_vertex(t_lmdata *ldata, char *raw, char flag,
|
||||
t_syntax *synt)
|
||||
{
|
||||
char **tab;
|
||||
t_node *new;
|
||||
|
||||
if (!(new = (t_node *)ft_memalloc(sizeof(t_node))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, ' ');
|
||||
if (tab != NULL)
|
||||
{
|
||||
if (lm_check_room_before(tab, synt))
|
||||
{
|
||||
if ((new->name = ft_strdup(tab[0])) == NULL)
|
||||
ft_del_words_tables(&tab);
|
||||
if (new->name == NULL)
|
||||
return (0);
|
||||
new->x = ft_atoi(tab[1]);
|
||||
new->y = ft_atoi(tab[2]);
|
||||
new->role = flag;
|
||||
new->ind = ldata->nb_nodes;
|
||||
new->next = NULL;
|
||||
lm_add_vertex_sub(ldata, new);
|
||||
ft_del_words_tables(&tab);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_find_index(t_lmdata *data, char *str)
|
||||
{
|
||||
t_node *nodes;
|
||||
|
||||
nodes = data->nodes_data;
|
||||
while (nodes)
|
||||
{
|
||||
if (ft_strcmp(nodes->name, str) == 0)
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int lm_ext_conn(t_holder *holder, t_lmdata *data, char *raw)
|
||||
{
|
||||
char **tab;
|
||||
t_temp *temp;
|
||||
t_temp *new;
|
||||
|
||||
if (!(new = (t_temp *)ft_memalloc(sizeof(t_temp))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, '-');
|
||||
if (tab != NULL)
|
||||
{
|
||||
new->src_ind = lm_find_index(data, tab[0]);
|
||||
new->dest_ind = lm_find_index(data, tab[1]);
|
||||
new->next = NULL;
|
||||
if (holder->data == NULL)
|
||||
holder->data = new;
|
||||
else
|
||||
{
|
||||
temp = holder->data;
|
||||
while (temp->next)
|
||||
temp = temp->next;
|
||||
temp->next = new;
|
||||
}
|
||||
(holder->count)++;
|
||||
ft_del_words_tables(&tab);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
99
srcs/lm_parser.c
Normal file
99
srcs/lm_parser.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:30:51 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 09:51:29 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_locate_cd(int index, t_syntax *synt, char *line)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
tmp = ft_strchr(line, '#');
|
||||
if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && line[1] == '#'))
|
||||
{
|
||||
if (!synt->s_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##start") == 0)
|
||||
{
|
||||
synt->s_cmd = 1;
|
||||
synt->s_pos = index;
|
||||
}
|
||||
}
|
||||
if (!synt->e_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##end") == 0)
|
||||
{
|
||||
synt->e_cmd = 1;
|
||||
synt->e_pos = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int lm_get_ant_(int counter, t_lmdata *ldata, t_syntax *synt, char *line)
|
||||
{
|
||||
int value;
|
||||
|
||||
if (counter == 0)
|
||||
{
|
||||
value = lm_get_value(line);
|
||||
if (value != -1)
|
||||
{
|
||||
ldata->nbants = value;
|
||||
synt->nb_state = 1;
|
||||
return (1);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int lm_get_vertices(int count, t_syntax *synt,
|
||||
t_lmdata *data, t_holder *holder, char *line)
|
||||
{
|
||||
lm_get_cmd_vert(count, synt, data, line);
|
||||
if (!synt->s_error && !synt->e_error)
|
||||
{
|
||||
lm_get_vert_link(count, synt, data, holder, line);
|
||||
if (!synt->v_error && !synt->l_error)
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_parser(t_syntax *synt, t_lmdata *ldata,
|
||||
t_holder *holder)
|
||||
{
|
||||
char *raw;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
while (ft_getline(&raw) > 0)
|
||||
{
|
||||
ft_printf("%s\n", raw);
|
||||
if (!(lm_get_ant_(index, ldata, synt, raw)) && index == 0)
|
||||
{
|
||||
ft_strdel(&raw);
|
||||
return (0);
|
||||
}
|
||||
lm_locate_cd(index, synt, raw);
|
||||
if (!(lm_get_vertices(index, synt, ldata, holder, raw)))
|
||||
{
|
||||
ft_strdel(&raw);
|
||||
return (0);
|
||||
}
|
||||
ft_strdel(&raw);
|
||||
index++;
|
||||
}
|
||||
ft_strdel(&raw);
|
||||
ft_putchar('\n');
|
||||
return (1);
|
||||
}
|
75
srcs/lm_parser_error_check.c
Normal file
75
srcs/lm_parser_error_check.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_parser_error_check.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/08 06:42:37 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/08 17:04:05 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_chk_format_nbr(char *raw)
|
||||
{
|
||||
while (*raw)
|
||||
{
|
||||
if (*raw == '-' || *raw == '+')
|
||||
raw++;
|
||||
if (!ft_isdigit(*raw))
|
||||
return (-1);
|
||||
raw++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_check_max(char *raw)
|
||||
{
|
||||
if (ft_atoi(raw) < INT_MIN || ft_atoi(raw) > INT_MAX)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_validate_name(char *name)
|
||||
{
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
if (name[0] == 'L')
|
||||
return (-1);
|
||||
while (name[index])
|
||||
{
|
||||
if (name[index] == '-')
|
||||
return (-1);
|
||||
index++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_validate_rooms(char *name, char *x, char *y)
|
||||
{
|
||||
if (lm_validate_name(name) == -1)
|
||||
return (-1);
|
||||
if (lm_chk_format_nbr(x) == -1 ||
|
||||
lm_check_max(x) == -1 || (ft_atoi(x) > 0 &&
|
||||
(ft_atoi(x) == 0)) ||
|
||||
ft_strlen(x) > 19)
|
||||
return (0);
|
||||
if (lm_chk_format_nbr(y) == -1 ||
|
||||
lm_check_max(y) == -1 || (ft_atoi(y) > 0 &&
|
||||
(ft_atoi(y) == 0)) ||
|
||||
ft_strlen(y) > 19)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int lm_error_nbr(char *raw)
|
||||
{
|
||||
if (lm_chk_format_nbr(raw) == -1 || lm_check_max(raw) == -1 ||
|
||||
(ft_atoi(raw) > 0 && (ft_atoi(raw) == 0)) ||
|
||||
ft_strlen(raw) > 19)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
90
srcs/lm_utils_parser.c
Normal file
90
srcs/lm_utils_parser.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_utils_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/04 09:24:45 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/08 16:51:51 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_get_value(char *line)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (line != NULL)
|
||||
{
|
||||
index = ft_atoi(line);
|
||||
if (index > INT_MIN && index < INT_MAX)
|
||||
{
|
||||
if (index > 0)
|
||||
return (index);
|
||||
else
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void lm_get_cmd_vert(int count, t_syntax *synt
|
||||
, t_lmdata *ldata, char *line)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
if (synt->s_pos == count - 1)
|
||||
{
|
||||
tmp = ft_strchr(line, '#');
|
||||
if (tmp == NULL && count != synt->e_vert)
|
||||
{
|
||||
synt->s_vert = count;
|
||||
if (!(lm_add_vertex(ldata, line, 's', synt)))
|
||||
synt->e_error = 1;
|
||||
}
|
||||
}
|
||||
if (synt->e_pos == count - 1)
|
||||
{
|
||||
tmp = ft_strchr(line, '#');
|
||||
if (tmp == NULL && count != synt->s_vert)
|
||||
{
|
||||
synt->e_vert = count;
|
||||
if (!(lm_add_vertex(ldata, line, 'e', synt)))
|
||||
synt->s_error = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lm_get_vert_link(int count, t_syntax *synt, t_lmdata *ldata
|
||||
, t_holder *holder, char *line)
|
||||
{
|
||||
char *link_sign;
|
||||
char *hash;
|
||||
|
||||
|
||||
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
|
||||
&& (count != synt->s_pos && count != synt->e_pos))
|
||||
{
|
||||
link_sign = ft_strchr(line, '-');
|
||||
hash = ft_strchr(line, '#');
|
||||
if (link_sign == NULL && hash == NULL)
|
||||
{
|
||||
if (!(lm_add_vertex(ldata, line, 'v', synt)))
|
||||
{
|
||||
synt->v_error = 1;
|
||||
}
|
||||
}
|
||||
if (link_sign != NULL && hash == NULL)
|
||||
{
|
||||
if (!(lm_ext_conn(holder, ldata, line)))
|
||||
{
|
||||
synt->l_error = 1;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
61
srcs/lst_ind.c
Normal file
61
srcs/lst_ind.c
Normal file
@@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lst_ind.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_ind **lst_indinit(t_lmdata *data)
|
||||
{
|
||||
data->adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * data->nb_nodes);
|
||||
return (data->adj);
|
||||
}
|
||||
|
||||
int lst_indadd_link(t_lmdata *data, int n1, int n2)
|
||||
{
|
||||
return (lst_indadd(&(data->adj[n1]), n2) && lst_indadd(&(data->adj[n2])
|
||||
, n1));
|
||||
}
|
||||
|
||||
t_ind *lst_indadd(t_ind **lst, int ind)
|
||||
{
|
||||
t_ind *new;
|
||||
|
||||
if ((new = (t_ind*)ft_memalloc(sizeof(t_ind))) != NULL)
|
||||
{
|
||||
new->index = ind;
|
||||
new->weight = 1;
|
||||
new->next = *lst;
|
||||
*lst = new;
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
|
||||
void lst_inddel(t_ind **lst)
|
||||
{
|
||||
t_ind *tmp;
|
||||
|
||||
while (lst && *lst != NULL)
|
||||
{
|
||||
tmp = *lst;
|
||||
*lst = (*lst)->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void tablst_inddel(t_ind **tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
lst_inddel(&(tab[i++]));
|
||||
ft_memdel((void**)&tab);
|
||||
}
|
95
srcs/push_ants.c
Normal file
95
srcs/push_ants.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_ants.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/18 19:38:55 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void clean_ants(t_ants **ants)
|
||||
{
|
||||
t_ants *it;
|
||||
t_ants *tmp;
|
||||
|
||||
while (*ants && (*ants)->end)
|
||||
{
|
||||
tmp = *ants;
|
||||
*ants = (*ants)->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
it = *ants;
|
||||
while (it && it->next)
|
||||
{
|
||||
if (it->next->end)
|
||||
{
|
||||
tmp = it->next;
|
||||
it->next = tmp->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
else
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, t_ind **paths)
|
||||
{
|
||||
t_ants *it;
|
||||
|
||||
it = *ants;
|
||||
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]
|
||||
, it->nb_node)->next->index;
|
||||
else
|
||||
it->end = 1;
|
||||
if (it->next)
|
||||
ft_putchar(' ');
|
||||
else
|
||||
ft_putchar('\n');
|
||||
it = it->next;
|
||||
}
|
||||
clean_ants(ants);
|
||||
}
|
||||
|
||||
int push_ants(t_lmdata *data, t_ind **paths, int nb_paths)
|
||||
{
|
||||
t_ants *ants;
|
||||
int ant_c;
|
||||
int i;
|
||||
|
||||
ants = NULL;
|
||||
i = 0;
|
||||
ant_c = 2;
|
||||
while (paths[i] && paths[i]->weight == 0)
|
||||
{
|
||||
ft_printf("path: %d\n", i);
|
||||
i++;
|
||||
}
|
||||
ft_printf("ant path: %d\n", i);
|
||||
if (add_ant(&ants, 1, i, paths) == NULL)
|
||||
return (0);
|
||||
while (ants || ant_c <= data->nbants)
|
||||
{
|
||||
while (++i < nb_paths)
|
||||
if (paths[i]->weight > 0)
|
||||
{
|
||||
if (!add_ant(&ants, ant_c, i, paths))
|
||||
del_ants(&ants);
|
||||
if (!ants)
|
||||
return (0);
|
||||
ant_c++;
|
||||
}
|
||||
print_ants(data, &ants, paths);
|
||||
i = -1;
|
||||
}
|
||||
return (1);
|
||||
}
|
71
srcs/push_ants_utils.c
Normal file
71
srcs/push_ants_utils.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_ants_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/10 11:23:36 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/18 19:04:49 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 *new;
|
||||
t_ants *it;
|
||||
|
||||
if ((new = (t_ants*)ft_memalloc(sizeof(t_ants))) != NULL)
|
||||
{
|
||||
new->nb_ant = nb_ant;
|
||||
new->nb_path = nb_path;
|
||||
new->nb_node = paths[nb_path]->index;
|
||||
new->end = 0;
|
||||
new->next = NULL;
|
||||
if (*ants == NULL)
|
||||
*ants = new;
|
||||
else
|
||||
{
|
||||
it = *ants;
|
||||
while (it->next)
|
||||
it = it->next;
|
||||
it->next = new;
|
||||
}
|
||||
paths[nb_path]->weight--;
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
|
||||
void del_ants(t_ants **ants)
|
||||
{
|
||||
t_ants *tmp;
|
||||
|
||||
while (*ants)
|
||||
{
|
||||
tmp = *ants;
|
||||
*ants = (*ants)->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
}
|
||||
|
||||
t_node *get_node(t_lmdata *data, int index)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = data->nodes_data;
|
||||
while (it && it->ind != index)
|
||||
it = it->next;
|
||||
return (it);
|
||||
}
|
||||
|
||||
t_ind *get_node_path(t_ind *lst, int index)
|
||||
{
|
||||
t_ind *it;
|
||||
|
||||
it = lst;
|
||||
while (it && it->index != index)
|
||||
it = it->next;
|
||||
return (it);
|
||||
}
|
89
srcs/score_utils.c
Normal file
89
srcs/score_utils.c
Normal file
@@ -0,0 +1,89 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* score_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/18 18:05:06 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/18 20:13:29 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void init_scores(t_ind **ret, int *scores)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret[i])
|
||||
{
|
||||
scores[i] = ret[i]->weight;
|
||||
ret[i]->weight = 0;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
static int get_nb_nodes(t_ind *lst)
|
||||
{
|
||||
t_ind *it;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
it = lst;
|
||||
while (it && (++i))
|
||||
it = it->next;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int get_score(t_lmdata *data, t_ind **ret, int nb_paths)
|
||||
{
|
||||
int nbants;
|
||||
int i;
|
||||
int min;
|
||||
int scores[nb_paths];
|
||||
|
||||
if (nb_paths == 1)
|
||||
{
|
||||
ret[0]->weight = data->nbants;
|
||||
min = (ret[0]->weight > 0) * (get_nb_nodes(ret[0]) + ret[0]->weight - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
min = FT_INT_MAX;
|
||||
i = -1;
|
||||
nbants = data->nbants;
|
||||
init_scores(ret, scores);
|
||||
while (ret[++i])
|
||||
if (min > scores[i])
|
||||
min = scores[i];
|
||||
while (nbants > 0 && !(i = 0))
|
||||
{
|
||||
ft_printf("=== debug score ===\nnb_paths: %d\nscore[%d]: %d\nmin: %d\nnbants: %d\n\n", nb_paths, i, scores[i], min, nbants);
|
||||
while (i < nb_paths && nbants > 0)
|
||||
{
|
||||
ft_printf("score[%d]: %d\n\n", i, scores[i]);
|
||||
if (scores[i] == min)
|
||||
{
|
||||
ft_printf("increment\n\n");
|
||||
nbants--;
|
||||
ret[i]->weight++;
|
||||
scores[i] = (ret[i]->weight > 0) * (get_nb_nodes(ret[i]) + ret[i]->weight - 1);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
i = -1;
|
||||
min = FT_INT_MAX;
|
||||
while (ret[++i])
|
||||
if (min > scores[i])
|
||||
min = scores[i];
|
||||
}
|
||||
i = -1;
|
||||
min = 0;
|
||||
while (ret[++i])
|
||||
if (min < scores[i])
|
||||
min = scores[i];
|
||||
}
|
||||
return (min);
|
||||
}
|
Reference in New Issue
Block a user