forked from carykh/evolutionSteer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Axon.pde
49 lines (45 loc) · 1.28 KB
/
Axon.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
class Axon {
final double MUTABILITY_MUTABILITY = 0.7;
final int mutatePower = 9;
final double MUTATE_MULTI;
double weight;
double mutability;
public Axon(double w, double m){
weight = w;
mutability = m;
MUTATE_MULTI = Math.pow(0.8,mutatePower);
}
public Axon copyAxon(){
return new Axon(this.weight,this.mutability);
}
public Axon mutateAxon(){
double mutabilityMutate = Math.pow(0.5,pmRan()*MUTABILITY_MUTABILITY);
return new Axon(weight+r()*mutability/MUTATE_MULTI,mutability*mutabilityMutate);
}
public double r(){
return Math.pow(pmRan(),mutatePower);
}
public double pmRan(){
return (Math.random()*2-1);
}
public void saveToJson(JsonGenerator g){
try {
g.writeNumberField("weight", weight);
g.writeNumberField("mutability", mutability);
} catch(Exception e){
writeToErrorLog(e);
}
}
public void loadFromJson(JsonParser p){
try{
while(p.nextToken() != JsonToken.END_OBJECT){
String fieldName = p.getCurrentName();
p.nextToken();
if(fieldName.equals("weight")){ this.weight = p.getDoubleValue(); }
else if(fieldName.equals("mutability")){ this.mutability = p.getDoubleValue(); }
}
} catch(Exception e){
writeToErrorLog(e);
}
}
}