-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
148 lines (130 loc) · 4.15 KB
/
core.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
// Execution parameters
int N; // Number of propositions.
int K; // Number of clauses.
int M; // Number of propositions per clause.
int *Problem; // This is a table to keep all the clauses of the problem.
clock_t t1, t2; // CPU timers.
int mem_error; // Constant for errors while allocating memory. If mem_error -1 programm exhausted all available memory and terminates.
// Frontier's node structure.
struct frontier_node {
int *vector; // Node's vector.
struct frontier_node *previous; // Pointer to the previous frontier node.
struct frontier_node *next; // Pointer to the next frontier node.
};
struct frontier_node *head = NULL; // The one end of the frontier.
struct frontier_node *tail = NULL; // The other end of the frontier.
// Reading the input file.
int readfile(char *filename)
{
int i, j;
FILE *infile;
int err;
// Opening the input file.
infile = fopen(filename, "r");
if (infile == NULL) {
printf("Cannot open input file. Program terminates.\n");
return -1;
}
// Reading the number of propositions.
err = fscanf(infile, "%d", &N);
if (err < 1) {
printf("Cannot read the number of propositions. Program terminates.\n");
fclose(infile);
return -1;
}
if (N < 1) {
printf("Small number of propositions. Program terminates.\n");
fclose(infile);
return -1;
}
// Reading the number of clauses.
err = fscanf(infile, "%d", &K);
if (err<1) {
printf("Cannot read the number of clauses. Program terminates.\n");
fclose(infile);
return -1;
}
if (K < 1) {
printf("Low number of clauses. Program terminates.\n");
fclose(infile);
return -1;
}
// Reading the number of propositions per clause.
err = fscanf(infile, "%d", &M);
if (err < 1) {
printf("Cannot read the number of propositions per clause. Program terminates.\n");
fclose(infile);
return -1;
}
if (M < 2) {
printf("Low number of propositions per clause. Program terminates.\n");
fclose(infile);
return -1;
}
// Allocating memory for the clauses...
Problem = (int*)malloc(K * M * sizeof(int));
if (Problem == NULL) {
printf("Error: malloc for Problem failed.\n");
return -1;
}
// ...and read them
for (i = 0; i < K; i++) {
for (j = 0; j < M; j++) {
err = fscanf(infile, "%d", Problem + (i * M) + j);
if (err < 1) {
printf("Cannot read the #%d proposition of the #%d clause. Program terminates.\n", j + 1, i + 1);
fclose(infile);
return -1;
}
if (Problem[i*M + j] == 0 || Problem[(i * M) + j] > N || Problem[(i * M) + j] < -N) {
printf("Wrong value for the #%d proposition of the #%d clause. Program terminates.\n", j + 1, i + 1);
fclose(infile);
return -1;
}
}
}
fclose(infile);
return 0;
}
// Auxiliary function that displays all the clauses of the problem.
void display_problem()
{
printf("The current problem:\n");
printf("====================\n");
for (int i = 0; i < K; i++) {
for (int j = 0; j < M; j++) {
if (j > 0) {
printf(" or ");
}
if (Problem[(i * M) + j] > 0) {
printf("P%d", Problem[(i * M) + j]);
} else {
printf("not P%d", -Problem[(i * M) + j]);
}
}
printf("\n");
}
}
// Auxiliary function that displays the current assignment of truth values to the propositions.
void display(int *vector)
{
for (int i = 0; i < N; i++) {
if (vector[i] == 1) {
printf("P%d=true ", i + 1);
} else {
printf("P%d=false ", i + 1);
}
}
}
// Auxiliary function that copies the values of one vector to another.
void copy(int *vector1, int *vector2)
{
for (int i = 0; i < N; i++) {
vector2[i] = vector1[i];
}
}