abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cuddSolve.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [cuddSolve.c]
4 
5  PackageName [cudd]
6 
7  Synopsis [Boolean equation solver and related functions.]
8 
9  Description [External functions included in this modoule:
10  <ul>
11  <li> Cudd_SolveEqn()
12  <li> Cudd_VerifySol()
13  </ul>
14  Internal functions included in this module:
15  <ul>
16  <li> cuddSolveEqnRecur()
17  <li> cuddVerifySol()
18  </ul> ]
19 
20  SeeAlso []
21 
22  Author [Balakrishna Kumthekar]
23 
24  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
25 
26  All rights reserved.
27 
28  Redistribution and use in source and binary forms, with or without
29  modification, are permitted provided that the following conditions
30  are met:
31 
32  Redistributions of source code must retain the above copyright
33  notice, this list of conditions and the following disclaimer.
34 
35  Redistributions in binary form must reproduce the above copyright
36  notice, this list of conditions and the following disclaimer in the
37  documentation and/or other materials provided with the distribution.
38 
39  Neither the name of the University of Colorado nor the names of its
40  contributors may be used to endorse or promote products derived from
41  this software without specific prior written permission.
42 
43  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
46  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
47  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
48  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
51  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
53  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54  POSSIBILITY OF SUCH DAMAGE.]
55 
56 ******************************************************************************/
57 
58 #include "misc/util/util_hack.h"
59 #include "cuddInt.h"
60 
62 
63 
64 
65 /*---------------------------------------------------------------------------*/
66 /* Constant declarations */
67 /*---------------------------------------------------------------------------*/
68 
69 
70 /*---------------------------------------------------------------------------*/
71 /* Type declarations */
72 /*---------------------------------------------------------------------------*/
73 
74 
75 /*---------------------------------------------------------------------------*/
76 /* Structure declarations */
77 /*---------------------------------------------------------------------------*/
78 
79 
80 /*---------------------------------------------------------------------------*/
81 /* Variable declarations */
82 /*---------------------------------------------------------------------------*/
83 
84 #ifndef lint
85 static char rcsid[] DD_UNUSED = "$Id: cuddSolve.c,v 1.12 2004/08/13 18:04:51 fabio Exp $";
86 #endif
87 
88 /*---------------------------------------------------------------------------*/
89 /* Macro declarations */
90 /*---------------------------------------------------------------------------*/
91 
92 
93 /**AutomaticStart*************************************************************/
94 
95 /*---------------------------------------------------------------------------*/
96 /* Static function prototypes */
97 /*---------------------------------------------------------------------------*/
98 
99 
100 /**AutomaticEnd***************************************************************/
101 
102 
103 /*---------------------------------------------------------------------------*/
104 /* Definition of exported functions */
105 /*---------------------------------------------------------------------------*/
106 
107 
108 /**Function********************************************************************
109 
110  Synopsis [Implements the solution of F(x,y) = 0.]
111 
112  Description [Implements the solution for F(x,y) = 0. The return
113  value is the consistency condition. The y variables are the unknowns
114  and the remaining variables are the parameters. Returns the
115  consistency condition if successful; NULL otherwise. Cudd_SolveEqn
116  allocates an array and fills it with the indices of the
117  unknowns. This array is used by Cudd_VerifySol.]
118 
119  SideEffects [The solution is returned in G; the indices of the y
120  variables are returned in yIndex.]
121 
122  SeeAlso [Cudd_VerifySol]
123 
124 ******************************************************************************/
125 DdNode *
127  DdManager * bdd,
128  DdNode * F /* the left-hand side of the equation */,
129  DdNode * Y /* the cube of the y variables */,
130  DdNode ** G /* the array of solutions (return parameter) */,
131  int ** yIndex /* index of y variables */,
132  int n /* numbers of unknowns */)
133 {
134  DdNode *res;
135  int *temp;
136 
137  *yIndex = temp = ABC_ALLOC(int, n);
138  if (temp == NULL) {
139  bdd->errorCode = CUDD_MEMORY_OUT;
140  (void) fprintf(bdd->out,
141  "Cudd_SolveEqn: Out of memory for yIndex\n");
142  return(NULL);
143  }
144 
145  do {
146  bdd->reordered = 0;
147  res = cuddSolveEqnRecur(bdd, F, Y, G, n, temp, 0);
148  } while (bdd->reordered == 1);
149 
150  return(res);
151 
152 } /* end of Cudd_SolveEqn */
153 
154 
155 /**Function********************************************************************
156 
157  Synopsis [Checks the solution of F(x,y) = 0.]
158 
159  Description [Checks the solution of F(x,y) = 0. This procedure
160  substitutes the solution components for the unknowns of F and returns
161  the resulting BDD for F.]
162 
163  SideEffects [Frees the memory pointed by yIndex.]
164 
165  SeeAlso [Cudd_SolveEqn]
166 
167 ******************************************************************************/
168 DdNode *
170  DdManager * bdd,
171  DdNode * F /* the left-hand side of the equation */,
172  DdNode ** G /* the array of solutions */,
173  int * yIndex /* index of y variables */,
174  int n /* numbers of unknowns */)
175 {
176  DdNode *res;
177 
178  do {
179  bdd->reordered = 0;
180  res = cuddVerifySol(bdd, F, G, yIndex, n);
181  } while (bdd->reordered == 1);
182 
183  ABC_FREE(yIndex);
184 
185  return(res);
186 
187 } /* end of Cudd_VerifySol */
188 
189 
190 /*---------------------------------------------------------------------------*/
191 /* Definition of internal functions */
192 /*---------------------------------------------------------------------------*/
193 
194 
195 /**Function********************************************************************
196 
197  Synopsis [Implements the recursive step of Cudd_SolveEqn.]
198 
199  Description [Implements the recursive step of Cudd_SolveEqn.
200  Returns NULL if the intermediate solution blows up
201  or reordering occurs. The parametric solutions are
202  stored in the array G.]
203 
204  SideEffects [none]
205 
206  SeeAlso [Cudd_SolveEqn, Cudd_VerifySol]
207 
208 ******************************************************************************/
209 DdNode *
211  DdManager * bdd,
212  DdNode * F /* the left-hand side of the equation */,
213  DdNode * Y /* the cube of remaining y variables */,
214  DdNode ** G /* the array of solutions */,
215  int n /* number of unknowns */,
216  int * yIndex /* array holding the y variable indices */,
217  int i /* level of recursion */)
218 {
219  DdNode *Fn, *Fm1, *Fv, *Fvbar, *T, *w, *nextY, *one;
220  DdNodePtr *variables;
221 
222  int j;
223 
224  statLine(bdd);
225  variables = bdd->vars;
226  one = DD_ONE(bdd);
227 
228  /* Base condition. */
229  if (Y == one) {
230  return F;
231  }
232 
233  /* Cofactor of Y. */
234  yIndex[i] = Y->index;
235  nextY = Cudd_T(Y);
236 
237  /* Universal abstraction of F with respect to the top variable index. */
238  Fm1 = cuddBddExistAbstractRecur(bdd, Cudd_Not(F), variables[yIndex[i]]);
239  if (Fm1) {
240  Fm1 = Cudd_Not(Fm1);
241  cuddRef(Fm1);
242  } else {
243  return(NULL);
244  }
245 
246  Fn = cuddSolveEqnRecur(bdd, Fm1, nextY, G, n, yIndex, i+1);
247  if (Fn) {
248  cuddRef(Fn);
249  } else {
250  Cudd_RecursiveDeref(bdd, Fm1);
251  return(NULL);
252  }
253 
254  Fv = cuddCofactorRecur(bdd, F, variables[yIndex[i]]);
255  if (Fv) {
256  cuddRef(Fv);
257  } else {
258  Cudd_RecursiveDeref(bdd, Fm1);
259  Cudd_RecursiveDeref(bdd, Fn);
260  return(NULL);
261  }
262 
263  Fvbar = cuddCofactorRecur(bdd, F, Cudd_Not(variables[yIndex[i]]));
264  if (Fvbar) {
265  cuddRef(Fvbar);
266  } else {
267  Cudd_RecursiveDeref(bdd, Fm1);
268  Cudd_RecursiveDeref(bdd, Fn);
269  Cudd_RecursiveDeref(bdd, Fv);
270  return(NULL);
271  }
272 
273  /* Build i-th component of the solution. */
274  w = cuddBddIteRecur(bdd, variables[yIndex[i]], Cudd_Not(Fv), Fvbar);
275  if (w) {
276  cuddRef(w);
277  } else {
278  Cudd_RecursiveDeref(bdd, Fm1);
279  Cudd_RecursiveDeref(bdd, Fn);
280  Cudd_RecursiveDeref(bdd, Fv);
281  Cudd_RecursiveDeref(bdd, Fvbar);
282  return(NULL);
283  }
284 
285  T = cuddBddRestrictRecur(bdd, w, Cudd_Not(Fm1));
286  if(T) {
287  cuddRef(T);
288  } else {
289  Cudd_RecursiveDeref(bdd, Fm1);
290  Cudd_RecursiveDeref(bdd, Fn);
291  Cudd_RecursiveDeref(bdd, Fv);
292  Cudd_RecursiveDeref(bdd, Fvbar);
293  Cudd_RecursiveDeref(bdd, w);
294  return(NULL);
295  }
296 
297  Cudd_RecursiveDeref(bdd,Fm1);
298  Cudd_RecursiveDeref(bdd,w);
299  Cudd_RecursiveDeref(bdd,Fv);
300  Cudd_RecursiveDeref(bdd,Fvbar);
301 
302  /* Substitute components of solution already found into solution. */
303  for (j = n-1; j > i; j--) {
304  w = cuddBddComposeRecur(bdd,T, G[j], variables[yIndex[j]]);
305  if(w) {
306  cuddRef(w);
307  } else {
308  Cudd_RecursiveDeref(bdd, Fn);
309  Cudd_RecursiveDeref(bdd, T);
310  return(NULL);
311  }
312  Cudd_RecursiveDeref(bdd,T);
313  T = w;
314  }
315  G[i] = T;
316 
317  Cudd_Deref(Fn);
318 
319  return(Fn);
320 
321 } /* end of cuddSolveEqnRecur */
322 
323 
324 /**Function********************************************************************
325 
326  Synopsis [Implements the recursive step of Cudd_VerifySol. ]
327 
328  Description []
329 
330  SideEffects [none]
331 
332  SeeAlso [Cudd_VerifySol]
333 
334 ******************************************************************************/
335 DdNode *
337  DdManager * bdd,
338  DdNode * F /* the left-hand side of the equation */,
339  DdNode ** G /* the array of solutions */,
340  int * yIndex /* array holding the y variable indices */,
341  int n /* number of unknowns */)
342 {
343  DdNode *w, *R;
344 
345  int j;
346 
347  R = F;
348  cuddRef(R);
349  for(j = n - 1; j >= 0; j--) {
350  w = Cudd_bddCompose(bdd, R, G[j], yIndex[j]);
351  if (w) {
352  cuddRef(w);
353  } else {
354  return(NULL);
355  }
356  Cudd_RecursiveDeref(bdd,R);
357  R = w;
358  }
359 
360  cuddDeref(R);
361 
362  return(R);
363 
364 } /* end of cuddVerifySol */
365 
366 
367 /*---------------------------------------------------------------------------*/
368 /* Definition of static functions */
369 /*---------------------------------------------------------------------------*/
370 
371 
373 
374 
DdNode * cuddBddExistAbstractRecur(DdManager *manager, DdNode *f, DdNode *cube)
Definition: cuddBddAbs.c:352
#define cuddRef(n)
Definition: cuddInt.h:584
#define Cudd_T(node)
Definition: cudd.h:440
DdNode * cuddVerifySol(DdManager *bdd, DdNode *F, DdNode **G, int *yIndex, int n)
Definition: cuddSolve.c:336
#define cuddDeref(n)
Definition: cuddInt.h:604
void Cudd_RecursiveDeref(DdManager *table, DdNode *n)
Definition: cuddRef.c:154
Definition: cudd.h:278
#define Cudd_Not(node)
Definition: cudd.h:367
DdNode * cuddBddComposeRecur(DdManager *dd, DdNode *f, DdNode *g, DdNode *proj)
Definition: cuddCompose.c:850
void Cudd_Deref(DdNode *node)
Definition: cuddRef.c:438
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
#define statLine(dd)
Definition: cuddInt.h:1037
int reordered
Definition: cuddInt.h:409
DdNode * Cudd_SolveEqn(DdManager *bdd, DdNode *F, DdNode *Y, DdNode **G, int **yIndex, int n)
Definition: cuddSolve.c:126
static DdNode * one
Definition: cuddDecomp.c:112
FILE * out
Definition: cuddInt.h:441
DdNode * Cudd_bddCompose(DdManager *dd, DdNode *f, DdNode *g, int v)
Definition: cuddCompose.c:167
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
static ABC_NAMESPACE_IMPL_START char rcsid[] DD_UNUSED
Definition: cuddSolve.c:85
DdNode * cuddCofactorRecur(DdManager *dd, DdNode *f, DdNode *g)
Definition: cuddCof.c:230
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
DdNode * cuddSolveEqnRecur(DdManager *bdd, DdNode *F, DdNode *Y, DdNode **G, int n, int *yIndex, int i)
Definition: cuddSolve.c:210
DdHalfWord index
Definition: cudd.h:279
#define ABC_FREE(obj)
Definition: abc_global.h:232
DdNode ** vars
Definition: cuddInt.h:390
#define DD_ONE(dd)
Definition: cuddInt.h:911
DdNode * Cudd_VerifySol(DdManager *bdd, DdNode *F, DdNode **G, int *yIndex, int n)
Definition: cuddSolve.c:169
Cudd_ErrorType errorCode
Definition: cuddInt.h:447
DdNode * cuddBddRestrictRecur(DdManager *dd, DdNode *f, DdNode *c)
Definition: cuddGenCof.c:912
DdNode * cuddBddIteRecur(DdManager *dd, DdNode *f, DdNode *g, DdNode *h)
Definition: cuddBddIte.c:633