void saved_individual_gc(void) { int j; saved_ind *shp = saved_head->next; saved_ind *shm = saved_head; while (shp) { if (shp->refcount == 0) { /** found one that needs to be deleted. **/ /* dereference its trees' ERCs and delete the trees. */ for (j = 0; j < tree_count; ++j) { reference_ephem_constants(shp->ind->tr[j].data, -1); free_tree(shp->ind->tr + j); } FREE(shp->ind->tr); FREE(shp->ind); /* cut the record out of the linked list. */ shm->next = shp->next; if (saved_tail == shp) saved_tail = shm; FREE(shp); /* bug fix, [email protected] */ shp = shm->next; /* the refcount field of the list head (a dummy node) holds the size of the list. */ --saved_head->refcount; } else { /* move down the list. */ shm = shp; shp = shp->next; } } }
//HJJA population *change_population ( population *oldpop, breedphase *bp ) { population *newpop; int i, j; int numphases; double totalrate = 0.0; double r, r2; int prob_oper = atoi ( get_parameter ( "probabilistic_operators" ) ); /* allocate the new population. */ newpop = allocate_population ( oldpop->size ); globaldata* g = get_globaldata();//get the lists if(g->bUseHFC){ if( IsHighestActiveLevel(g->current_population)){ /*reproduce best guys into the new population. */ for(i=0;i<run_stats[0].bestn;i++){ //printf("best %d ind\n", i); duplicate_individual ( (newpop->ind)+newpop->next, run_stats[0].best[i]->ind); newpop->ind[newpop->next].flags = FLAG_NONE; ++(newpop->next); //HJJS structure niching: mutation //Assumption: we think any structure mutation will create a new structure //for numeric mutation, we won't change the structure CStruct* pSt; int structID = newpop->ind[newpop->next].structID; pSt = g->pStructHash->find(structID); if(pSt==NULL){ //make sure,though redudency code pSt = new CStruct(); pSt->structID = ++(g->currMaxStructID); pSt->age=1; pSt->lastGen=g->current_generation+1; pSt->nIndividuals=0; pSt->nIndNextGen=1; newpop->ind[newpop->next].structID=pSt->structID; g->pStructHash->insert(pSt); } else{ if(pSt->lastGen != g->current_generation+1){//the first individual in this generation pSt->age++; pSt->lastGen = g->current_generation+1; } pSt->nIndNextGen++;//increase the ind no of this population } //HJJS end } } //End HJJ_ELITISM }//End useHFC /* the first element of the breedphase table is a dummy -- its operator field stores the number of phases. */ numphases = bp[0].operatorID; /* call the start method for each phase. */ for ( i = 1; i <= numphases; ++i ) { totalrate += bp[i].rate; if ( bp[i].operator_start ) bp[i].operator_start ( oldpop, bp[i].data ); } /* now fill the new population. */ while ( newpop->next < newpop->size ) { /** select an operator, either stochastically or not depending on the probabilistic_operators parameter. **/ if ( prob_oper ) r = totalrate * random_double(); else r = totalrate * ((double)newpop->next/(double)newpop->size); r2 = bp[1].rate; for ( i = 1; r2 < r; ) r2 += bp[++i].rate; #ifdef DEBUG fprintf ( stderr, "picked %10.3lf; operator %d\n", r, i ); #endif /* call the phase's method to do the operation. */ if ( bp[i].operator_operate ){ bp[i].operator_operate ( oldpop, newpop, bp[i].data ); } } /* call each phase's method to do cleanup. */ for ( i = 1; i <= numphases; ++i ) { if ( bp[i].operator_end ) bp[i].operator_end ( bp[i].data ); } /* mark all the ERCs referenced in the new population. */ for ( i = 0; i < newpop->size; ++i ) for ( j = 0; j < tree_count; ++j ) reference_ephem_constants ( newpop->ind[i].tr[j].data, 1 ); /* free the old population. */ free_population ( oldpop ); //Bug when do reproduction ? return ( newpop ); }
void calculate_pop_stats(popstats *s, population *pop, int gen, int subpop) { int i, j, k, l; int b; saved_ind *shp; individual **temp; /* allocate a list of the top N individuals. */ s->best = (saved_ind **) MALLOC(s->bestn * sizeof(saved_ind *)); temp = (individual **) MALLOC((s->bestn + 1) * sizeof(individual *)); s->size = pop->size; /** this is all pretty obvious -- set all the max and min values to the first individual's values, then go through the population looking for things that are bigger/smaller/better/worse/etc. **/ s->maxnodes = s->minnodes = s->totalnodes = s->bestnodes = s->worstnodes = individual_size(pop->ind + 0); s->maxdepth = s->mindepth = s->totaldepth = s->bestdepth = s->worstdepth = individual_depth(pop->ind + 0); s->maxhits = s->minhits = s->totalhits = s->besthits = s->worsthits = pop->ind[0].hits; s->bestfit = s->worstfit = s->totalfit = pop->ind[0].a_fitness; temp[0] = pop->ind; b = 1; s->bestgen = s->worstgen = gen; s->bestpop = s->worstpop = subpop; for (i = 1; i < s->size; ++i) { j = individual_size(pop->ind + i); s->totalnodes += j; if (j < s->minnodes) s->minnodes = j; if (j > s->maxnodes) s->maxnodes = j; k = individual_depth(pop->ind + i); s->totaldepth += k; if (k < s->mindepth) s->mindepth = k; if (k > s->maxdepth) s->maxdepth = k; l = pop->ind[i].hits; s->totalhits += l; if (l < s->minhits) s->minhits = l; if (l > s->maxhits) s->maxhits = l; s->totalfit += pop->ind[i].a_fitness; if (pop->ind[i].a_fitness > s->bestfit) { s->bestfit = pop->ind[i].a_fitness; s->bestnodes = j; s->bestdepth = k; s->besthits = l; } else if (pop->ind[i].a_fitness < s->worstfit) { s->worstfit = pop->ind[i].a_fitness; s->worstnodes = j; s->worstdepth = k; s->worsthits = l; } /** insert the current individual into the top N list (if it belongs there). **/ for (j = b; j > 0; --j) { if (pop->ind[i].a_fitness < temp[j - 1]->a_fitness) break; temp[j] = temp[j - 1]; } if (j < s->bestn) temp[j] = pop->ind + i; if (b < s->bestn) ++b; } /** now save copies of the individuals in the "temp" list **/ for (i = 0; i < b; ++i) { shp = (saved_ind *) MALLOC(sizeof(saved_ind)); shp->ind = (individual *) MALLOC(sizeof(individual)); shp->ind->tr = (tree *) MALLOC(tree_count * sizeof(tree)); duplicate_individual(shp->ind, temp[i]); for (j = 0; j < tree_count; ++j) reference_ephem_constants(shp->ind->tr[j].data, 1); shp->refcount = 1; shp->next = NULL; saved_tail->next = shp; saved_tail = shp; ++saved_head->refcount; s->best[i] = shp; } #ifdef DEBUG printf ( "the best list is:\n" ); for ( j = 0; j < s->bestn; ++j ) printf ( " %08x %lf\n", s->best[j], s->best[j]->ind->a_fitness ); #endif FREE(temp); }