abc/src/bdd/cudd/cuddGenetic.c
/**Function********************************************************************

  Synopsis [Performs the crossover between two parents.]

  Description [Performs the crossover between two randomly chosen
  parents, and creates two children, x1 and x2. Uses the Partially
  Matched Crossover operator.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
Function
Synopsis [Performs the crossover between two parents.]

Description [Performs the crossover between two randomly chosen
parents, and creates two children, x1 and x2. Uses the Partially
Matched Crossover operator.]




Function Synopsis [ Performs the crossover between two parents . ] Description [ Performs the crossover between two randomly chosen parents and creates two children x1 and x2 . Uses the Partially Matched Crossover operator . ]
static int
PMX(
  int  maxvar)
{
    int         cut1,cut2;      
    int         mom,dad;        
    int         *inv1;          
    int         *inv2;
    int         i;              
    int         u,v;            
    inv1 = ABC_ALLOC(int,maxvar);
    if (inv1 == NULL) {
        return(0);
    }
    inv2 = ABC_ALLOC(int,maxvar);
    if (inv2 == NULL) {
        ABC_FREE(inv1);
        return(0);
    }
    if (!roulette(&mom,&dad)) {
        ABC_FREE(inv1);
        ABC_FREE(inv2);
        return(0);
    }
    cut1 = rand_int(numvars-1);
    do {
        cut2 = rand_int(numvars-1);
    } while (cut1 == cut2);
#if 0
    (void) fprintf(table->out,
                   "Crossover of %d (mom) and %d (dad) between %d and %d\n",
                   mom,dad,cut1,cut2);
    for (i = 0; i < numvars; i++) {
        if (i == cut1 || i == cut2) (void) fprintf(table->out,"|");
        (void) fprintf(table->out,"%2d ",STOREDD(mom,i));
    }
    (void) fprintf(table->out,"\n");
    for (i = 0; i < numvars; i++) {
        if (i == cut1 || i == cut2) (void) fprintf(table->out,"|");
        (void) fprintf(table->out,"%2d ",STOREDD(dad,i));
    }
    (void) fprintf(table->out,"\n");
#endif
    for (i = 0; i < maxvar; i++) {
        inv1[i] = -1;
        inv2[i] = -1;
    }
    for (i = cut1; i != cut2; i = (i == numvars-1) ? 0 : i+1) {
        STOREDD(popsize,i) = STOREDD(dad,i);
        inv1[STOREDD(popsize,i)] = i;
        STOREDD(popsize+1,i) = STOREDD(mom,i);
        inv2[STOREDD(popsize+1,i)] = i;
    }
    for (i = cut2; i != cut1; i = (i == numvars-1 ) ? 0 : i+1) {
        v = i;
        do {
            u = STOREDD(mom,v);
            v = inv1[u];
        } while (v != -1);
        STOREDD(popsize,i) = u;
        inv1[u] = i;
        v = i;
        do {
            u = STOREDD(dad,v);
            v = inv2[u];
        } while (v != -1);
        STOREDD(popsize+1,i) = u;
        inv2[u] = i;
    }
#if 0
    for (i = 0; i < numvars; i++) {
        if (i == cut1 || i == cut2) (void) fprintf(table->out,"|");
        (void) fprintf(table->out,"%2d ",STOREDD(popsize,i));
    }
    (void) fprintf(table->out,"\n");
    for (i = 0; i < numvars; i++) {
        if (i == cut1 || i == cut2) (void) fprintf(table->out,"|");
        (void) fprintf(table->out,"%2d ",STOREDD(popsize+1,i));
    }
    (void) fprintf(table->out,"\n");
#endif
    ABC_FREE(inv1);
    ABC_FREE(inv2);
    return(1);
}
static int PMX ( int maxvar ) { int cut1 , cut2 ; int mom , dad ; int * inv1 ; int * inv2 ; int i ; int u , v ; inv1 = ABC_ALLOC ( int , maxvar ) ; if ( inv1 == NULL ) { return ( 0 ) ; } inv2 = ABC_ALLOC ( int , maxvar ) ; if ( inv2 == NULL ) { ABC_FREE ( inv1 ) ; return ( 0 ) ; } if ( ! roulette ( & mom , & dad ) ) { ABC_FREE ( inv1 ) ; ABC_FREE ( inv2 ) ; return ( 0 ) ; } cut1 = rand_int ( numvars - 1 ) ; do { cut2 = rand_int ( numvars - 1 ) ; } while ( cut1 == cut2 ) ; #if 0 ( void ) fprintf ( table -> out , " \n " , mom , dad , cut1 , cut2 ) ; for ( i = 0 ; i < numvars ; i ++ ) { if ( i == cut1 || i == cut2 ) ( void ) fprintf ( table -> out , " " ) ; ( void ) fprintf ( table -> out , " " , STOREDD ( mom , i ) ) ; } ( void ) fprintf ( table -> out , " \n " ) ; for ( i = 0 ; i < numvars ; i ++ ) { if ( i == cut1 || i == cut2 ) ( void ) fprintf ( table -> out , " " ) ; ( void ) fprintf ( table -> out , " " , STOREDD ( dad , i ) ) ; } ( void ) fprintf ( table -> out , " \n " ) ; #endif for ( i = 0 ; i < maxvar ; i ++ ) { inv1 [ i ] = -1 ; inv2 [ i ] = -1 ; } for ( i = cut1 ; i != cut2 ; i = ( i == numvars - 1 ) ? 0 : i + 1 ) { STOREDD ( popsize , i ) = STOREDD ( dad , i ) ; inv1 [ STOREDD ( popsize , i ) ] = i ; STOREDD ( popsize + 1 , i ) = STOREDD ( mom , i ) ; inv2 [ STOREDD ( popsize + 1 , i ) ] = i ; } for ( i = cut2 ; i != cut1 ; i = ( i == numvars - 1 ) ? 0 : i + 1 ) { v = i ; do { u = STOREDD ( mom , v ) ; v = inv1 [ u ] ; } while ( v != -1 ) ; STOREDD ( popsize , i ) = u ; inv1 [ u ] = i ; v = i ; do { u = STOREDD ( dad , v ) ; v = inv2 [ u ] ; } while ( v != -1 ) ; STOREDD ( popsize + 1 , i ) = u ; inv2 [ u ] = i ; } #if 0 for ( i = 0 ; i < numvars ; i ++ ) { if ( i == cut1 || i == cut2 ) ( void ) fprintf ( table -> out , " " ) ; ( void ) fprintf ( table -> out , " " , STOREDD ( popsize , i ) ) ; } ( void ) fprintf ( table -> out , " \n " ) ; for ( i = 0 ; i < numvars ; i ++ ) { if ( i == cut1 || i == cut2 ) ( void ) fprintf ( table -> out , " " ) ; ( void ) fprintf ( table -> out , " " , STOREDD ( popsize + 1 , i ) ) ; } ( void ) fprintf ( table -> out , " \n " ) ; #endif ABC_FREE ( inv1 ) ; ABC_FREE ( inv2 ) ; return ( 1 ) ; }
Function
Synopsis [Performs the crossover between two parents.]
Function Synopsis [ Performs the crossover between two parents . ]
/* the two cut positions (random) */ /* the two randomly chosen parents */ /* inverse permutations for repair algo */ /* loop vars */ /* aux vars */ /* Choose two orders from the population using roulette wheel. */ /* Choose two random cut positions. A cut in position i means that
    ** the cut immediately precedes position i.  If cut1 < cut2, we
    ** exchange the middle of the two orderings; otherwise, we
    ** exchange the beginnings and the ends.
    */ /* Print out the parents. */ /* Initialize the inverse permutations: -1 means yet undetermined. */ /* Copy the portions whithin the cuts. */ /* Now apply the repair algorithm outside the cuts. */ /* Print the results of crossover. */