double GD_API Random(int end) { if ( end <= 0 ) return 0; std::uniform_int_distribution<int> randomDist(0, end); return randomDist(randomEngine); }
void thread_mcl::run_kdl_sampling(){ //--- SETTING VARIABLES --- int T = 0; int limit_min = 300; int k, M, Mx; float confidence = 0.3; // ztable is from right side of mean (Values between 0 ~ .5) float error = 0.4; float zvalue = 4.1; float noise_foward = 0.01; float noise_turn = 0.01; float noise_sense = 3.0; movement mv; random_device generator; mt19937 gen(generator()); uniform_int_distribution<int> randomDist(1, 3); std::uniform_real_distribution<double> randomTh(-.5,.5); //------------------------- this->build_tablez(); robo->representation(); myMcl->set_noise_particles(noise_foward, noise_turn, noise_sense); if(localization) //TRUE = LOCAL myMcl->set_position(robo->robot_pose); confidence = fmin(0.49998,fmax(0,confidence)); for(int i = 0; i < ztable.size(); i++){ if(ztable[i] >= confidence){ zvalue = i/100.00; cout<<"ztable["<<i<<"] = "<<ztable[i]<<" >= "<<confidence<<endl; cout<<"Z VALUE: "<<zvalue<<endl; break; } } while(true){ particle new_particle; vector<particle> new_particles; k = M = 0; Mx = numeric_limits<int>::max(); this->myMap->set_empty_map(); //SET EMPTY MAP mv.dist = randomDist(gen); mv.angle = randomTh(gen); robo->move(mv); img = myMcl->Gera_Imagem_Pixmap(this->robo); myMcl->normalizing_particle(); new_particles.clear(); do{ new_particle = myMcl->resample_Roleta_single(); //RESAMPLING state = "\n\t\t\t Resampling"; new_particle = myMcl->sampling_single(new_particle, mv); //SAMPLING state = "\n\t\t\t Sampling"; new_particle = myMcl->weight_particles_single(new_particle, robo->sense()); //WEIGHTING state = "\n\t\t\t Weighting"; new_particles.push_back(new_particle); if(bin_is_empty(new_particle)){ k++; if(k > 1){ Mx = (int)ceil(((k-1)/(2*error))*pow(1 - (2/(9.0*(k-1))) + (sqrt(2/(9.0*(k-1))))*zvalue,3)); cout<<"Mx VALUE: "<<Mx<<endl; } } M++; if(Mx < limit_min) Mx = limit_min; cout<<"M:"<<M<<" Mx:"<<Mx; cout<<" -- Scale particle:"<<new_particle.s<<endl; }while(M < Mx); n_particles = M; myMcl->particles.clear(); myMcl->particles = new_particles; sleep(1); } }
void thread_mcl::run_normal(){ //--- SETTING VARIABLES --- int T = 0; int timeSleep = 90000; bool flag_neff = false; float noise_foward = 0.01; float noise_turn = 0.01; float noise_sense = 3.0; movement mv; random_device generator; mt19937 gen(generator()); uniform_int_distribution<int> randomDist(1, 3); uniform_real_distribution<float> randomTh(-.5,.5); //------------------------- robo->representation(); myMcl->set_noise_particles(noise_foward, noise_turn, noise_sense); if(localization) //TRUE = LOCAL myMcl->set_position(robo->robot_pose); this->myMap->set_empty_map(); //SET EMPTY MAP - KLD sampling while(true){ mv.dist = randomDist(gen); mv.angle = randomTh(gen); img = myMcl->Gera_Imagem_Pixmap(this->robo); robo->move(mv); myMcl->sampling(mv); state = "\n\t\t\t Sampling"; usleep(timeSleep); myMcl->weight_particles(robo->sense()); state = "\n\t\t\t Weighting"; usleep(timeSleep); if(flag_neff){ //TRUE = neff on - FALSE = neff off float neff2 = myMcl->number_effective(); if(neff2 < myMcl->num_particles/2){ cout<<"NEFF: "<<neff2<<" - Vou fazer resample "; // myMcl->resample(); myMcl->resample_Roleta(); state = "\n\t\t\t Resampling"; usleep(timeSleep); } neff = QString::number(neff2); }else{ myMcl->resample_Roleta(); state = "\n\t\t\t Resampling"; usleep(timeSleep); neff = "OFF"; } cout<<"T:"<<T++<<endl; } }
void World::turnlyUpdate(){ boost::random::uniform_int_distribution<int> dist(1, 100); incrementElapsedTurns(); getCurrentTime().increment(); //The weather will tend to change every ~8 hours, or 480 minutes. //Ideally, something like this: //1 hour (60) -> 4% //2 hours (120) -> 6% //3 hours (180) -> 8% //4 hours (240) -> 12.5% //5 hours (300) -> 16% //6 hours (360) -> 24% //7 hours (420) -> 30% //8 hours (480) -> 40% for(auto& effect : weatherEffects){ effect.second += minutesPerTurn; } auto effect = std::begin(weatherEffects); while(effect != std::end(weatherEffects)){ int removeEffect{dist(masterManager.randomEngine)}; //For instance, if the effect has been there for 240 minutes, it will have an //40% chance of being removed. There is a hard cap of 80%. int totalChance = effect->second / 15; if(totalChance > 50){ totalChance = 50; } if(removeEffect <= totalChance){ effect = weatherEffects.erase(effect); } else{ effect++; } } int hourlyChance = 20; int turnlyChance = hourlyChance / (60/minutesPerTurn); int randomRoll{dist(masterManager.randomEngine)}; if (randomRoll <= turnlyChance){ addWeather(); } //////////////////////////////////////////////////////////////////////////////// //////////////////MUD CREATION AND REMOVAL////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// //Firstly, depending on whether the rain is heavy or light, there will be a varying //amount of mud formations per turn. bool rain{false}; int minimumMudFormations{0}; for(auto& effect : weatherEffects){ if(effect.first == World::Weather::HEAVY_RAIN){ rain = true; minimumMudFormations = 4; break; } else if(effect.first == World::Weather::LIGHT_RAIN){ rain = true; minimumMudFormations = 2; break; } } //A random tile on the map that we call the origin is converted to mud (if possible). if(rain){ for (int i{0}; i < minimumMudFormations; ++i){ boost::random::uniform_int_distribution<int> randomIndexDist(0, getDimensions().x * getDimensions().y); int randomIndex{randomIndexDist(masterManager.randomEngine)}; sf::Vector2i originPosition{cartesianPosAtIndex(randomIndex)}; TerrainTile* origin = terrainAtCartesianPos(originPosition); bool originConvertedToMud{false}; if(origin->getTerrainType() == TerrainTile::TerrainType::MEADOW){ toggleMud(origin); originConvertedToMud = true; } if(!originConvertedToMud){ break; } //There is then an 4/5 chance that a neighboring tile is also converted, if possible... int randomNumber{0}; do{ boost::random::uniform_int_distribution<int> randomDist(1, 5); randomNumber = randomDist(masterManager.randomEngine); if(randomNumber >= 2){ boost::random::uniform_int_distribution<int> randomDisplacementDist(-1, 1); int randomDisplacementX{randomDisplacementDist(masterManager.randomEngine)}; int randomDisplacementY{randomDisplacementDist(masterManager.randomEngine)}; sf::Vector2i newPosition{originPosition.x + randomDisplacementX, originPosition.y + randomDisplacementY}; TerrainTile* newTerrain = terrainAtCartesianPos(newPosition); if(newTerrain != nullptr){ if(newTerrain->getTerrainType() == TerrainTile::TerrainType::MEADOW){ toggleMud(newTerrain); } } } //... and a 3/5 chance for this process to repeat. }while(randomNumber >= 3); } //While new mud tiles are formed, every old mud tile has a 1/8 chance to spread to a neighboring tile, if possible //IMPORTANT NOTE: here we use an integer-based index rather than a foreach loop, because toggleMud() //itself actually erases a mud tile from mudTiles. So if we do a foreach mudTiles loop, we end up with //messy dangling pointers and, in my case, days of debugging crashes. for(std::vector<Mud*>::size_type i{0}; i < mudTiles.size(); i++){ boost::random::uniform_int_distribution<int> randomDist(1, 8); int randomNumber = randomDist(masterManager.randomEngine); if(randomNumber == 1){ boost::random::uniform_int_distribution<int> randomDisplacementDist(-1, 1); int randomDisplacementX{randomDisplacementDist(masterManager.randomEngine)}; int randomDisplacementY{randomDisplacementDist(masterManager.randomEngine)}; sf::Vector2i originPosition = mudTiles[i]->getCartesianPos(); sf::Vector2i newPosition{originPosition.x + randomDisplacementX, originPosition.y + randomDisplacementY}; TerrainTile* newTerrain = terrainAtCartesianPos(newPosition); if(newTerrain != nullptr){ if(newTerrain->getTerrainType() == TerrainTile::TerrainType::MEADOW){ toggleMud(newTerrain); } } } } } //Finally, after the rain has stopped, each mud tile has a 9/10 chance to dry up. else if (!rain){ //IMPORTANT NOTE: here we use an integer-based index rather than a foreach loop, because toggleMud() //itself actually erases a mud tile from mudTiles. So if we do a foreach mudTiles loop, we end up with //messy dangling pointers and, in my case, days of debugging crashes. for(std::vector<Mud*>::size_type i{0}; i < mudTiles.size(); i++){ boost::random::uniform_int_distribution<int> randomDryingChanceDist(1, 10); int randomDryingChance{randomDryingChanceDist(masterManager.randomEngine)}; if(randomDryingChance <= 9){ toggleMud(mudTiles[i]); } } } unhighlightVisibleTiles(); visibleTiles.clear(); }
double GD_API RandomFloat(float end) { if (end <= 0) return 0; std::uniform_real_distribution<double> randomDist(0, end); return randomDist(randomEngine); }