-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDNA.pde
101 lines (72 loc) · 2.28 KB
/
DNA.pde
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
101
// Akash Yadav
// @Hudson Lane, Delhi
// 26th June 18
// http://natureofcode.com
// Genetic Algorithm, Evolving Shakespeare
// A class to describe a psuedo-DNA, i.e. genotype
// Here, a virtual organism's DNA is an array of character.
// Functionality:
// -- convert DNA into a string
// -- calculate DNA's "fitness"
// -- mate DNA with another set of DNA
// -- mutate DNA
class DNA {
// The genetic sequence
PImage genes;
float fitness;
// Constructor (makes a random DNA)
DNA(PImage roleModel) {
//rolemodel is needed just for dimention
genes = createImage(roleModel.width, roleModel.height, RGB);
genes.loadPixels();
for(int i = 0; i < genes.pixels.length; i++){
float clr = (int)random(256)%256;
genes.pixels[i] = color(clr, clr, clr);
}
genes.updatePixels();
}
// Returns genes image
PImage getImage() {
return genes;
}
// Fitness function (returns floating point % of "correct" characters)
void fitness (PImage target) {
int score = 0;
genes.loadPixels();
target.loadPixels();
for (int i = 0; i < genes.pixels.length; i++)
if (genes.pixels[i] == target.pixels[i])
score++;
fitness = (float)score / (float)target.pixels.length;
genes.updatePixels();
target.updatePixels();
}
// Crossover
DNA crossover(DNA partner) {
// A new child
DNA child = new DNA(this.genes);
int midpoint = int(random(genes.pixels.length)); // Pick a midpoint
this.genes.loadPixels();
child.genes.loadPixels();
partner.genes.loadPixels();
// Half from one, half from the other
for (int i = 0; i < genes.pixels.length; i++) {
if (i > midpoint) child.genes.pixels[i] = this.genes.pixels[i];
else child.genes.pixels[i] = partner.genes.pixels[i];
}
this.genes.updatePixels();
child.genes.updatePixels();
partner.genes.updatePixels();
return child;
}
// Based on a mutation probability, picks a new random character
void mutate(float mutationRate) {
this.genes.loadPixels();
for (int i = 0; i < genes.pixels.length; i++)
if (random(1) < mutationRate){
float clr = (int)random(256)%256;
genes.pixels[i] = color(clr, clr, clr);
}
this.genes.updatePixels();
}
}