void pool::mutate(genome& g){ double coefficient[2] = {0.95, 1.05263}; std::uniform_int_distribution<int> coin_flip(0, 1); g.mutation_rates.enable_mutation_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.disable_mutation_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.connection_mutate_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.node_mutation_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.link_mutation_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.bias_mutation_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.crossover_chance *= coefficient[coin_flip(this->generator)]; g.mutation_rates.perturb_chance *= coefficient[coin_flip(this->generator)]; std::uniform_real_distribution<double> mutate_or_not_mutate(0.0, 1.0); if (mutate_or_not_mutate(this->generator) < g.mutation_rates.connection_mutate_chance) this->mutate_weight(g); double p; p = g.mutation_rates.link_mutation_chance; while (p > 0.0) { if (mutate_or_not_mutate(this->generator) < p) this->mutate_link(g, false); p = p - 1.0; } p = g.mutation_rates.bias_mutation_chance; while (p > 0.0) { if (mutate_or_not_mutate(this->generator) < p) this->mutate_link(g, true); p = p - 1.0; } p = g.mutation_rates.node_mutation_chance; while (p > 0.0) { if (mutate_or_not_mutate(this->generator) < p) this->mutate_node(g); p = p - 1.0; } p = g.mutation_rates.enable_mutation_chance;; while (p > 0.0) { if (mutate_or_not_mutate(this->generator) < p) this->mutate_enable_disable(g, true); p = p - 1.0; } p = g.mutation_rates.disable_mutation_chance; while (p > 0.0) { if (mutate_or_not_mutate(this->generator) < p) this->mutate_enable_disable(g, false); p = p - 1.0; } }
double Random::random_laplace(double width) { thread_local static std::mt19937_64 generator(std::random_device{}()); using ed = std::exponential_distribution<double>; thread_local static auto dist = ed{}; return (coin_flip() ? 1 : -1)*dist(generator, ed::param_type{1.0/width}); }
Spectrum GlassBSDF::sample_f(const Vector3D& wo, Vector3D* wi, float* pdf) { // TODO Part 5: // Compute Fresnel coefficient and either reflect or refract based on it. if (!refract(wo, wi, ior)) { reflect(wo, wi); *pdf = 1.0; return reflectance * (1.0 / fabs(wi->z)); } double R = (ior - 1.0) * (ior - 1.0) / (ior + 1.0) / (ior + 1.0); double c = 1.0 - fabs(wi->z); R = R + (1.0 - R) * c * c * c * c * c; if (coin_flip(R)) { reflect(wo, wi); *pdf = R; return reflectance * (R / fabs(wi->z)); } *pdf = 1.0 - R; double nr; if (wi->z > 0) { nr = ior; } else { nr = 1.0 / ior; } return transmittance * ((1.0 - R) * nr * nr / fabs(wi->z)); }
int main() { srand((long)time(NULL)); // seed the RNG int heads = 0; for(int i = 0; i < 1000000 ; i++) { if (coin_flip()) { heads++; } } printf("the number of heads is %d/1000000\n", heads); printf("%d\n", (int)time(NULL)); }
Spectrum GlassBSDF::sample_f(const Vector3D& wo, Vector3D* wi, float* pdf) { // TODO Part 5: // Compute Fresnel coefficient and either reflect or refract based on it. double no, ni; if (wo.z < 0) { //r = ior/1; no = ior; ni = 1.0; } else { //r = 1.0/ior; no = 1.0; ni = ior; } bool totalIR = refract(wo, wi, ior); if (!totalIR) { reflect(wo, wi); *pdf = 1.0; return reflectance / abs_cos_theta(*wi); } else { float Ro = ((no - ni)/ (no + ni)) * ((no - ni)/ (no + ni)); float R = Ro + (1.0 - Ro)*pow((1.0 - abs_cos_theta(wo)), 5.0); R = clamp(R, 0.0, 1.0); if (coin_flip(R)) { reflect(wo, wi); *pdf = R; return R * (reflectance / abs_cos_theta(*wi)); } else { refract(wo, wi, ior); *pdf = 1.0 - R; //double r; //printf("%s\n", "here"); return (1.0 - R) * (transmittance * no/ni * no/ni) / abs_cos_theta(*wi); } } }
List_hd_t Reduce_fun(List_tl_t* in_, void* op_env) { List_t* in; cut { /* Copy the input to a fresh allocation. We side-effect this allocation as we run. (Side-effecting the input itself is not desirable--it requires that it have AWAR capability). */ in = alloc(List_t); *in = *in_; } /* While the input contains more than one element... */ while(1) { long at_basecase = 0; cut { List_t l = *in; if(l == NULL) abort(); else { /* Check for the base case: Only 1 element in list. */ at_basecase = ( (*List_tl(l)) == NULL ); } } if(at_basecase) { List_t c = *in; List_hd_t hd = List_hd(c); return hd; } else { /* The list has several elements. Reduce its length by (stable) sampling. */ memo(fresh_scope){ List_t* next_in = in; List_t in = *next_in; coin_t* coin = coin_biased_4(); List_tl_t* out = alloc(List_tl_t); List_tl_t* tail = out; /* Loop until list is empty... */ while(1) { List_hd_t hd = List_hd(in); /* Sample some more elements for hd. */ while(in = *List_tl(in), in && coin_flip(coin, (void*) in)) { hd = Monoid_binop(op_env, hd, List_hd(in)); } /* Append hd to the next list */ List_t c = memo(List_cons(hd)); memo; /* Advance the tail pointer. */ *tail = c; tail = List_tl(c); /* Are we at the end? */ if(in == NULL) { *tail = NULL; break; } else continue; } /* Process the output list as input. */ *next_in = *out; } continue; } } }
int rand_height() { int height = 1; for (; height < MAX_HEIGHT && coin_flip(); ++height) {} return height; }