Start implementing actual search

This commit is contained in:
Markus Koch 2020-05-02 17:31:26 +02:00
parent 127e0d23ca
commit 7d41c1cdf7
4 changed files with 83 additions and 9 deletions

View File

@ -5,18 +5,14 @@
#define DIJKSTRA_WEIGHT_AUTO (-1) #define DIJKSTRA_WEIGHT_AUTO (-1)
typedef int dijkstra_cost; typedef unsigned int dijkstra_cost;
#define DIJKSTRA_COST_MAX UINT_MAX
struct dijkstra_solver { struct dijkstra_solver {
GList *nodes; GList *nodes;
GList *paths; GList *paths;
}; };
struct dijkstra_state {
dijkstra_cost cost;
int visited;
};
struct position { struct position {
float x; float x;
float y; float y;

45
src/dijkstrasearch.c Normal file
View File

@ -0,0 +1,45 @@
#include "dijkstrasearch.h"
void dijkstra_search_reset(struct dijkstra_search *search)
{
unsigned int i;
for (i=0; i < search->node_count; i++) {
search->states[i]->cost = DIJKSTRA_COST_MAX;
search->states[i]->visited = 0;
}
}
// WARNING: After creating this object, you must NOT modify the solver!
struct dijkstra_search *dijkstra_search_new(struct dijkstra_solver *solver)
{
struct dijkstra_search *search;
unsigned int i;
search = malloc(sizeof(*search));
if (search) {
search->states = malloc(sizeof(*search->states) * g_list_length(solver->nodes));
search->node_count = g_list_length(solver->nodes);
for (i=0; i < search->node_count; i++) {
search->states[i] = malloc(sizeof(struct dijkstra_state));
if (!search->states) {
// TODO: I should dealloc all allocated memory before returning,
// but whatever, we'll crash and burn one way of the other.
return NULL;
}
}
dijkstra_search_reset(search);
}
return search;
}
void dijkstra_search_free(struct dijkstra_search *search)
{
unsigned int i;
for (i=0; i < search->node_count; i++) {
free(search->states[i]);
}
free(search->states);
free(search);
}

19
src/dijkstrasearch.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef DIJKSTRASEARCH_H
#define DIJKSTRASEARCH_H
#include "dijkstragraph.h"
struct dijkstra_state {
dijkstra_cost cost;
int visited;
};
struct dijkstra_search {
struct dijkstra_state **states;
unsigned int node_count;
};
struct dijkstra_search *dijkstra_search_new(struct dijkstra_solver *solver);
void dijkstra_search_free(struct dijkstra_search *search);
#endif // DIJKSTRASEARCH_H

View File

@ -1,9 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#include <glib.h>
#include "dijkstragraph.h" #include "dijkstragraph.h"
#include <glib/glist.h> #include "dijkstrasearch.h"
#include "geojson.h" #include "geojson.h"
#include "logging.h" #include "logging.h"
@ -44,9 +45,12 @@ int main(int argc, char *argv[])
struct dijkstra_node *node; struct dijkstra_node *node;
struct dijkstra_path *path; struct dijkstra_path *path;
struct dijkstra_search *search;
/* Init stuff */
set_log_level(LL_INFO); set_log_level(LL_INFO);
// Process arguments /* Process arguments */
while((opt = getopt(argc, argv, "vs")) != -1) { while((opt = getopt(argc, argv, "vs")) != -1) {
switch(opt) switch(opt)
{ {
@ -70,13 +74,14 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
/* Generate solver */
solver = dijkstra_solver_new(); solver = dijkstra_solver_new();
if (!solver) { if (!solver) {
report(LL_CRITICAL, "Memory allocation failure."); report(LL_CRITICAL, "Memory allocation failure.");
return 2; return 2;
} }
/* Parse file list */ /* Load map data -> Generate graph */
for (i = optind; i < argc; ++i) { for (i = optind; i < argc; ++i) {
add_geojson_to_dijkstra(solver, argv[i]); add_geojson_to_dijkstra(solver, argv[i]);
} }
@ -104,6 +109,15 @@ int main(int argc, char *argv[])
g_list_length(solver->nodes), g_list_length(solver->nodes),
g_list_length(solver->paths)); g_list_length(solver->paths));
/* Perform search */
search = dijkstra_search_new(solver);
if (!search) {
report(LL_ERROR, "Error allocating dijkstra_search");
dijkstra_solver_free(solver);
return -3;
}
dijkstra_search_free(search);
dijkstra_solver_free(solver); dijkstra_solver_free(solver);
return 0; return 0;