void annealing(TSP *tsp) { Path p; int i, j, pathchg; int numOnPath, numNotOnPath; DTYPE pathlen; int n = tsp->n; double energyChange, T; pathlen = pathLength (tsp); for (T = T_INIT; T > FINAL_T; T *= COOLING) /* annealing schedule */ { pathchg = 0; for (j = 0; j < TRIES_PER_T; j++) { do { p[0] = unifRand (n); p[1] = unifRand (n); /* non-empty path */ if (p[0] == p[1]) p[1] = MOD(p[0]+1,n); numOnPath = MOD(p[1]-p[0],n) + 1; numNotOnPath = n - numOnPath; } while (numOnPath < 2 || numNotOnPath < 2); /* non-empty path */ if (RANDOM() % 2) /* threeWay */ { do { p[2] = MOD(unifRand (numNotOnPath)+p[1]+1,n); } while (p[0] == MOD(p[2]+1,n)); /* avoids a non-change */ energyChange = getThreeWayCost (tsp, p); if (energyChange < 0 || RREAL < exp(-energyChange/T) ) { pathchg++; pathlen += energyChange; doThreeWay (tsp, p); } } else /* path Reverse */ { energyChange = getReverseCost (tsp, p); if (energyChange < 0 || RREAL < exp(-energyChange/T)) { pathchg++; pathlen += energyChange; doReverse(tsp, p); } } // if the new length is better than best then save it as best if (pathlen < tsp->bestlen) { tsp->bestlen = pathlen; for (i=0; i<tsp->n; i++) tsp->border[i] = tsp->iorder[i]; } if (pathchg > IMPROVED_PATH_PER_T) break; /* finish early */ } DBG("T:%f L:%f B:%f C:%d", T, pathlen, tsp->bestlen, pathchg); if (pathchg == 0) break; /* if no change then quit */ } }
double RandomNetwork::selectEntry(int i, int j, double p){ if (i != j){ if (unifRand()<=(1-p)){return 0.0;} else {return unifRand();} } else {return 0.0;} }
void makeRandomVoxBot(int xDim, int yDim, int zDim, int nMat, std::vector<int> &structure, VoxBotCreator &creator) { // declare structure array // initialization, empty voxels int*** strArr = new int**[xDim]; for(int x = 0; x < xDim; ++x){strArr[x] = new int*[yDim];for(int y = 0; y < yDim; ++y){strArr[x][y] = new int[zDim]; for(int z = 0; z < yDim; ++z){strArr[x][y][z] = 0;}}} // holds every voxel that it is set to a non-zero value for(int x = 0; x < xDim; ++x) { for(int y = 0; y < yDim; ++y) { for(int z = 0; z < yDim; ++z) { strArr[x][y][z] = (unifRand() < PROB_ADD); if(strArr[x][y][z] == 1) { strArr[x][y][z] += unifRand(creator.GetNumMaterials() - 1) -1; } } } } // set the created structure in the std::vector. std::vector<int> structureTmp; for (int x1 = 0; x1 < xDim; ++x1){for (int y1 = 0; y1 < yDim; ++y1){for (int z1 = 0; z1 < zDim; ++z1){structureTmp.push_back(strArr[x1][y1][z1]);}}} structure = makeOneShapeOnly(structureTmp); // free up memory for (int x2 = 0; x2 < xDim; ++x2) {for (int y2 = 0; y2 < yDim; ++y2)delete [] strArr[x2][y2]; delete [] strArr[x2];}delete [] strArr; }
void pcl::RandomSample<sensor_msgs::PointCloud2>::applyFilter (std::vector<int> &indices) { rng_->base ().seed (seed_); unsigned N = input_->width * input_->height; // If sample size is 0 or if the sample size is greater then input cloud size // then return all indices if (sample_ >= N) { indices = *indices_; } else { // Resize output indices to sample size indices.resize (sample_); // Algorithm A float one_over_N = 0.f; float top = 0.f; size_t index = 0; std::vector<bool> added; if (extract_removed_indices_) added.resize (indices_->size (), false); size_t i = 0; for (size_t n = sample_; n > 1; n--) { top = N - n; // N are the remaining number of elements, n the remaining number of wanted samples one_over_N = 1.f / static_cast<float> (N); //we need to re-calculate N^{-1} float V = unifRand (); size_t S = 0; float quot = top * one_over_N; while (quot > V) { S ++; N --; quot = quot * (top * one_over_N); } N--; // this together with N-- above is the same than N - S - 1 (paper Vit84) index += S; if (extract_removed_indices_) added[index] = true; indices[i] = (*indices_)[index]; i ++; index ++; } index += static_cast<size_t> (N * unifRand ()); if (extract_removed_indices_) added[index] = true; indices[i] = (*indices_)[index]; } }
void pcl::RandomSample<pcl::PCLPointCloud2>::applyFilter (PCLPointCloud2 &output) { unsigned N = input_->width * input_->height; // If sample size is 0 or if the sample size is greater then input cloud size // then return entire copy of cloud if (sample_ >= N) { output = *input_; } else { // Resize output cloud to sample size output.data.resize (sample_ * input_->point_step); // Copy the common fields output.fields = input_->fields; output.is_bigendian = input_->is_bigendian; output.row_step = input_->row_step; output.point_step = input_->point_step; output.height = 1; // Set random seed so derived indices are the same each time the filter runs std::srand (seed_); unsigned top = N - sample_; unsigned i = 0; unsigned index = 0; // Algorithm A for (size_t n = sample_; n >= 2; n--) { float V = unifRand (); unsigned S = 0; float quot = float (top) / float (N); while (quot > V) { S++; top--; N--; quot = quot * float (top) / float (N); } index += S; memcpy (&output.data[i++ * output.point_step], &input_->data[index++ * output.point_step], output.point_step); N--; } index += N * static_cast<unsigned> (unifRand ()); memcpy (&output.data[i++ * output.point_step], &input_->data[index++ * output.point_step], output.point_step); output.width = sample_; output.row_step = output.point_step * output.width; } }
void pcl::RandomSample<pcl::PCLPointCloud2>::applyFilter (std::vector<int> &indices) { unsigned N = input_->width * input_->height; // If sample size is 0 or if the sample size is greater then input cloud size // then return all indices if (sample_ >= N) { indices = *indices_; } else { // Resize output indices to sample size indices.resize (sample_); // Set random seed so derived indices are the same each time the filter runs std::srand (seed_); unsigned top = N - sample_; unsigned i = 0; unsigned index = 0; // Algorithm A for (size_t n = sample_; n >= 2; n--) { float V = unifRand (); unsigned S = 0; float quot = float (top) / float (N); while (quot > V) { S++; top--; N--; quot = quot * float (top) / float (N); } index += S; indices[i++] = (*indices_)[index++]; N--; } index += N * static_cast<unsigned> (unifRand ()); indices[i++] = (*indices_)[index++]; } }
int dist_iniz(long double x[], long double x0[], long double R,unsigned int num_dist){ switch (num_dist) { case 2: x[0] = x0[0]+R*sqrt(unifRand())*cos(2*pi*unifRand()); x[1] = x0[1]+R*sqrt(unifRand())*sin(2*pi*unifRand()); return 1; break; case 3: x[0] = x0[0]-R+2*R*unifRand(); x[1] = x0[1]; return 1; break; case 4: x[1] = x0[1]-R+2*R*unifRand(); x[0] = x0[0]; return 1; break; default: x[0]=x0[0]; x[1]=x0[1]; return 0; break; } return 0; }
void CUDAANNMP::generation(bool print = false){ p2.copy(p); int r1,r2,r3; double ran; double nnMSE; int i,j,k; int v; #pragma omp parallel for private(v,r1,r2,r3,ran,nnMSE,i,j,k) for( v = 0 ; v < maxPopulation ; v++){ do{ r1 = unifRandInt (0,maxPopulation-1); r2 = unifRandInt (0,maxPopulation-1); r3 = unifRandInt (0,maxPopulation-1); }while(r1 == r2 || r1 == r3 || r2 == r3 || r1 == v || r2 == v || r3 == v); for( i = 1 ; i < nbLayers ; i++){ for( j = 0 ; j < neuronPerLayer[i] ; j++){ for( k = 0 ; k < neuronPerLayer[i-1]+1 ; k++){ ran = unifRand(); if(ran < crossRate){ p.pop[v]->W[i][j][k] = p2.pop[r1]->W[i][j][k] + (mutRate * (p2.pop[r3]->W[i][j][k] - p2.pop[r2]->W[i][j][k])); }else{ p.pop[v]->W[i][j][k] = p2.pop[v]->W[i][j][k]; } } } } p.pop[v]->MSE( getFitness (p.pop[v]) ); if(p.pop[v]->MSE() > p2.pop[v]->MSE() ){ for( i = 1 ; i < nbLayers ; i++){ for( j = 0 ; j < neuronPerLayer[i] ; j++){ for( k = 0 ; k < neuronPerLayer[i-1]+1 ; k++){ p.pop[v]->W[i][j][k] = p2.pop[v]->W[i][j][k]; } } } p.pop[v]->MSE(p2.pop[v]->MSE()); }//if(fitnessTrialIndividual < p.pop[v]->MSE() ) }//End for(int i = 0 ; i < maxPopulation ; i++) s.stat(p); if(print==true){ if(printBestChromosome==true){ p.pop[0]->print(); //nn[0].print(); } } vectorFitness.push_back (p.pop[0]->MSE()); }//end for
std::vector<T> unifRandVector(T start, T stop, int num) { std::vector<T> ret(num); std::generate_n(ret.begin(), num, [&]() {return unifRand(start, stop);}); return ret; }
T unifRand(T start, T stop) { return static_cast<T>((stop - start) * unifRand()) + start; }
/**@brief Get a vector of doubles in [0,1) * * @param num The number of random numbers to generate * @return A vector of double in [0,1) */ std::vector<double> unifRandVector(uint32_t num) { std::vector<double> ret(num); std::generate(ret.begin(), ret.end(), [&]() { return unifRand();}); return ret; }
/**@brief Calls unifRand() so you don't have to type the whole thing * * @return a double in [0,1) */ double operator()() { return unifRand(); }
void pcl::RandomSample<sensor_msgs::PointCloud2>::applyFilter (PointCloud2 &output) { rng_->base ().seed (seed_); unsigned N = input_->width * input_->height; // If sample size is 0 or if the sample size is greater then input cloud size // then return entire copy of cloud if (sample_ >= N) { output = *input_; } else { // Resize output cloud to sample size output.data.resize (sample_ * input_->point_step); // Copy the common fields output.fields = input_->fields; output.is_bigendian = input_->is_bigendian; output.row_step = input_->row_step; output.point_step = input_->point_step; output.height = 1; // Algorithm A float one_over_N = 0.f; float top = 0.f; size_t index = 0; std::vector<bool> added; if (extract_removed_indices_) added.resize (indices_->size (), false); size_t i = 0; for (size_t n = sample_; n > 1; n--) { top = N - n; // N are the remaining number of elements, n the remaining number of wanted samples one_over_N = 1.f / static_cast<float> (N); //we need to re-calculate N^{-1} float V = unifRand (); size_t S = 0; float quot = top * one_over_N; while (quot > V) { S ++; N --; quot = quot * (top * one_over_N); } N--; // this together with N-- above is the same than N - S - 1 (paper Vit84) index += S; memcpy (&output.data[i * output.point_step], &input_->data[index * output.point_step], output.point_step); i ++; index ++; } index += static_cast<size_t> (N * unifRand ()); memcpy (&output.data[i * output.point_step], &input_->data[index * output.point_step], output.point_step); } output.width = sample_; output.row_step = output.point_step * output.width; }
double P2Uniform() { return unifRand(arguments.a, arguments.b); }
// seed function void seed(){ srand(time(0)); for (int i = 0; i < 10; ++i) unifRand();}
//returns a uniform integer number from 0 to n int unifRand(int n){ if (n < 0) n = -n; if (n==0) return 0; int guard = (int) (unifRand() * n) +1; return (guard > n)? n : guard; }
void pcl::RandomSample<PointT>::applyFilter (std::vector<int> &indices) { unsigned N = static_cast<unsigned> (indices_->size ()); float one_over_N = 1.0f / float (N); unsigned int sample_size = negative_ ? N - sample_ : sample_; // If sample size is 0 or if the sample size is greater then input cloud size // then return all indices if (sample_size >= N) { indices = *indices_; removed_indices_->clear (); } else { // Resize output indices to sample size indices.resize (static_cast<size_t> (sample_size)); if (extract_removed_indices_) removed_indices_->resize (static_cast<size_t> (N - sample_size)); // Set random seed so derived indices are the same each time the filter runs std::srand (seed_); // Algorithm A unsigned top = N - sample_size; unsigned i = 0; unsigned index = 0; std::vector<bool> added; if (extract_removed_indices_) added.resize (indices_->size (), false); for (size_t n = sample_size; n >= 2; n--) { unsigned int V = static_cast<unsigned int>( unifRand () ); unsigned S = 0; float quot = float (top) * one_over_N; while (quot > V) { S++; top--; N--; quot = quot * float (top) * one_over_N; } index += S; if (extract_removed_indices_) added[index] = true; indices[i++] = (*indices_)[index++]; N--; } index += N * static_cast<unsigned> (unifRand ()); if (extract_removed_indices_) added[index] = true; indices[i++] = (*indices_)[index++]; // Now populate removed_indices_ appropriately if (extract_removed_indices_) { unsigned ri = 0; for (size_t i = 0; i < added.size (); i++) { if (!added[i]) { (*removed_indices_)[ri++] = (*indices_)[i]; } } } } }
int EO_rIG_tExp::agg_dyn(long double dt, long double t) // E' il cuore della classe ogni batterio implementa questo in modo diverso. { if (salto_==1) { long double sigma_x=f_sigma(); long double lambda_star=lambda_r+dt*f_lambda()+sigma_x*deltaW_ec(dt); bool condition_salto = (exp(-2*(barriera_r-lambda_r)*(barriera_r-lambda_star)/(dt*pow(sigma_x,2)))>=unifRand()) || (lambda_r>barriera_r) ; if (condition_salto) { { barriera_r=reset_barrier(); lambda_r=0.0; return salto_=0; } } else{ lambda_r=lambda_star; } }else if(salto_==-1){ lambda_t+=dt/tau_t; if (lambda_t>=barriera_t) { // barriera_t=min(Exp_dist(),1.5); barriera_t=reset_barrier_t(); lambda_t=0.0; return salto_=2; } } return salto_; }
// // Generate a random number in a real interval. // param a one end point of the interval // param b the other end of the interval // return a inform rand numberin [a,b]. float unifRand(float a, float b) { return (b-a)*unifRand() + a; }