Skip to content

Commit 7fb4ef6

Browse files
committed
stack based num_solutions works and adding free
1 parent 11ccda3 commit 7fb4ef6

11 files changed

+140
-117
lines changed

2d_array_utils.c

+49-19
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,65 @@
22
#include "stdlib.h"
33
#include "optional_cell_values.h"
44

5-
void copy_board_values(int** to, int** from, int size) {
5+
void copy_board_values(int **to, int **from, int size)
6+
{
67
int i;
78
int j;
8-
for (i = 0; i < size; ++i) {
9-
for (j = 0; j < size; ++j) {
9+
for (i = 0; i < size; ++i)
10+
{
11+
for (j = 0; j < size; ++j)
12+
{
1013
to[i][j] = from[i][j];
1114
}
1215
}
1316
}
1417

15-
void free_2d_array(int **array, int size) {
18+
void free_2d_array(int **array, int size)
19+
{
1620
int i;
17-
for (i = 0; i < size; ++i) {
21+
for (i = 0; i < size; ++i)
22+
{
1823
free(array[i]);
1924
}
2025

2126
free(array);
2227
}
2328

24-
int** create_2d_array(int size) {
29+
void free_possible_values_2d_array(OptionalCellValues **array, int size)
30+
{
2531
int i;
26-
int ** array;
27-
array = (int **)malloc(size * sizeof(int*));
32+
int j;
33+
for (i = 0; i < size; ++i)
34+
{
35+
for (j = 0; j < size; j++)
36+
{
37+
free(array[i][j].propabilities);
38+
}
39+
40+
free(array[i]);
41+
}
42+
43+
free(array);
44+
}
45+
46+
int **create_2d_array(int size)
47+
{
48+
int i;
49+
int **array;
50+
array = (int **)malloc(size * sizeof(int *));
2851

29-
if (array == NULL) {
52+
if (array == NULL)
53+
{
3054
printf("Error: create_2d_array has failed\n");
3155
exit(0);
3256
}
3357

34-
for (i=0; i<size; i++) {
35-
array[i] = (int *) calloc(size, sizeof(int));
58+
for (i = 0; i < size; i++)
59+
{
60+
array[i] = (int *)calloc(size, sizeof(int));
3661

37-
if(array[i] == NULL) {
62+
if (array[i] == NULL)
63+
{
3864
printf("Error: create_2d_array has failed\n");
3965
exit(0);
4066
}
@@ -43,20 +69,24 @@ int** create_2d_array(int size) {
4369
return array;
4470
}
4571

46-
OptionalCellValues ** create_2d_possible_values_array(int size) {
72+
OptionalCellValues **create_2d_possible_values_array(int size)
73+
{
4774
int i;
48-
OptionalCellValues ** array;
49-
array = (OptionalCellValues **)calloc(size , sizeof(OptionalCellValues*));
75+
OptionalCellValues **array;
76+
array = (OptionalCellValues **)calloc(size, sizeof(OptionalCellValues *));
5077

51-
if (array == NULL) {
78+
if (array == NULL)
79+
{
5280
printf("Error: create_2d_array has failed\n");
5381
exit(0);
5482
}
5583

56-
for (i=0; i<size; i++) {
57-
array[i] = (OptionalCellValues *) calloc(size, sizeof(OptionalCellValues));
84+
for (i = 0; i < size; i++)
85+
{
86+
array[i] = (OptionalCellValues *)calloc(size, sizeof(OptionalCellValues));
5887

59-
if(array[i] == NULL) {
88+
if (array[i] == NULL)
89+
{
6090
printf("Error: create_2d_array has failed\n");
6191
exit(0);
6292
}

2d_array_utils.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ int** create_2d_array(int size);
1111

1212
OptionalCellValues ** create_2d_possible_values_array(int size);
1313

14+
void free_possible_values_2d_array(OptionalCellValues **array, int size);
15+
1416
#endif

LP.c

+14-40
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ int get_num_of_parameters(Board *board)
3030
}
3131
}
3232

33+
free(possible_values);
34+
3335
return counter;
3436
}
3537

@@ -67,6 +69,7 @@ gurobi_var *initilize_gurobi_vars(int num_of_params, Board *board)
6769
}
6870
}
6971

72+
free(possible_values);
7073
return vars;
7174
}
7275

@@ -199,6 +202,8 @@ int add_single_value_per_cell_constraints(Board *board, GRBmodel *model, int *in
199202
add_vectors_to_constraints(model, num_of_idexes_for_constraint, ind, val, env);
200203
}
201204
}
205+
206+
free(gurobi_indexes_for_constraint);
202207
return 1;
203208
}
204209

@@ -229,7 +234,7 @@ int add_rows_constraints(Board *board, GRBmodel *model, int *ind, double *val, G
229234
add_vectors_to_constraints(model, num_of_idexes_for_constraint, ind, val, env);
230235
}
231236
}
232-
237+
free(gurobi_indexes_for_constraint);
233238
return 1;
234239
}
235240

@@ -259,7 +264,7 @@ int add_column_constraints(Board *board, GRBmodel *model, int *ind, double *val,
259264
add_vectors_to_constraints(model, num_of_idexes_for_constraint, ind, val, env);
260265
}
261266
}
262-
267+
free(gurobi_indexes_for_constraint);
263268
return 1;
264269
}
265270

@@ -302,6 +307,9 @@ int add_square_constraints(Board *board, GRBmodel *model, int *ind, double *val,
302307
add_vectors_to_constraints(model, num_of_idexes_for_constraint, ind, val, env);
303308
}
304309
}
310+
311+
free(gurobi_indexes_for_constraint);
312+
free_2d_array(square_num_matrix, board->num_of_columns);
305313
return 1;
306314
}
307315

@@ -516,19 +524,12 @@ OptionalCellValues get_possible_values_from_sol(Board *board, double *sol, int r
516524
{
517525
if (sol[gurobi_relevant_indexes[i]] < threshold)
518526
{
519-
/*printf("index %d,%d and represented num of: %d which has index %d of gurobi has prob of %f filtterd by threshold\n",
520-
vars[gurobi_relevant_indexes[i]].row, vars[gurobi_relevant_indexes[i]].column, vars[gurobi_relevant_indexes[i]].possible_value,
521-
gurobi_relevant_indexes[i], sol[gurobi_relevant_indexes[i]]); */
522527
continue;
523528
}
524-
525-
/* printf("index %d,%d and represented num of: %d which has index %d of gurobi has prob of %f was not filtterd by threshold\n",
526-
vars[gurobi_relevant_indexes[i]].row, vars[gurobi_relevant_indexes[i]].column, vars[gurobi_relevant_indexes[i]].possible_value,
527-
gurobi_relevant_indexes[i], sol[gurobi_relevant_indexes[i]]); */
528-
529529
cell_values.propabilities[vars[gurobi_relevant_indexes[i]].possible_value - 1] = sol[gurobi_relevant_indexes[i]];
530530
}
531531

532+
free(gurobi_relevant_indexes);
532533
return cell_values;
533534
}
534535

@@ -562,35 +563,6 @@ int prob_based_decide_result(float *cell_probs, float threshold, int n)
562563
return BOARD_NULL_VALUE;
563564
}
564565

565-
void print_cell_results(OptionalCellValues cell_values, int size)
566-
{
567-
int k;
568-
for (k = 0; k < size; k++)
569-
{
570-
if (cell_values.propabilities[k] > 0)
571-
{
572-
printf("value %d for index %d,%d has prob of %f\n", k + 1, cell_values.row, cell_values.column, cell_values.propabilities[k]);
573-
}
574-
}
575-
}
576-
577-
void print_gurobi_results(Board *board, double *sol, float threshold, gurobi_var *vars, int possilbe_values_size)
578-
{
579-
int i;
580-
int j;
581-
int chosen_val;
582-
int board_size = board->num_of_columns * board->num_of_rows;
583-
OptionalCellValues cell_values;
584-
for (i = 0; i < board_size; i++)
585-
{
586-
for (j = 0; j < board_size; j++)
587-
{
588-
cell_values = get_possible_values_from_sol(board, sol, i, j, threshold, vars, possilbe_values_size);
589-
print_cell_results(cell_values, board_size);
590-
}
591-
}
592-
}
593-
594566
void fill_results_to_board(Board *board, double *sol, float threshold, gurobi_var *vars, int num_of_params)
595567
{
596568
int i;
@@ -628,6 +600,8 @@ void fill_results_to_board(Board *board, double *sol, float threshold, gurobi_va
628600
}
629601
}
630602
}
603+
604+
free_possible_values_2d_array(cell_values, board_size);
631605
}
632606

633607
int fill_board(Board *board, int is_integer, float threshold)
@@ -648,7 +622,7 @@ int fill_board(Board *board, int is_integer, float threshold)
648622
return 1;
649623
}
650624

651-
OptionalCellValues get_value_for_cell(Board *board, int row, int column, int is_integer, int* is_succedded)
625+
OptionalCellValues get_value_for_cell(Board *board, int row, int column, int is_integer, int *is_succedded)
652626
{
653627
int i;
654628
int board_size = board->num_of_columns * board->num_of_rows;

backtrack_state.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef struct _Backtrack_state {
1111

1212
/* 1 for true */
1313
int is_default;
14+
int was_empty_cell;
1415

1516
} BacktrackState;
1617

parser.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int parse_command(char *command, Board *board, Curr_move move)
6464
int x, y, z;
6565
float threshold;
6666
char *token, *next;
67-
int succeeded;
67+
int succeeded;
6868
int size = board->num_of_columns * board->num_of_rows;
6969
int i = 0;
7070

@@ -241,8 +241,9 @@ int parse_command(char *command, Board *board, Curr_move move)
241241
}
242242
return 0;
243243
}
244-
244+
245245
/*COMMAND 6*/
246+
246247
if (strcmp(token, "validate") == 0)
247248
{
248249
if (board->mode == INIT)
@@ -273,7 +274,7 @@ int parse_command(char *command, Board *board, Curr_move move)
273274
}
274275
return 6;
275276
}
276-
277+
277278
/*COMMAND 7*/
278279
if (strcmp(token, "guess") == 0)
279280
{
@@ -294,7 +295,7 @@ int parse_command(char *command, Board *board, Curr_move move)
294295
print_error_number("first");
295296
return 0;
296297
}
297-
298+
298299
next = (strtok(NULL, " \t\r\n"));
299300
if(next != NULL)
300301
{
@@ -308,7 +309,7 @@ int parse_command(char *command, Board *board, Curr_move move)
308309
return 0;
309310
}
310311

311-
guess(board, threshold);
312+
guess(board, threshold);
312313
clean_nexts(move);
313314
add_new_move(move, board);
314315
print_board(board);
@@ -454,7 +455,7 @@ int parse_command(char *command, Board *board, Curr_move move)
454455
return 0;
455456
}
456457

457-
succeeded = save(board, token);
458+
succeeded = save(board, token);
458459
if (!succeeded)
459460
return 0;
460461
return 11;

stack.c

+1-12
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,10 @@ void push(Stack* stack, BacktrackState item)
2828

2929
BacktrackState pop(Stack* stack)
3030
{
31-
BacktrackState default_state = {-1,-1,0,NULL,0,1};
31+
BacktrackState default_state = {-1,-1,0,NULL,0,1,0};
3232
if (isEmpty(stack))
3333

3434
// because it was initilized on the stack, the value could not be used outside
3535
return default_state;
3636
return stack->items[(stack->top_item_index)--];
3737
}
38-
39-
// Function to return the top from stack without removing it
40-
BacktrackState peek(Stack* stack)
41-
{
42-
BacktrackState default_state = {-1,-1,0,NULL,0,1};
43-
if (isEmpty(stack)) {
44-
// because it was initilized on the stack, the value could not be used outside
45-
return default_state;
46-
}
47-
return stack->items[stack->top_item_index];
48-
}

stack.h

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ typedef struct _Stack {
1313
int isEmpty(Stack* stack);
1414
void push(Stack* stack, BacktrackState item);
1515
BacktrackState pop(Stack* stack);
16-
BacktrackState peek(Stack* stack);
1716
Stack* createStack(int capacity);
1817

1918
#endif

0 commit comments

Comments
 (0)