cuddBddCorr.c 15 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9
/**CFile***********************************************************************

  FileName    [cuddBddCorr.c]

  PackageName [cudd]

  Synopsis    [Correlation between BDDs.]

  Description [External procedures included in this module:
10 11 12 13 14 15 16 17 18 19 20 21 22
                <ul>
                <li> Cudd_bddCorrelation()
                <li> Cudd_bddCorrelationWeights()
                </ul>
            Static procedures included in this module:
                <ul>
                <li> bddCorrelationAux()
                <li> bddCorrelationWeightsAux()
                <li> CorrelCompare()
                <li> CorrelHash()
                <li> CorrelCleanUp()
                </ul>
                ]
Alan Mishchenko committed
23 24 25

  Author      [Fabio Somenzi]

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  Copyright   [Copyright (c) 1995-2004, Regents of the University of Colorado

  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

  Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  Neither the name of the University of Colorado nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.]
Alan Mishchenko committed
57 58 59

******************************************************************************/

60
#include "misc/util/util_hack.h"
Alan Mishchenko committed
61 62
#include "cuddInt.h"

63 64 65
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
66

67

Alan Mishchenko committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/*---------------------------------------------------------------------------*/
/* Constant declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Stucture declarations                                                     */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Type declarations                                                         */
/*---------------------------------------------------------------------------*/

typedef struct hashEntry {
    DdNode *f;
    DdNode *g;
} HashEntry;


/*---------------------------------------------------------------------------*/
/* Variable declarations                                                     */
/*---------------------------------------------------------------------------*/

#ifndef lint
93
static char rcsid[] DD_UNUSED = "$Id: cuddBddCorr.c,v 1.14 2004/08/13 18:04:46 fabio Exp $";
Alan Mishchenko committed
94 95 96
#endif

#ifdef CORREL_STATS
97
static  int     num_calls;
Alan Mishchenko committed
98 99 100 101 102 103
#endif

/*---------------------------------------------------------------------------*/
/* Macro declarations                                                        */
/*---------------------------------------------------------------------------*/

104 105 106
#ifdef __cplusplus
extern "C" {
#endif
Alan Mishchenko committed
107 108 109 110 111 112 113

/**AutomaticStart*************************************************************/

/*---------------------------------------------------------------------------*/
/* Static function prototypes                                                */
/*---------------------------------------------------------------------------*/

114 115
static double bddCorrelationAux (DdManager *dd, DdNode *f, DdNode *g, st__table *table);
static double bddCorrelationWeightsAux (DdManager *dd, DdNode *f, DdNode *g, double *prob, st__table *table);
116 117
static int CorrelCompare (const char *key1, const char *key2);
static int CorrelHash (const char *key, int modulus);
118
static enum st__retval CorrelCleanUp (char *key, char *value, char *arg);
Alan Mishchenko committed
119 120 121

/**AutomaticEnd***************************************************************/

122 123 124 125
#ifdef __cplusplus
}
#endif

Alan Mishchenko committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

/*---------------------------------------------------------------------------*/
/* Definition of exported functions                                          */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Computes the correlation of f and g.]

  Description [Computes the correlation of f and g. If f == g, their
  correlation is 1. If f == g', their correlation is 0.  Returns the
  fraction of minterms in the ON-set of the EXNOR of f and g.  If it
  runs out of memory, returns (double)CUDD_OUT_OF_MEM.]

  SideEffects [None]

  SeeAlso     [Cudd_bddCorrelationWeights]

******************************************************************************/
double
Cudd_bddCorrelation(
  DdManager * manager,
  DdNode * f,
  DdNode * g)
{

153
    st__table    *table;
154
    double      correlation;
Alan Mishchenko committed
155 156 157 158 159

#ifdef CORREL_STATS
    num_calls = 0;
#endif

160
    table = st__init_table(CorrelCompare,CorrelHash);
Alan Mishchenko committed
161 162
    if (table == NULL) return((double)CUDD_OUT_OF_MEM);
    correlation = bddCorrelationAux(manager,f,g,table);
163 164
    st__foreach(table, CorrelCleanUp, NIL(char));
    st__free_table(table);
Alan Mishchenko committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    return(correlation);

} /* end of Cudd_bddCorrelation */


/**Function********************************************************************

  Synopsis    [Computes the correlation of f and g for given input
  probabilities.]

  Description [Computes the correlation of f and g for given input
  probabilities. On input, prob\[i\] is supposed to contain the
  probability of the i-th input variable to be 1.
  If f == g, their correlation is 1. If f == g', their
  correlation is 0.  Returns the probability that f and g have the same
  value. If it runs out of memory, returns (double)CUDD_OUT_OF_MEM. The
  correlation of f and the constant one gives the probability of f.]

  SideEffects [None]

  SeeAlso     [Cudd_bddCorrelation]

******************************************************************************/
double
Cudd_bddCorrelationWeights(
  DdManager * manager,
  DdNode * f,
  DdNode * g,
  double * prob)
{

196
    st__table    *table;
197
    double      correlation;
Alan Mishchenko committed
198 199 200 201 202

#ifdef CORREL_STATS
    num_calls = 0;
#endif

203
    table = st__init_table(CorrelCompare,CorrelHash);
Alan Mishchenko committed
204 205
    if (table == NULL) return((double)CUDD_OUT_OF_MEM);
    correlation = bddCorrelationWeightsAux(manager,f,g,prob,table);
206 207
    st__foreach(table, CorrelCleanUp, NIL(char));
    st__free_table(table);
Alan Mishchenko committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    return(correlation);

} /* end of Cudd_bddCorrelationWeights */


/*---------------------------------------------------------------------------*/
/* Definition of internal functions                                          */
/*---------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------*/
/* Definition of static functions                                            */
/*---------------------------------------------------------------------------*/


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_bddCorrelation.]

  Description [Performs the recursive step of Cudd_bddCorrelation.
  Returns the fraction of minterms in the ON-set of the EXNOR of f and
  g.]

  SideEffects [None]

  SeeAlso     [bddCorrelationWeightsAux]

******************************************************************************/
static double
bddCorrelationAux(
  DdManager * dd,
  DdNode * f,
  DdNode * g,
241
  st__table * table)
Alan Mishchenko committed
242
{
243 244 245
    DdNode      *Fv, *Fnv, *G, *Gv, *Gnv;
    double      min, *pmin, min1, min2, *dummy;
    HashEntry   *entry;
Alan Mishchenko committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    unsigned int topF, topG;

    statLine(dd);
#ifdef CORREL_STATS
    num_calls++;
#endif

    /* Terminal cases: only work for BDDs. */
    if (f == g) return(1.0);
    if (f == Cudd_Not(g)) return(0.0);

    /* Standardize call using the following properties:
    **     (f EXNOR g)   = (g EXNOR f)
    **     (f' EXNOR g') = (f EXNOR g).
    */
261
    if (cuddF2L(f) > cuddF2L(g)) {
262 263
        DdNode *tmp = f;
        f = g; g = tmp;
Alan Mishchenko committed
264 265
    }
    if (Cudd_IsComplement(f)) {
266 267
        f = Cudd_Not(f);
        g = Cudd_Not(g);
Alan Mishchenko committed
268 269 270
    }
    /* From now on, f is regular. */
    
Alan Mishchenko committed
271
    entry = ABC_ALLOC(HashEntry,1);
Alan Mishchenko committed
272
    if (entry == NULL) {
273 274
        dd->errorCode = CUDD_MEMORY_OUT;
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
275 276 277 278 279 280 281
    }
    entry->f = f; entry->g = g;

    /* We do not use the fact that
    ** correlation(f,g') = 1 - correlation(f,g)
    ** to minimize the risk of cancellation.
    */
282
    if ( st__lookup(table, (const char *)entry, (char **)&dummy)) {
283 284 285
        min = *dummy;
        ABC_FREE(entry);
        return(min);
Alan Mishchenko committed
286 287 288 289 290 291 292 293
    }

    G = Cudd_Regular(g);
    topF = cuddI(dd,f->index); topG = cuddI(dd,G->index);
    if (topF <= topG) { Fv = cuddT(f); Fnv = cuddE(f); } else { Fv = Fnv = f; }
    if (topG <= topF) { Gv = cuddT(G); Gnv = cuddE(G); } else { Gv = Gnv = G; }

    if (g != G) {
294 295
        Gv = Cudd_Not(Gv);
        Gnv = Cudd_Not(Gnv);
Alan Mishchenko committed
296 297 298 299
    }

    min1 = bddCorrelationAux(dd, Fv, Gv, table) / 2.0;
    if (min1 == (double)CUDD_OUT_OF_MEM) {
300 301
        ABC_FREE(entry);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
302 303 304
    }
    min2 = bddCorrelationAux(dd, Fnv, Gnv, table) / 2.0; 
    if (min2 == (double)CUDD_OUT_OF_MEM) {
305 306
        ABC_FREE(entry);
        return(CUDD_OUT_OF_MEM);
Alan Mishchenko committed
307 308 309
    }
    min = (min1+min2);
    
Alan Mishchenko committed
310
    pmin = ABC_ALLOC(double,1);
Alan Mishchenko committed
311
    if (pmin == NULL) {
312 313
        dd->errorCode = CUDD_MEMORY_OUT;
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
314 315 316
    }
    *pmin = min;

317
    if ( st__insert(table,(char *)entry, (char *)pmin) == st__OUT_OF_MEM) {
318 319 320
        ABC_FREE(entry);
        ABC_FREE(pmin);
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    }
    return(min);

} /* end of bddCorrelationAux */


/**Function********************************************************************

  Synopsis    [Performs the recursive step of Cudd_bddCorrelationWeigths.]

  Description []

  SideEffects [None]

  SeeAlso     [bddCorrelationAux]

******************************************************************************/
static double
bddCorrelationWeightsAux(
  DdManager * dd,
  DdNode * f,
  DdNode * g,
  double * prob,
344
  st__table * table)
Alan Mishchenko committed
345
{
346 347 348 349
    DdNode      *Fv, *Fnv, *G, *Gv, *Gnv;
    double      min, *pmin, min1, min2, *dummy;
    HashEntry   *entry;
    int         topF, topG, index;
Alan Mishchenko committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363

    statLine(dd);
#ifdef CORREL_STATS
    num_calls++;
#endif

    /* Terminal cases: only work for BDDs. */
    if (f == g) return(1.0);
    if (f == Cudd_Not(g)) return(0.0);

    /* Standardize call using the following properties:
    **     (f EXNOR g)   = (g EXNOR f)
    **     (f' EXNOR g') = (f EXNOR g).
    */
364
    if (cuddF2L(f) > cuddF2L(g)) {
365 366
        DdNode *tmp = f;
        f = g; g = tmp;
Alan Mishchenko committed
367 368
    }
    if (Cudd_IsComplement(f)) {
369 370
        f = Cudd_Not(f);
        g = Cudd_Not(g);
Alan Mishchenko committed
371 372 373
    }
    /* From now on, f is regular. */
    
Alan Mishchenko committed
374
    entry = ABC_ALLOC(HashEntry,1);
Alan Mishchenko committed
375
    if (entry == NULL) {
376 377
        dd->errorCode = CUDD_MEMORY_OUT;
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
378 379 380 381 382 383 384
    }
    entry->f = f; entry->g = g;

    /* We do not use the fact that
    ** correlation(f,g') = 1 - correlation(f,g)
    ** to minimize the risk of cancellation.
    */
385
    if ( st__lookup(table, (const char *)entry, (char **)&dummy)) {
386 387 388
        min = *dummy;
        ABC_FREE(entry);
        return(min);
Alan Mishchenko committed
389 390 391 392 393
    }

    G = Cudd_Regular(g);
    topF = cuddI(dd,f->index); topG = cuddI(dd,G->index);
    if (topF <= topG) {
394 395
        Fv = cuddT(f); Fnv = cuddE(f);
        index = f->index;
Alan Mishchenko committed
396
    } else {
397 398
        Fv = Fnv = f;
        index = G->index;
Alan Mishchenko committed
399 400 401 402
    }
    if (topG <= topF) { Gv = cuddT(G); Gnv = cuddE(G); } else { Gv = Gnv = G; }

    if (g != G) {
403 404
        Gv = Cudd_Not(Gv);
        Gnv = Cudd_Not(Gnv);
Alan Mishchenko committed
405 406 407 408
    }

    min1 = bddCorrelationWeightsAux(dd, Fv, Gv, prob, table) * prob[index];
    if (min1 == (double)CUDD_OUT_OF_MEM) {
409 410
        ABC_FREE(entry);
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
411 412 413
    }
    min2 = bddCorrelationWeightsAux(dd, Fnv, Gnv, prob, table) * (1.0 - prob[index]); 
    if (min2 == (double)CUDD_OUT_OF_MEM) {
414 415
        ABC_FREE(entry);
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
416 417 418
    }
    min = (min1+min2);
    
Alan Mishchenko committed
419
    pmin = ABC_ALLOC(double,1);
Alan Mishchenko committed
420
    if (pmin == NULL) {
421 422
        dd->errorCode = CUDD_MEMORY_OUT;
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
423 424 425
    }
    *pmin = min;

426
    if ( st__insert(table,(char *)entry, (char *)pmin) == st__OUT_OF_MEM) {
427 428 429
        ABC_FREE(entry);
        ABC_FREE(pmin);
        return((double)CUDD_OUT_OF_MEM);
Alan Mishchenko committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
    }
    return(min);

} /* end of bddCorrelationWeightsAux */


/**Function********************************************************************

  Synopsis    [Compares two hash table entries.]

  Description [Compares two hash table entries. Returns 0 if they are
  identical; 1 otherwise.]

  SideEffects [None]

******************************************************************************/
static int
CorrelCompare(
  const char * key1,
  const char * key2)
{
    HashEntry *entry1;
    HashEntry *entry2;

    entry1 = (HashEntry *) key1;
    entry2 = (HashEntry *) key2;
    if (entry1->f != entry2->f || entry1->g != entry2->g) return(1);

    return(0);

} /* end of CorrelCompare */


/**Function********************************************************************

  Synopsis    [Hashes a hash table entry.]

  Description [Hashes a hash table entry. It is patterned after
468
  st__strhash. Returns a value between 0 and modulus.]
Alan Mishchenko committed
469 470 471 472 473 474

  SideEffects [None]

******************************************************************************/
static int
CorrelHash(
475
  const char * key,
Alan Mishchenko committed
476 477
  int  modulus)
{
478
    HashEntry *entry;
Alan Mishchenko committed
479 480
    int val = 0;

481
    entry = (HashEntry *) key;
Alan Mishchenko committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
#if SIZEOF_VOID_P == 8 && SIZEOF_INT == 4
    val = ((int) ((long)entry->f))*997 + ((int) ((long)entry->g));
#else
    val = ((int) entry->f)*997 + ((int) entry->g);
#endif

    return ((val < 0) ? -val : val) % modulus;

} /* end of CorrelHash */


/**Function********************************************************************

  Synopsis    [Frees memory associated with hash table.]

  Description [Frees memory associated with hash table. Returns
498
  st__CONTINUE.]
Alan Mishchenko committed
499 500 501 502

  SideEffects [None]

******************************************************************************/
503
static enum st__retval
Alan Mishchenko committed
504 505 506 507 508
CorrelCleanUp(
  char * key,
  char * value,
  char * arg)
{
509
    double      *d;
Alan Mishchenko committed
510 511 512
    HashEntry *entry;

    entry = (HashEntry *) key;
Alan Mishchenko committed
513
    ABC_FREE(entry);
Alan Mishchenko committed
514
    d = (double *)value;
Alan Mishchenko committed
515
    ABC_FREE(d);
516
    return st__CONTINUE;
Alan Mishchenko committed
517 518 519

} /* end of CorrelCleanUp */

520

521 522
ABC_NAMESPACE_IMPL_END

523