-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeGenerator.java
More file actions
112 lines (102 loc) · 4.09 KB
/
Copy pathTreeGenerator.java
File metadata and controls
112 lines (102 loc) · 4.09 KB
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
package GP;
import GP.functions.*;
public class TreeGenerator {
private Node generateRandomTreeHelper(int height, TerminalNode[] terminals) {
if (terminals == null || terminals.length == 0) {
throw new IllegalArgumentException("Terminals array cannot be null or empty");
}
// Base case: If height is 0, return a TerminalNode
if (height == 0) {
int randomIndex = Utils.getGlobalRandom().nextInt(terminals.length);
return terminals[randomIndex].clone();
}
// Recursive case: Randomly decide between UnopNode and BinopNode
if (Utils.getGlobalRandom().nextBoolean()) {
Node child = generateRandomTreeHelper(height - 1, terminals);
UnopNode result = createRandomUnopNode(child);
if (child instanceof TerminalNode) {
((TerminalNode) child).parent = result;
}
if (child instanceof OperatorNode) {
((OperatorNode) child).parent = result;
}
return result;
} else {
Node leftChild = generateRandomTreeHelper(height - 1, terminals);
Node rightChild = generateRandomTreeHelper(height - 1, terminals);
BinopNode result = createRandomBinopNode(leftChild, rightChild);
if (leftChild instanceof TerminalNode) {
((TerminalNode) leftChild).parent = result;
}
if (leftChild instanceof OperatorNode) {
((OperatorNode) leftChild).parent = result;
}
if (rightChild instanceof TerminalNode) {
((TerminalNode) rightChild).parent = result;
}
if (rightChild instanceof OperatorNode) {
((OperatorNode) rightChild).parent = result;
}
return result;
}
}
public OperatorNode generateRandomTree(int height, TerminalNode[] terminals) {
if (Config.DEBUG_PRINT) System.out.println("generateRandomTree: height = " + height);
if (height < 0) {
throw new IllegalArgumentException("Height cannot be negative");
}
if (terminals == null || terminals.length == 0) {
throw new IllegalArgumentException("Terminals array cannot be null or empty");
}
Node root = generateRandomTreeHelper(height, terminals);
if (root instanceof OperatorNode) {
return (OperatorNode) root;
} else {
throw new IllegalStateException("Generated tree root is not an OperatorNode");
}
}
private UnopNode createRandomUnopNode(Node child) {
switch (Utils.getGlobalRandom().nextInt(10)) {
case 0:
return new SinNode(child);
case 1:
return new CosNode(child);
case 2:
return new TanNode(child);
case 3:
return new SinSquaredNode(child);
case 4:
return new CosSquaredNode(child);
case 5:
return new TanSquaredNode(child);
case 6:
return new ExpNode(child);
case 7:
return new LogNode(child);
case 8:
return new SqrtNode(child);
case 9:
return new AbsNode(child);
default:
throw new IllegalStateException("Unexpected value");
}
}
private BinopNode createRandomBinopNode(Node leftChild, Node rightChild) {
switch (Utils.getGlobalRandom().nextInt(4)) {
case 0:
return new AddNode(leftChild, rightChild);
case 1:
return new SubtractNode(leftChild, rightChild);
case 2:
return new MultiplyNode(leftChild, rightChild);
case 3:
return new DivideNode(leftChild, rightChild);
case 4:
return new MinNode(leftChild, rightChild);
case 5:
return new MaxNode(leftChild, rightChild);
default:
throw new IllegalStateException("Unexpected value");
}
}
}