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.

346 lines
9.5 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup cudd
  4. @brief Boolean equation solver and related functions.
  5. @author Balakrishna Kumthekar
  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. /* Type declarations */
  41. /*---------------------------------------------------------------------------*/
  42. /*---------------------------------------------------------------------------*/
  43. /* Structure declarations */
  44. /*---------------------------------------------------------------------------*/
  45. /*---------------------------------------------------------------------------*/
  46. /* Variable declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Macro declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /** \cond */
  52. /*---------------------------------------------------------------------------*/
  53. /* Static function prototypes */
  54. /*---------------------------------------------------------------------------*/
  55. /** \endcond */
  56. /*---------------------------------------------------------------------------*/
  57. /* Definition of exported functions */
  58. /*---------------------------------------------------------------------------*/
  59. /**
  60. @brief Implements the solution of F(x,y) = 0.
  61. @details The return value is the consistency condition. The y
  62. variables are the unknowns and the remaining variables are the
  63. parameters. Cudd_SolveEqn allocates an array and fills it with the
  64. indices of the unknowns. This array is used by Cudd_VerifySol.
  65. @return the consistency condition if successful; NULL otherwise.
  66. @sideeffect The solution is returned in G; the indices of the y
  67. variables are returned in yIndex.
  68. @see Cudd_VerifySol
  69. */
  70. DdNode *
  71. Cudd_SolveEqn(
  72. DdManager * bdd /**< CUDD manager */,
  73. DdNode * F /**< the left-hand side of the equation */,
  74. DdNode * Y /**< the cube of the y variables */,
  75. DdNode ** G /**< the array of solutions (return parameter) */,
  76. int ** yIndex /**< index of y variables */,
  77. int n /**< numbers of unknowns */)
  78. {
  79. DdNode *res;
  80. int *temp;
  81. *yIndex = temp = ALLOC(int, n);
  82. if (temp == NULL) {
  83. bdd->errorCode = CUDD_MEMORY_OUT;
  84. (void) fprintf(bdd->out,
  85. "Cudd_SolveEqn: Out of memory for yIndex\n");
  86. return(NULL);
  87. }
  88. do {
  89. bdd->reordered = 0;
  90. res = cuddSolveEqnRecur(bdd, F, Y, G, n, temp, 0);
  91. } while (bdd->reordered == 1);
  92. if (bdd->errorCode == CUDD_TIMEOUT_EXPIRED && bdd->timeoutHandler) {
  93. bdd->timeoutHandler(bdd, bdd->tohArg);
  94. }
  95. return(res);
  96. } /* end of Cudd_SolveEqn */
  97. /**
  98. @brief Checks the solution of F(x,y) = 0.
  99. @details This procedure substitutes the solution components for the
  100. unknowns of F and returns the resulting %BDD for F.
  101. @sideeffect Frees the memory pointed by yIndex.
  102. @see Cudd_SolveEqn
  103. */
  104. DdNode *
  105. Cudd_VerifySol(
  106. DdManager * bdd /**< CUDD manager */,
  107. DdNode * F /**< the left-hand side of the equation */,
  108. DdNode ** G /**< the array of solutions */,
  109. int * yIndex /**< index of y variables */,
  110. int n /**< numbers of unknowns */)
  111. {
  112. DdNode *res;
  113. do {
  114. bdd->reordered = 0;
  115. res = cuddVerifySol(bdd, F, G, yIndex, n);
  116. } while (bdd->reordered == 1);
  117. FREE(yIndex);
  118. if (bdd->errorCode == CUDD_TIMEOUT_EXPIRED && bdd->timeoutHandler) {
  119. bdd->timeoutHandler(bdd, bdd->tohArg);
  120. }
  121. return(res);
  122. } /* end of Cudd_VerifySol */
  123. /*---------------------------------------------------------------------------*/
  124. /* Definition of internal functions */
  125. /*---------------------------------------------------------------------------*/
  126. /**
  127. @brief Implements the recursive step of Cudd_SolveEqn.
  128. @return NULL if the intermediate solution blows up or reordering
  129. occurs.
  130. @sideeffect The parametric solutions are stored in the array G.
  131. @see Cudd_SolveEqn, Cudd_VerifySol
  132. */
  133. DdNode *
  134. cuddSolveEqnRecur(
  135. DdManager * bdd /**< CUDD manager */,
  136. DdNode * F /**< the left-hand side of the equation */,
  137. DdNode * Y /**< the cube of remaining y variables */,
  138. DdNode ** G /**< the array of solutions */,
  139. int n /**< number of unknowns */,
  140. int * yIndex /**< array holding the y variable indices */,
  141. int i /**< level of recursion */)
  142. {
  143. DdNode *Fn, *Fm1, *Fv, *Fvbar, *T, *w, *nextY, *one;
  144. DdNodePtr *variables;
  145. int j;
  146. statLine(bdd);
  147. variables = bdd->vars;
  148. one = DD_ONE(bdd);
  149. /* Base condition. */
  150. if (Y == one) {
  151. return F;
  152. }
  153. /* Cofactor of Y. */
  154. yIndex[i] = Y->index;
  155. nextY = Cudd_T(Y);
  156. /* Universal abstraction of F with respect to the top variable index. */
  157. Fm1 = cuddBddExistAbstractRecur(bdd, Cudd_Not(F), variables[yIndex[i]]);
  158. if (Fm1) {
  159. Fm1 = Cudd_Not(Fm1);
  160. cuddRef(Fm1);
  161. } else {
  162. return(NULL);
  163. }
  164. Fn = cuddSolveEqnRecur(bdd, Fm1, nextY, G, n, yIndex, i+1);
  165. if (Fn) {
  166. cuddRef(Fn);
  167. } else {
  168. Cudd_RecursiveDeref(bdd, Fm1);
  169. return(NULL);
  170. }
  171. Fv = cuddCofactorRecur(bdd, F, variables[yIndex[i]]);
  172. if (Fv) {
  173. cuddRef(Fv);
  174. } else {
  175. Cudd_RecursiveDeref(bdd, Fm1);
  176. Cudd_RecursiveDeref(bdd, Fn);
  177. return(NULL);
  178. }
  179. Fvbar = cuddCofactorRecur(bdd, F, Cudd_Not(variables[yIndex[i]]));
  180. if (Fvbar) {
  181. cuddRef(Fvbar);
  182. } else {
  183. Cudd_RecursiveDeref(bdd, Fm1);
  184. Cudd_RecursiveDeref(bdd, Fn);
  185. Cudd_RecursiveDeref(bdd, Fv);
  186. return(NULL);
  187. }
  188. /* Build i-th component of the solution. */
  189. w = cuddBddIteRecur(bdd, variables[yIndex[i]], Cudd_Not(Fv), Fvbar);
  190. if (w) {
  191. cuddRef(w);
  192. } else {
  193. Cudd_RecursiveDeref(bdd, Fm1);
  194. Cudd_RecursiveDeref(bdd, Fn);
  195. Cudd_RecursiveDeref(bdd, Fv);
  196. Cudd_RecursiveDeref(bdd, Fvbar);
  197. return(NULL);
  198. }
  199. T = cuddBddRestrictRecur(bdd, w, Cudd_Not(Fm1));
  200. if(T) {
  201. cuddRef(T);
  202. } else {
  203. Cudd_RecursiveDeref(bdd, Fm1);
  204. Cudd_RecursiveDeref(bdd, Fn);
  205. Cudd_RecursiveDeref(bdd, Fv);
  206. Cudd_RecursiveDeref(bdd, Fvbar);
  207. Cudd_RecursiveDeref(bdd, w);
  208. return(NULL);
  209. }
  210. Cudd_RecursiveDeref(bdd,Fm1);
  211. Cudd_RecursiveDeref(bdd,w);
  212. Cudd_RecursiveDeref(bdd,Fv);
  213. Cudd_RecursiveDeref(bdd,Fvbar);
  214. /* Substitute components of solution already found into solution. */
  215. for (j = n-1; j > i; j--) {
  216. w = cuddBddComposeRecur(bdd,T, G[j], variables[yIndex[j]]);
  217. if(w) {
  218. cuddRef(w);
  219. } else {
  220. Cudd_RecursiveDeref(bdd, Fn);
  221. Cudd_RecursiveDeref(bdd, T);
  222. return(NULL);
  223. }
  224. Cudd_RecursiveDeref(bdd,T);
  225. T = w;
  226. }
  227. G[i] = T;
  228. Cudd_Deref(Fn);
  229. return(Fn);
  230. } /* end of cuddSolveEqnRecur */
  231. /**
  232. @brief Implements the recursive step of Cudd_VerifySol.
  233. @sideeffect none
  234. @see Cudd_VerifySol
  235. */
  236. DdNode *
  237. cuddVerifySol(
  238. DdManager * bdd /**< CUDD manager */,
  239. DdNode * F /**< the left-hand side of the equation */,
  240. DdNode ** G /**< the array of solutions */,
  241. int * yIndex /**< array holding the y variable indices */,
  242. int n /**< number of unknowns */)
  243. {
  244. DdNode *w, *R;
  245. int j;
  246. R = F;
  247. cuddRef(R);
  248. for(j = n - 1; j >= 0; j--) {
  249. w = Cudd_bddCompose(bdd, R, G[j], yIndex[j]);
  250. if (w) {
  251. cuddRef(w);
  252. } else {
  253. return(NULL);
  254. }
  255. Cudd_RecursiveDeref(bdd,R);
  256. R = w;
  257. }
  258. cuddDeref(R);
  259. return(R);
  260. } /* end of cuddVerifySol */
  261. /*---------------------------------------------------------------------------*/
  262. /* Definition of static functions */
  263. /*---------------------------------------------------------------------------*/