Solver algorithm implemented

This commit is contained in:
Rory Healy 2021-10-21 16:10:20 +11:00
parent ae85d7dbff
commit cf9d366133
24 changed files with 55 additions and 13 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.vscode/

View file

@ -5,8 +5,8 @@
## Makefile ## Makefile
## ##
#CC = gcc -Wall -Wextra -O3 -g CC = gcc -Wall -Wextra -O3 -g
CC = gcc -Wall -Wextra -g #CC = gcc -Wall -Wextra -g
RM = rm -f RM = rm -f

BIN
lib/my_putchar.o Normal file

Binary file not shown.

BIN
lib/my_putstr.o Normal file

Binary file not shown.

BIN
sokoban Executable file

Binary file not shown.

1
solution.txt Normal file
View file

@ -0,0 +1 @@
drdrdrdrrruuuuuulllllddrdrdrDulululldRdRdrdRRllululuurdrdrDululldRuuluurrrrrdddddrddlUUUUUUruLLLLLulDDdrdddrdRRlluluurdrDulldruluulldRuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdddRRlluurDldRuulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDDdrdrdrddRluulululldRdRluuuurrrrrdddddrddlUUUUUUruLLLLLulDD

View file

@ -59,18 +59,14 @@ char* save_solution( node_t* solution_node ){
* Copy a src into a dst state * Copy a src into a dst state
*/ */
void copy_state(sokoban_t* init_data, state_t* dst, state_t* src){ void copy_state(sokoban_t* init_data, state_t* dst, state_t* src){
dst->map = malloc(sizeof(char *) * init_data->lines); dst->map = malloc(sizeof(char *) * init_data->lines);
for ( int i = 0; i < init_data->lines; ++i ){ for ( int i = 0; i < init_data->lines; ++i ){
int width = strlen(src->map[i]) + 1; int width = strlen(src->map[i]) + 1;
dst->map[i] = malloc(width); dst->map[i] = malloc(width);
memcpy(dst->map[i], src->map[i], width); memcpy(dst->map[i], src->map[i], width);
} }
dst->player_x = src->player_x; dst->player_x = src->player_x;
dst->player_y = src->player_y; dst->player_y = src->player_y;
} }
/** /**
@ -184,7 +180,6 @@ void find_solution(sokoban_t* init_data, bool show_solution)
unsigned generated_nodes = 0; unsigned generated_nodes = 0;
unsigned expanded_nodes = 0; unsigned expanded_nodes = 0;
unsigned duplicated_nodes = 0; unsigned duplicated_nodes = 0;
int max_depth = 0;
unsigned solution_size = 0; unsigned solution_size = 0;
// Choose initial capacity of PRIME NUMBER // Choose initial capacity of PRIME NUMBER
@ -216,6 +211,39 @@ void find_solution(sokoban_t* init_data, bool show_solution)
* FILL IN THE GRAPH ALGORITHM * FILL IN THE GRAPH ALGORITHM
* */ * */
while (pq.count > 0) {
n = heap_delete(&pq);
update_explore_table(n, expanded_nodes);
expanded_nodes += 1;
if (winning_condition(init_data, &(n->state))) {
solution = save_solution(n);
solution_size = n->depth;
break;
}
for (move_t action = left; action <= down; action++) {
node_t *new = malloc(sizeof(*new));
bool player_moved = applyAction(init_data, n, &new, action);
generated_nodes += 1;
if (!player_moved || simple_corner_deadlock(init_data, &(new->state))) {
free(new->state.map);
free(new);
continue;
}
flatten_map(init_data, &flat_map, new->state.map);
if (ht_lookup(&hashTable, flat_map) != NULL) {
duplicated_nodes += 1;
free(new->state.map);
free(new);
continue;
}
ht_insert(&hashTable, flat_map, &flat_map);
heap_push(&pq, new);
}
}
//---------------------------- //----------------------------
@ -224,13 +252,24 @@ void find_solution(sokoban_t* init_data, bool show_solution)
ht_destroy(&hashTable); ht_destroy(&hashTable);
free_memory(expanded_nodes); free_memory(expanded_nodes);
free(flat_map); free(flat_map);
for (int i = 0; i < init_data->lines; i++) {
free((init_data->map)[i]);
free((init_data->map_save)[i]);
}
free(init_data->map);
free(init_data->map_save);
free(init_data->buffer);
emptyPQ(&pq);
// free(pq.heaparr);
// printf("%d\n", pq.count);
//---------------------------- //----------------------------
// Stop clock // Stop clock
clock_t end = clock(); clock_t end = clock();
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
// Show Soltion // Show solution
if( show_solution && solution != NULL ) play_solution( *init_data, solution); if( show_solution && solution != NULL ) play_solution( *init_data, solution);
endwin(); endwin();
@ -257,7 +296,6 @@ void find_solution(sokoban_t* init_data, bool show_solution)
printf("\tExpanded/seconds: %d\n", (int)(expanded_nodes/cpu_time_used) ); printf("\tExpanded/seconds: %d\n", (int)(expanded_nodes/cpu_time_used) );
printf("\tTime (seconds): %f\n", cpu_time_used ); printf("\tTime (seconds): %f\n", cpu_time_used );
} }
void solve(char const *path, bool show_solution) void solve(char const *path, bool show_solution)

BIN
src/ai/ai.o Normal file

Binary file not shown.

BIN
src/ai/hashtable.o Normal file

Binary file not shown.

View file

@ -101,7 +101,10 @@ node_t* heap_delete(struct heap* h)
void emptyPQ(struct heap* pq) { void emptyPQ(struct heap* pq) {
while(pq->count != 0) { while(pq->count != 0) {
node_t* n = heap_delete(pq); node_t* n = heap_delete(pq);
for (int i = size; i >= 0; i--) {
free(n->state.map[i]);
}
free(n->state.map);
free(n); free(n);
//printf("<<%d", heap_delete(pq));
} }
} }

BIN
src/ai/priority_queue.o Normal file

Binary file not shown.

View file

@ -50,4 +50,3 @@ bool winning_condition(sokoban_t *init_data, state_t *state);
void play_solution( sokoban_t init_data, char* solution ); void play_solution( sokoban_t init_data, char* solution );
void print_map(sokoban_t *init_data, state_t *state); void print_map(sokoban_t *init_data, state_t *state);
#endif #endif

BIN
src/ai/utils.o Normal file

Binary file not shown.

BIN
src/find_player.o Normal file

Binary file not shown.

BIN
src/helper.o Normal file

Binary file not shown.

BIN
src/key_check.o Normal file

Binary file not shown.

BIN
src/loose_check.o Normal file

Binary file not shown.

BIN
src/main.o Normal file

Binary file not shown.

BIN
src/map_check.o Normal file

Binary file not shown.

BIN
src/map_reading.o Normal file

Binary file not shown.

BIN
src/movement.o Normal file

Binary file not shown.

BIN
src/play.o Normal file

Binary file not shown.

BIN
src/win_check.o Normal file

Binary file not shown.

BIN
src/zone_check.o Normal file

Binary file not shown.