Skip to content

Commit e0317d7

Browse files
author
mkouril
committed
Added DNF input format
1 parent e48d374 commit e0317d7

File tree

4 files changed

+200
-29
lines changed

4 files changed

+200
-29
lines changed

include/formats.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@
4242

4343
void write_output(char formatout, Tracer * &tracer);
4444
int read_input(Tracer * &tracer);
45+
void DNF_to_BDD ();
4546

4647
#endif

src/formats/bdd2cnf.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ Recd *resolve (int *truth_table, int sign, int no_vars, int no_out_vars) {
707707
}
708708

709709
void printBDDToCNFQM () {
710-
int *tempint = NULL, z;
710+
int *tempint=NULL, z;
711711
long tempint_max=0;
712712

713713
func_object **funcs;

src/formats/cnf.cc

Lines changed: 194 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
int zecc_limit;
4848
int *zecc_arr;
4949

50+
void cnf_process(store *integers, int num_minmax, minmax * min_max_store);
51+
5052
//If you want character support for strings and things, look in markbdd.c and copy that...
5153
//returns m - ?, # - for xcnf, i - for integer, e - end/error
5254
char getNextSymbol_CNF (char macros[20], int *intnum) {
@@ -193,8 +195,7 @@ void CNF_to_BDD(int cnf)
193195
long tempint_max = 0;
194196
char macros[20], order;
195197
int intnum;
196-
long out, count, num;
197-
long y, z, i, j;
198+
int y,i;
198199

199200

200201
//FILE *fin;
@@ -208,7 +209,10 @@ void CNF_to_BDD(int cnf)
208209

209210
//Get number of inputs and number of outputs from
210211
//the header of the CNF file
211-
fscanf(finputfile, "%ld %ld\n", &numinp, &numout);
212+
if (fscanf(finputfile, "%ld %ld\n", &numinp, &numout) != 2) {
213+
fprintf(stderr, "Error while parsing CNF input: bad header\n");
214+
exit(1);
215+
};
212216
numinp+=2;
213217
store *integers = new store[numout+2];
214218
store *old_integers = new store[numout+2];
@@ -308,7 +312,24 @@ void CNF_to_BDD(int cnf)
308312
}
309313
numout = y-1;
310314
}
315+
316+
cnf_process(integers, num_minmax, min_max_store);
317+
for(long x = 1; x < old_numout + 1; x++) {
318+
delete [] old_integers[x].num;
319+
}
320+
delete [] integers;
321+
delete [] old_integers;
322+
ite_free((void**)&tempint); tempint_max = 0;
311323

324+
d3_printf2("Number of BDDs - %ld\n", numout);
325+
d2_printf1("\rReading CNF ... Done \n");
326+
}
327+
328+
void
329+
cnf_process(store *integers, int num_minmax, minmax * min_max_store)
330+
{
331+
long out, count, num;
332+
long y, z, i, j;
312333
d3_printf2("numinp: %ld\n", numinp);
313334
if(DO_CLUSTER) {
314335
int *twopos_temp = (int *)calloc(numinp+1, sizeof(int));
@@ -953,26 +974,186 @@ void CNF_to_BDD(int cnf)
953974
if (functions[x] == true_ptr)
954975
count--;
955976
}
956-
957-
for(long x = 1; x < old_numout + 1; x++) {
958-
delete [] old_integers[x].num;
959-
}
960-
delete [] integers;
961-
delete [] old_integers;
962-
ite_free((void**)&tempint); tempint_max = 0;
963-
977+
964978
numout = count+1;
965979
nmbrFunctions = numout;
980+
}
981+
982+
983+
void DNF_to_CNF () {
984+
typedef struct {
985+
int num[50];
986+
} node1;
987+
node1 *integers;
988+
int lines = 0, length;
989+
int y = 0;
990+
if (fscanf(finputfile, "%ld %ld\n", &numinp, &numout) != 2) {
991+
fprintf(stderr, "Error while parsing DNF input: bad header\n");
992+
exit(1);
993+
};
994+
length = numout;
995+
integers = new node1[numout+1];
996+
for(int x = 0; x < length; x++) {
997+
y = 0;
998+
999+
do {
1000+
if (fscanf(finputfile, "%d", &integers[x].num[y]) != 1) {
1001+
fprintf(stderr, "Error while parsing DNF input\n");
1002+
exit(1);
1003+
};
1004+
y++;
1005+
lines++;
1006+
}
1007+
while(integers[x].num[y - 1] != 0);
1008+
}
1009+
char string1[1024];
1010+
lines = lines + 1;
1011+
sprintf(string1, "p cnf %ld %d\n", numinp + length, lines);
1012+
fprintf(foutputfile, "%s", string1);
1013+
for(int y = 0; y < length; y++) {
1014+
sprintf(string1, "%ld ", y + numinp + 1);
1015+
fprintf(foutputfile, "%s", string1);
1016+
}
1017+
sprintf(string1, "0\n");
1018+
fprintf(foutputfile, "%s", string1);
1019+
for(int x = 0; x < numout; x++) {
1020+
sprintf(string1, "%ld ", x + numinp + 1);
1021+
fprintf(foutputfile, "%s", string1);
1022+
for(int y = 0; integers[x].num[y] != 0; y++) {
1023+
sprintf(string1, "%d ", -integers[x].num[y]);
1024+
fprintf(foutputfile, "%s", string1);
1025+
}
1026+
sprintf(string1, "0\n");
1027+
fprintf(foutputfile, "%s", string1);
1028+
for(int y = 0; integers[x].num[y] != 0; y++) {
1029+
sprintf(string1, "%ld ", -(x + numinp + 1));
1030+
fprintf(foutputfile, "%s", string1);
1031+
sprintf(string1, "%d ", integers[x].num[y]);
1032+
fprintf(foutputfile, "%s", string1);
1033+
sprintf(string1, "0\n");
1034+
fprintf(foutputfile, "%s", string1);
1035+
}
1036+
}
1037+
sprintf(string1, "c\n");
1038+
fprintf(foutputfile, "%s", string1);
1039+
}
1040+
1041+
1042+
void
1043+
DNF_to_BDD ()
1044+
{
1045+
store *dnf_integers = NULL;
1046+
long dnf_numinp=0, dnf_numout=0;
1047+
store *cnf_integers = NULL;
1048+
long cnf_numinp=0, cnf_numout=0;
1049+
int y = 0;
1050+
if (fscanf(finputfile, "%ld %ld\n", &dnf_numinp, &dnf_numout) != 2) {
1051+
fprintf(stderr, "Error while parsing DNF input: bad header\n");
1052+
exit(1);
1053+
}
1054+
dnf_integers = (store*)ite_calloc(dnf_numout+1, sizeof(store), 9, "integers");
1055+
for(int x = 0; x < dnf_numout; x++) {
1056+
y = 0;
1057+
do {
1058+
if (dnf_integers[x].num_alloc <= y) {
1059+
dnf_integers[x].num = (int*)ite_recalloc((void*)dnf_integers[x].num, dnf_integers[x].num_alloc,
1060+
dnf_integers[x].num_alloc+100, sizeof(int), 9, "integers[].num");
1061+
dnf_integers[x].num_alloc += 100;
1062+
}
1063+
int intnum;
1064+
if (fscanf(finputfile, "%d", &intnum) != 1) {
1065+
fprintf(stderr, "Error while parsing DNF input\n");
1066+
exit(1);
1067+
}
1068+
if(abs(intnum) > dnf_numinp) { //Could change this to be number of vars instead of max vars
1069+
fprintf(stderr, "Variable in input file is greater than allowed:%ld...exiting\n", (long)dnf_numinp);
1070+
exit(1);
1071+
}
1072+
#ifdef CNF_USES_SYMTABLE
1073+
intnum = intnum==0?0:i_getsym_int(intnum, SYM_VAR);
1074+
#endif
1075+
dnf_integers[x].num[y] = intnum;
1076+
y++;
1077+
cnf_numout++;
1078+
} while(dnf_integers[x].num[y - 1] != 0);
1079+
dnf_integers[x].max = y-1;
1080+
}
1081+
int extra=0;
1082+
if (fscanf(finputfile, "%d", &extra) == 1) {
1083+
fprintf(stderr, "Error while parsing DNF input: extra data\n");
1084+
exit(1);
1085+
}
1086+
1087+
// DNF -> CNF
1088+
cnf_numout = cnf_numout + 1; // add the big clause
1089+
cnf_numinp = dnf_numinp + dnf_numout; // for every clause add a variable
1090+
cnf_integers = (store*)ite_calloc(cnf_numout+1, sizeof(store), 9, "integers"); // the big clause
1091+
1092+
int cnf_out_idx = 1; /* one based index? */
1093+
cnf_integers[cnf_out_idx].num_alloc = dnf_numout+1;
1094+
cnf_integers[cnf_out_idx].num = (int*)ite_calloc(cnf_integers[cnf_out_idx].num_alloc, sizeof(int), 9, "integers[].num");
1095+
for(int y = 0; y < dnf_numout; y++) {
1096+
cnf_integers[cnf_out_idx].num[y] = y + dnf_numinp + 1;
1097+
#ifdef CNF_USES_SYMTABLE
1098+
i_getsym_int(y+dnf_numinp+1, SYM_VAR);
1099+
#endif
1100+
}
1101+
cnf_integers[cnf_out_idx].num[dnf_numout] = 0;
1102+
cnf_out_idx++;
1103+
1104+
for(int x = 0; x < dnf_numout; x++) {
1105+
cnf_integers[cnf_out_idx].num_alloc = dnf_integers[x].max+2; /* plus clause var and 0 */
1106+
cnf_integers[cnf_out_idx].num = (int*)ite_calloc(cnf_integers[cnf_out_idx].num_alloc,
1107+
sizeof(int), 9, "integers[].num");
1108+
cnf_integers[cnf_out_idx].num[0] = x + dnf_numinp + 1;
1109+
1110+
int y;
1111+
for(y = 0; dnf_integers[x].num[y] != 0; y++) {
1112+
cnf_integers[cnf_out_idx].num[y+1] = -dnf_integers[x].num[y];
1113+
}
1114+
cnf_integers[cnf_out_idx].num[y+1] = 0;
1115+
assert(y == dnf_integers[x].max);
1116+
cnf_out_idx++;
1117+
1118+
for(int y = 0; dnf_integers[x].num[y] != 0; y++) {
1119+
cnf_integers[cnf_out_idx].num_alloc = 3;
1120+
cnf_integers[cnf_out_idx].num = (int*)ite_calloc(cnf_integers[cnf_out_idx].num_alloc,
1121+
sizeof(int), 9, "integers[].num");
1122+
cnf_integers[cnf_out_idx].num[0] = -(x + dnf_numinp + 1);
1123+
cnf_integers[cnf_out_idx].num[1] = dnf_integers[x].num[y];
1124+
cnf_integers[cnf_out_idx].num[2] = 0;
1125+
cnf_out_idx++;
1126+
}
1127+
}
1128+
assert(cnf_numout == cnf_out_idx-1);
1129+
1130+
long x;
1131+
for(x = 1; x <= cnf_numout; x++) {
1132+
cnf_integers[x].length = cnf_integers[x].num_alloc-1;;
1133+
cnf_integers[x].dag = -1;
1134+
}
1135+
numinp = cnf_numinp;
1136+
numout = cnf_numout;
1137+
cnf_process(cnf_integers, 0, NULL);
1138+
for(x = 1; x < cnf_numout + 1; x++) {
1139+
ite_free((void**)&cnf_integers[x].num);
1140+
}
1141+
for(x = 0; x < dnf_numout + 1; x++) {
1142+
ite_free((void**)&dnf_integers[x].num);
1143+
}
1144+
ite_free((void**)&cnf_integers);
1145+
ite_free((void**)&dnf_integers);
1146+
9661147
d3_printf2("Number of BDDs - %ld\n", numout);
967-
d2_printf1("\rReading CNF ... Done \n");
1148+
d2_printf1("\rReading DNF ... Done \n");
9681149
}
1150+
9691151
/*
9701152
void DNF_to_CNF () {
9711153
typedef struct {
9721154
int num[50];
9731155
} node1;
9741156
node1 *integers;
975-
>>>> char string;
9761157
int lines = 0, length;
9771158
int y = 0;
9781159
fscanf(finputfile, "%ld %ld\n", &numinp, &numout);
@@ -983,7 +1164,6 @@ void DNF_to_CNF () {
9831164
9841165
do {
9851166
fscanf(finputfile, "%d", &integers[x].num[y]);
986-
>>>> string = fgetc(finputfile);
9871167
y++;
9881168
lines++;
9891169
}

src/formats/read_input.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,14 @@ read_input(Tracer * &tracer)
116116

117117
case 'u': Smurfs_to_BDD (); break;
118118

119-
case 'c': if (formatout == 'l') {
120-
//Do_Lemmas ();
121-
return 1;
122-
} else CNF_to_BDD (1);
119+
case 'c': CNF_to_BDD (1);
123120
break;
124-
/*
125-
case 'd': if (formatout == 'd') {
126-
if (KEDAR) fprintf (stdout, "S*****\n");
127-
DNF_to_CNF ();
128-
if (KEDAR) fprintf (stdout, "F*****\n");
129-
return 1;
130-
} else CNF_to_BDD (0);
121+
122+
case 'd': DNF_to_BDD ();
131123
break;
132-
*/
124+
133125
case 's': if (formatout == 'c') {
134-
if (KEDAR) fprintf (stdout, "S*****\n");
135126
SAT_to_CNF ();
136-
if (KEDAR) fprintf (stdout, "F*****\n");
137127
return 1;
138128
} else {
139129

0 commit comments

Comments
 (0)