void Population::reproduction() { std::uniform_int_distribution<uint64_t> rnd_int(0, _parents.size() - 1); std::bernoulli_distribution crossover(Settings::inst()->_crossover_probability); std::bernoulli_distribution mutate(Settings::inst()->_mutation_probability); _children.erase(_children.begin(), _children.end()); //_children from previous run for (uint64_t i = 0; i < Settings::inst()->_children_count; ++i) { std::unique_ptr<Individual> offspring; if (crossover(Settings::inst()->_randomness_source)) offspring = _genome[ _parents[ rnd_int(Settings::inst()->_randomness_source)]] ->cross_over( _genome[ _parents[ rnd_int(Settings::inst()->_randomness_source)]]); else offspring = _genome[ _parents[ rnd_int(Settings::inst()->_randomness_source)]] ->get_copy(); while (mutate(Settings::inst()->_randomness_source)) { offspring->mutate(); } _children.push_back(move(offspring)); } }
/** * In case of a infection this function is called. * * @param host host to get infected * @param inc_agent agent infecting the host * @param inc_time time point to capture start of infection */ void host_infect(Host *host, Agent inc_agent, unsigned inc_time) { bool is_contained = ((host->immunities & inc_agent.immunogenicity_factor) == inc_agent.immunogenicity_factor); if (is_contained) { return; } /* copy agent from other host */ host->agent = inc_agent; host->infection_time = inc_time; host->state = INFECTED; if (rnd_percent(rng) < host->agent.mutation_prob) { agent_mutate(&host->agent); } // immunogenicity checked against random value if (rnd_percent(rng) < inc_agent.immunogenicity) { host->will_immune = 1; } else { host->will_immune = 0; } // outbreak time gets calculated int diff = inc_agent.incubation_max_dur - inc_agent.incubation_mean_dur; int time_variation = (rnd_int(rng) % (2 * diff + 1)) - diff; unsigned incubation_time = inc_agent.incubation_mean_dur + time_variation; host->outbreak_time = inc_time + incubation_time; if (incubation_time > world.max_incubation) { world.max_incubation = incubation_time; } else if (incubation_time < world.min_incubation) { world.min_incubation = incubation_time; } world.sum_incubation += (double)incubation_time; // outbreak probability checked against random value if (rnd_percent(rng) < inc_agent.outbreak_prob) { host->will_ill = 1; // mortality is checked against random value if (rnd_percent(rng) < inc_agent.mortality) { host->will_die = 1; } else { host->will_die = 0; } // disease endpoint gets calculated diff = inc_agent.illness_max_dur - inc_agent.illness_mean_dur; int time_variation_2 = (rnd_int(rng) % (2 * diff + 1)) - diff; // disease time calculated beginning with outbreak_time!! host->end_of_disease = host->outbreak_time + inc_agent.illness_mean_dur + time_variation_2; } else { host->will_ill = 0; host->will_die = 0; host->end_of_disease = host->outbreak_time; } }
void Population::parent_selection_stochastic_uniform() { std::uniform_int_distribution<uint64_t> rnd_int(0, _genome.size() - 1); for (uint64_t i = 0; i < Settings::inst()->_parent_count; ++i) { _parents.push_back(rnd_int(Settings::inst()->_randomness_source)); } }
void Individual::mutate() { std::uniform_int_distribution<uint64_t> rnd_int(0, 0xFFFFFFFFFFFFFFFF); std::uniform_real_distribution<float> rnd_flt(-1, 1); Layer l = _ann[rnd_int(Settings::inst()->_randomness_source) % _ann.size()]; Neuron n = l._v[rnd_int(Settings::inst()->_randomness_source) % l._v.size()]; n.mutate(); }
void Population::parent_selection_tournament() { std::uniform_int_distribution<uint64_t> rnd_int(0, _genome.size()-1); for (uint64_t i = 0; i < Settings::inst()->_parent_count; ++i) { uint64_t par = rnd_int(Settings::inst()->_randomness_source); // to tournament is nominated_tournament_k individuals, the best is choosed as parent for (uint64_t j = 0; j < Settings::inst()->_tournament_k; ++j) { uint64_t alt = rnd_int(Settings::inst()->_randomness_source); if (_genome[alt]->getFitness() > _genome[par]->getFitness()) par = alt; } _parents.push_back(par); } }
/** * Find a position for a host during creation process. * Additional function to prevent the possibility of a deadlock * which could happen in host_move during creation process. * If a host is placed on a reserved field which is surrounded * by reserved fields there's no way to find a free position. * If not all positions in world are reserved, host_find_initial_pos * will find a free one. Despite, this removes the check for * host->field != NULL in host_move. * * @param host host to find position for */ static void host_find_initial_pos(Host *host) { int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); unsigned col_rank = rank % world.process_matrix_dimensions.cols; unsigned row_rank = rank / world.process_matrix_dimensions.cols; unsigned x_offset = world.width_process_mid * col_rank; unsigned y_offset = world.height_process_mid * row_rank; bool keep_searching = true; while (keep_searching) { host->pos.x = rnd_int(rng) % world.width_process + x_offset; host->pos.y = rnd_int(rng) % world.height_process + y_offset; // if another host is on the same position, keep searching keep_searching = world_host_position_collides(host); } }
void Population::parent_selection_rank() { std::vector<uint64_t> stacked_fitness; std::sort(_genome.begin(), _genome.end(), _increasing_comparator); stacked_fitness.push_back(1); for (uint64_t i = 1; i < _genome.size(); ++i) { stacked_fitness.push_back(i + stacked_fitness[i - 1] + 1); } std::uniform_int_distribution<uint64_t> rnd_int(0, stacked_fitness[stacked_fitness.size() - 1]); for (uint64_t i = 0; i < Settings::inst()->_parent_count; ++i) { uint64_t r = rnd_int(Settings::inst()->_randomness_source); uint64_t par = 0; while (par < stacked_fitness.size() && r > stacked_fitness[par]) ++par; _parents.push_back(par); } }
Host *host_create() { int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); Host *new_host = calloc(1, sizeof(Host)); new_host->state = HEALTHY; new_host->field = NULL; // hosts are placed in their own fields // find free position for host host_find_initial_pos(new_host); // minimum of 24 + random[0, 48] new_host->disappear_duration = 24 + (rnd_int(rng) % 49); // immunity list new_host->immunities = 0L; return new_host; }
void host_move(Host *host) { int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); bool keep_searching = true; unsigned old_x_pos = host->pos.x, old_y_pos = host->pos.y; very_short_int rnd_dir_x, rnd_dir_y; while (keep_searching) { // possible values rand%3={0,1,2} -> -1 ={-1, 0, 1} rnd_dir_x = (rnd_int(rng) % 3) - 1; rnd_dir_y = (rnd_int(rng) % 3) - 1; host->pos.x = (old_x_pos + rnd_dir_x + world.size.width) % world.size.width; host->pos.y = (old_y_pos + rnd_dir_y + world.size.height) % world.size.height; int target_rank = world_get_process_for_host(host); // if needed send host to other process if (rank != target_rank) { ++world.hosts_send_count; MPI_Request req; world_remove_host_from_field(host); MPI_Isend(host, 1, mpi_host, target_rank, tag_host, MPI_COMM_WORLD, &req); MPI_Wait(&req, MPI_STATUS_IGNORE); --world.residents; switch (host->state) { case IMMUNE: world.immune_total--; world.healthy_current--; break; case HEALTHY: world.healthy_current--; break; case INFECTED: world.infected_total--; world.infected_current--; break; case ILL: world.ill_total--; world.ill_current--; break; default: ; } free(host); // printf("\nsend from %i => to %i", rank, target_rank); // receiving process checks for position collision return; } // if another host is on the same position, keep searching keep_searching = world_host_position_collides(host); } // host stays in this process but has to switch fields Field *correct_field = world_get_field_for_host(host); if (host->field != correct_field) { world_transfer_host_to_field(host, correct_field); } }
void rnd_test(rnd_t rnd) { int dist[20]; int i,r; uint64_t total; double dr, dt; clock_t start,stop; uint32_t max32 = rnd_max32(); uint64_t max64 = rnd_max64(); unsigned size; uint32_t *state; char *state_str; for (i=0; i < 20; i++) dist[i] = 0; printf("Testing the random number generator\n"); printf("rnd max32: %u\n",max32); printf("rnd max64: %llu\n",max64); printf("\n"); printf("state size = %u bytes\n\n",rnd_get_state_size_bytes()); printf("Setting state with string (4 bytes)\n"); state_str = "12345678"; printf("In : %s\n",state_str); rnd_string_to_state(rnd, state_str); print_state(rnd); printf("Setting state with string (40 bytes)\n"); state_str = "01234567a1a2a3a4b1b2b3b4c1c2c3c4d1d2d3d4e1e2e3e4f1f2f3f4A5A6A7A8B5B6B7B8C5C6C7C8"; printf("In : %s\n",state_str); rnd_string_to_state(rnd, state_str); print_state(rnd); printf("Setting state with array (4 bytes)\n"); size = sizeof(uint32_t); state = malloc(size); state[0] = 0x12345678UL; printf("In : %0x\n",state[0]); rnd_array_to_state(rnd, state, size); free(state); print_state(rnd); printf("Setting state with array (40 bytes)\n"); size = sizeof(uint32_t)*10; state = malloc(size); state[0] = 0x12345678UL; state[1] = 0xa1a2a3a4UL; state[2] = 0xb1b2b3b4UL; state[3] = 0xc1c2c3c4UL; state[4] = 0xd1d2d3d4UL; state[5] = 0xe1e2e3e4UL; state[6] = 0xf1f2f3f4UL; state[7] = 0xA5A6A7A8UL; state[8] = 0xB5B6B7B8UL; state[9] = 0xC5C6C7C8UL; printf("In : "); for (i=0; i<10; i++) { printf("%0x",state[i]); } printf("\n"); rnd_array_to_state(rnd, state, size); free(state); print_state(rnd); printf("Initializing state with rnd_int() using time\n"); rnd_init(rnd, (unsigned long)time(NULL)); print_state(rnd); printf("Testing rnd_u32\n"); dt = 0.0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dt += rnd_u32(rnd); } stop = clock(); printf("Avg=%.0f (Expected: %.0f) (Time: %5.3f)\n\n", dt/TEST_SIZE, (double)max32/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); printf("Testing rnd_u64\n"); dt = 0.0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dt += rnd_u64(rnd); } stop = clock(); printf("Avg=%.0f (Expected: %.0f) (Time: %5.3f)\n\n", dt/TEST_SIZE, (double)max64/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); printf("Testing rnd_closed\n"); dt = 0.0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dt += rnd_closed(rnd); } stop = clock(); printf("Avg=%7.5f (Expected: %7.5f) (Time: %5.3f)\n\n", dt/TEST_SIZE, 0.5, ((double)(stop-start)/CLOCKS_PER_SEC)); printf("Testing rnd_open\n"); dt = 0.0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dt += rnd_open(rnd); } stop = clock(); printf("Avg=%7.5f (Expected: %7.5f) (Time: %5.3f)\n\n", dt/TEST_SIZE, 0.5, ((double)(stop-start)/CLOCKS_PER_SEC)); printf("Testing rnd_int\n"); total = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { r = rnd_int(rnd,0,19); dist[r]++; total += r; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 0, 19, (double)total/TEST_SIZE, (0+19)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_roll\n"); for (i=0; i < 20; i++) dist[i] = 0; total = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { r = rnd_roll(rnd,3,6); dist[r]++; total += r; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 3, 18, (double)total/TEST_SIZE, (3+18)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_roll_mid\n"); for (i=0; i < 20; i++) dist[i] = 0; total = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { r = rnd_roll_mid(rnd,18); dist[r]++; total += r; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 1, 18, (double)total/TEST_SIZE, (1+18)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_dist_uniform\n"); for (i=0; i < 20; i++) dist[i] = 0; dt = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dr = rnd_dist_uniform(rnd,0,20); dt += dr; dist[(int)floor(dr)]++; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 0, 19, (double)dt/TEST_SIZE, (0+20)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_dist_normal\n"); for (i=0; i < 20; i++) dist[i] = 0; dt = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dr = rnd_dist_normal(rnd,10,1.0); dt += dr; dist[(int)floor(dr)]++; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 0, 19, (double)dt/TEST_SIZE, (0+20)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_dist_triangle\n"); for (i=0; i < 20; i++) dist[i] = 0; dt = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dr = rnd_dist_triangle(rnd,0,20); dt += dr; dist[(int)floor(dr)]++; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 0, 19, (double)dt/TEST_SIZE, (0+20)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); printf("Testing rnd_dist_irwin_hall\n"); for (i=0; i < 20; i++) dist[i] = 0; dt = 0; start = clock(); for (i=0; i < TEST_SIZE; i++) { dr = rnd_dist_irwin_hall(rnd,12,4,16); dt += dr; dist[(int)floor(dr)]++; } stop = clock(); printf("Range: %d-%d: Avg=%5.3f (Expected: %5.3f) (Time: %5.3f)\n", 0, 19, (double)dt/TEST_SIZE, (0+20)/2.0, ((double)(stop-start)/CLOCKS_PER_SEC)); print_dist(0, 19, dist); }