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.

515 lines
14 KiB

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