|
From: <tim...@us...> - 2008-02-24 12:12:07
|
Revision: 62
http://sga.svn.sourceforge.net/sga/?rev=62&view=rev
Author: tim-watson
Date: 2008-02-24 04:12:04 -0800 (Sun, 24 Feb 2008)
Log Message:
-----------
Further modularisation of sga.c in tim branch.
Modified Paths:
--------------
branches/tim/code/sga.c
Modified: branches/tim/code/sga.c
===================================================================
--- branches/tim/code/sga.c 2008-02-24 11:57:10 UTC (rev 61)
+++ branches/tim/code/sga.c 2008-02-24 12:12:04 UTC (rev 62)
@@ -95,6 +95,7 @@
void init_pop(void);
void calc_fit(void);
void calc_stats(int, int *, int *, int);
+void next_gen(void);
@@ -225,8 +226,8 @@
/* calculate stats, and display stats and individuals, if required */
-void calc_stats(int gen, int *fconv, int *pconv, int last_gen) {
- int i, j, count[80], pconv_count;
+void calc_stats(int gen, int *fcptr, int *pcptr, int last_gen) {
+ int i, j, count[80], pc_count;
mean = best = worst = fit[0];
for (i=1; i<popsize; i++) {
@@ -241,8 +242,8 @@
for (i=0; i<popsize; i++)
std_dev += (fit[i]-mean)*(fit[i]-mean) / popsize;
std_dev = sqrt(std_dev);
- if ((!*fconv) && (std_dev==0)) /* if fit converged */
- *fconv = gen;
+ if ((!*fcptr) && (std_dev==0)) /* if fit converged */
+ *fcptr = gen;
printf("gen=%d best=%d worst=%d mean=%.2f std.dev.=%.2f\n",
gen, best, worst, mean, std_dev);
for (i=0; i<chromsize; i++) /* init column counts */
@@ -257,35 +258,76 @@
for (; i<popsize; i++)
for (j=0; j<chromsize; j++)
count[j] += curr[i][j] - '0';
- pconv_count = 0;
+ pc_count = 0;
for (i=0; i<chromsize; i++) {
if ((count[i]!=0) && (count[i]!=popsize))
count[i] = count[i]*10/popsize + '0';
else {
count[i] = '-';
- pconv_count++;
+ pc_count++;
}
putchar(count[i]);
}
- if ((!*pconv) && (pconv_count==chromsize)) /* if pop converged */
- *pconv = gen;
+ if ((!*pcptr) && (pc_count==chromsize)) /* if pop converged */
+ *pcptr = gen;
printf("\n--------------------------------------------------\n");
if (last_gen) {
- printf("fitness converged at generation %d\n", *fconv);
- printf("population converged at generation %d\n", *pconv);
+ printf("fitness converged at generation %d\n", *fcptr);
+ printf("population converged at generation %d\n", *pcptr);
printf("--------------------------------------------------\n");
}
}
+
+/* produce next generation and make it the current generation */
+void next_gen() {
+ int i, j, k, choice;
+ for (i=1; i<popsize; i++) /* calculate proportions */
+ fit[i] += fit[i-1];
+
+ for (i=0; i<popsize; i++) { /* reproduce */
+ choice = RNG(fit[popsize-1]) + 1;
+ j=0;
+ while (choice > fit[j])
+ j++;
+ for (k=0; k<chromsize; k++)
+ next[i][k] = curr[j][k];
+ }
+
+ for (i=0; i<popsize; i+=2) /* crossover */
+ if (crossrate > RNG(100)) {
+ choice = RNG(chromsize) + 1;
+ for (j=0; j<choice; j++) {
+ curr[i][j] = next[i][j];
+ curr[i+1][j] = next[i+1][j];
+ }
+ for (; j<chromsize; j++) {
+ curr[i][j] = next[i+1][j];
+ curr[i+1][j] = next[i][j];
+ }
+ } else
+ for (j=0; j<chromsize; j++) {
+ curr[i][j] = next[i][j];
+ curr[i+1][j] = next[i+1][j];
+ }
+
+ if (muterate) /* mutate */
+ for (i=0; i<popsize; i++)
+ for (j=0; j<chromsize; j++)
+ if (RNG(muterate) == 0)
+ curr[i][j] ^= 1; /* XOR the lsb to swap '0' and '1' */
+}
+
+
+
int main(int argc, char *argv[]) {
- int i, j, k, gen, choice, *fconv, *pconv;
+ int gen;
+ /* fc and pc store the gen when fitness and pop converge */
+ int fc=0, pc=0, *fcptr=&fc, *pcptr=&pc;
- /* fitness and population convergence initially set at gen 0 */
- *fconv = *pconv = 0;
-
accept_options(argc, argv);
display_params();
init_prng();
@@ -295,49 +337,12 @@
for (gen=1; gen!=gens; gen++) {
calc_fit();
if (((gen-1)%displfreq) == 0)
- calc_stats(gen, fconv, pconv, 0);
-
- /* produce next generation and make it the current generation */
- for (i=1; i<popsize; i++) /* calculate proportions */
- fit[i] += fit[i-1];
-
- for (i=0; i<popsize; i++) { /* reproduce */
- choice = RNG(fit[popsize-1]) + 1;
- j=0;
- while (choice > fit[j])
- j++;
- for (k=0; k<chromsize; k++)
- next[i][k] = curr[j][k];
- }
-
- for (i=0; i<popsize; i+=2) /* crossover */
- if (crossrate > RNG(100)) {
- choice = RNG(chromsize) + 1;
- for (j=0; j<choice; j++) {
- curr[i][j] = next[i][j];
- curr[i+1][j] = next[i+1][j];
- }
- for (; j<chromsize; j++) {
- curr[i][j] = next[i+1][j];
- curr[i+1][j] = next[i][j];
- }
- } else
- for (j=0; j<chromsize; j++) {
- curr[i][j] = next[i][j];
- curr[i+1][j] = next[i+1][j];
- }
-
- if (muterate) /* mutate */
- for (i=0; i<popsize; i++)
- for (j=0; j<chromsize; j++)
- if (RNG(muterate) == 0)
- curr[i][j] ^= 1; /* XOR the lsb to swap '0' and '1' */
-
- /* end of main loop of genetic algorithm */
+ calc_stats(gen, fcptr, pcptr, 0);
+ next_gen();
}
calc_fit();
- calc_stats(gen, fconv, pconv, 1);
+ calc_stats(gen, fcptr, pcptr, 1);
#ifndef NOGSL
gsl_rng_free(prng);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|