1 | /* BDD satisfying valuation routines */ |
---|
2 | |
---|
3 | |
---|
4 | #include "bddint.h" |
---|
5 | |
---|
6 | #ifdef STDC_HEADERS |
---|
7 | # include <stdlib.h> |
---|
8 | #else |
---|
9 | extern void qsort(pointer, unsigned long, unsigned long, int (*)(const void *, const void *)); |
---|
10 | #endif |
---|
11 | |
---|
12 | static |
---|
13 | bdd |
---|
14 | cmu_bdd_satisfy_step(cmu_bdd_manager bddm, bdd f) |
---|
15 | { |
---|
16 | bdd temp; |
---|
17 | bdd result; |
---|
18 | |
---|
19 | BDD_SETUP(f); |
---|
20 | if (BDD_IS_CONST(f)) |
---|
21 | return (f); |
---|
22 | if (BDD_THEN(f) == BDD_ZERO(bddm)) |
---|
23 | { |
---|
24 | temp=cmu_bdd_satisfy_step(bddm, BDD_ELSE(f)); |
---|
25 | result=bdd_find(bddm, BDD_INDEXINDEX(f), BDD_ZERO(bddm), temp); |
---|
26 | } |
---|
27 | else |
---|
28 | { |
---|
29 | temp=cmu_bdd_satisfy_step(bddm, BDD_THEN(f)); |
---|
30 | result=bdd_find(bddm, BDD_INDEXINDEX(f), temp, BDD_ZERO(bddm)); |
---|
31 | } |
---|
32 | return (result); |
---|
33 | } |
---|
34 | |
---|
35 | |
---|
36 | /* cmu_bdd_satisfy(bddm, f) returns a BDD which implies f, is true for */ |
---|
37 | /* some valuation on which f is true, and which has at most one node */ |
---|
38 | /* at each level. */ |
---|
39 | |
---|
40 | bdd |
---|
41 | cmu_bdd_satisfy(cmu_bdd_manager bddm, bdd f) |
---|
42 | { |
---|
43 | if (bdd_check_arguments(1, f)) |
---|
44 | { |
---|
45 | if (f == BDD_ZERO(bddm)) |
---|
46 | { |
---|
47 | cmu_bdd_warning("cmu_bdd_satisfy: argument is false"); |
---|
48 | return (f); |
---|
49 | } |
---|
50 | FIREWALL(bddm); |
---|
51 | RETURN_BDD(cmu_bdd_satisfy_step(bddm, f)); |
---|
52 | } |
---|
53 | return ((bdd)0); |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | static |
---|
58 | bdd |
---|
59 | cmu_bdd_satisfy_support_step(cmu_bdd_manager bddm, bdd f, bdd_indexindex_type *support) |
---|
60 | { |
---|
61 | bdd temp; |
---|
62 | bdd result; |
---|
63 | |
---|
64 | BDD_SETUP(f); |
---|
65 | if (!*support) |
---|
66 | return (cmu_bdd_satisfy_step(bddm, f)); |
---|
67 | if (BDD_INDEX(bddm, f) <= bddm->indexes[*support]) |
---|
68 | { |
---|
69 | if (BDD_INDEXINDEX(f) == *support) |
---|
70 | ++support; |
---|
71 | if (BDD_THEN(f) == BDD_ZERO(bddm)) |
---|
72 | { |
---|
73 | temp=cmu_bdd_satisfy_support_step(bddm, BDD_ELSE(f), support); |
---|
74 | result=bdd_find(bddm, BDD_INDEXINDEX(f), BDD_ZERO(bddm), temp); |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | temp=cmu_bdd_satisfy_support_step(bddm, BDD_THEN(f), support); |
---|
79 | result=bdd_find(bddm, BDD_INDEXINDEX(f), temp, BDD_ZERO(bddm)); |
---|
80 | } |
---|
81 | } |
---|
82 | else |
---|
83 | { |
---|
84 | temp=cmu_bdd_satisfy_support_step(bddm, f, support+1); |
---|
85 | result=bdd_find(bddm, *support, BDD_ZERO(bddm), temp); |
---|
86 | } |
---|
87 | return (result); |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | static |
---|
92 | int |
---|
93 | index_cmp(pointer p1, pointer p2) |
---|
94 | { |
---|
95 | bdd_index_type i1, i2; |
---|
96 | |
---|
97 | i1= *(bdd_indexindex_type *)p1; |
---|
98 | i2= *(bdd_indexindex_type *)p2; |
---|
99 | if (i1 < i2) |
---|
100 | return (-1); |
---|
101 | if (i1 > i2) |
---|
102 | return (1); |
---|
103 | return (0); |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | /* cmu_bdd_satisfy_support(bddm, f) returns a BDD which implies f, is true */ |
---|
108 | /* for some valuation on which f is true, which has at most one node */ |
---|
109 | /* at each level, and which has exactly one node corresponding to each */ |
---|
110 | /* variable which is associated with something in the current variable */ |
---|
111 | /* association. */ |
---|
112 | |
---|
113 | bdd |
---|
114 | cmu_bdd_satisfy_support(cmu_bdd_manager bddm, bdd f) |
---|
115 | { |
---|
116 | bdd_indexindex_type *support, *p; |
---|
117 | long i; |
---|
118 | bdd result; |
---|
119 | |
---|
120 | if (bdd_check_arguments(1, f)) |
---|
121 | { |
---|
122 | if (f == BDD_ZERO(bddm)) |
---|
123 | { |
---|
124 | cmu_bdd_warning("cmu_bdd_satisfy_support: argument is false"); |
---|
125 | return (f); |
---|
126 | } |
---|
127 | support=(bdd_indexindex_type *)mem_get_block((bddm->vars+1)*sizeof(bdd)); |
---|
128 | FIREWALL1(bddm, |
---|
129 | if (retcode == BDD_ABORTED || retcode == BDD_OVERFLOWED) |
---|
130 | { |
---|
131 | mem_free_block((pointer)support); |
---|
132 | return ((bdd)0); |
---|
133 | } |
---|
134 | ); |
---|
135 | for (i=0, p=support; i < bddm->vars; ++i) |
---|
136 | if (bddm->curr_assoc->assoc[i+1]) |
---|
137 | { |
---|
138 | *p=bddm->indexes[i+1]; |
---|
139 | ++p; |
---|
140 | } |
---|
141 | *p=0; |
---|
142 | qsort((pointer)support, (unsigned)(p-support), |
---|
143 | sizeof(bdd_indexindex_type), |
---|
144 | (int (*)(const void *, const void *))index_cmp); |
---|
145 | while (p != support) |
---|
146 | { |
---|
147 | --p; |
---|
148 | *p=bddm->indexindexes[*p]; |
---|
149 | } |
---|
150 | result=cmu_bdd_satisfy_support_step(bddm, f, support); |
---|
151 | mem_free_block((pointer)support); |
---|
152 | RETURN_BDD(result); |
---|
153 | } |
---|
154 | return ((bdd)0); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | double |
---|
159 | cmu_bdd_satisfying_fraction_step(cmu_bdd_manager bddm, bdd f) |
---|
160 | { |
---|
161 | union { |
---|
162 | long cache_result[2]; |
---|
163 | double result; |
---|
164 | } u; |
---|
165 | |
---|
166 | BDD_SETUP(f); |
---|
167 | if (BDD_IS_CONST(f)) |
---|
168 | { |
---|
169 | if (f == BDD_ZERO(bddm)) |
---|
170 | return (0.0); |
---|
171 | return (1.0); |
---|
172 | } |
---|
173 | if (bdd_lookup_in_cache1d(bddm, OP_SATFRAC, f, |
---|
174 | &(u.cache_result[0]), &(u.cache_result[1]))) |
---|
175 | { |
---|
176 | return (u.result); |
---|
177 | } |
---|
178 | u.result=0.5*cmu_bdd_satisfying_fraction_step(bddm, BDD_THEN(f))+ |
---|
179 | 0.5*cmu_bdd_satisfying_fraction_step(bddm, BDD_ELSE(f)); |
---|
180 | bdd_insert_in_cache1d(bddm, OP_SATFRAC, f, u.cache_result[0], |
---|
181 | u.cache_result[1]); |
---|
182 | return (u.result); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | /* cmu_bdd_satisfying_fraction(bddm, f) returns the fraction of valuations */ |
---|
187 | /* which make f true. (Note that this fraction is independent of */ |
---|
188 | /* whatever set of variables f is supposed to be a function of.) */ |
---|
189 | |
---|
190 | double |
---|
191 | cmu_bdd_satisfying_fraction(cmu_bdd_manager bddm, bdd f) |
---|
192 | { |
---|
193 | if (bdd_check_arguments(1, f)) |
---|
194 | return (cmu_bdd_satisfying_fraction_step(bddm, f)); |
---|
195 | return (0.0); |
---|
196 | } |
---|