/* Main evolution method, called by user and handles main loop */ static VALUE method_evolve(VALUE self, VALUE vN_gens, VALUE vPop_size) { // This is the object to be evolved: store globally phenome = self; // Process method parameters int n_gens = FIX2INT(vN_gens); int pop_size = FIX2INT(vPop_size); // We need to call encode once, to obtain genome length VALUE ary_genome = rb_funcall(self, rb_intern("encode"), 0); int g_len = RARRAY(ary_genome)->len; // space for new genotype to be created char *child_genome = (char *)malloc(g_len * sizeof(char)); // create a new population with random genotypes Population *pop = new_population(self, pop_size, g_len, TRUE); update_fitness(pop, g_len); // main loop int g; for(g=0; g < n_gens; ++g) { if(g % 100 == 0) printf("Processing generation %d\n", g); Candidate *c1 = tournament_select(pop); Candidate *c2 = tournament_select(pop); two_point_crossover(child_genome, c1, c2, g_len); Candidate *low = weakest(pop); low->genome = child_genome; low->rq_update = TRUE; update_fitness(pop, g_len); Candidate *high = strongest(pop); printf("weakest is %i\n", low->fitness); printf("strongest is %i\n", high->fitness); } }
int hivpopulation::read_resistance_coefficients(istream &model){ if (HIVPOP_VERBOSE){ cerr<<"hivpopulation::read_resistance_coefficients(): read coefficients "; } if (model.bad()){ cerr<<"hivpopulation::read_resistance_coefficients(): BAD MODEL STREAM!"<<endl; return HIVPOP_BADARG; } double val, wt_resistance=0; vector <int> loci; vector<string> strs; string line; // reset the hypercube trait[1].reset(); // read the stream while(!model.eof()){ strs.clear(); loci.clear(); getline(model, line); boost::split(strs, line, boost::is_any_of("\t ")); //cout <<"a "<<line<<" "<<strs.size(); if (strs.size()>1){ for (unsigned int entry=0; entry<strs.size()-1; entry++){ loci.push_back(atoi(strs[entry].c_str())); //cout<<loci.back()<<" "<<strs[entry].c_str()<<" "; } val=atof(strs.back().c_str()); add_trait_coefficient(val, loci,1); wt_resistance+=val*pow(-1.0,(double)loci.size()); //cout <<loci.size()<<" "<<wt_resistance<<endl; } //cout<<loci[0]<<" "<<val<<" "<<loci.size()<<endl; } trait[1].hypercube_mean=-wt_resistance; // update the replication and fitness of all clones update_traits(); update_fitness(); if (HIVPOP_VERBOSE){ cerr<<"...done"<<endl; } return 0; }
/* * every two prisoners play against each other */ void play_game(Group* grp) { int A, B; // indexes of player A and B int num_players = grp->num_chrs; // reset fitness to 0 before every iteration reset_fitness(grp); for (A = 0; A < num_players; A++) { for (B = A + 1; B < num_players; B++) { select_tactics(grp, A, B); update_fitness(grp, A, B); update_history(grp, A, B); } } }
int main(int argc, char* argv[]) { srand((unsigned)time(NULL)); int i, rank, size; int job_id; int num_slaves; // # of slaves int num_gen = 100; // # of generations int num_genes = 2; // # of gene segments int num_chrs = 8; // # of chromosomes double cross_rate = 0.95; // crossover rate double mutate_rate = 0.001; // mutation rate double fitness; unsigned char* genes = (unsigned char*)malloc(num_genes * sizeof(unsigned char)); Group* grp = init_group(num_genes, num_chrs, cross_rate, mutate_rate); MPI_Status stat; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); /* master is here */ if (0 == rank) { num_slaves = size - 1; job_id = 0; for (i = 1; i < size; i++) { // send next job i, send grp->chrs[i]->genes MPI_Send(grp->chrs[job_id]->genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); job_id++; } while (job_id < num_chrs) { i = job_id % num_slaves + 1; // get result s, recv grp->chrs[s]->genes MPI_Recv(&(grp->chrs[job_id-num_slaves]->fitness), 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, &stat); // send next job s, send grp->chrs[s]->genes MPI_Send(grp->chrs[job_id]->genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); job_id++; } int job_left = num_chrs % num_slaves; for (i = 0; i < num_genes; i++) { genes[i] = 0; } for (i = 1; i < size; i++) { int pid = (i + job_left - 1) % num_slaves + 1; // get result s, recv grp->chrs[s]->fitness MPI_Recv(&(grp->chrs[job_id-num_slaves]->fitness), 1, MPI_DOUBLE, pid, 0, MPI_COMM_WORLD, &stat); job_id++; // no more jobs s, send genes with all 0 values to represent no job MPI_Send(genes, num_genes, MPI_UNSIGNED_CHAR, i, 0, MPI_COMM_WORLD); } } else { /* slaves are here */ while (1) { // get next job s, recv grp->chrs[s]->genes MPI_Recv(genes, num_genes, MPI_UNSIGNED_CHAR, 0, 0, MPI_COMM_WORLD, &stat); // stop process if no job unsigned char sum = 0; for (i = 0; i < num_genes; i++) { sum += genes[i]; } // if sum of values of all gene segments is 0, // it means no job and should stop if (0 == sum) break; // do work fitness = update_fitness(num_genes, genes); // send result s, send fitness MPI_Send(&fitness, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD); } } /* Genetic Algorithm Process done by master */ if (0 == rank) { for (i = 0; i < num_gen; i++) { update_fit_rate(grp); evolve(grp); } } free(genes); free_group(grp); MPI_Finalize(); return 0; }
/* * Main function. */ int main(int argc, char *args[]) { float fitness; // The type of formation is decided by the user through the world char* formation = DEFAULT_FORMATION; if(argc == 2) { formation_type = atoi(args[1]); } if(formation_type == 0) formation = "line"; else if(formation_type == 1) formation = "column"; else if(formation_type == 2) formation = "wedge"; else if(formation_type == 3) formation = "diamond"; printf("Chosen formation: %s.\n", formation); //////////////////////////////////////////////////////////////////////////////////////////////////// // PSO // //////////////////////////////////////////////////////////////////////////////////////////////////// // The paramethers that we don't optimise are commented. // To add them to the simulation you should uncomment it here, in pso_ocba and change DIMENSIONALITY // each motorschema's weight parameter_ranges[0][0] = 0; parameter_ranges[0][1] = 10; parameter_ranges[1][0] = 0; parameter_ranges[1][1] = 10; parameter_ranges[2][0] = 0; parameter_ranges[2][1] = 10; parameter_ranges[3][0] = 0; parameter_ranges[3][1] = 10; /* parameter_ranges[4][0] = 0; parameter_ranges[4][1] = 4;*/ /* // thresholds parameter_ranges[ 5][0] = 0.001; // obstacle avoidance min parameter_ranges[ 5][1] = 0.5; parameter_ranges[ 6][0] = 0.001; // obstacle avoidance min + range parameter_ranges[ 6][1] = 1; parameter_ranges[ 7][0] = 0.001; // move_to_goal min parameter_ranges[ 7][1] = 0.5; parameter_ranges[ 8][0] = 0.001; // move_to_goal min + range parameter_ranges[ 8][1] = 1; parameter_ranges[ 9][0] = 0.001; // avoid_robot min parameter_ranges[ 9][1] = 0.2; parameter_ranges[10][0] = 0.001; // avoid_robot min + range parameter_ranges[10][1] = 1; parameter_ranges[11][0] = 0.001; // keep_formation min parameter_ranges[11][1] = 0.3; parameter_ranges[12][0] = 0.001; // keep_formation min + range parameter_ranges[12][1] = 1; // noise parameter_ranges[13][0] = 1; // noise_gen frequency parameter_ranges[13][1] = 20; parameter_ranges[14][0] = 0; // fade or not parameter_ranges[14][1] = 1; */ float optimal_params[DIMENSIONALITY]; initialize(); reset(); pso_ocba(optimal_params); // each motorschema's weight w_goal = optimal_params[0]; w_keep_formation = optimal_params[1]; w_avoid_robot = optimal_params[2]; w_avoid_obstacles = optimal_params[3];/* w_noise = optimal_params[4]; // thresholds avoid_obst_min_threshold = optimal_params[5]; avoid_obst_max_threshold = optimal_params[5] + optimal_params[6]; move_to_goal_min_threshold = optimal_params[7]; move_to_goal_max_threshold = optimal_params[7] + optimal_params[8]; avoid_robot_min_threshold = optimal_params[9]; avoid_robot_max_threshold = optimal_params[9] + optimal_params[10]; keep_formation_min_threshold = optimal_params[11]; keep_formation_max_threshold = optimal_params[11] + optimal_params[12]; // noise parameters noise_gen_frequency = optimal_params[13]; fading = round(optimal_params[14]); // = 0 or 1 */ printf("\n\n\nThe optimal parameters are: \n"); printf("___________ w_goal...................... = %f\n", w_goal); printf("___________ w_keep_formation............ = %f\n", w_keep_formation); printf("___________ w_avoid_robo................ = %f\n", w_avoid_robot); printf("___________ w_avoid_obstacles........... = %f\n", w_avoid_obstacles); printf("___________ w_noise..................... = %f\n", w_noise); printf("___________ noise_gen_frequency......... = %d\n", noise_gen_frequency); printf("___________ fading...................... = %d\n", fading); printf("___________ avoid_obst_min_threshold.... = %f\n", avoid_obst_min_threshold); printf("___________ avoid_obst_max_threshold.... = %f\n", avoid_obst_max_threshold); printf("___________ move_to_goal_min_threshold.. = %f\n", move_to_goal_min_threshold); printf("___________ move_to_goal_max_threshold.. = %f\n", move_to_goal_max_threshold); printf("___________ avoid_robot_min_threshold... = %f\n", avoid_robot_min_threshold); printf("___________ avoid_robot_max_threshold... = %f\n", avoid_robot_max_threshold); printf("___________ keep_formation_min_threshold = %f\n", keep_formation_min_threshold); printf("___________ keep_formation_max_threshold = %f\n", keep_formation_max_threshold); printf("\n\n"); //////////////////////////////////////////////////////////////////////////////////////////////////// // REAL RUN // //////////////////////////////////////////////////////////////////////////////////////////////////// // Real simulation with optimized parameters: initialize(); // reset and communication part printf("Beginning of the real simulation\n"); reset_to_initial_values(); printf("Supervisor reset.\n"); send_real_run_init_poses(); printf("Init poses sent.\n Chosen formation: %s.\n", formation); // sending weights send_weights(); printf("Weights sent\n"); bool is_goal_reached=false; // infinite loop for(;;) { wb_robot_step(TIME_STEP); // every 10 TIME_STEP (640ms) if (simulation_duration % 10 == 0) { send_current_poses(); update_fitness(); } simulation_duration += TIME_STEP; if (simulation_has_ended()) { is_goal_reached=true; printf("\n\n\n\n______________________________________GOAL REACHED!______________________________________\n\n"); // stop the robots w_goal = 0; w_keep_formation = 0; w_avoid_robot = 0; w_avoid_obstacles = 0; w_noise = 0; send_weights(); break; } } if (is_goal_reached){ fitness = compute_fitness(FORMATION_SIZE,loc); } else { fitness = compute_fitness(FORMATION_SIZE,loc); } printf("fitness = %f\n",fitness); while (1) wb_robot_step(TIME_STEP); //wait forever return 0; }