- Timestamp:
- 07/14/20 20:20:54 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/_demos/evol_test.cpp
r1007 r1008 5 5 6 6 #include <vector> 7 #include <numeric> //std::accumulate() 7 8 #include "common/loggers/loggertostdout.h" 8 9 #include "frams/genetics/preconfigured.h" … … 22 23 } 23 24 24 void evaluate_fitness(Individual &ind, const char *fitness_def)25 double get_fitness(const Individual &ind, const char *fitness_def) 25 26 { 26 27 SString genotype = ind.geno.getGenes(); … … 32 33 switch (*p) 33 34 { 35 case '0': 36 break; 37 case '!': //special symbol for current fitness (used only in printing population stats) 38 fitness += ind.fitness; 39 break; 34 40 case 'l': 35 41 case 'L': … … 59 65 p++; 60 66 } 61 ind.fitness = fitness; 67 return fitness; 68 } 69 70 void update_fitness(Individual &ind, const char *fitness_def) 71 { 72 ind.fitness = get_fitness(ind, fitness_def); 73 } 74 75 void print_stats(const vector<Individual> &population, char criterion) 76 { 77 vector<double> criterion_values; 78 char crit[2] = { 0 }; 79 crit[0] = criterion; 80 for (const Individual& ind : population) 81 criterion_values.push_back(get_fitness(ind, crit)); 82 printf("%g,%g,%g", *std::min_element(criterion_values.begin(), criterion_values.end()), 83 std::accumulate(criterion_values.begin(), criterion_values.end(), 0.0) / criterion_values.size(), 84 *std::max_element(criterion_values.begin(), criterion_values.end())); 62 85 } 63 86 … … 76 99 77 100 78 / ** A minimalistic steady-state evolutionary algorithm. */101 // A minimalistic steady-state evolutionary algorithm. 79 102 int main(int argc, char *argv[]) 80 103 { … … 93 116 printf("Too few parameters!\n"); 94 117 printf("Command line: <deterministic?_0_or_1> <population_size> <nr_evaluations> <prob_mut> <prob_xover> <genetic_format> <fitness_definition>\n"); 95 printf("Example: 1 10 50 0. 5 0.5 4 NC\n\n\n");118 printf("Example: 1 10 50 0.6 0.4 4 NC\n\n"); 96 119 printf("Fitness definition is a sequence of capital (+1 weight) and small (-1 weight) letters.\n"); 97 120 printf("Each letter corresponds to one fitness criterion, and they are all weighted and added together.\n"); 121 printf(" 0 - a constant value of 0 that provides a flat fitness landscape (e.g. for testing biases of genetic operators).\n"); 98 122 printf(" l or L - genotype length in characters.\n"); 99 123 printf(" p or P - the number of Parts.\n"); … … 103 127 //TODO add b - bounding box volume (from Model), s - surface area (from geometry), v - volume (from geometry), h,w,d - three consecutive dimensions (from geometry) 104 128 129 printf("\nThe output contains 7 columns separated by the TAB character.\n"); 130 printf("The first column is the number of mutated or crossed over and evaluated genotypes.\n"); 131 printf("The remaining columns are triplets of min,avg,max (in the population) of fitness, Parts, Joints, Neurons, Connections, genotype characters.\n"); 132 printf("Finally, the genotypes in the last population are printed with their fitness values.\n"); 105 133 return 1; 106 134 } … … 126 154 return 2; 127 155 } 128 evaluate_fitness(ind, fitness_def);156 update_fitness(ind, fitness_def); 129 157 } 130 158 for (int i = 0; i < nr_evals; i++) … … 136 164 if (rnd < prob_mut) 137 165 { 138 population[selected_negative].geno = genman.mutate(population[selected_positive].geno); 139 //TODO handle failed mutation 140 evaluate_fitness(population[selected_negative], fitness_def); 166 Geno mutant = genman.mutate(population[selected_positive].geno); 167 if (mutant.getGenes() == "") 168 { 169 printf("Failed mutation (%s) of '%s'\n", mutant.getComment().c_str(), population[selected_positive].geno.getGenes().c_str()); 170 } 171 else 172 { 173 population[selected_negative].geno = mutant; 174 update_fitness(population[selected_negative], fitness_def); 175 } 141 176 } 142 177 else 143 178 { 144 //TODO crossover 179 int selected_positive2 = tournament(population, max(2, int(sqrt(population.size()) / 2))); 180 Geno xover = genman.crossOver(population[selected_positive].geno, population[selected_positive2].geno); 181 if (xover.getGenes() == "") 182 { 183 printf("Failed crossover (%s) of '%s' and '%s'\n", xover.getComment().c_str(), population[selected_positive].geno.getGenes().c_str(), population[selected_positive2].geno.getGenes().c_str()); 184 } 185 else 186 { 187 population[selected_negative].geno = xover; 188 update_fitness(population[selected_negative], fitness_def); 189 } 145 190 } 146 191 147 192 if (i % population.size() == 0 || i == nr_evals - 1) 148 printf("Evaluation %d\t...\n", i); //TODO print min,avg,max fitness \t min,avg,max genotype length \t min,avg,max parts \t min,avg,max neurons 193 { 194 printf("Evaluation %d\t", i); 195 for (char c : string("!PJNCL")) 196 { 197 print_stats(population, c); 198 printf("\t"); 199 } 200 printf("\n"); 201 } 149 202 } 150 203 for (const Individual& ind : population)
Note: See TracChangeset
for help on using the changeset viewer.