-
Notifications
You must be signed in to change notification settings - Fork 6
/
ConnectionGene.js
47 lines (40 loc) · 1.88 KB
/
ConnectionGene.js
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
//a connection between 2 nodes
class connectionGene {
constructor(from, to, w, inno) {
this.fromNode = from;
this.toNode = to;
this.weight = w;
this.enabled = true;
this.innovationNo = inno; //each connection is given a innovation number to compare genomes
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//changes the this.weight
mutateWeight() {
var rand2 = random(1);
if (rand2 < 0.1) { //10% of the time completely change the this.weight
this.weight = random(-1, 1);
} else { //otherwise slightly change it
this.weight += (randomGaussian() / 50);
//keep this.weight between bounds
if (this.weight > 1) {
this.weight = 1;
}
if (this.weight < -1) {
this.weight = -1;
}
}
}
//----------------------------------------------------------------------------------------------------------
//returns a copy of this connectionGene
clone(from, to) {
var clone = new connectionGene(from, to, this.weight, this.innovationNo);
clone.enabled = this.enabled;
return clone;
}
//-------------------------------------------------------------------------------------------------------------------
//returns whether or not these 2 connection genes are the same
isEqualTo(otherConnectionGene) {
return this.fromNode.number === otherConnectionGene.fromNode.number && this.toNode.number === otherConnectionGene.toNode.number
&& this.weight === otherConnectionGene.weight && this.enabled === otherConnectionGene.enabled && this.innovationNo === otherConnectionGene.innovationNo;
}
}