static void all5s_mutate(population *pop, entity *father, entity *son) { int i; /* Loop variable over all chromosomes */ int point; /* Index of allele to mutate */ int dir=random_boolean()?-1:1; /* The direction of drift. */ /* Sanity check. */ if (!father || !son) die("Null pointer to entity structure passed"); /* Select mutation locus. */ point = random_int(pop->len_chromosomes); /* Copy genome from father. */ for (i=0; i<pop->len_chromosomes; i++) ((int *)son->chromosome[0])[i] = ((int *)father->chromosome[0])[i]; /* * Mutate by tweaking a single allele. */ ((int *)son->chromosome[0])[point] += dir; if (((int *)son->chromosome[0])[point]>10) ((int *)son->chromosome[0])[point]=0; else if (((int *)son->chromosome[0])[point]<0) ((int *)son->chromosome[0])[point]=10; return; }
float particle_float(float fv) { float f; if (fv==0.0f) return(0.0f); f=random_float(fv); if (random_boolean()) return(-f); return(f); }
int particle_int(int iv) { int i; if (iv==0) return(0); i=random_int(iv); if (random_boolean()) return(-i); return(i); }
void pingpong_crossover(population *pop, entity *mother, entity *father, entity *daughter, entity *son) { int i, j; /* Team members. */ for (i=0; i<25; i++) { if (random_boolean()) { ((int *)son->chromosome[0])[i] = ((int *)father->chromosome[0])[i]; ((int *)daughter->chromosome[0])[i] = ((int *)mother->chromosome[0])[i]; } else { ((int *)son->chromosome[0])[i] = ((int *)father->chromosome[0])[i]; ((int *)daughter->chromosome[0])[i] = ((int *)mother->chromosome[0])[i]; } } for (i=1; i<25; i++) { for (j=0; j<i; j++) { if (((int *)son->chromosome[0])[j] == ((int *)son->chromosome[0])[i]) { if (((int *)son->chromosome[0])[i]==24) ((int *)son->chromosome[0])[i]=0; else ((int *)son->chromosome[0])[i]++; j=0; } } for (j=0; j<i; j++) { if (((int *)daughter->chromosome[0])[j] == ((int *)daughter->chromosome[0])[i]) { if (((int *)daughter->chromosome[0])[i]==24) ((int *)daughter->chromosome[0])[i]=0; else ((int *)daughter->chromosome[0])[i]++; j=0; } } } return; }
boolean wildfire_seed(population *pop, entity *adam) { int i, j, k; /* Map square. */ if (random_boolean()) { for(i=0; i<WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION; i++) { ((int *)adam->chromosome[0])[i] = random_boolean_prob((double)WILDFIRE_CISTERNS/ ((double)WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION)); } } else { for(i=0; i<WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION; i++) { ((int *)adam->chromosome[0])[i] = 0; } /* Deliberately places slightly fewer cisterns than allowed. */ i = random_int(WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION); j = random_int(WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION); k = WILDFIRE_CISTERNS; while (k>0) { i = (i+j)%(WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION); if (((int *)adam->chromosome[0])[i] == 1) { j = random_int(WILDFIRE_X_DIMENSION*WILDFIRE_Y_DIMENSION); } else { ((int *)adam->chromosome[0])[i] = 1; } k--; } } return TRUE; }
GAULFUNC boolean random_test(void) { unsigned int i, j, k; /* Loop variables. */ double r; /* Pseudo-random number. */ long bins[NUM_BINS]; /* Bin. */ double sum=0,sumsq=0; /* Stats. */ int numtrue=0, numfalse=0; /* Count booleans. */ unsigned int rchi=100; /* Number of bins in chisq test. */ unsigned int nchi=1000; /* Number of samples in chisq test. */ double chisq; /* Chisq error. */ double elimit = 2*sqrt((double)rchi); /* Chisq error limit. */ double nchi_rchi = (double)nchi / (double)rchi; /* Speed calculation. */ FILE *rfile=NULL; /* Handle for file of random integers. */ random_init(); printf("Testing random numbers.\n"); /* * Uniform Distribution. */ printf("Uniform distribution. Mean should be about 0.5.\n"); for (i=0;i<NUM_BINS;i++) bins[i] = 0; for (i=0;i<NUM_SAMPLES;i++) { r = random_unit_uniform(); if (r >= 0.0 && r < 1.0) { bins[(int)(r*NUM_BINS)]++; sum += r; sumsq += SQU(r); } else { printf("Number generated out of range 0.0<=r<1.0.\n"); } } printf("Mean = %f\n", sum / NUM_SAMPLES); printf("Standard deviation = %f\n", (sumsq - sum*sum/NUM_SAMPLES)/NUM_SAMPLES); for (i=0;i<NUM_BINS;i++) printf("%5.3f %ld\n", i/(double)NUM_BINS, bins[i]); /* * Gaussian Distribution. */ printf("Gaussian distribution. Mean should be about 0.45. Standard deviation should be about 0.05.\n"); sum=0; sumsq=0; for (i=0;i<NUM_BINS;i++) bins[i] = 0; for (i=0;i<NUM_SAMPLES;i++) { r = random_gaussian(0.45,0.05); if (r >= 0.0 && r < 1.0) { bins[(int)(r*NUM_BINS)]++; sum += r; sumsq += SQU(r); } else { printf("Number generated out of range 0.0<=r<1.0.\n"); } } printf("Mean = %f\n", sum / NUM_SAMPLES); printf("Standard deviation = %f\n", (sumsq - sum*sum/NUM_SAMPLES)/NUM_SAMPLES); for (i=0;i<NUM_BINS;i++) printf("%5.3f %ld\n", i/(double)NUM_BINS, bins[i]); /* * Unit Gaussian Distribution. */ printf("Gaussian distribution. Mean should be about 0.0. Standard deviation should be about 1.0.\n"); sum=0; sumsq=0; for (i=0;i<NUM_BINS;i++) bins[i] = 0; for (i=0;i<NUM_SAMPLES;i++) { r = random_unit_gaussian(); if (r >= -5.0 && r < 5.0) { bins[(int)((r+5.0)*NUM_BINS)/10]++; sum += r; sumsq += SQU(r); } else { printf("Number generated out of range -5.0<=r<5.0.\n"); } } printf("Mean = %f\n", sum / NUM_SAMPLES); printf("Standard deviation = %f\n", (sumsq - sum*sum/NUM_SAMPLES)/NUM_SAMPLES); for (i=0;i<NUM_BINS;i++) printf("%5.3f %ld\n", -5.0+10*i/(double)NUM_BINS, bins[i]); /* * Random Boolean. */ printf("Random Booleans. Two counts should be approximately equal.\n"); for (i=0;i<NUM_SAMPLES;i++) { if ( random_boolean() ) numtrue++; else numfalse++; } printf("TRUE/FALSE = %d/%d\n", numtrue, numfalse); /* * Random int. */ printf("Random Integers. The distribution should be approximately uniform.\n"); for (i=0;i<NUM_BINS;i++) bins[i] = 0; for (i=0;i<NUM_SAMPLES;i++) bins[random_int(NUM_BINS)]++; for (i=0;i<NUM_BINS;i++) printf("%u %ld\n", i, bins[i]); /* * Chi squared test. This is the standard basic test for randomness of a PRNG. * We would expect any moethod to fail about about one out of ten runs. * The error is r*t/N - N and should be within 2*sqrt(r) of r. */ printf("Chi Squared Test of Random Integers. We would expect a couple of failures.\n"); if (rchi>NUM_BINS) die("Internal error: too few bins."); for (j=0;j<NUM_CHISQ;j++) { printf("Run %u. chisq should be within %f of %u.\n", j, elimit, rchi); for(k=0; k<10; k++) { memset(bins, 0, rchi*sizeof(long)); chisq = 0.0; for(i=0; i<nchi; i++) bins[random_int(rchi)]++; for(i=0; i<rchi; i++) chisq += SQU((double)bins[i] - nchi_rchi); chisq /= nchi_rchi; printf("chisq = %f - %s\n", chisq, fabs(chisq - rchi)>elimit?"FAILED":"PASSED"); } } printf("Creating file (\"randtest.dat\") of 5000 random integer numbers for external analysis.\n"); rfile = fopen("randtest.dat", "w"); for (i=0; i<5000; i++) { r = (double) random_rand(); fprintf(rfile, "%f %f\n", /*i, r,*/ (double)i/(double)5000, r/(double)RANDOM_RAND_MAX); } fclose(rfile); return TRUE; }
void wildfire_crossover( population *pop, entity *mother, entity *father, entity *daughter, entity *son ) { int i, j; /* Loop over map squares. */ int split; /* Crossover locus. */ /* Checks. */ if (!mother || !father) die("Null pointer to entity structure passed."); if (!daughter || !son) die("Null pointer to entity structure passed."); if (random_boolean()) { /* Vertical split. */ split = random_int(WILDFIRE_X_DIMENSION); for (i=0; i<=split; i++) { for (j=0; j<WILDFIRE_Y_DIMENSION; j++) { ((int *)son->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)mother->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; ((int *)daughter->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)father->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; } } for (; i<WILDFIRE_X_DIMENSION; i++) { for (j=0; j<WILDFIRE_Y_DIMENSION; j++) { ((int *)daughter->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)mother->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; ((int *)son->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)father->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; } } } else { /* Horizontal split. */ split = random_int(WILDFIRE_Y_DIMENSION); for (j=0; j<=split; j++) { for (i=0; i<WILDFIRE_X_DIMENSION; i++) { ((int *)son->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)mother->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; ((int *)daughter->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)father->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; } } for (; j<WILDFIRE_Y_DIMENSION; j++) { for (i=0; i<WILDFIRE_X_DIMENSION; i++) { ((int *)daughter->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)mother->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; ((int *)son->chromosome[0])[j*WILDFIRE_X_DIMENSION+i] = ((int *)father->chromosome[0])[j*WILDFIRE_X_DIMENSION+i]; } } } return; }