__swift_uint32_t _swift_stdlib_cxx11_mt19937_uniform(__swift_uint32_t upper_bound) { if (upper_bound > 0) upper_bound--; std::uniform_int_distribution<__swift_uint32_t> RandomUniform(0, upper_bound); return RandomUniform(getGlobalMT19937()); }
void DESolver::SelectSamples( int candidate, int *r1, int *r2, int *r3, int *r4, int *r5 ) { if (r1) { do { *r1 = (int)RandomUniform(0.0, (double)nPop); } while (*r1 == candidate); } if (r2) { do { *r2 = (int)RandomUniform(0.0, (double)nPop); } while ((*r2 == candidate) || (*r2 == *r1)); } if (r3) { do { *r3 = (int)RandomUniform(0.0, (double)nPop); } while ((*r3 == candidate) || (*r3 == *r2) || (*r3 == *r1)); } if (r4) { do { *r4 = (int)RandomUniform(0.0, (double)nPop); } while ((*r4 == candidate) || (*r4 == *r3) || (*r4 == *r2) || (*r4 == *r1)); } if (r5) { do { *r5 = (int)RandomUniform(0.0, (double)nPop); } while ((*r5 == candidate) || (*r5 == *r4) || (*r5 == *r3) || (*r5 == *r2) || (*r5 == *r1)); } }
/* ALGORITHM 712, COLLECTED ALGORITHMS FROM ACM. THIS WORK PUBLISHED IN TRANSACTIONS ON MATHEMATICAL SOFTWARE, VOL. 18, NO. 4, DECEMBER, 1992, PP. 434-435. The function returns a normally distributed pseudo-random number with a given mean and standard devaiation. Calls are made to a function subprogram which must return independent random numbers uniform in the interval (0,1). The algorithm uses the ratio of uniforms method of A.J. Kinderman and J.F. Monahan augmented with quadratic bounding curves. */ double RandomGaussian(double mean,double stddev) { double q,u,v,x,y; /* Generate P = (u,v) uniform in rect. enclosing acceptance region Make sure that any random numbers <= 0 are rejected, since gaussian() requires uniforms > 0, but RandomUniform() delivers >= 0. */ do { u = RandomUniform(); v = RandomUniform(); if (u <= 0.0 || v <= 0.0) { u = 1.0; v = 1.0; } v = 1.7156 * (v - 0.5); /* Evaluate the quadratic form */ x = u - 0.449871; y = fabs(v) + 0.386595; q = x * x + y * (0.19600 * y - 0.25472 * x); /* Accept P if inside inner ellipse */ if (q < 0.27597) break; /* Reject P if outside outer ellipse, or outside acceptance region */ } while ((q > 0.27846) || (v * v > -4.0 * log(u) * u * u)); /* Return ratio of P's coordinates as the normal deviate */ return (mean + stddev * v / u); }
void DESolver::Setup( double *min, double *max, int deStrategy, double diffScale, double crossoverProb, double ftol, unsigned long rngSeed ) { int i; strategy = deStrategy; scale = diffScale; probability = crossoverProb; tolerance = ftol; // PE: seed the (Mersenne Twister) RNG if (rngSeed > 0) init_genrand(rngSeed); else init_genrand((unsigned long)time((time_t *)NULL)); CopyVector(minBounds, min); CopyVector(maxBounds, max); for (i = 0; i < nPop; i++) { for (int j = 0; j < nDim; j++) Element(population,i,j) = RandomUniform(min[j], max[j]); popEnergy[i] = 1.0E20; } for (i = 0; i < nDim; i++) bestSolution[i] = 0.0; }
int sample(Cdf* cdf) { double r = RandomUniform(); for (int j = 0; j < cdf->n; j++) if (r < cdf->vals[j]) return cdf->ids[j]; }
void DESolver::RandToBest1Exp( int candidate ) { int r1, r2; int n; SelectSamples(candidate, &r1, &r2); n = (int)RandomUniform(0.0, (double)nDim); CopyVector(trialSolution, RowVector(population, candidate)); for (int i = 0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++) { trialSolution[n] += scale * (bestSolution[n] - trialSolution[n]) + scale * (Element(population,r1,n) - Element(population,r2,n)); n = (n + 1) % nDim; } return; }
void DESolver::Rand2Bin( int candidate ) { int r1, r2, r3, r4, r5; int n; SelectSamples(candidate, &r1, &r2, &r3, &r4, &r5); n = (int)RandomUniform(0.0, (double)nDim); CopyVector(trialSolution, RowVector(population, candidate)); for (int i = 0; i < nDim; i++) { if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1))) trialSolution[n] = Element(population,r1,n) + scale * (Element(population,r2,n) + Element(population,r3,n) - Element(population,r4,n) - Element(population,r5,n)); n = (n + 1) % nDim; } }
void DESolver::Best1Bin( int candidate ) { int r1, r2; int n; SelectSamples(candidate, &r1, &r2); n = (int)RandomUniform(0.0, (double)nDim); CopyVector(trialSolution, RowVector(population, candidate)); for (int i = 0; i < nDim; i++) { if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1))) trialSolution[n] = bestSolution[n] + scale * (Element(population,r1,n) - Element(population,r2,n)); n = (n + 1) % nDim; } return; }
/* Return random float within a range, lower -> upper */ double RandomDouble(double lower,double upper) { return((upper - lower) * RandomUniform() + lower); }
/* Return random integer within a range, lower -> upper INCLUSIVE */ int RandomInt(int lower,int upper) { return((int)(RandomUniform() * (upper - lower + 1)) + lower); }
int DESolver::Solve( int maxGenerations, int verbose ) { int generation; int candidate; bool bAtSolution; double relativeDeltas[3] = {100.0, 100.0, 100.0}; double lastBestEnergy; bestEnergy = 1.0E20; lastBestEnergy = bestEnergy; bAtSolution = false; for (generation = 0; (generation < maxGenerations) && !bAtSolution; generation++) { for (candidate = 0; candidate < nPop; candidate++) { // modified by PE //(this->*calcTrialSolution)(candidate); CalcTrialSolution(candidate); // trialSolution now contains a newly generated parameter vector // check for out-of-bounds values and generate random values w/in the bounds CopyVector(oldValues, RowVector(population, candidate)); // oldValues is guaranteed to lie between minBounds and maxBounds for (int j = 0; j < nDim; j++) { if (trialSolution[j] < minBounds[j]) trialSolution[j] = minBounds[j] + RandomUniform(0.0,1.0)*(oldValues[j] - minBounds[j]); if (trialSolution[j] > maxBounds[j]) trialSolution[j] = maxBounds[j] - RandomUniform(0.0,1.0)*(maxBounds[j] - oldValues[j]); } // Test our newly mutated/bred trial parameter vector trialEnergy = EnergyFunction(trialSolution, bAtSolution); if (trialEnergy < popEnergy[candidate]) { // New low for this candidate popEnergy[candidate] = trialEnergy; CopyVector(RowVector(population,candidate), trialSolution); // Check if all-time low if (trialEnergy < bestEnergy) { bestEnergy = trialEnergy; CopyVector(bestSolution, trialSolution); } } } // Debugging printout code added by PE -- print an update every 10 generations double relativeDeltaEnergy = 0.0; if ((generation % 10) == 0) { if (verbose > 0) printf("\nGeneration %4d: bestEnergy = %12.10f", generation, bestEnergy); if (generation == 20) { relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy); relativeDeltas[0] = relativeDeltaEnergy; if (verbose > 0) printf(" (relative change = %e)", relativeDeltaEnergy); } else if (generation == 30) { relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy); relativeDeltas[1] = relativeDeltas[0]; relativeDeltas[0] = relativeDeltaEnergy; if (verbose > 0) printf(" (relative change = %e)", relativeDeltaEnergy); } else if (generation >= 40) { relativeDeltaEnergy = fabs(1.0 - lastBestEnergy/bestEnergy); relativeDeltas[2] = relativeDeltas[1]; relativeDeltas[1] = relativeDeltas[0]; relativeDeltas[0] = relativeDeltaEnergy; if (verbose > 0) printf(" (relative change = %e)", relativeDeltaEnergy); if (TestConverged(relativeDeltas, tolerance)) { generations = generation; bAtSolution = true; return 1; } } lastBestEnergy = bestEnergy; } if (isnan(bestEnergy)) { // fprintf(stderr, "\n*** NaN-valued fit statistic detected (DE optimization)!\n"); printf("\n\tcandidate %d, bestEnergy = %f\n", candidate, bestEnergy); } } generations = generation; return 5; }
void DESolver::Setup(double *min,double *max, int deStrategy,double diffScale,double crossoverProb) { int i; strategy = deStrategy; scale = diffScale; probability = crossoverProb; for (i=0; i < nPop; i++) { for (int j=0; j < nDim; j++) Element(population,i,j) = RandomUniform(min[j],max[j]); popEnergy[i] = 1.0E20; } for (i=0; i < nDim; i++) bestSolution[i] = 0.0; switch (strategy) { case 0: calcTrialSolution = &DESolver::Best1Exp; break; case 1: calcTrialSolution = &DESolver::Rand1Exp; break; case stRandToBest1Exp: calcTrialSolution = &DESolver::RandToBest1Exp; break; case stBest2Exp: calcTrialSolution = &DESolver::Best2Exp; break; case stRand2Exp: calcTrialSolution = &DESolver::Rand2Exp; break; case stBest1Bin: calcTrialSolution = &DESolver::Best1Bin; break; case stRand1Bin: calcTrialSolution = &DESolver::Rand1Bin; break; case stRandToBest1Bin: calcTrialSolution = &DESolver::RandToBest1Bin; break; case stBest2Bin: calcTrialSolution = &DESolver::Best2Bin; break; case stRand2Bin: calcTrialSolution = &DESolver::Rand2Bin; break; } return; }