-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeInitial.java
164 lines (147 loc) · 5.06 KB
/
NodeInitial.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Random;
public class NodeInitial {
Boolean isTerminal;
char value;
NodeInitial leftChild;
NodeInitial rightChild;
Random rand;
//function set: (arity 2)
// - A: If food in column up
// - B: If food in row right
// - C: If food in column down
// - D: If food in row left
// - ifDanger1Up - if danger is in cell 2 - 'E'
// - ifDanger1Right - if danger is in cell 4 - 'F'
// - ifDanger1Down - if danger is in cell 6 - 'G'
// - ifDanger1Left - if danger is in cell 8- 'H'
char[] functionSet = {'A' , 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
//terminal set:
// - moveUp - move up - 'U'
// - moveRight - move right - 'R'
// - moveDown - move down - 'D'
// - moveLeft - move left - 'L'
char[] terminalSet = {'U', 'R', 'D', 'L'};
public NodeInitial(Boolean isTerminal, Random rand) {
this.isTerminal = isTerminal;
leftChild = null;
rightChild = null;
this.rand = rand;
if(isTerminal) {
value = terminalSet[rand.nextInt(4)];
}
else {
value = functionSet[rand.nextInt(8)];
}
}
public NodeInitial copyNode() {
if(isTerminal) {
Random rand2 = new Random(this.rand.nextInt());
NodeInitial newNode = new NodeInitial(isTerminal, rand2);
newNode.value = value;
return newNode;
}
else {
Random rand2 = new Random(this.rand.nextInt());
NodeInitial newNode = new NodeInitial(isTerminal, rand2);
newNode.value = value;
newNode.leftChild = leftChild.copyNode();
newNode.rightChild = rightChild.copyNode();
return newNode;
}
}
//mutate the node
public void mutate(int chance, int depth){
//randomly change the value if the chance is less than the chance percent + depth
if(rand.nextInt(100) < chance ){
if(isTerminal){
value = terminalSet[rand.nextInt(4)];
}
else{
//small chance to change the node to a terminal, else randomly change the value
if(rand.nextInt(100) < 30 && depth != 0 && depth != 1 && depth != 2){
isTerminal = true;
value = terminalSet[rand.nextInt(4)];
leftChild = null;
rightChild = null;
}
else{
value = functionSet[rand.nextInt(8)];
}
}
}
else{
if(!isTerminal){
if(rand.nextInt(100)<50){
leftChild.mutate(chance, depth + 1);
}
else{
rightChild.mutate(chance, depth + 1);
}
}
}
}
public Boolean crossover(NodeInitial node, int chance){
if (rand.nextInt(100) < chance){
//replace this node with the other node
value = node.value;
isTerminal = node.isTerminal;
leftChild = node.leftChild;
rightChild = node.rightChild;
return true;
}
else{
if(!isTerminal && !node.isTerminal){
if(this.leftChild.crossover(node.leftChild, chance)){
return true;
}
else if(this.rightChild.crossover(node.rightChild, chance)){
return true;
}
}
return false;
}
}
public String printTree(){
if(isTerminal){
if(value == 'U'){
return "U";
}
else if(value == 'R'){
return "R";
}
else if(value == 'D'){
return "D";
}
else{
return "L";
}
}
else{
if(value == 'A'){
return "FU(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'B'){
return "FR(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'C'){
return "FD(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'D'){
return "FL(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'E'){
return "D1U(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'F'){
return "D1R(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'G'){
return "D1D(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else if(value == 'H'){
return "D1L(" + leftChild.printTree() + ", " + rightChild.printTree() + ")";
}
else throw new IllegalArgumentException("Invalid node value");
}
}
}