Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Fitness {
- public float evaluate(Specimen s);
- }
-
- public interface Breeder {
- public Specimen breed(Specimen[] parents);
- }
-
- class Population {
- Specimen[] generation;
- int size;
- float[] scores;
- float maxScore;
- int genotypeSize;
-
- Population(int size, int genotypeSize) {
- this.size = size;
- scores = new float[size];
- generation = new Specimen[size];
- this.genotypeSize = genotypeSize;
-
- for(int i = 0; i < size; i++) {
- scores[i] = 0;
- generation[i] = new Specimen(genotypeSize);
- }
-
- maxScore = 0;
- }
-
- void evaluate(Fitness environment) {
-
- for(int i = 0; i < size; i++) {
- scores[i] = environment.evaluate(generation[i]);
- if(scores[i] > maxScore) maxScore = scores[i];
- }
- }
-
- float getMaxScore() {
- return maxScore;
- }
-
- float getAverageScore() {
- float sum = 0;
- for(int i = 0; i < size; i++)
- sum += scores[i];
- return sum / float(size);
- }
-
-
- float[] getNormalizedScores() {
- if(maxScore == 0) return scores;
-
- float[] normalizedScores = new float[size];
- for(int i = 0; i < size; i++)
- normalizedScores[i] = scores[i] / maxScore;
-
- return normalizedScores;
- }
-
- float[] getPowerScores(float evolutionaryPressure) {
- if(evolutionaryPressure == 1) return scores;
-
- float[] powerScores = new float[size];
- for(int i = 0; i < size; i++)
- powerScores[i] = pow(scores[i], evolutionaryPressure);
-
- return powerScores;
- }
-
- void breed(float mutationRate) {
- breed(mutationRate, 1);
- }
-
- void breed(float mutationRate, float evolutionaryPressure, Breeder breeder) {
- float[] powerScores = getPowerScores(evolutionaryPressure);
- float maxPowerScore = pow(maxScore, evolutionaryPressure);
- Specimen nextGeneration[] = new Specimen[size];
- for(int i = 0; i < size; i++) {
- Specimen[] parents = {getRandomSpecimen(powerScores, maxPowerScore),
- getRandomSpecimen(powerScores, maxPowerScore)};
- Specimen child = breeder.breed(parents);
- child.mutate(mutationRate);
- nextGeneration[i] = child;
- }
-
- generation = nextGeneration;
- }
-
- void breed(float mutationRate, float evolutionaryPressure) {
- breed(mutationRate, evolutionaryPressure, new Breeder() {
- public Specimen breed(Specimen[] parents) {
- float[] genotype = new float[genotypeSize];
- for(int g = 0; g < genotypeSize; g++)
- genotype[g] = parents[floor(random(parents.length))].getGene(g);
- return new Specimen(genotype);
- }
- });
- }
-
- Specimen getRandomSpecimen(float[] scores, float maxScore) {
- while(true) {
- int index = int(random(size));
- if(random(maxScore) >= scores[index]) return generation[index];
- }
- }
-
- float[][] getGenerationAsArray() {
- float[][] generationArray = new float[size][genotypeSize + 1];
- for(int i = 0; i < size; i++)
- generationArray[i] = append(generation[i].getGenotype(), scores[i]);
- return generationArray;
- }
- }
-
- class Specimen {
- float[] genotype;
-
- Specimen(int genotypeSize) {
- genotype = new float[genotypeSize];
- for(int i = 0; i < genotype.length; i++)
- genotype[i] = random(1);
- }
-
- Specimen(float[] genotype) {
- this.genotype = genotype;
- }
-
- float[] getGenotype() {
- return genotype;
- }
-
- float getGene(int i) {
- return genotype[i];
- }
-
- int getGenotypeSize() {
- return genotype.length;
- }
-
- void mutate(float mutationRate) {
- for(int i = 0; i < genotype.length; i++) {
- while(random(1) < mutationRate)
- genotype[i] += random(-0.001, 0.001);
- while(genotype[i] < 0) genotype[i]++;
- while(genotype[i] > 1) genotype[i]--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement