/
population.cpp
101 lines (82 loc) · 2.89 KB
/
population.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// population.cpp - copyright (C) 2001-2011 by Patrick Hanevold
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "population.h"
#include "net.h"
#include "genetic.h"
using namespace std;
inline int irand( int range ){
return( rand() % range );
}
// Create a population of individual neural networks
// input: individuals = the number of neural networks to create in the population
// inputs = the number of inputs in every neural network
// outputs = the number of outputs in every neural network
// a & b = neuron normalization values (min & max)
Population::Population(int individuals, int inputs, int outputs, double a, double b){
this->individuals = individuals;
int layers = 0;
for(int n=inputs; n>outputs; n/=2) layers++;
typedef Net* netptr;
individual = new netptr[individuals];
for(int n=0; n<individuals; n++){
individual[n] = new Net(inputs,outputs,layers,a,b);
}
}
// initialize a neural network with random weights
// input: i = which neural network in the population to initialize
// a & b = neuron normalization values (min & max)
void Population::init(int i, double a, double b){
individual[i]->init(a,b);
}
// mutate a single neuron with another by randomized blending
// input: i = the neural network to mix a random neuron in
// j = the other neural network to mix a random neuron with (random source neuron)
void Population::mix(int i, int j){
individual[i]->mix(individual[j]);
}
// mutate a random neuron in a neural network by radiation (noise blending)
// input: i = whch neural network to radiate a random neuron
// level = radiation level
void Population::mutate(int i, double level){
individual[i]->mutate(level);
}
void Population::copy(int i1, int i2){
(*individual[i1]) = (*individual[i2]);
}
void Population::copy(int i1, Population *pop, int i2){
*individual[i1] = *pop->getIndividual(i2);
}
void Population::swap(int i1, int i2){
Net *t = individual[i1];
individual[i1] = individual[i2];
individual[i2] = t;
}
Net *Population::getIndividual(int i){
return individual[i];
}
// inperfectly sort the population according to fitness
// input: cnt = the number of iterations in the sort algorithm (simple bouble sorting)
// gen = the gene pool the population belong to
void Population::sort(int cnt, Genetic *gen){
// first evaluate all individuals in population
double *scores = new double[individuals];
for(int n=0; n<individuals; n++){
gen->evaluate(individual[n]);
scores[n]=individual[n]->getScore();
if(isinf(scores[n])) scores[n]=-10000000;
}
// then sort the entire population according to score (lame sort)
for(int n=0; n<cnt; n++){
for(int i=0 ; i<(individuals-1); i++){
if(scores[i+1]>scores[i]){
swap(i,i+1);
double a=scores[i];
scores[i]=scores[i+1];
scores[i+1]=a;
}
}
}
delete []scores;
}