-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeNode.java
executable file
·54 lines (47 loc) · 1.28 KB
/
TreeNode.java
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
/* TreeNode.java - Superclass for all nodes */
package se.ga4;
import java.io.*;
import java.util.*;
import se.util.*;
public abstract class TreeNode implements Serializable, Cloneable {
static final long serialVersionUID=GeneticAlgorithm.globalSerial;
protected static boolean longform;
public static boolean mutated=false;
/** Create a node and fill it up with random nodes below */
public TreeNode() {
}
public abstract void init();
public abstract Object clone();
public abstract void mutate(double pmut);
public abstract void copy(TreeNode tn);
public abstract String string();
/* Return a random node */
static double poper=0.4; // must be less than 0.5 for binary operators
public static TreeNode randomNode() {
double psel=Util.dran(), pop=Util.dran();
TreeNode out;
if(pop<poper) {
if(psel<0.3) out=new AddNode();
else if(psel<0.6) out=new NegateNode();
else out=new MultiplyNode();
}
else {
if(psel<0.5) out=new ParNode();
else out=new VarNode();
}
out.init();
return out;
}
public static double randomPar(double min, double max) {
double x=Util.dran(Math.log(min),Math.log(max));
return Math.exp(x);
}
public String toString() {
longform=false;
return string();
}
public String toCode() {
longform=true;
return string();
}
}