The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

282 lines
8.9 KiB

4 weeks ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Computation of signatures.
  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. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. static double * ddCofMintermAux (DdManager *dd, DdNode *node, st_table *table);
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Computes the fraction of minterms in the on-set of all the
  62. positive cofactors of a %BDD or %ADD.
  63. @details The array has as many positions as there are %BDD variables
  64. in the manager plus one. The last position of the array contains the
  65. fraction of the minterms in the ON-set of the function represented
  66. by the %BDD or %ADD. The other positions of the array hold the
  67. variable signatures.
  68. @return the pointer to an array of doubles if successful; NULL
  69. otherwise.
  70. @sideeffect None
  71. */
  72. double *
  73. Cudd_CofMinterm(
  74. DdManager * dd,
  75. DdNode * node)
  76. {
  77. st_table *table;
  78. double *values;
  79. double *result = NULL;
  80. int i, firstLevel;
  81. int size;
  82. #ifdef DD_STATS
  83. unsigned long startTime;
  84. startTime = util_cpu_time();
  85. dd->num_calls = 0;
  86. #endif
  87. table = st_init_table(st_ptrcmp, st_ptrhash);
  88. if (table == NULL) {
  89. (void) fprintf(dd->err,
  90. "out-of-memory, couldn't measure DD cofactors.\n");
  91. dd->errorCode = CUDD_MEMORY_OUT;
  92. return(NULL);
  93. }
  94. size = dd->size;
  95. values = ddCofMintermAux(dd, node, table);
  96. if (values != NULL) {
  97. result = ALLOC(double,size + 1);
  98. if (result != NULL) {
  99. if (Cudd_IsConstantInt(node))
  100. firstLevel = 1;
  101. else
  102. firstLevel = cuddI(dd,Cudd_Regular(node)->index);
  103. for (i = 0; i < size; i++) {
  104. if (i >= cuddI(dd,Cudd_Regular(node)->index)) {
  105. result[dd->invperm[i]] = values[i - firstLevel];
  106. } else {
  107. result[dd->invperm[i]] = values[size - firstLevel];
  108. }
  109. }
  110. result[size] = values[size - firstLevel];
  111. } else {
  112. dd->errorCode = CUDD_MEMORY_OUT;
  113. }
  114. }
  115. if (Cudd_Regular(node)->ref == 1) FREE(values);
  116. st_foreach(table, cuddStCountfree, NULL);
  117. st_free_table(table);
  118. #ifdef DD_STATS
  119. (void) fprintf(dd->out,"Number of calls: %d\n", dd->num_calls);
  120. (void) fprintf(dd->out,"Time to compute measures: %s\n",
  121. util_print_time(util_cpu_time() - startTime));
  122. #endif
  123. if (result == NULL) {
  124. (void) fprintf(dd->out,
  125. "out-of-memory, couldn't measure DD cofactors.\n");
  126. dd->errorCode = CUDD_MEMORY_OUT;
  127. }
  128. return(result);
  129. } /* end of Cudd_CofMinterm */
  130. /*---------------------------------------------------------------------------*/
  131. /* Definition of internal functions */
  132. /*---------------------------------------------------------------------------*/
  133. /*---------------------------------------------------------------------------*/
  134. /* Definition of static functions */
  135. /*---------------------------------------------------------------------------*/
  136. /**
  137. @brief Recursive Step for Cudd_CofMinterm function.
  138. @details Traverses the %DD node and computes the fraction of minterms
  139. in the on-set of all positive cofactors simultaneously. It
  140. allocates an array with two more entries than there are variables
  141. below the one labeling the node. One extra entry (the first in the
  142. array) is for the variable labeling the node. The other entry (the
  143. last one in the array) holds the fraction of minterms of the
  144. function rooted at node. Each other entry holds the value for one
  145. cofactor. The array is put in a symbol table, to avoid repeated
  146. computation, and its address is returned by the procedure, for use
  147. by the caller.
  148. @return a pointer to the array of cofactor measures.
  149. @sideeffect None
  150. */
  151. static double *
  152. ddCofMintermAux(
  153. DdManager * dd,
  154. DdNode * node,
  155. st_table * table)
  156. {
  157. DdNode *N; /* regular version of node */
  158. DdNode *Nv, *Nnv;
  159. double *values;
  160. double *valuesT, *valuesE;
  161. int i;
  162. int localSize, localSizeT, localSizeE;
  163. double vT, vE;
  164. int size = dd->size;
  165. statLine(dd);
  166. #ifdef DD_STATS
  167. dd->num_calls++;
  168. #endif
  169. if (st_lookup(table, node, (void **) &values)) {
  170. return(values);
  171. }
  172. N = Cudd_Regular(node);
  173. if (cuddIsConstant(N)) {
  174. localSize = 1;
  175. } else {
  176. localSize = size - cuddI(dd,N->index) + 1;
  177. }
  178. values = ALLOC(double, localSize);
  179. if (values == NULL) {
  180. dd->errorCode = CUDD_MEMORY_OUT;
  181. return(NULL);
  182. }
  183. if (cuddIsConstant(N)) {
  184. if (node == DD_ZERO(dd) || node == Cudd_Not(DD_ONE(dd))) {
  185. values[0] = 0.0;
  186. } else {
  187. values[0] = 1.0;
  188. }
  189. } else {
  190. Nv = Cudd_NotCond(cuddT(N),N!=node);
  191. Nnv = Cudd_NotCond(cuddE(N),N!=node);
  192. valuesT = ddCofMintermAux(dd, Nv, table);
  193. if (valuesT == NULL) return(NULL);
  194. valuesE = ddCofMintermAux(dd, Nnv, table);
  195. if (valuesE == NULL) return(NULL);
  196. if (Cudd_IsConstantInt(Nv)) {
  197. localSizeT = 1;
  198. } else {
  199. localSizeT = size - cuddI(dd,Cudd_Regular(Nv)->index) + 1;
  200. }
  201. if (Cudd_IsConstantInt(Nnv)) {
  202. localSizeE = 1;
  203. } else {
  204. localSizeE = size - cuddI(dd,Cudd_Regular(Nnv)->index) + 1;
  205. }
  206. values[0] = valuesT[localSizeT - 1];
  207. for (i = 1; i < localSize; i++) {
  208. if (i >= cuddI(dd,Cudd_Regular(Nv)->index) - cuddI(dd,N->index)) {
  209. vT = valuesT[i - cuddI(dd,Cudd_Regular(Nv)->index) +
  210. cuddI(dd,N->index)];
  211. } else {
  212. vT = valuesT[localSizeT - 1];
  213. }
  214. if (i >= cuddI(dd,Cudd_Regular(Nnv)->index) - cuddI(dd,N->index)) {
  215. vE = valuesE[i - cuddI(dd,Cudd_Regular(Nnv)->index) +
  216. cuddI(dd,N->index)];
  217. } else {
  218. vE = valuesE[localSizeE - 1];
  219. }
  220. values[i] = (vT + vE) / 2.0;
  221. }
  222. if (Cudd_Regular(Nv)->ref == 1) FREE(valuesT);
  223. if (Cudd_Regular(Nnv)->ref == 1) FREE(valuesE);
  224. }
  225. if (N->ref > 1) {
  226. if (st_add_direct(table, node, values) == ST_OUT_OF_MEM) {
  227. FREE(values);
  228. return(NULL);
  229. }
  230. }
  231. return(values);
  232. } /* end of ddCofMintermAux */