void alter_vector(point_3D &what, double range) { what.x += (random_double() * 2 - 1) * range; what.y += (random_double() * 2 - 1) * range; what.z += (random_double() * 2 - 1) * range; normalize(what); }
bool Spin_qsic_trim_3::begin(double &trim_s12, double &trim_s23, double &trim_s31) const { const int MAXIMUM_NUMBER_OF_INITIAL_GUESSES = 250; int number_of_initial_guesses = 0; while (++number_of_initial_guesses < MAXIMUM_NUMBER_OF_INITIAL_GUESSES) { double guess_s12 = random_double(-1, 1); double guess_s23 = random_double(-1, 1); double guess_s31 = random_double(-1, 1); double guess_radius = random_double(0, 1); if (!try_refine_begin(guess_s12, guess_s23, guess_s31, guess_radius, trim_s12, trim_s23, trim_s31)) { //std::cerr << "begin FAILED after " << MAXIMUM_NUMBER_OF_INITIAL_GUESSES << std::endl; } else { //std::cerr << "begin succeeded after " << number_of_initial_guesses << " initial guesess: " << trim_s12 << ", " << trim_s23 << ", " << trim_s31 << std::endl; return true; } } return false; }
state_full three_body_system::generate_refining_state(state_full &s){ return state_full(s.index, s.ang, random_double(0.8*s.alpha_r, 1.2*s.alpha_r), random_double(0.9*s.alpha_rho, 1.1*s.alpha_rho), random_double(1.0*s.alpha_com, 1.0*s.alpha_com), s.channel); }
vec3f vec3f::random(){ static vec3f rnd; rnd.x=random_double()*2-1; rnd.y=random_double()*2-1; rnd.z=random_double()*2-1; rnd.normalize(); return rnd; }
state_full three_body_system::generate_random_state(double min_r, double max_r, double min_rho, double max_rho, double min_com, double max_com, int angular_index, int channel){ int quiet = 1; int cind = channel_index(channel); double random_log_r = random_double( log(min_r), log(max_r) ); double alpha_r = scale_factor_r[cind] * exp(random_log_r); double random_log_rho = random_double( log(min_rho), log(max_rho) ); double alpha_rho = scale_factor_rho[cind] * exp(random_log_rho); double alpha_com = scale_factor_com[cind] * random_double(min_com, max_com); return state_full(states.size(), &ang->states[angular_index], pow(alpha_r,-2), pow(alpha_rho,-2), pow(alpha_com,-2), channel); }
int compute(int tries) { int hits = 0; double x, y; for(int i = 0; i < tries; ++i) { x = random_double(); y = random_double(); if (x*x + y*y <= 1) { ++hits; } } return hits; }
boolean test_seed(population *pop, entity *adam){ int i; /* Checks. */ if (!pop) die("Null pointer to population structure passed."); if (!adam) die("Null pointer to entity structure passed."); /* Seeding. */ for(i = 0;i<TSIZE(amp);i++){ ((double *)adam->chromosome[0])[i] = real_out->r[i]*(1-random_double(0.1)); ((double *)adam->chromosome[0])[TSIZE(amp)+i] = real_out->c[i]*(1.0-random_double(0.1)); } return TRUE; }
void random_displacement(double displacement[3]) { // distance follows a negative exponential + 1 // the +1 minimizes how often the current cell is picked again double r = -log(random_double()) + M_SQRT2; double theta = 2*M_PI*random_double(); double cos_phi = 2*random_double() - 1; double sin_phi = sqrt(1 - cos_phi*cos_phi); // convert to cartesian coordinates displacement[0] = r*sin_phi*cos(theta); displacement[1] = r*sin_phi*sin(theta); displacement[2] = r*cos_phi; }
boolean test_seed(population *pop, entity *adam) { /* Checks. */ if (!pop) die("Null pointer to population structure passed."); if (!adam) die("Null pointer to entity structure passed."); /* Seeding. */ ((double *)adam->chromosome[0])[0] = random_double(2.0); ((double *)adam->chromosome[0])[1] = random_double(2.0); ((double *)adam->chromosome[0])[2] = random_double(2.0); ((double *)adam->chromosome[0])[3] = random_double(2.0); return TRUE; }
void Scene::generate_random_sites(const unsigned nb) { std::vector<Point> points; for (unsigned i = 0; i < nb; ++i) { double x = random_double(0.0, 1.0); double y = random_double(0.0, 1.0); points.push_back(Point(x, y)); } std::vector<FT> noise(points.size(), 0.0); std::vector<FT> weights(points.size(), 0.0); construct_triangulation(points, weights, noise); std::cout << "Insert " << points.size() << std::endl; }
void sample_hidden(crbm *m, double *ph, double *h, int batch_size){ int i; for(i = 0; i < m->nhidden * batch_size; i++){ h[i] = random_double(0, 1) < ph[i] ? 1 : 0; } }
void sample_visible(crbm *m, double *pv, double *v, int batch_size){ int i; for(i = 0; i < m->nvisible * batch_size; i++){ v[i] = random_double(0, 1) < pv[i] ? 1 : 0; } }
void Split::randomize(int size) { assert(size < ntaxa); int num = countTaxa(); int cnt; // repeat at most 10 times const int MAX_STEP = 20; const int PROB_STEP = 5; for (int step = 0; step < MAX_STEP && num < size; step++) { // probability of including a taxon double prob = (double)(size - num) / ntaxa; // increase the probability if passing too many iterations if (step >= PROB_STEP) prob *= 2.0; if (step >= PROB_STEP*2) prob *= 2.0; if (step == MAX_STEP - 1) prob = 1.0; // now scan through all elements, pick up at random for (cnt = 0; cnt < ntaxa && num < size; cnt++) if (!containTaxon(cnt) && ( random_double() <= prob )) { addTaxon(cnt); num++; } } //report(cout); if (num >= size) return; cerr << "WARNING: random set has less than " << size << "taxa." << endl; }
int RateGamma::computePatternRates(DoubleVector &pattern_rates, IntVector &pattern_cat) { //cout << "Computing Gamma site rates by empirical Bayes..." << endl; phylo_tree->computePatternLhCat(WSL_RATECAT); int npattern = phylo_tree->aln->getNPattern(); pattern_rates.resize(npattern); pattern_cat.resize(npattern); double *lh_cat = phylo_tree->_pattern_lh_cat; for (int i = 0; i < npattern; i++) { double sum_rate = 0.0, sum_lh = 0.0; int best = 0; for (int c = 0; c < ncategory; c++) { sum_rate += rates[c] * lh_cat[c]; sum_lh += lh_cat[c]; if (lh_cat[c] > lh_cat[best] || (lh_cat[c] == lh_cat[best] && random_double()<0.5)) // break tie at random best = c; } pattern_rates[i] = sum_rate / sum_lh; pattern_cat[i] = best; lh_cat += ncategory; } return ncategory; // pattern_rates.clear(); // pattern_rates.insert(pattern_rates.begin(), ptn_rates, ptn_rates + npattern); // pattern_cat.resize(npattern, 0); // for (int i = 0; i < npattern; i++) // for (int j = 1; j < ncategory; j++) // if (fabs(rates[j] - ptn_rates[i]) < fabs(rates[pattern_cat[i]] - ptn_rates[i])) // pattern_cat[i] = j; // delete [] ptn_rates; }
void debug_rand_gen_double(const std::string file_name) { unsigned long N_runs = 100000; //std::cout << "Num of runs:"; //std::cin >> N_runs; std::vector<unsigned long> histogram; histogram.resize(N_bins); for (unsigned long i = 0; i < N_bins; i++) { histogram[i] = 0; } for (unsigned long i_run = 0; i_run < N_runs; i_run++) { bining(histogram, random_double()); } std::ofstream file; // open for writing, clear previous content file.open (file_name, std::ios::out | std::ios::trunc); double bin_value = 0.0; for (unsigned long i = 0; i < N_bins; i++) { bin_value = min_double + static_cast<double>(i) * (max_double - min_double) / static_cast<double>(N_bins); file << bin_value << ";" << histogram[i] << std::endl; } histogram.clear(); file.close(); }
/* * 激励最大化第 layerdx 层, */ void LayerWiseRBMs::activitionMaximization(int layerIdx, double argvNorm, int epoch, char * AMSampleFile){ int AMnumOut = layers[layerIdx]->numHid; int AMnumIn = layers[0]->numVis; if(AMSample == NULL){ AMSample = new double[AMnumOut*AMnumIn]; } for(int i=0; i<AMnumOut*AMnumIn; i++){ AMSample[i] = random_double(0, 1); } FILE * fp = fopen(AMSampleFile, "w+"); fwrite(&AMnumIn, sizeof(int), 1, fp); fwrite(&AMnumOut, sizeof(int), 1, fp); for(int i=0; i<AMnumOut; i++){ double * unitSample = AMSample + i*AMnumIn; time_t start, stop; start = time(NULL); double maxValue = maximizeUnit(layerIdx, i, unitSample, argvNorm, epoch); stop = time(NULL); printf("layer: %d , unit: %d, max value : %.6lf\t time: %.2lf s\n", layerIdx, i, maxValue, difftime(stop, start)); saveSample(fp, unitSample, AMnumIn); } fclose(fp); }
// Calculates energy of a configuration and stores it into the final element of // the MatrixPSIP // Note: When calculating the potential, I am not sure whether we need to use // absolute value of distance or the distance, itself. // Note: Inefficient. Can calculate energy on the fly with every generation of // psip. Think about it. // Note: v_ref will be a dummy variable for now. void find_weights(h3plus& state) { // Note that this is specific to the Anderson paper // Finding the distance between electrons, then adding the distances // from the protons to the electrons. double potential_sum = 0.0; for (auto& particle : state.particles) { double dist = sqrt((particle.coords[0] - particle.coords[3]) * (particle.coords[0] - particle.coords[3]) + (particle.coords[1] - particle.coords[4]) * (particle.coords[1] - particle.coords[4]) + (particle.coords[2] - particle.coords[5]) * (particle.coords[2] - particle.coords[5])); double potential = 1.0 / dist; for (const auto& coord : particle.coords) { potential -= 1.0 / std::abs(coord); } particle.potential = potential; potential_sum += potential; } // defining the new reference potential double num_particles = state.particles.size(); state.energy = potential_sum / num_particles; state.v_ref = state.energy - ((num_particles - INITIAL_SIZE) / (INITIAL_SIZE * state.dt)); std::uniform_real_distribution<double> uniform(0.0, 1.0); for (auto& particle : state.particles) { double w = 1.0 - (particle.potential - state.v_ref) * state.dt; particle.m_n = std::min((int)(w + random_double(uniform)), 3); } }
void Neuron::create_synapse_from(Neuron &other) { Synapse *synapse = new Synapse(&other, this); synapse->weight = random_double(-.24, .24); incoming_synapses.push_back(synapse); other.outgoing_synapses.push_back(synapse); }
int RateGammaInvar::computePatternRates(DoubleVector &pattern_rates, IntVector &pattern_cat) { //cout << "Computing Gamma site rates by empirical Bayes..." << endl; phylo_tree->computePatternLhCat(WSL_RATECAT); int npattern = phylo_tree->aln->getNPattern(); pattern_rates.resize(npattern); pattern_cat.resize(npattern); double *lh_cat = phylo_tree->_pattern_lh_cat; for (int i = 0; i < npattern; i++) { double sum_rate = 0.0, sum_lh = phylo_tree->ptn_invar[i]; int best = 0; double best_lh = phylo_tree->ptn_invar[i]; for (int c = 0; c < ncategory; c++) { sum_rate += rates[c] * lh_cat[c]; sum_lh += lh_cat[c]; if (lh_cat[c] > best_lh || (lh_cat[c] == best_lh && random_double()<0.5)) { // break tie at random best = c+1; best_lh = lh_cat[c]; } } pattern_rates[i] = sum_rate / sum_lh; pattern_cat[i] = best; lh_cat += ncategory; } return ncategory+1; }
void initWeightTanh(double *weight, int numIn, int numOut){ double low,high; high = sqrt(6. / (numIn + numOut)); low = -1 * high; for(int i=0; i<numIn * numOut; ++i){ weight[i] = random_double(low, high); } }
// 6D random walk void random_walk(h3plus& state) { std::normal_distribution<double> gaussian(0.0, 1.0); for (auto& particle : state.particles) { for (auto& coord : particle.coords) { coord += sqrt(state.dt) * random_double(gaussian); } } }
// INITIALIZATION rl_variables initializate_variables(rl_data* data) { //variables data->pv; int i = 0; data->td_error = 0; data->variables.critic_value = 0; data->reinforcement_signal = 0; data->variables.sigma_critical_deviation = 0; data->total_error_quadratico = 0; for (i = 0; i < 3;i++) { data->variables.U[i] = 0; data->variables.error[i] = 0; data->variables.error_order[i] = 0; } data->variables.recomended_pid_params[0] = 1250; data->variables.recomended_pid_params[1] = 600; data->variables.recomended_pid_params[2] = 25; data->variables.pid_params[0] = 1250;//0.12;//1250 data->variables.pid_params[1] = 600;//0.32; //600 data->variables.pid_params[2] = 25;//0.08; //25 // INITIALIZE PAST VARIABLES data->pv.critic_value = 0; data->pv.sigma_critical_deviation = 0; for (i = 0; i < 3;i++) { data->pv.U[i] = 0; data->pv.error[i] = 0; data->pv.error_order[i] = 0; } data->pv.recomended_pid_params[0] = 1250; data->pv.recomended_pid_params[1] = 600; data->pv.recomended_pid_params[2] = 25; data->pv.pid_params[0] = 1250;//0.12; //1250 data->pv.pid_params[1] = 600;//0.32; //600; // data->pv.pid_params[2] = 25;//0.08; //25; // // INITIALIZE NETWORK for (i = 0; i < neural_network_size; i++) { data->network[i].w_neuron_weight[0] = /*2.55*/5.1 + random_double() * FF_MULTIPLIER;//W_kp data->network[i].w_neuron_weight[1] = /*1.2*/2.5 + random_double() * FF_MULTIPLIER; data->network[i].w_neuron_weight[2] = -0.025 + random_double() * FF_MULTIPLIER; data->network[i].v_neuron_weight = random_double() * FF_MULTIPLIER; data->network[i].center_vector[0] = random_double() * RBF_MULTIPLIER; data->network[i].center_vector[1] = random_double() * RBF_MULTIPLIER; data->network[i].center_vector[2] = random_double() * RBF_MULTIPLIER; data->network[i].width_scalar_sigma = random_double() * RBF_MULTIPLIER; data->network[i].phi_value = 1;//random_double() * RBF_MULTIPLIER; } return data->pv; }
// Calculates energy of a configuration and stores it into the final element of // the MatrixPSIP // Note: When calculating the potential, I am not sure whether we need to use // absolute value of distance or the distance, itself. // Note: Inefficient. Can calculate energy on the fly with every generation of // psip. Think about it. // Note: v_ref will be a dummy variable for now. void find_weights(h3plus& state){ // Note that this is specific to the Anderson paper // Finding the distance between electrons, then adding the distances // from the protons to the electrons. double potential_sum = 0.0, dist2; for (auto& particle : state.particles) { double dist1 = sqrt((particle.coords[0] - particle.coords[3]) * (particle.coords[0] - particle.coords[3]) + (particle.coords[1] - particle.coords[4]) * (particle.coords[1] - particle.coords[4]) + (particle.coords[2] - particle.coords[5]) * (particle.coords[2] - particle.coords[5])); double potential = 1.0 / dist1; for (size_t i = 0; i < 3; i++){ dist1 = sqrt((particle.coords[0] - state.proton.pos[i][0]) * (particle.coords[0] - state.proton.pos[i][0]) + (particle.coords[1] - state.proton.pos[i][1]) * (particle.coords[1] - state.proton.pos[i][1]) + (particle.coords[2] - state.proton.pos[i][2]) * (particle.coords[2] - state.proton.pos[i][2])); dist2 = sqrt((particle.coords[3] - state.proton.pos[i][0]) * (particle.coords[3] - state.proton.pos[i][0]) + (particle.coords[4] - state.proton.pos[i][1]) * (particle.coords[4] - state.proton.pos[i][1]) + (particle.coords[5] - state.proton.pos[i][2]) * (particle.coords[5] - state.proton.pos[i][2])); potential -= ((1/dist1) + (1/dist2)); } particle.potential = potential; potential_sum += potential; } size_t num_particles = state.particles.size(); std::uniform_real_distribution<double> uniform(0.0, 1.0); // This needs type promotion to either double or int so it doesn't // underflow. Since it's going to be used as a double, might as well // do that directly. double particles_diff = static_cast<double>(num_particles) - INITIAL_SIZE; // defining the new reference potential state.energy = potential_sum / num_particles; state.v_ref = state.energy - particles_diff / (INITIAL_SIZE * state.dt); #pragma omp parallel for for (size_t i = 0; i < num_particles; ++i) { auto& particle = state.particles[i]; double w = 1.0 - (particle.potential - state.v_ref) * state.dt; particle.m_n = std::min((int)(w + random_double(uniform)), 3); } }
SolverEvolver::Genome SolverEvolver::random_genome() { Genome ret; for (int i = 0; i < ret.size(); ++i) { ret[i] = random_double(0.0f); } return std::move(ret); }
double corrupt(const double* x, double* nx, int n, double level){ for(int i = 0; i < n; i++){ if(random_double(0, 1) < level){ nx[i] = 0; }else{ nx[i] = x[i]; } } }
void initializeWeightTanh(double *weight, int numIn, int numOut){ double low, high; low = -sqrt((double)6 / (numIn + numOut)); high = sqrt((double)6 / (numIn + numOut)); for(int i = 0; i < numIn*numOut; i++){ weight[i] = random_double(low, high); } }
state_full three_body_system::generate_random_state(int state_generation_mode){ int quiet = 1; int channel = 0; while (abs(channel) == 0){ channel = random_int(-3, 3); } channel = 3; int cind = channel_index(channel); double random_log_r = random_double( log(basis_min_r_scaled), log(basis_max_r) ); if (true){ int randomiser = random_int(0,3); if (randomiser != 0){ random_log_r = random_double( log(0.9*basis_min_r_scaled), log(1000.0 * basis_min_r_scaled) ); } } double alpha_r = scale_factor_r[cind] * exp(random_log_r); double random_log_rho = random_double( log(basis_min_rho_scaled), log(basis_max_rho) ); double alpha_rho = scale_factor_rho[cind] * exp(random_log_rho); double alpha_com = scale_factor_com[cind] * random_double(basis_min_com_scaled, basis_max_com); int temp_int = -1 + ang->generation_modes.size(); int temp_mode = min(state_generation_mode, temp_int); int temp_mode_max = ang->generation_modes[temp_mode].size() - 1; int angular_index = ang->generation_modes[temp_mode][random_int(0, temp_mode_max)]; if (only_2body){ while (true){ angular_index = ang->generation_modes[temp_mode][random_int(0, temp_mode_max)]; if (&ang->states[angular_index].lcom == 0 && ang->states[angular_index].lcoup == ang->ltotal){ break; } } } if (!quiet){ cout << "Generated random state: " << state_full(-1, &ang->states[angular_index], alpha_r, alpha_rho, alpha_com, channel); } return state_full(states.size(), &ang->states[angular_index], pow(alpha_r,-2), pow(alpha_rho,-2), pow(alpha_com,-2), channel); }
void binomial(double *px, double *x, int size){ for(int i = 0; i < size; i++){ double p = random_double(0, 1); if(p < px[i]){ x[i] = 1.0; }else{ x[i] = 0.0; } } }
void SolverEvolver::mutate(SolverEvolver::Genome& t) { for (size_t i = 0; i < t.size(); i++) { if (rand_.NextBool(mutation_p_)) { t[i] = random_double(t[i]); } } }
void SpeakingAgent::draw() { // store state placement2d::start_transform(); // background al_draw_filled_rectangle(0, 0, size.x, size.y, color::hex("319cff")); // mouth ALLEGRO_BITMAP *mouth_shape = _get_phoneme_image(Speaker::get_current_phoneme()); if (mouth_shape) al_draw_bitmap(mouth_shape, 0, 10, NULL); // eyes float eye_y = size.y/4; float eye_r = 2.3; float eye_1x = size.x/4; float eye_2x = size.x/4 * 3; if (blink_counter < 0.1) { al_draw_line(eye_1x-eye_r*2, eye_y, eye_1x+eye_r*2, eye_y, color::black, eye_r); al_draw_line(eye_2x-eye_r*2, eye_y, eye_2x+eye_r*2, eye_y, color::black, eye_r); if (blink_counter < 0.0) blink_counter = random_double(2.0, 6.0); } else { al_draw_filled_circle(eye_1x, eye_y, eye_r, color::black); al_draw_filled_circle(eye_2x, eye_y, eye_r, color::black); } blink_counter -= 1.0/60; // words float bx = size.x+size.x/4; float by = -size.y/3; float bw = size.x*2.5; float bh = size.y*0.8; float border_radius = 7; al_draw_filled_rounded_rectangle(bx, by, bx+bw, by+bh, border_radius, border_radius, color::white); float tx = bx+border_radius*4; float ty = by+bh-1; float ts = 20; al_draw_filled_triangle(tx, ty, tx+ts, ty, tx, ty+ts, color::white); std::string word = Speaker::get_current_word(); ALLEGRO_FONT *font = fonts["lacuna.ttf 26"]; if (is_paused()) word = "[PAUSED]"; if (font) al_draw_text(font, color::black, bx+bw/2, by+bh/2-al_get_font_line_height(font)/2, ALLEGRO_ALIGN_CENTRE, word.c_str()); // show stream # //al_draw_text(font, color::white, 0, 0, NULL, tostring(voice->get_current_stream()).c_str()); // restore state placement2d::restore_transform(); //draw_crosshair(x, y, color::white); }