void Cross() { int i, j, l, loc, temp; int NCross = 0; for (i=0; i<MaxPop; i++) /* for each individual in the population */ { if (FRandom(1.0) > ProbCross) continue; /* skip unless within prob'y */ NCross++; do j = IRandom(MaxPop); while (j == i); /* select random mate (not with itself) */ loc = IRandom(CodeSize - 2) + 1; /* cross-over location 1 to size-1 */ for (l=loc; l<CodeSize; l++) /* for loci at loc and beyond, exchange */ { temp = Pop[i].Code[l]; Pop[i].Code[l] = Pop[j].Code[l]; Pop[j].Code[l] = temp; } } if (NTop) /* print unless NTop is 0 */ printf( "\n There were %d crossovers. \n",NCross); }
/* reproduce the next generation based on survivablility */ void Repro() { int i, j; float Cut; /* the point in total goodness where individual is selected */ float Tot; /* cumulative goodness */ for (i=0; i<MaxPop; i++) /* for each individual in the new population */ { Cut = FRandom(TotGood); for (j=0, Tot=0; j<MaxPop; j++) /* search old ones to locate cut */ { Tot += Pop[j].Good; if (Cut < Tot) { NewPop[i] = Pop[j]; /* select individual for new group */ break; } NewPop[i] = Pop[MaxPop]; /* only if cut bigger than total ??? */ } } for (i=0; i<MaxPop; i++) /* copy each individual in the population */ Pop[i] = NewPop[i]; }/* function to handle cross-overs in the current generation */
/* ------------------------------------------------------------ a sample fitness function for the 2-armed bandit problem ------------------------------------------------------------ */ float fitness(int arm) { float prob[2] = {0.25,0.75}; if (FRandom(1.0) < prob[arm]) return 1.0; else return 0.0; }
void S_Chicken::Think( ) { S_BaseMobile::Think(); SetMovingForward( true ); // do chicken stuff SetNextThink( FRandom( 0.5f, 1.0f ) ); }
//Creates a heightmap from your seed. void MAPGEN::create(float fractalfactor, int seed){ srand(seed); int w=msize,h=msize; int i, j, k; float amount = 256;//, l; // Do the corners map[0][0] = 0; //FRandom(amount); map[w-1][0] = 0; //FRandom(amount); map[w-1][h-1] = 0; //FRandom(amount); map[0][h-1] = 0; //FRandom(amount); amount /= fractalfactor; for (i = 128; i > 0; i /= 2){ // This is the square phase for (j = i; j < w; j += 2 * i) for (k = i; k < h; k += 2 * i){ map[j][k] = (map[Minus(j, i)][Minus(k, i)] + map[Minus(j, i)][Plus(k, i)] + map[Plus(j, i)][Plus(k, i)] + map[Plus(j, i)][Minus(k, i)]) / 4. + FRandom(amount); } amount /= fractalfactor; // This is the diamond phase for (j = 0; j < w; j += i) for (k = 0; k < h; k += i) if ((((j + k) / i) % 2) == 1){ // In this phase we might fall off the edge // when we count the average of neighbours // Minus and Plus take care of that map[j][k] = (map[Minus(j, i)][k] + map[j][Minus(k, i)] + map[Plus(j, i)][k] + map[j][Plus(k, i)]) / 4 + FRandom(amount); } amount /= fractalfactor; } }
/* perform mutations for this generation */ void Mutate() { int i, l; int NMut = 0; for (i=0; i<MaxPop; i++) /* for each individual in the population */ { if (FRandom(1.0) > ProbMut) continue; /* skip unless within prob'y */ NMut++; l = IRandom(CodeSize); /* select locus to mutate */ Pop[i].Code[l] = (Pop[i].Code[l] +IRandom(CodeSchema[l]-2)+1)%CodeSchema[l]; /* different value */ } if (NTop) /* print unless NTop is 0 */ printf( "\n There were %d mutations. \n", NMut); }