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.

366 lines
11 KiB

  1. /**CFile***********************************************************************
  2. FileName [cuddSolve.c]
  3. PackageName [cudd]
  4. Synopsis [Boolean equation solver and related functions.]
  5. Description [External functions included in this modoule:
  6. <ul>
  7. <li> Cudd_SolveEqn()
  8. <li> Cudd_VerifySol()
  9. </ul>
  10. Internal functions included in this module:
  11. <ul>
  12. <li> cuddSolveEqnRecur()
  13. <li> cuddVerifySol()
  14. </ul> ]
  15. SeeAlso []
  16. Author [Balakrishna Kumthekar]
  17. Copyright [Copyright (c) 1995-2012, Regents of the University of Colorado
  18. All rights reserved.
  19. Redistribution and use in source and binary forms, with or without
  20. modification, are permitted provided that the following conditions
  21. are met:
  22. Redistributions of source code must retain the above copyright
  23. notice, this list of conditions and the following disclaimer.
  24. Redistributions in binary form must reproduce the above copyright
  25. notice, this list of conditions and the following disclaimer in the
  26. documentation and/or other materials provided with the distribution.
  27. Neither the name of the University of Colorado nor the names of its
  28. contributors may be used to endorse or promote products derived from
  29. this software without specific prior written permission.
  30. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  33. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  34. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  35. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  36. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  39. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. POSSIBILITY OF SUCH DAMAGE.]
  42. ******************************************************************************/
  43. #include "util.h"
  44. #include "cuddInt.h"
  45. /*---------------------------------------------------------------------------*/
  46. /* Constant declarations */
  47. /*---------------------------------------------------------------------------*/
  48. /*---------------------------------------------------------------------------*/
  49. /* Type declarations */
  50. /*---------------------------------------------------------------------------*/
  51. /*---------------------------------------------------------------------------*/
  52. /* Structure declarations */
  53. /*---------------------------------------------------------------------------*/
  54. /*---------------------------------------------------------------------------*/
  55. /* Variable declarations */
  56. /*---------------------------------------------------------------------------*/
  57. #ifndef lint
  58. static char rcsid[] DD_UNUSED = "$Id: cuddSolve.c,v 1.13 2012/02/05 01:07:19 fabio Exp $";
  59. #endif
  60. /*---------------------------------------------------------------------------*/
  61. /* Macro declarations */
  62. /*---------------------------------------------------------------------------*/
  63. /**AutomaticStart*************************************************************/
  64. /*---------------------------------------------------------------------------*/
  65. /* Static function prototypes */
  66. /*---------------------------------------------------------------------------*/
  67. /**AutomaticEnd***************************************************************/
  68. /*---------------------------------------------------------------------------*/
  69. /* Definition of exported functions */
  70. /*---------------------------------------------------------------------------*/
  71. /**Function********************************************************************
  72. Synopsis [Implements the solution of F(x,y) = 0.]
  73. Description [Implements the solution for F(x,y) = 0. The return
  74. value is the consistency condition. The y variables are the unknowns
  75. and the remaining variables are the parameters. Returns the
  76. consistency condition if successful; NULL otherwise. Cudd_SolveEqn
  77. allocates an array and fills it with the indices of the
  78. unknowns. This array is used by Cudd_VerifySol.]
  79. SideEffects [The solution is returned in G; the indices of the y
  80. variables are returned in yIndex.]
  81. SeeAlso [Cudd_VerifySol]
  82. ******************************************************************************/
  83. DdNode *
  84. Cudd_SolveEqn(
  85. DdManager * bdd,
  86. DdNode * F /* the left-hand side of the equation */,
  87. DdNode * Y /* the cube of the y variables */,
  88. DdNode ** G /* the array of solutions (return parameter) */,
  89. int ** yIndex /* index of y variables */,
  90. int n /* numbers of unknowns */)
  91. {
  92. DdNode *res;
  93. int *temp;
  94. *yIndex = temp = ALLOC(int, n);
  95. if (temp == NULL) {
  96. bdd->errorCode = CUDD_MEMORY_OUT;
  97. (void) fprintf(bdd->out,
  98. "Cudd_SolveEqn: Out of memory for yIndex\n");
  99. return(NULL);
  100. }
  101. do {
  102. bdd->reordered = 0;
  103. res = cuddSolveEqnRecur(bdd, F, Y, G, n, temp, 0);
  104. } while (bdd->reordered == 1);
  105. return(res);
  106. } /* end of Cudd_SolveEqn */
  107. /**Function********************************************************************
  108. Synopsis [Checks the solution of F(x,y) = 0.]
  109. Description [Checks the solution of F(x,y) = 0. This procedure
  110. substitutes the solution components for the unknowns of F and returns
  111. the resulting BDD for F.]
  112. SideEffects [Frees the memory pointed by yIndex.]
  113. SeeAlso [Cudd_SolveEqn]
  114. ******************************************************************************/
  115. DdNode *
  116. Cudd_VerifySol(
  117. DdManager * bdd,
  118. DdNode * F /* the left-hand side of the equation */,
  119. DdNode ** G /* the array of solutions */,
  120. int * yIndex /* index of y variables */,
  121. int n /* numbers of unknowns */)
  122. {
  123. DdNode *res;
  124. do {
  125. bdd->reordered = 0;
  126. res = cuddVerifySol(bdd, F, G, yIndex, n);
  127. } while (bdd->reordered == 1);
  128. FREE(yIndex);
  129. return(res);
  130. } /* end of Cudd_VerifySol */
  131. /*---------------------------------------------------------------------------*/
  132. /* Definition of internal functions */
  133. /*---------------------------------------------------------------------------*/
  134. /**Function********************************************************************
  135. Synopsis [Implements the recursive step of Cudd_SolveEqn.]
  136. Description [Implements the recursive step of Cudd_SolveEqn.
  137. Returns NULL if the intermediate solution blows up
  138. or reordering occurs. The parametric solutions are
  139. stored in the array G.]
  140. SideEffects [none]
  141. SeeAlso [Cudd_SolveEqn, Cudd_VerifySol]
  142. ******************************************************************************/
  143. DdNode *
  144. cuddSolveEqnRecur(
  145. DdManager * bdd,
  146. DdNode * F /* the left-hand side of the equation */,
  147. DdNode * Y /* the cube of remaining y variables */,
  148. DdNode ** G /* the array of solutions */,
  149. int n /* number of unknowns */,
  150. int * yIndex /* array holding the y variable indices */,
  151. int i /* level of recursion */)
  152. {
  153. DdNode *Fn, *Fm1, *Fv, *Fvbar, *T, *w, *nextY, *one;
  154. DdNodePtr *variables;
  155. int j;
  156. statLine(bdd);
  157. variables = bdd->vars;
  158. one = DD_ONE(bdd);
  159. /* Base condition. */
  160. if (Y == one) {
  161. return F;
  162. }
  163. /* Cofactor of Y. */
  164. yIndex[i] = Y->index;
  165. nextY = Cudd_T(Y);
  166. /* Universal abstraction of F with respect to the top variable index. */
  167. Fm1 = cuddBddExistAbstractRecur(bdd, Cudd_Not(F), variables[yIndex[i]]);
  168. if (Fm1) {
  169. Fm1 = Cudd_Not(Fm1);
  170. cuddRef(Fm1);
  171. } else {
  172. return(NULL);
  173. }
  174. Fn = cuddSolveEqnRecur(bdd, Fm1, nextY, G, n, yIndex, i+1);
  175. if (Fn) {
  176. cuddRef(Fn);
  177. } else {
  178. Cudd_RecursiveDeref(bdd, Fm1);
  179. return(NULL);
  180. }
  181. Fv = cuddCofactorRecur(bdd, F, variables[yIndex[i]]);
  182. if (Fv) {
  183. cuddRef(Fv);
  184. } else {
  185. Cudd_RecursiveDeref(bdd, Fm1);
  186. Cudd_RecursiveDeref(bdd, Fn);
  187. return(NULL);
  188. }
  189. Fvbar = cuddCofactorRecur(bdd, F, Cudd_Not(variables[yIndex[i]]));
  190. if (Fvbar) {
  191. cuddRef(Fvbar);
  192. } else {
  193. Cudd_RecursiveDeref(bdd, Fm1);
  194. Cudd_RecursiveDeref(bdd, Fn);
  195. Cudd_RecursiveDeref(bdd, Fv);
  196. return(NULL);
  197. }
  198. /* Build i-th component of the solution. */
  199. w = cuddBddIteRecur(bdd, variables[yIndex[i]], Cudd_Not(Fv), Fvbar);
  200. if (w) {
  201. cuddRef(w);
  202. } else {
  203. Cudd_RecursiveDeref(bdd, Fm1);
  204. Cudd_RecursiveDeref(bdd, Fn);
  205. Cudd_RecursiveDeref(bdd, Fv);
  206. Cudd_RecursiveDeref(bdd, Fvbar);
  207. return(NULL);
  208. }
  209. T = cuddBddRestrictRecur(bdd, w, Cudd_Not(Fm1));
  210. if(T) {
  211. cuddRef(T);
  212. } else {
  213. Cudd_RecursiveDeref(bdd, Fm1);
  214. Cudd_RecursiveDeref(bdd, Fn);
  215. Cudd_RecursiveDeref(bdd, Fv);
  216. Cudd_RecursiveDeref(bdd, Fvbar);
  217. Cudd_RecursiveDeref(bdd, w);
  218. return(NULL);
  219. }
  220. Cudd_RecursiveDeref(bdd,Fm1);
  221. Cudd_RecursiveDeref(bdd,w);
  222. Cudd_RecursiveDeref(bdd,Fv);
  223. Cudd_RecursiveDeref(bdd,Fvbar);
  224. /* Substitute components of solution already found into solution. */
  225. for (j = n-1; j > i; j--) {
  226. w = cuddBddComposeRecur(bdd,T, G[j], variables[yIndex[j]]);
  227. if(w) {
  228. cuddRef(w);
  229. } else {
  230. Cudd_RecursiveDeref(bdd, Fn);
  231. Cudd_RecursiveDeref(bdd, T);
  232. return(NULL);
  233. }
  234. Cudd_RecursiveDeref(bdd,T);
  235. T = w;
  236. }
  237. G[i] = T;
  238. Cudd_Deref(Fn);
  239. return(Fn);
  240. } /* end of cuddSolveEqnRecur */
  241. /**Function********************************************************************
  242. Synopsis [Implements the recursive step of Cudd_VerifySol. ]
  243. Description []
  244. SideEffects [none]
  245. SeeAlso [Cudd_VerifySol]
  246. ******************************************************************************/
  247. DdNode *
  248. cuddVerifySol(
  249. DdManager * bdd,
  250. DdNode * F /* the left-hand side of the equation */,
  251. DdNode ** G /* the array of solutions */,
  252. int * yIndex /* array holding the y variable indices */,
  253. int n /* number of unknowns */)
  254. {
  255. DdNode *w, *R;
  256. int j;
  257. R = F;
  258. cuddRef(R);
  259. for(j = n - 1; j >= 0; j--) {
  260. w = Cudd_bddCompose(bdd, R, G[j], yIndex[j]);
  261. if (w) {
  262. cuddRef(w);
  263. } else {
  264. return(NULL);
  265. }
  266. Cudd_RecursiveDeref(bdd,R);
  267. R = w;
  268. }
  269. cuddDeref(R);
  270. return(R);
  271. } /* end of cuddVerifySol */
  272. /*---------------------------------------------------------------------------*/
  273. /* Definition of static functions */
  274. /*---------------------------------------------------------------------------*/