Skip to content

Commit

Permalink
added statistical information and some changes to code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGAzed committed Sep 7, 2024
1 parent 1577d63 commit 9e34012
Show file tree
Hide file tree
Showing 25 changed files with 416 additions and 368 deletions.
Binary file added assets/puzzles/hard/1.kkr
Binary file not shown.
1 change: 0 additions & 1 deletion program/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ add_library(program
source/instance/argument.c
source/instance/settings.c
source/instance/statistics.c
source/instance/expect.c

source/structures/concrete/board.c
source/structures/concrete/state.c
Expand Down
5 changes: 3 additions & 2 deletions program/include/gui/default.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

#include <structures/concrete/board.h>

#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 550
#define WINDOW_WIDTH 750
#define WINDOW_HEIGHT 800

#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
Expand All @@ -37,6 +37,7 @@ struct nk_super {
struct nk_context * context;
struct nk_solver solver;
struct nk_media media;
int width, height;
};

#endif /* GUI_DEFAULT_H */
6 changes: 4 additions & 2 deletions program/include/gui/interface/solver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <structures/concrete/state.h>

#include <pthread.h>

typedef enum ds_action {
CREATE_DS, DESTROY_DS, NEXT_VALUE_DS, NEXT_VALUE_TIMED_DS,
PREV_VALUE_DS, RESET_DS, CURRENT_VALUE_DS, PREV_VALUE_TIMED_DS,
Expand All @@ -24,7 +26,7 @@ typedef enum player_state {
} player_state_e;

typedef enum solver_state {
SOLVE_UNSET_E, SOLVE_RUNNING_E, SOLVE_FINISHED_E,
SOLVE_UNSET_E, SOLVE_RUNNING_E, SOLVE_FINISHED_E, SOLVE_STOPPED_E,
} solver_state_e;

typedef struct player {
Expand All @@ -33,7 +35,7 @@ typedef struct player {
} player_s;

player_s * get_player_singleton(void);
void solve(struct nk_solver * data);
pthread_t solve(void);
state_array_s state_provider(const ds_action_e action);

#endif /* GUI_INTERFACE_SOLVER_H */
79 changes: 41 additions & 38 deletions program/include/instance/expect.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,51 @@
#include <assert.h>
#include <stdlib.h>

/**
* Enum modes that determine what action should happen after executing error_action.
*/
typedef enum error_mode_type {
DEFAULT_E, ABORT_E, ASSERT_E, EXIT_E,
DEFAULT_E, ABORT_E, EXIT_E,
} error_mode_e;

extern error_mode_e error_mode;
extern FILE * error_log;

/**
* Static error mode indicator.
*/
static error_mode_e error_mode;
/**
* External global error log FILE pointer variable to write errors to file.
*/
extern FILE * error_log;

/**
* Placeholder macro that doesn't execute anything.
*/
#define NO_ACTION (void)(0)

#ifdef ERROR_LOG_FILE_PATH

#define expect(assertion, error_action, ...) \
{ \
if (!(assertion)) { \
fprintf(error_log, __VA_ARGS__); \
fprintf(error_log, "\n"); \
fflush(error_log ? error_log : stderr); \
switch (error_mode) { \
case ABORT_E : { error_action; abort(); break; } \
case ASSERT_E : { error_action; assert(0 && (assertion)); break; } \
default : { error_action; break; } \
} \
} \
/**
* Placeholder debug action that asserts 0 (false) for debug break.
*/
#define DEBUG_ACTION (assert(0))

/**
* @brief An assert-esque macro with extra features like running
* suer defined action code after assertion fails.
* @param assertion statement that is expected to be true
* @param error_action action to perform after assertion is false
*/
#define expect(assertion, error_action, ...) \
{ \
if (!(assertion)) { \
FILE * temp = error_log ? error_log : stderr; \
fprintf(temp, #assertion "\n" __VA_ARGS__); \
fprintf(temp, "\n"); \
fflush(temp); \
error_action; \
switch (error_mode) { \
case ABORT_E : { abort(); break; } \
case EXIT_E : { exit(EXIT_FAILURE); break; } \
default : { break; } \
} \
} \
}

#else

#define expect(assertion, error_action, ...) \
{ \
if (!(assertion)) { \
fprintf(error_log ? error_log : stderr, __VA_ARGS__); \
fprintf(error_log ? error_log : stderr, "\n"); \
fflush(error_log ? error_log : stderr); \
switch (error_mode) { \
case ABORT_E : { error_action; abort(); break; } \
case ASSERT_E : { error_action; assert(0 && (assertion)); break;} \
case EXIT_E : { error_action; exit(EXIT_FAILURE); break; } \
default : { error_action; break; } \
} \
} \
}

#endif /* ERROR_LOG_FILE_PATH */

#endif /* INSTANCE_EXPECT_H */
20 changes: 14 additions & 6 deletions program/include/instance/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
#include <stdbool.h>
#include <time.h>

/**
* Maximum delay when accesing states in gui's 'forward/backward play'
* player mode.
*/
#define MAX_DELAY_MS 500

/**
* The settings that can be set by arguments by the user
*/
typedef struct settings {
char * filepath;
char * filepath; // -fp or --filepath : the path of the file containing a kakuro board to solve

bool is_backtrack;
bool is_forward_check;
bool is_arc_consistency;
bool is_reduce;
bool is_backtrack; // -bt --backtrack : sets 'backtracking' to true
bool is_forward_check; // -fch --forward-check : sets 'forward checking' to true
bool is_arc_consistency; // -ac --arc-consistency : sets 'arc consistency' to true
bool is_reduce; // -r --reduce : sets 'initial state reduction' to true

clock_t time_ms;
clock_t time_ms; // time delay to set next state if player is playing (only variable that gets set in gui)
} settings_s;

settings_s * get_settings_singleton(void);
Expand Down
14 changes: 5 additions & 9 deletions program/include/instance/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@
#include <stdbool.h>

typedef struct statistics {
size_t backtrack_call_count;
size_t forward_check_call_count;
size_t look_ahead_call_count;
double backtrack_count;
double bad_forward_check_count;
double bad_arc_consistency_count;

size_t invalid_state_backtrack_count;
size_t invalid_state_forward_check_count;
size_t invalid_state_look_ahead_count;

size_t dfs_iteration_count;
size_t dfs_stack_max_size;
double dfs_iteration_count;
double dfs_stack_max_size;
} Statistics;

Statistics * get_stat_singleton(void);
Expand Down
2 changes: 1 addition & 1 deletion program/include/structures/abstract/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#ifndef FINITE_STACK

#define STACK_LIST_ARRAY_SIZE (1 << 8)
#define STACK_LIST_ARRAY_SIZE (1 << 10)

typedef struct stack_list_array {
STACK_DATA_TYPE elements[STACK_LIST_ARRAY_SIZE];
Expand Down
7 changes: 4 additions & 3 deletions program/include/structures/concrete/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#define GRID_DIMENTIONS 2

typedef int8_t lookup_t;
typedef uint8_t ulookup_t;
typedef int16_t llookup_t;
typedef uint8_t ulookup_t;
typedef enum kakuro_grid_sizes { ROW_E = 0, COLUMN_E = 1, } KGSizes;
typedef struct board_grid {
lookup_t **grids[GRID_DIMENTIONS];
Expand All @@ -20,7 +21,7 @@ typedef struct board_grid {
typedef struct board {
board_grid_s game;

lookup_t ** grid;
llookup_t ** grid;
union {
struct {
ulookup_t * coords[GRID_DIMENTIONS];
Expand All @@ -36,7 +37,7 @@ typedef enum check {
COLCHECK = 2, CHEKCED = 3,
} check_e;

board_s create_board(FILE * kakuro_file);
board_s create_board(char * kakuro_filepath);
void destroy_board(board_s * board);
bool is_wall_hit(board_s board, ulookup_t row, ulookup_t col);
void print_board(board_s board);
Expand Down
1 change: 1 addition & 0 deletions program/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
int main(const int argc, char ** argv) {
setup_program(argc, argv);
run_program();

return 0;
}
14 changes: 7 additions & 7 deletions program/source/algorithms/arc_consistency.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ bool _reduce_no_col_combination(board_s board, state_array_s * current_state, u
bool _reduce_no_row_combination(board_s board, state_array_s * current_state, ulookup_t index);

bool look_ahead(const board_s board, state_array_s * current_state) {
error_mode = ASSERT_E;
expect(current_state, NO_ACTION, "ERROR: current state parameter is NULL (%p)", (void*)current_state);
error_mode = EXIT_E;
expect(current_state, DEBUG_ACTION, "ERROR: current state parameter is NULL (%p)", (void*)current_state);

if (!get_settings_singleton()->is_arc_consistency) return true;

get_stat_singleton()->look_ahead_call_count++;

check_e * checks = calloc(board.game.empty_count, sizeof(check_e));
error_mode = ASSERT_E;
expect(checks, NO_ACTION, "ERROR: 'checks' variable allocation failed (%p)", (void*)checks);
error_mode = EXIT_E;
expect(checks, DEBUG_ACTION, "ERROR: 'checks' variable allocation failed (%p)", (void*)checks);
do {
_reduce_one_values(board, current_state, checks);
} while (valid_states(*current_state) && _reduce_no_combination(board, current_state, checks));
free(checks);

return !invalid_state_look_ahead_stat(!valid_states(*current_state));
bool valid_arc_consistency = valid_states(*current_state);
if (!valid_arc_consistency) get_stat_singleton()->bad_arc_consistency_count++;
return valid_arc_consistency;
}

void _reduce_one_values(board_s board, state_array_s * current_state, check_e * checks) {
Expand Down
5 changes: 2 additions & 3 deletions program/source/algorithms/backtrack.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ bool _backtrack_col_repeat(board_s board, state_array_s current_state, size_t in
bool backtrack(board_s board, state_array_s current_state) {
if (!get_settings_singleton()->is_backtrack) return _backtrack_valid_sums(board, current_state);

get_stat_singleton()->backtrack_call_count++;

check_e * checks = calloc(board.game.empty_count, sizeof(check_e));

bool is_backtrack = false;
Expand All @@ -36,7 +34,8 @@ bool backtrack(board_s board, state_array_s current_state) {

free(checks);

return (is_backtrack);
if (is_backtrack) get_stat_singleton()->backtrack_count++;
return is_backtrack;
}

bool _backtrack_row_sum(board_s board, state_array_s current_state, size_t index) {
Expand Down
17 changes: 7 additions & 10 deletions program/source/algorithms/forward_checking.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ bool _row_forward_check(board_s board, state_array_s * current_state, ulookup_t
bool _col_forward_check(board_s board, state_array_s * current_state, ulookup_t index);

bool forward_checking(board_s board, state_array_s * current_state, ulookup_t index) {
error_mode = ASSERT_E;
expect(current_state, NO_ACTION, "current state parameter is NULL (%p)", (void*)current_state);
expect(is_one_value(current_state->elements[index]), NO_ACTION, "current state element at index %u is not a one value", index);
expect(index < current_state->size, NO_ACTION, "index '%u' is out of bounds of current state size '%u'", index, current_state->size);
error_mode = EXIT_E;
expect(current_state, DEBUG_ACTION, "current state parameter is NULL (%p)", (void*)current_state);
expect(is_one_value(current_state->elements[index]), DEBUG_ACTION, "current state element at index %u is not a one value", index);
expect(index < current_state->size, DEBUG_ACTION, "index '%u' is out of bounds of current state size '%u'", index, current_state->size);

if (!get_settings_singleton()->is_forward_check) return true;

get_stat_singleton()->forward_check_call_count++;

return !invalid_state_forward_check_stat(!(
_row_forward_check(board, current_state, index) &&
_col_forward_check(board, current_state, index)
));
bool valid_forward_check = _row_forward_check(board, current_state, index) && _col_forward_check(board, current_state, index);
if (!valid_forward_check) get_stat_singleton()->bad_forward_check_count++;
return valid_forward_check;
}

bool _row_forward_check(board_s board, state_array_s * current_state, ulookup_t index) {
Expand Down
Loading

0 comments on commit 9e34012

Please sign in to comment.