You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

470 lines
12 KiB

  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Correlation between BDDs.
  5. @author Fabio Somenzi
  6. @copyright@parblock
  7. Copyright (c) 1995-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include "util.h"
  35. #include "cuddInt.h"
  36. /*---------------------------------------------------------------------------*/
  37. /* Constant declarations */
  38. /*---------------------------------------------------------------------------*/
  39. /*---------------------------------------------------------------------------*/
  40. /* Stucture declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Type declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /** Type of hash-table key. */
  46. typedef struct hashEntry {
  47. DdNode *f;
  48. DdNode *g;
  49. } HashEntry;
  50. /*---------------------------------------------------------------------------*/
  51. /* Variable declarations */
  52. /*---------------------------------------------------------------------------*/
  53. #ifdef CORREL_STATS
  54. static int num_calls;
  55. #endif
  56. /*---------------------------------------------------------------------------*/
  57. /* Macro declarations */
  58. /*---------------------------------------------------------------------------*/
  59. /** \cond */
  60. /*---------------------------------------------------------------------------*/
  61. /* Static function prototypes */
  62. /*---------------------------------------------------------------------------*/
  63. static double bddCorrelationAux (DdManager *dd, DdNode *f, DdNode *g, st_table *table);
  64. static double bddCorrelationWeightsAux (DdManager *dd, DdNode *f, DdNode *g, double *prob, st_table *table);
  65. static int CorrelCompare (const void *key1, const void *key2);
  66. static int CorrelHash (void const *key, int modulus);
  67. static enum st_retval CorrelCleanUp (void *key, void *value, void *arg);
  68. /** \endcond */
  69. /*---------------------------------------------------------------------------*/
  70. /* Definition of exported functions */
  71. /*---------------------------------------------------------------------------*/
  72. /**
  73. @brief Computes the correlation of f and g.
  74. @details If f == g, their correlation is 1. If f == g', their
  75. correlation is 0.
  76. @return the fraction of minterms in the ON-set of the EXNOR of f and
  77. g. If it runs out of memory, returns (double)CUDD_OUT_OF_MEM.
  78. @sideeffect None
  79. @see Cudd_bddCorrelationWeights
  80. */
  81. double
  82. Cudd_bddCorrelation(
  83. DdManager * manager,
  84. DdNode * f,
  85. DdNode * g)
  86. {
  87. st_table *table;
  88. double correlation;
  89. #ifdef CORREL_STATS
  90. num_calls = 0;
  91. #endif
  92. table = st_init_table(CorrelCompare,CorrelHash);
  93. if (table == NULL) return((double)CUDD_OUT_OF_MEM);
  94. correlation = bddCorrelationAux(manager,f,g,table);
  95. st_foreach(table, CorrelCleanUp, NIL(void));
  96. st_free_table(table);
  97. return(correlation);
  98. } /* end of Cudd_bddCorrelation */
  99. /**
  100. @brief Computes the correlation of f and g for given input
  101. probabilities.
  102. @details On input, prob\[i\] is supposed to contain the probability
  103. of the i-th input variable to be 1. If f == g, their correlation is
  104. 1. If f == g', their correlation is 0. The correlation of f and the
  105. constant one gives the probability of f.
  106. @return the probability that f and g have the same value. If it runs
  107. out of memory, returns (double)CUDD_OUT_OF_MEM.
  108. @sideeffect None
  109. @see Cudd_bddCorrelation
  110. */
  111. double
  112. Cudd_bddCorrelationWeights(
  113. DdManager * manager,
  114. DdNode * f,
  115. DdNode * g,
  116. double * prob)
  117. {
  118. st_table *table;
  119. double correlation;
  120. #ifdef CORREL_STATS
  121. num_calls = 0;
  122. #endif
  123. table = st_init_table(CorrelCompare,CorrelHash);
  124. if (table == NULL) return((double)CUDD_OUT_OF_MEM);
  125. correlation = bddCorrelationWeightsAux(manager,f,g,prob,table);
  126. st_foreach(table, CorrelCleanUp, NIL(void));
  127. st_free_table(table);
  128. return(correlation);
  129. } /* end of Cudd_bddCorrelationWeights */
  130. /*---------------------------------------------------------------------------*/
  131. /* Definition of internal functions */
  132. /*---------------------------------------------------------------------------*/
  133. /*---------------------------------------------------------------------------*/
  134. /* Definition of static functions */
  135. /*---------------------------------------------------------------------------*/
  136. /**
  137. @brief Performs the recursive step of Cudd_bddCorrelation.
  138. @return the fraction of minterms in the ON-set of the EXNOR of f and
  139. g.
  140. @sideeffect None
  141. @see bddCorrelationWeightsAux
  142. */
  143. static double
  144. bddCorrelationAux(
  145. DdManager * dd,
  146. DdNode * f,
  147. DdNode * g,
  148. st_table * table)
  149. {
  150. DdNode *Fv, *Fnv, *G, *Gv, *Gnv;
  151. double min, *pmin, min1, min2;
  152. void *dummy;
  153. HashEntry *entry;
  154. int topF, topG;
  155. statLine(dd);
  156. #ifdef CORREL_STATS
  157. num_calls++;
  158. #endif
  159. /* Terminal cases: only work for BDDs. */
  160. if (f == g) return(1.0);
  161. if (f == Cudd_Not(g)) return(0.0);
  162. /* Standardize call using the following properties:
  163. ** (f EXNOR g) = (g EXNOR f)
  164. ** (f' EXNOR g') = (f EXNOR g).
  165. */
  166. if (f > g) {
  167. DdNode *tmp = f;
  168. f = g; g = tmp;
  169. }
  170. if (Cudd_IsComplement(f)) {
  171. f = Cudd_Not(f);
  172. g = Cudd_Not(g);
  173. }
  174. /* From now on, f is regular. */
  175. entry = ALLOC(HashEntry,1);
  176. if (entry == NULL) {
  177. dd->errorCode = CUDD_MEMORY_OUT;
  178. return(CUDD_OUT_OF_MEM);
  179. }
  180. entry->f = f; entry->g = g;
  181. /* We do not use the fact that
  182. ** correlation(f,g') = 1 - correlation(f,g)
  183. ** to minimize the risk of cancellation.
  184. */
  185. if (st_lookup(table, entry, &dummy)) {
  186. min = *(double *) dummy;
  187. FREE(entry);
  188. return(min);
  189. }
  190. G = Cudd_Regular(g);
  191. topF = cuddI(dd,f->index); topG = cuddI(dd,G->index);
  192. if (topF <= topG) { Fv = cuddT(f); Fnv = cuddE(f); } else { Fv = Fnv = f; }
  193. if (topG <= topF) { Gv = cuddT(G); Gnv = cuddE(G); } else { Gv = Gnv = G; }
  194. if (g != G) {
  195. Gv = Cudd_Not(Gv);
  196. Gnv = Cudd_Not(Gnv);
  197. }
  198. min1 = bddCorrelationAux(dd, Fv, Gv, table) / 2.0;
  199. if (min1 == (double)CUDD_OUT_OF_MEM) {
  200. FREE(entry);
  201. return(CUDD_OUT_OF_MEM);
  202. }
  203. min2 = bddCorrelationAux(dd, Fnv, Gnv, table) / 2.0;
  204. if (min2 == (double)CUDD_OUT_OF_MEM) {
  205. FREE(entry);
  206. return(CUDD_OUT_OF_MEM);
  207. }
  208. min = (min1+min2);
  209. pmin = ALLOC(double,1);
  210. if (pmin == NULL) {
  211. dd->errorCode = CUDD_MEMORY_OUT;
  212. return((double)CUDD_OUT_OF_MEM);
  213. }
  214. *pmin = min;
  215. if (st_insert(table, entry, pmin) == ST_OUT_OF_MEM) {
  216. FREE(entry);
  217. FREE(pmin);
  218. return((double)CUDD_OUT_OF_MEM);
  219. }
  220. return(min);
  221. } /* end of bddCorrelationAux */
  222. /**
  223. @brief Performs the recursive step of Cudd_bddCorrelationWeigths.
  224. @sideeffect None
  225. @see bddCorrelationAux
  226. */
  227. static double
  228. bddCorrelationWeightsAux(
  229. DdManager * dd,
  230. DdNode * f,
  231. DdNode * g,
  232. double * prob,
  233. st_table * table)
  234. {
  235. DdNode *Fv, *Fnv, *G, *Gv, *Gnv;
  236. double min, *pmin, min1, min2;
  237. void *dummy;
  238. HashEntry *entry;
  239. int topF, topG;
  240. unsigned index;
  241. statLine(dd);
  242. #ifdef CORREL_STATS
  243. num_calls++;
  244. #endif
  245. /* Terminal cases: only work for BDDs. */
  246. if (f == g) return(1.0);
  247. if (f == Cudd_Not(g)) return(0.0);
  248. /* Standardize call using the following properties:
  249. ** (f EXNOR g) = (g EXNOR f)
  250. ** (f' EXNOR g') = (f EXNOR g).
  251. */
  252. if (f > g) {
  253. DdNode *tmp = f;
  254. f = g; g = tmp;
  255. }
  256. if (Cudd_IsComplement(f)) {
  257. f = Cudd_Not(f);
  258. g = Cudd_Not(g);
  259. }
  260. /* From now on, f is regular. */
  261. entry = ALLOC(HashEntry,1);
  262. if (entry == NULL) {
  263. dd->errorCode = CUDD_MEMORY_OUT;
  264. return((double)CUDD_OUT_OF_MEM);
  265. }
  266. entry->f = f; entry->g = g;
  267. /* We do not use the fact that
  268. ** correlation(f,g') = 1 - correlation(f,g)
  269. ** to minimize the risk of cancellation.
  270. */
  271. if (st_lookup(table, entry, &dummy)) {
  272. min = *(double *) dummy;
  273. FREE(entry);
  274. return(min);
  275. }
  276. G = Cudd_Regular(g);
  277. topF = cuddI(dd,f->index); topG = cuddI(dd,G->index);
  278. if (topF <= topG) {
  279. Fv = cuddT(f); Fnv = cuddE(f);
  280. index = f->index;
  281. } else {
  282. Fv = Fnv = f;
  283. index = G->index;
  284. }
  285. if (topG <= topF) { Gv = cuddT(G); Gnv = cuddE(G); } else { Gv = Gnv = G; }
  286. if (g != G) {
  287. Gv = Cudd_Not(Gv);
  288. Gnv = Cudd_Not(Gnv);
  289. }
  290. min1 = bddCorrelationWeightsAux(dd, Fv, Gv, prob, table) * prob[index];
  291. if (min1 == (double)CUDD_OUT_OF_MEM) {
  292. FREE(entry);
  293. return((double)CUDD_OUT_OF_MEM);
  294. }
  295. min2 = bddCorrelationWeightsAux(dd, Fnv, Gnv, prob, table) * (1.0 - prob[index]);
  296. if (min2 == (double)CUDD_OUT_OF_MEM) {
  297. FREE(entry);
  298. return((double)CUDD_OUT_OF_MEM);
  299. }
  300. min = (min1+min2);
  301. pmin = ALLOC(double,1);
  302. if (pmin == NULL) {
  303. dd->errorCode = CUDD_MEMORY_OUT;
  304. return((double)CUDD_OUT_OF_MEM);
  305. }
  306. *pmin = min;
  307. if (st_insert(table, entry, pmin) == ST_OUT_OF_MEM) {
  308. FREE(entry);
  309. FREE(pmin);
  310. return((double)CUDD_OUT_OF_MEM);
  311. }
  312. return(min);
  313. } /* end of bddCorrelationWeightsAux */
  314. /**
  315. @brief Compares two hash table entries.
  316. @return 0 if they are identical; 1 otherwise.
  317. @sideeffect None
  318. */
  319. static int
  320. CorrelCompare(
  321. void const * key1,
  322. void const * key2)
  323. {
  324. HashEntry const *entry1 = (HashEntry const *) key1;
  325. HashEntry const *entry2 = (HashEntry const *) key2;
  326. if (entry1->f != entry2->f || entry1->g != entry2->g) return(1);
  327. return(0);
  328. } /* end of CorrelCompare */
  329. /**
  330. @brief Hashes a hash table entry.
  331. @details It is patterned after st_strhash.
  332. @return a value between 0 and modulus.
  333. @sideeffect None
  334. */
  335. static int
  336. CorrelHash(
  337. void const * key,
  338. int modulus)
  339. {
  340. HashEntry const *entry = (HashEntry const *) key;
  341. int val = 0;
  342. val = (int) (((ptrint)entry->f)*997 + ((ptrint)entry->g));
  343. return ((val < 0) ? -val : val) % modulus;
  344. } /* end of CorrelHash */
  345. /**
  346. @brief Frees memory associated with hash table.
  347. @return ST_CONTINUE.
  348. @sideeffect None
  349. */
  350. static enum st_retval
  351. CorrelCleanUp(
  352. void * key,
  353. void * value,
  354. void * arg)
  355. {
  356. double *d = (double *) value;
  357. HashEntry *entry = (HashEntry *) key;
  358. (void) arg; /* avoid warning */
  359. FREE(entry);
  360. FREE(d);
  361. return ST_CONTINUE;
  362. } /* end of CorrelCleanUp */