-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGeneticOperations.cs
More file actions
176 lines (147 loc) · 6.15 KB
/
GeneticOperations.cs
File metadata and controls
176 lines (147 loc) · 6.15 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
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
165
166
167
168
169
170
171
172
173
174
175
176
using System.Collections.Generic;
using UnityEngine;
public class GeneticOperations
{
private System.Random random;
public GeneticOperations(int seed = -1)
{
random = seed >= 0 ? new System.Random(seed) : new System.Random();
}
public ExpressionNode GenerateRandomTree(int maxDepth, float constantRange = 10f)
{
if (maxDepth <= 0 || random.NextDouble() < 0.3)
{
if (random.NextDouble() < 0.5)
return new ExpressionNode(NodeType.Variable);
else
return new ExpressionNode(NodeType.Constant,
(float)(random.NextDouble() * constantRange * 2 - constantRange));
}
NodeType[] binaryOps = { NodeType.Add, NodeType.Subtract, NodeType.Multiply,
NodeType.Divide, NodeType.Power };
NodeType[] unaryOps = { NodeType.Sin, NodeType.Cos, NodeType.Log,
NodeType.Exp, NodeType.Sqrt };
bool useBinary = random.NextDouble() < 0.7;
ExpressionNode node;
if (useBinary)
{
node = new ExpressionNode(binaryOps[random.Next(binaryOps.Length)]);
node.left = GenerateRandomTree(maxDepth - 1, constantRange);
node.right = GenerateRandomTree(maxDepth - 1, constantRange);
}
else
{
node = new ExpressionNode(unaryOps[random.Next(unaryOps.Length)]);
node.left = GenerateRandomTree(maxDepth - 1, constantRange);
}
return node;
}
public void MutateConstant(ExpressionNode node, float mutationStrength = 1f)
{
List<ExpressionNode> constants = new List<ExpressionNode>();
CollectConstants(node, constants);
if (constants.Count > 0)
{
ExpressionNode target = constants[random.Next(constants.Count)];
target.constantValue += (float)((random.NextDouble() * 2 - 1) * mutationStrength);
}
}
public void MutateSubtree(ExpressionNode node, int maxDepth = 3)
{
List<ExpressionNode> allNodes = new List<ExpressionNode>();
CollectAllNodes(node, allNodes);
if (allNodes.Count > 1)
{
ExpressionNode target = allNodes[random.Next(1, allNodes.Count)];
ExpressionNode newSubtree = GenerateRandomTree(maxDepth);
target.nodeType = newSubtree.nodeType;
target.constantValue = newSubtree.constantValue;
target.left = newSubtree.left;
target.right = newSubtree.right;
}
}
public void MutateDelete(ExpressionNode node)
{
List<ExpressionNode> nodes = new List<ExpressionNode>();
CollectAllNodes(node, nodes);
if (nodes.Count > 1)
{
ExpressionNode target = nodes[random.Next(1, nodes.Count)];
if (target.left != null && target.right == null)
{
ExpressionNode child = target.left;
target.nodeType = child.nodeType;
target.constantValue = child.constantValue;
target.left = child.left;
target.right = child.right;
}
else if (target.left != null && target.right != null)
{
ExpressionNode child = random.NextDouble() < 0.5 ? target.left : target.right;
target.nodeType = child.nodeType;
target.constantValue = child.constantValue;
target.left = child.left;
target.right = child.right;
}
}
}
public void MutateSimplify(ExpressionNode node)
{
SimplifyNode(node);
}
private bool SimplifyNode(ExpressionNode node)
{
if (node == null) return false;
bool leftSimplified = node.left != null && SimplifyNode(node.left);
bool rightSimplified = node.right != null && SimplifyNode(node.right);
if (IsConstantSubtree(node) && node.nodeType != NodeType.Constant)
{
float value = node.Evaluate(0);
node.nodeType = NodeType.Constant;
node.constantValue = value;
node.left = null;
node.right = null;
return true;
}
return leftSimplified || rightSimplified;
}
private bool IsConstantSubtree(ExpressionNode node)
{
if (node == null) return true;
if (node.nodeType == NodeType.Variable) return false;
if (node.nodeType == NodeType.Constant) return true;
return IsConstantSubtree(node.left) && IsConstantSubtree(node.right);
}
public Individual Crossover(Individual parent1, Individual parent2)
{
ExpressionNode child = parent1.root.Clone();
List<ExpressionNode> childNodes = new List<ExpressionNode>();
List<ExpressionNode> parent2Nodes = new List<ExpressionNode>();
CollectAllNodes(child, childNodes);
CollectAllNodes(parent2.root, parent2Nodes);
if (childNodes.Count > 0 && parent2Nodes.Count > 0)
{
ExpressionNode crossoverPoint1 = childNodes[random.Next(childNodes.Count)];
ExpressionNode crossoverPoint2 = parent2Nodes[random.Next(parent2Nodes.Count)].Clone();
crossoverPoint1.nodeType = crossoverPoint2.nodeType;
crossoverPoint1.constantValue = crossoverPoint2.constantValue;
crossoverPoint1.left = crossoverPoint2.left;
crossoverPoint1.right = crossoverPoint2.right;
}
return new Individual(child);
}
private void CollectConstants(ExpressionNode node, List<ExpressionNode> constants)
{
if (node == null) return;
if (node.nodeType == NodeType.Constant) constants.Add(node);
CollectConstants(node.left, constants);
CollectConstants(node.right, constants);
}
private void CollectAllNodes(ExpressionNode node, List<ExpressionNode> nodes)
{
if (node == null) return;
nodes.Add(node);
CollectAllNodes(node.left, nodes);
CollectAllNodes(node.right, nodes);
}
}