📄 exch.c
字号:
} } #ifdef DEBUG /* print out information on this exchange. */ printf ( "exchange %d:\n", i+1 ); printf ( "to: %d; count: %d; select: %s\n", mpop->exch[i].to, mpop->exch[i].count, mpop->exch[i].tosc ); if ( mpop->exch[i].copywhole == -1 ) { for ( j = 0; j < tree_count; ++j ) { param = mpop->exch[i].fromsc[j]; printf ( " %3d: from: %3d as: %3d select: %s\n", j, mpop->exch[i].from[j], mpop->exch[i].as[j], param==NULL?"NULL":param ); } } else { param = mpop->exch[i].fromsc[0]; printf ( "copywhole: %d select: %s\n", mpop->exch[i].copywhole, param==NULL?"NULL":param ); }#endif } /* if any errors occurred then stop now. */ if ( errors ) error ( E_FATAL_ERROR, "Errors occurred while building topology. Aborting." ); /* print out the summary of exchanges. */ oprintf ( OUT_SYS, 30, " %d exchange(s) total.\n", mpop->exchanges ); for ( i = 0; i < mpop->exchanges; ++i ) { oprintf ( OUT_SYS, 30, " exchange %d:\n", i+1 ); oprintf ( OUT_SYS, 30, " replace %d individual(s) in subpop %d (selected by %s)\n", mpop->exch[i].count, mpop->exch[i].to+1, mpop->exch[i].tosc ); if ( mpop->exch[i].copywhole != -1 ) oprintf ( OUT_SYS, 30, " with individual(s) from subpop %d (selected by %s)\n", mpop->exch[i].copywhole+1, mpop->exch[i].fromsc[0] ); else for ( j = 0; j < tree_count; ++j ) { if ( mpop->exch[i].from[j] == -1 ) { if ( mpop->exch[i].as[j] == -1 ) oprintf ( OUT_SYS, 30, " tree %d: leaving original tree\n", j ); else oprintf ( OUT_SYS, 30, " tree %d: from same individual as tree %d\n", j, mpop->exch[i].as[j] ); } else oprintf ( OUT_SYS, 30, " tree %d: from subpop %d (selected by %s)\n", j, mpop->exch[i].from[j]+1, mpop->exch[i].fromsc[j] ); } }}/* free_topology() * * this frees the topology table. */void free_topology ( multipop *mpop ){ int i; for ( i = 0; i < mpop->exchanges; ++i ) { if ( mpop->exch[i].from ) FREE ( mpop->exch[i].from ); if ( mpop->exch[i].as ) FREE ( mpop->exch[i].as ); FREE ( mpop->exch[i].fromsc ); } if ( mpop->exch ) FREE ( mpop->exch );}/* exchange_subpopulations() * * this performs the actual exchanges, using the information stored * in the exchange table. */void exchange_subpopulations ( multipop *mpop ){ int i, j, k; sel_context *tocon; sel_context **fromcon; select_context_func_ptr select_con; int tp, *fp; int ti, *fi; /** arrays used for composite individuals. **/ /* fromcon[j] holds the selection context used to pick individual to take tree j from. */ fromcon = (sel_context **)MALLOC ( tree_count * sizeof ( sel_context * ) ); /* fp[j] holds the population from which to take tree j from. */ fp = (int *)MALLOC ( tree_count * sizeof ( int ) ); /* fi[j] holds the individual from which to take tree j from. */ fi = (int *)MALLOC ( tree_count * sizeof ( int ) ); for ( i = 0; i < mpop->exchanges; ++i ) {#ifdef DEBUG printf ( "working on exch[%d]\n", i+1 );#endif /* where individuals are going. */ tp = mpop->exch[i].to; /* set up selection method to pick individuals to be replaced. */ select_con = get_select_context ( mpop->exch[i].tosc ); tocon = select_con ( SELECT_INIT, NULL, mpop->pop[tp], mpop->exch[i].tosc ); /* are we copying whole individuals or creating composites? */ if ( mpop->exch[i].copywhole > -1 ) { /*** copying whole individuals. ***/ /* the source subpop. */ fp[0] = mpop->exch[i].copywhole; /* selection method for choosing individuals from source subpop. */ select_con = get_select_context ( mpop->exch[i].fromsc[0] ); fromcon[0] = select_con ( SELECT_INIT, NULL, mpop->pop[fp[0]], mpop->exch[i].fromsc[0] ); for ( k = 0; k < mpop->exch[i].count; ++k ) { /** pick an individual to be replaced that has not already been replaced during this exchange cycle. **/ do { ti = tocon->select_method ( tocon ); } while ( mpop->pop[tp]->ind[ti].flags & FLAG_NEWEXCH ); /* pick an individual from the source subpop. */ fi[0] = fromcon[0]->select_method ( fromcon[0] ); #ifdef DEBUG printf ( "COPYING WHOLE INDIVIDUAL: ind %d subpop %d --> ind %d subpop %d\n", fi[0], fp[0], ti, tp );#endif /** remove the old iondividual from the population. **/ for ( j = 0; j < tree_count; ++j ) { /* always dereference ERCs when removing trees from the population. */ reference_ephem_constants ( mpop->pop[tp]->ind[ti].tr[j].data, -1 ); free_tree ( mpop->pop[tp]->ind[ti].tr+j ); } /* copy the individual. */ duplicate_individual ( mpop->pop[tp]->ind+ti, mpop->pop[fp[0]]->ind+fi[0] ); /* reference the ERCs in the new individual. */ for ( j = 0; j < tree_count; ++j ) reference_ephem_constants ( mpop->pop[tp]->ind[ti].tr[j].data, 1 ); /* mark the individual as just coming from an exchange. */ mpop->pop[tp]->ind[ti].flags = FLAG_NEWEXCH; } /* all done with this exchange, delete the selection context. */ fromcon[0]->context_method ( SELECT_CLEAN, fromcon[0], NULL, NULL ); } else { /*** creating composite individuals. ***/ /** create selection contexts for each tree. **/ for ( j = 0; j < tree_count; ++j ) { /* does this tree need a context? */ if ( mpop->exch[i].fromsc[j] ) {#ifdef DEBUG printf ( "getting selection context for tree %d (%s)\n", j, mpop->exch[i].fromsc[j] );#endif /* create it. */ select_con = get_select_context ( mpop->exch[i].fromsc[j] ); fromcon[j] = select_con ( SELECT_INIT, NULL, mpop->pop[mpop->exch[i].from[j]], mpop->exch[i].fromsc[j] ); } else /* don't need one. */ fromcon[j] = NULL; } for ( k = 0; k < mpop->exch[i].count; ++k ) { /** select an individual to be replaced that hasn't already been during this exchange cycle. **/ do { ti = tocon->select_method ( tocon ); } while ( mpop->pop[tp]->ind[ti].flags & FLAG_NEWEXCH ); #ifdef DEBUG printf ( "SELECTED TREE %d FOR REPLACEMENT.\n", ti ); print_individual ( mpop->pop[tp]->ind+ti, stdout );#endif /** now select the individuals that we will merge to replace trees of the destination individual. **/ for ( j = 0; j < tree_count; ++j ) { /* we don't need to do a selection for a particular tree if (1) it uses the same individual as another tree or (2) it doesn't get replaced in the destination individual. */ fp[j] = mpop->exch[i].from[j]; if ( fp[j] != -1 ) { fi[j] = fromcon[fp[j]]->select_method ( fromcon[fp[j]] );#ifdef DEBUG printf ( "selecting using (%s) from subpop %d (for tree %d): individual %d\n", mpop->exch[i].fromsc[j], fp[j], j, fi[j] ); print_individual ( mpop->pop[fp[j]]->ind+fi[j], stdout );#endif } } /** now resolve "as_" references in the fp and fi arrays. */ for ( j = 0; j < tree_count; ++j ) if ( fp[j] == -1 ) { if ( mpop->exch[i].as[j] == -1 ) /* tree j doesn't get replaced, so set both values to -1. */ fp[j] = fi[j] = -1; else { /* tree j comes from the same individual as some other tree. */ fp[j] = fp[mpop->exch[i].as[j]]; fi[j] = fi[mpop->exch[i].as[j]]; } }#ifdef DEBUG printf ( "the fp,fi arrays are:\n" ); for ( j = 0; j < tree_count; ++j ) printf ( " %3d: fp = %3d fi = %4d\n", j, fp[j], fi[j] );#endif /** replace the appropriate parts of the old tree. **/ for ( j = 0; j < tree_count; ++j ) { /* skip trees that don't get replaced. */ if ( fp[j] == -1 ) continue; /* dereference ERCs in old tree. */ reference_ephem_constants ( mpop->pop[tp]->ind[ti].tr[j].data, -1 ); /* delete old tree. */ free_tree ( mpop->pop[tp]->ind[ti].tr+j ); /* copy new tree. */ copy_tree ( mpop->pop[tp]->ind[ti].tr+j, mpop->pop[fp[j]]->ind[fi[j]].tr+j ); /* reference ERCs in new tree. */ reference_ephem_constants ( mpop->pop[tp]->ind[ti].tr[j].data, 1 ); } /* evaluate the fitness of the new composite individual. */ app_eval_fitness ( mpop->pop[tp]->ind+ti ); mpop->pop[tp]->ind[ti].flags = FLAG_NEWEXCH;#ifdef DEBUG printf ( "the new individual is:\n" ); print_individual ( mpop->pop[tp]->ind+ti, stdout );#endif } /* destroy source selection contexts. */ for ( j = 0; j < tree_count; ++j ) if ( fromcon[j] ) fromcon[j]->context_method ( SELECT_CLEAN, fromcon[j], NULL, NULL ); } /* destroy destination selection context. */ tocon->context_method ( SELECT_CLEAN, tocon, NULL, NULL ); } FREE ( fromcon ); FREE ( fp ); FREE ( fi ); /* erase all the NEWEXCH flags. */ for ( i = 0; i < mpop->size; ++i ) for ( j = 0; j < mpop->pop[i]->size; ++j ) mpop->pop[i]->ind[j].flags &= ~FLAG_NEWEXCH; } /* rebuild_exchange_topology() * * rebuilds the exchange table. called from user code after making changes * to the parameters governing exchanges. */void rebuild_exchange_topology ( multipop *mpop ){ free_topology ( mpop ); initialize_topology ( mpop );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -