📄 gp.c
字号:
run_stats[0].bestnodes, run_stats[0].bestdepth ); oprintf ( OUT_GEN, 90, " worst: nodes: %d; depth: %d\n", run_stats[0].worstnodes, run_stats[0].worstdepth ); } if ( test_detail_level ( 90 ) ) { oprintf ( OUT_PRG, 90, " total population:\n" ); oprintf ( OUT_PRG, 90, " generation stats:\n" ); oprintf ( OUT_PRG, 90, " mean: hits: %.3lf (%d-%d); standardized fitness: %.*lf\n", (double)gen_stats[0].totalhits/gen_stats[0].size, gen_stats[0].minhits, gen_stats[0].maxhits, fd, (double)gen_stats[0].totalfit/gen_stats[0].size ); oprintf ( OUT_PRG, 90, " best: hits: %d; standardized fitness: %.*lf\n", gen_stats[0].besthits, fd, (double)gen_stats[0].bestfit ); oprintf ( OUT_PRG, 90, " worst: hits: %d; standardized fitness: %.*lf\n", gen_stats[0].worsthits, fd, (double)gen_stats[0].worstfit ); oprintf ( OUT_PRG, 90, " run stats: (%d trees)\n", run_stats[0].size ); oprintf ( OUT_PRG, 90, " mean: hits: %.3lf (%d-%d); standardized fitness: %.*lf\n", (double)run_stats[0].totalhits/run_stats[0].size, run_stats[0].minhits, run_stats[0].maxhits, fd, (double)run_stats[0].totalfit/run_stats[0].size ); oprintf ( OUT_PRG, 90, " best: hits: %d; standardized fitness: %.*lf; generation: %d\n", run_stats[0].besthits, fd, (double)run_stats[0].bestfit, run_stats[0].bestgen ); oprintf ( OUT_PRG, 90, " worst: hits: %d; standardized fitness: %.*lf; generation: %d\n", run_stats[0].worsthits, fd, (double)run_stats[0].worstfit, run_stats[0].worstgen ); } if ( gen%stt_interval == 0 ) { if ( test_detail_level ( 50 ) ) { oprintf ( OUT_STT, 50, "%d 0 ", gen ); oprintf ( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd, gen_stats[0].totalfit/gen_stats[0].size, fd, gen_stats[0].bestfit, fd, gen_stats[0].worstfit ); oprintf ( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ", (double)gen_stats[0].totalnodes/gen_stats[0].size, (double)gen_stats[0].totaldepth/gen_stats[0].size, gen_stats[0].bestnodes, gen_stats[0].bestdepth, gen_stats[0].worstnodes, gen_stats[0].worstdepth ); oprintf ( OUT_STT, 50, "%.*lf %.*lf %.*lf ", fd, run_stats[0].totalfit/run_stats[0].size, fd, run_stats[0].bestfit, fd, run_stats[0].worstfit ); oprintf ( OUT_STT, 50, "%.3lf %.3lf %d %d %d %d ", (double)run_stats[0].totalnodes/run_stats[0].size, (double)run_stats[0].totaldepth/run_stats[0].size, run_stats[0].bestnodes, run_stats[0].bestdepth, run_stats[0].worstnodes, run_stats[0].worstdepth ); oprintf ( OUT_STT, 50, "\n" ); } } /* rewrite the .bst file, and append to the .his file. */ output_stream_open ( OUT_BST ); oprintf ( OUT_BST, 10, "=== BEST-OF-RUN ===\n" ); oprintf ( OUT_BST, 10, " generation: %d\n", run_stats[0].bestgen ); if ( mpop->size > 1 ) oprintf ( OUT_BST, 10, " subpopulation: %d\n", run_stats[0].bestpop+1 ); oprintf ( OUT_BST, 10, " nodes: %d\n", run_stats[0].bestnodes ); oprintf ( OUT_BST, 10, " depth: %d\n", run_stats[0].bestdepth ); oprintf ( OUT_BST, 10, " hits: %d\n", run_stats[0].besthits ); oprintf ( OUT_HIS, 10, "=== BEST-OF-RUN ===\n" ); oprintf ( OUT_HIS, 10, " current generation: %d\n", gen ); oprintf ( OUT_HIS, 10, " generation: %d\n", run_stats[0].bestgen ); if ( mpop->size > 1 ) oprintf ( OUT_HIS, 10, " subpopulation: %d\n", run_stats[0].bestpop+1 ); oprintf ( OUT_HIS, 10, " nodes: %d\n", run_stats[0].bestnodes ); oprintf ( OUT_HIS, 10, " depth: %d\n", run_stats[0].bestdepth ); oprintf ( OUT_HIS, 10, " hits: %d\n", run_stats[0].besthits ); /* retrieve the (FILE *) for the .bst and .his files, so that the trees can be printed to them. */ bout = output_filehandle ( OUT_BST ); hout = output_filehandle ( OUT_HIS ); if ( run_stats[0].bestn == 1 ) { oprintf ( OUT_BST, 20, "TOP INDIVIDUAL:\n\n" ); oprintf ( OUT_HIS, 20, "TOP INDIVIDUAL:\n\n" ); } else { oprintf ( OUT_BST, 20, "TOP %d INDIVIDUALS (in order):\n\n", run_stats[0].bestn ); oprintf ( OUT_HIS, 20, "TOP %d INDIVIDUALS (in order):\n\n", run_stats[0].bestn ); } for ( i = 0; i < run_stats[0].bestn; ++i ) { oprintf ( OUT_BST, 20, "\n\n-- #%d --\n", i+1 ); oprintf ( OUT_BST, 20, " hits: %d\n", run_stats[0].best[i]->ind->hits ); oprintf ( OUT_BST, 20, " raw fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->r_fitness ); oprintf ( OUT_BST, 20, " standardized fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->s_fitness ); oprintf ( OUT_BST, 20, " adjusted fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->a_fitness ); oprintf ( OUT_HIS, 20, "\n\n-- #%d --\n", i+1 ); oprintf ( OUT_HIS, 20, " hits: %d\n", run_stats[0].best[i]->ind->hits ); oprintf ( OUT_HIS, 20, " raw fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->r_fitness ); oprintf ( OUT_HIS, 20, " standardized fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->s_fitness ); oprintf ( OUT_HIS, 20, " adjusted fitness: %.*lf\n", fd, run_stats[0].best[i]->ind->a_fitness ); /* print the tree to both files here. */ if ( test_detail_level ( 20 ) ) { pretty_print_individual ( run_stats[0].best[i]->ind, bout ); pretty_print_individual ( run_stats[0].best[i]->ind, hout ); } } /* call the end-of-evaluation callback. returns 1 if user termination criterion is met, 0 otherwise. */ ret = app_end_of_evaluation ( gen, mpop, newbest, gen_stats, run_stats ); /* close the .bst file. */ output_stream_close ( OUT_BST ); /* free stats structures for current generation. */ for ( i = 0; i < mpop->size+1; ++i ) { for ( j = 0; j < gen_stats[i].bestn; ++j ) --gen_stats[i].best[j]->refcount; FREE ( gen_stats[i].best ); } FREE ( gen_stats ); /* deallocate saved individuals that are no longer needed. */ saved_individual_gc(); /* return value the application callback gave us. */ return ret;}/* evaluate_pop() * * evaluates all the individuals in a population whose cached * fitness values are invalid. */void evaluate_pop ( population *pop ){ int i;#if defined(POSIX_MT) || defined(SOLARIS_MT) int start, end, inc, err; struct thread_param_t *t_param;#endif#if POSIX_MT pthread_t *t_ids;#endif#ifdef DEBUG print_individual ( pop->ind, stdout ); app_eval_fitness ( pop->ind ); exit(0);#endif #if !defined(POSIX_MT) && !defined(SOLARIS_MT) for ( i = 0; i < pop->size; ++i ) if ( pop->ind[i].evald != EVAL_CACHE_VALID ) app_eval_fitness ( (pop->ind)+i );#else#if POSIX_MT t_ids = (pthread_t *)malloc( numthreads * sizeof( pthread_t ));#endif t_param = (struct thread_param_t *)malloc( numthreads * sizeof( struct thread_param_t )); /* figure out how many pop members per thread */ inc = pop->size / numthreads; if (pop->size != inc * numthreads) inc++; start = 0; for (i=0; i<numthreads; i++) { end = start + inc; if (end > pop->size) end = pop->size; /* setup the paramater to pass */ t_param[i].pop = pop; t_param[i].startidx = start; t_param[i].endidx = end; t_param[i].g = *(get_globaldata());#ifdef POSIX_MT err = pthread_create(&t_ids[i], &pthread_attr, evaluate_pop_chunk, &t_param[i]);#endif#ifdef SOLARIS_MT err = thr_create(NULL, (int)NULL, evaluate_pop_chunk, &t_param[i], (int)NULL,NULL);#endif if (err != 0) { error ( E_FATAL_ERROR, "cannot create thread"); } start = end; }#ifdef SOLARIS_MT while (thr_join(NULL, NULL, NULL) == 0);#endif#ifdef POSIX_MT for (i=0; i < numthreads; i++) { pthread_join( t_ids[i], NULL); }#endif#endif}/* calculate_pop_stats() * * tabulates stats for a population: fitness and size of best, worst, * mean, etc. also finds top N individuals and saves them. */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 ); }/* accumulate_pop_stats() * * this merges the second statistics record into the first, so that it reflects * the "sum" of the underlying populations. returns 1 if the best individual * of the first record has changed (that is, if the second record has a better * best individual. */int accumulate_pop_stats ( popstats *total, popstats *n ){ int ret = 0; int i, j, k; saved_ind **temp; if ( total->size == -1 ) { /* if the "total" record is empty, then just copy the second record into it. */ memcpy ( total, n, sizeof ( popstats ) ); total->best = (saved_ind **)MALLOC ( total->bestn * sizeof ( saved_ind * ) ); memcpy ( total->best, n->best, total->bestn * sizeof ( saved_ind * ) ); ret = 1; } else { /* sum the totals. */ total->size += n->size; total->totalnodes += n->totalnodes; total->totaldepth += n->totaldepth; total->totalhits += n->totalhits; total->totalfit += n->totalfit; /* find the maximums. */ if ( n->maxnodes > total->maxnodes ) total->maxnodes = n->maxnodes; if ( n->maxdepth > total->maxdepth ) total->maxdepth = n->maxdepth; if ( n->maxhits > total->maxhits ) total->maxhits = n->maxhits; /* find the minimums. */ if ( n->minnodes < total->minnodes ) total->minnodes = n->minnodes; if ( n->mindepth < total->mindepth ) total->mindepth = n->mindepth; if ( n->minhits < total->minhits ) total->minhits = n->minhits; /* find the best individual's numbers. */ if ( n->bestfit > total->bestfit ) { total->bestfit = n->bestfit; total->bestnodes = n->bestnodes; total->bestdepth = n->bestdepth; total->besthits = n->besthits; total->bestgen = n->bestgen; total->bestpop = n->bestpop; ret = 1; } /* find the worst individual's numbers. */ if ( n->worstfit < total->worstfit ) { total->worstfit = n->worstfit; total->worstnodes = n->worstnodes; total->worstdepth = n->worstdepth; total->worsthits = n->worsthits; total->worstgen = n->worstgen; total->worstpop = n->worstpop; }#ifdef DEBUG printf ( "total list:\n" ); for ( i = 0; i < total->bestn; ++i ) printf ( " %08x %lf\n", total->best[i], total->best[i]->ind->a_fitness ); printf ( "new list:\n" ); for ( i = 0; i < n->bestn; ++i ) printf ( " %08x %lf\n",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -