-
Notifications
You must be signed in to change notification settings - Fork 1
/
BDD.java
579 lines (386 loc) · 12.1 KB
/
BDD.java
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
*
* @author Sudha Ravali Yellapantula
*/
import java.util.HashMap;
import java.util.ArrayList;
import java.io.*;
import java.util.Arrays;
public class BDD
{
ArrayList <Node> T;
HashMap <String,Integer> H;
int vars;
int x[];
int x_val[];
public static int count=0;
int restrict_var;
int restrict_var_val;
BDD restrict_robdd;
public static void main(String args[])
{
BDD obj=new BDD(5);
obj.build(0);
System.out.println("Output:\n\n");
System.out.println("Expression 1: equiv( (and (and (equiv(x[0], x[1]), equiv ( x[2], x[3])), not(x[4]))), (or(and(equiv(x[0],x[1]),equiv(x[2],x[4])),or(x[0],x[3])))");
System.out.println( "ROBDD Table T1 : \n");
System.out.println("u var Low High");
for (int i =0; i<obj.T.size(); i++){
System.out.print(i+" "+(((obj.T.get(i)).index)+" "+(obj.T.get(i)).low)+" "+((obj.T.get(i)).high));
System.out.println();
}
System.out.println();
System.out.println();
System.out.println();
BDD.count++;
BDD obj2=new BDD(5);
obj2.build(0);
System.out.println("Expression 2: and(equiv( (or (and (equiv(x[0], x[1]), equiv ( x[2], x[3])), not(x[4]))), (or(and(equiv(x[0],x[1]),equiv(x[2],x[4])),or(x[0],x[3])))),or((equiv(x[0],x[1])), x[2] ))");
System.out.println( "ROBDD Table T2 : \n");
System.out.println("u Index Low High");
for (int i =0; i<obj2.T.size(); i++){
System.out.print(i+" "+(((obj2.T.get(i)).index)+" "+(obj2.T.get(i)).low)+" "+((obj2.T.get(i)).high));
System.out.println();
}
//APPLY Code
//Instance of Apply Class
Apply res_robdd=new Apply(obj,obj2);
//Index of root nodes of two ROBDDs passed as parameters to the app() function.
int root1_u=obj.T.size()-1;
int root2_u=obj2.T.size()-1;
res_robdd.app(root1_u, root2_u);
BDD obj4=res_robdd.obj3;
System.out.println( "\n\n\nApplying AND operator on ROBDD-1 and ROBDD-2: ");
System.out.println("Expression 3: and(Expression-1, Expression-2)");
System.out.println("Resultant ROBDD Table-3:\n");
System.out.println("u Index Low High");
for (int i =0; i<obj4.T.size(); i++){
System.out.print(i+" "+(((obj4.T.get(i)).index)+" "+(obj4.T.get(i)).low)+" "+((obj4.T.get(i)).high));
System.out.println();
}
//Restrict Function Calls
int res_bdd_vars=obj2.vars;
BDD obj3=new BDD(res_bdd_vars);
obj3.restrict_robdd=obj2;
obj3.restrict_var=4;
obj3.restrict_var_val=0;
obj3.restrict(obj2.T.size()-1);
System.out.println("\n\n\nRestrict Function Result on Expression 2:\nRestrict variable= 4\nRestrict Variable boolean value=0");
System.out.println( "\nRestrict ROBDD Table T4 : ");
System.out.println("u Index Low High");
for (int i =0; i<obj3.T.size(); i++){
System.out.print(i+" "+(((obj3.T.get(i)).index)+" "+(obj3.T.get(i)).low)+" "+((obj3.T.get(i)).high));
System.out.println();
}
//SAT COUNT Function calls
System.out.println("\n\n\nSAT COUNT for Expression 2 ROBDD= "+obj2.count(obj2.T.size()-1));
//ANY SAT Function calls
int val=obj2.anysat(obj2.T.size()-1);
if(val==0) {
System.out.println("\n\n\nNO Satisfying Assignment Exists");
}
else
{
System.out.println("\n\n\nSatisfying Assignment for Expression 2");
for(int i=0;i<obj2.vars;i++)
{
System.out.println("x["+i+"]="+obj2.x_val[i]);
}
}
};
public BDD(int n)
{
vars = n;
x = new int[n];
x_val=new int[n];
for(int i=0;i<n;i++)
{
x[i] = -1;
x_val[i]=0;
}
this.T = new ArrayList<Node>();
this.H = new HashMap<String, Integer>();
//Initializing T
Node zero=new Node();
zero.addNodeVal(n,-1,-1);
Node one=new Node();
one.addNodeVal(n,-1,-1);
T.add(zero);
T.add(one);
}
//Expression..................................................................................................
boolean eval()
{
boolean exp;
if(count==0) {
exp =(equiv( (and (and (equiv(x[0], x[1]), equiv ( x[2], x[3])), not(x[4]))), (or(and(equiv(x[0],x[1]),equiv(x[2],x[4])),or(x[0],x[3])))) ==1) ?true:false;
//exp = ((and(imp(not(x[0]),equiv(1,x[1])),not(x[2]))) == 1)?true:false;
//exp = ((and(equiv(x[0], x[2]), not (x[3])))== 1)?true:false;
//exp = ((and (and (equiv(x[0], x[1]), equiv ( x[2], x[3])), not(x[4])))== 1)?true:false;
}
else {
//exp = ((and(equiv(x[0], x[2]), not (x[4])))== 1)?true:false;
//exp = ((or(equiv(x[0],x[1]),x[2]))== 1)?true:false;
//exp = ((and(equiv(x[0],x[1]),equiv(x[2],x[4]))) == 1)?true:false;
//exp = ((or(equiv(x[0],x[1]),x[1])) == 1)?true:false;
//exp = ((or((equiv(x[0],x[1])), x[2] ))== 1)?true:false;
//exp = (or(and(or(and(equiv(x[0],x[1]),equiv(x[2],x[4])),or(x[0],x[3])),and(equiv(x[0], x[2]), not (x[4]))),and(equiv(x[0], x[2]), not (x[3]))) == 1)?true:false;
exp =(and(equiv( (or (and (equiv(x[0], x[1]), equiv ( x[2], x[3])), not(x[4]))), (or(and(equiv(x[0],x[1]),equiv(x[2],x[4])),or(x[0],x[3])))),or((equiv(x[0],x[1])), x[2] )) ==1) ?true:false;
}
return exp;
}
//Expression End..................................................................................................
//Core Logic- Build and Make Functions............................................................................
public int build(int i)
{
if(i+1 > vars)
{
if(eval())
return 1;
else
return 0;
}
else
{
x[i] = 0;
int l = build(i + 1);
x[i] = 1;
int h = build(i + 1);
int val= mk(i,l,h);
return val;
}
}
public int mk(int i,int l,int h)
{
String key=Integer.toString(i)+Integer.toString(l)+Integer.toString(h)+Integer.toString((i*l*h)+(i+l+h));
if(l == h)
{
return l;
}
else if(H.containsKey(key))
{
return H.get(key);
}
else
{
Node n = new Node();
n.addNodeVal(i,l, h);
T.add(n);
int value=T.size()-1;
H.put(key, value);
return value;
}
}
// Core Logic Ends here............................................................................................
//Restrict Code Begins here........................................................................................
public int restrict(int u)
{
if(restrict_robdd.T.get(u).index > restrict_var) {
build_again(u);
return u;
}
else if(restrict_robdd.T.get(u).index < restrict_var) {
return mk(restrict_robdd.T.get(u).index,restrict(restrict_robdd.T.get(u).low),restrict(restrict_robdd.T.get(u).high));
}
else if (restrict_var_val==0){
return restrict(restrict_robdd.T.get(u).low);
}
else {
return restrict(restrict_robdd.T.get(u).high);
}
}
public void build_again(int u)
{
if(u==0 || u==1)
{
return;
}
else
{
int i= this.restrict_robdd.T.get(u).index;
int l = this.restrict_robdd.T.get(u).low;
int h = this.restrict_robdd.T.get(u).high;
build_again(l);
build_again(h);
mk(i,l,h);
}
}
//Restrict Ends here...............................................................................................
//SATCount.........................................................................................................
public int count(int u)
{
int sat_count=0;
if (u==0) {
return 0;
}
else if(u==1) {
return 1;
}
else {
double exp1=(this.T.get(this.T.get(u).low).index)-this.T.get(u).index-1;
double exp2=(this.T.get(this.T.get(u).high).index)-this.T.get(u).index-1;
sat_count=(int) (Math.pow(2, exp1)*count(this.T.get(u).low)+Math.pow(2, exp2)*count(this.T.get(u).high));
}
return sat_count;
}
//SAT Count End....................................................................................................
//AnySat.........................................................................................................
/*
public void anysat(int u){
if(u==0) {
return;
}
else if(u==1) {
return;
}
else if(this.T.get(u).low==0) {
this.x_val[this.T.get(u).index]=1;
anysat(this.T.get(u).high);
return;
}
else {
this.x_val[this.T.get(u).index]=0;
anysat(this.T.get(u).low);
return;
}
}
*/
public int anysat(int u){
if(u==0) {
return 0;
}
else if(u==1) {
return 1;
}
else {
int val=-1;
this.x_val[this.T.get(u).index]=1;
val=anysat(this.T.get(u).high);
if(val==1) {
return 1;
}
this.x_val[this.T.get(u).index]=0;
val=anysat(this.T.get(u).low);
if(val==1) {
return 1;
}
return 0;
}
}
//AnySat Ends here...............................................................................................
//Operators........................................................................................................
int and(int a, int b)
{
int ans = 0;
if((a == 0) || (b == 0))
ans = 0;
else
ans = 1;
return ans;
}
int or(int a, int b)
{
int ans = 0;
if((a == 0) && (b == 0))
ans = 0;
else
ans = 1;
return ans;
}
int not(int a)
{
int ans = 0;
if(a == 0)
ans = 1;
else
ans = 0;
return ans;
}
int imp(int a, int b)
{
int ans = 0;
if(a == 1 && b == 0)
ans = 0;
else
ans = 1;
return ans;
}
int equiv(int a, int b)
{
int ans = 0;
if(a == b)
ans = 1;
else
ans = 0;
return ans;
}
//End of Operators......................................................................................
}
class Node
{
int low;
int high;
int index;
Node()
{
this.low=-1;
this.high=-1;
}
public void addNodeVal(int index,int lowChild, int highChild)
{
this.index = index;
this.low = lowChild;
this.high = highChild;
}
}
class Apply{
BDD obj1;
BDD obj2;
int result;
BDD obj3;
Apply(BDD object1,BDD object2){
obj1=object1;
obj2=object2;
result=0;
int vars1=obj1.vars;
int vars2=obj2.vars;
//Initializing the new resultant BDD
if(vars2>vars1) {
obj3=new BDD(vars2);
}
else {
obj3=new BDD(vars1);
}
}
public int app(int u,int v) {
int u_var=obj1.T.get(u).index;
int v_var=obj2.T.get(v).index;
int u_low=obj1.T.get(u).low;
int v_low=obj2.T.get(v).low;
int u_high=obj1.T.get(u).high;
int v_high=obj2.T.get(v).high;
if((u==0||u==1)&&((v==0)||v==1)) {
result= and(u,v);
return result;
}
else if(u_var==v_var){
result=obj3.mk(u_var,app(u_low,v_low),app(u_high,v_high));
}
else if(u_var<v_var) {
result=obj3.mk(u_var,app(u_low,v),app(u_high,v));
}
else {
result=obj3.mk(u_var,app(u,v_low),app(u,v_high));
}
return result;
}
int and(int a, int b)
{
int ans = 0;
if((a == 0) || (b == 0))
ans = 0;
else
ans = 1;
return ans;
}
}