-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGPVisualizationManager.cs
More file actions
322 lines (265 loc) · 10.4 KB
/
GPVisualizationManager.cs
File metadata and controls
322 lines (265 loc) · 10.4 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GPVisualizationManager : MonoBehaviour
{
[Header("UI References")]
public Canvas mainCanvas;
public TextMeshProUGUI generationText;
public TextMeshProUGUI bestFitnessText;
public TextMeshProUGUI bestExpressionText;
public TextMeshProUGUI populationStatsText;
[Header("Graph Visualization")]
public RectTransform graphContainer;
public GameObject dotPrefab;
public Color graphLineColor = Color.cyan;
public float graphUpdateInterval = 0.1f;
[Header("Particle Effects")]
public ParticleSystem evolutionParticles;
public ParticleSystem bestFoundParticles;
public ParticleSystem migrationParticles;
[Header("Expression Tree Visualization")]
public RectTransform treeContainer;
public GameObject treeNodePrefab;
public float nodeSpacing = 80f;
public float levelSpacing = 100f;
private List<float> fitnessHistory;
private List<float> mseHistory;
private List<GameObject> graphDots;
private float lastGraphUpdate;
private List<GameObject> treeNodes;
void Start()
{
fitnessHistory = new List<float>();
mseHistory = new List<float>();
graphDots = new List<GameObject>();
treeNodes = new List<GameObject>();
SetupUI();
}
void SetupUI()
{
if (mainCanvas == null)
{
GameObject canvasObj = new GameObject("GP Visualization Canvas");
mainCanvas = canvasObj.AddComponent<Canvas>();
mainCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvasObj.AddComponent<CanvasScaler>();
canvasObj.AddComponent<GraphicRaycaster>();
}
CreateHolographicBackground();
}
void CreateHolographicBackground()
{
GameObject bgPanel = new GameObject("Holographic Background");
bgPanel.transform.SetParent(mainCanvas.transform, false);
RectTransform rect = bgPanel.AddComponent<RectTransform>();
rect.anchorMin = Vector2.zero;
rect.anchorMax = Vector2.one;
rect.sizeDelta = Vector2.zero;
Image img = bgPanel.AddComponent<Image>();
img.color = new Color(0, 0.1f, 0.2f, 0.3f);
}
public void UpdateVisualization(int generation, Individual best, List<Island> islands)
{
if (generationText != null)
generationText.text = $"<color=#00ffff>GENERATION:</color> {generation:D5}";
if (bestFitnessText != null)
bestFitnessText.text = $"<color=#00ff00>FITNESS:</color> {best.fitness:F6}";
if (bestExpressionText != null)
bestExpressionText.text = $"<color=#ffff00>f(x) = {best.root}</color>";
fitnessHistory.Add(best.fitness);
mseHistory.Add(best.mse);
// Calculate population statistics
float avgComplexity = 0f;
float avgFitness = 0f;
int totalPop = 0;
foreach (Island island in islands)
{
// Use foreach loop instead of Sum() to avoid LINQ issues
foreach (Individual ind in island.population)
{
avgComplexity += ind.complexity;
avgFitness += ind.fitness;
totalPop++;
}
}
if (totalPop > 0)
{
avgComplexity /= totalPop;
avgFitness /= totalPop;
}
if (populationStatsText != null)
{
populationStatsText.text = $"<color=#ff00ff>POPULATION:</color> {totalPop}\n" +
$"<color=#ff00ff>AVG COMPLEXITY:</color> {avgComplexity:F2}\n" +
$"<color=#ff00ff>AVG FITNESS:</color> {avgFitness:F4}\n" +
$"<color=#ff00ff>MSE:</color> {best.mse:F6}";
}
if (Time.time - lastGraphUpdate > graphUpdateInterval)
{
UpdateFitnessGraph();
lastGraphUpdate = Time.time;
}
EmitEvolutionParticles(best.fitness);
}
void UpdateFitnessGraph()
{
if (graphContainer == null || dotPrefab == null) return;
foreach (GameObject dot in graphDots)
{
Destroy(dot);
}
graphDots.Clear();
if (fitnessHistory.Count < 2) return;
float width = graphContainer.rect.width;
float height = graphContainer.rect.height;
float minFitness = fitnessHistory.Min();
float maxFitness = fitnessHistory.Max();
float range = maxFitness - minFitness;
if (range < 0.001f) range = 1f;
for (int i = 0; i < fitnessHistory.Count; i++)
{
float xPos = (i / (float)fitnessHistory.Count) * width;
float yPos = ((fitnessHistory[i] - minFitness) / range) * height;
GameObject dot = Instantiate(dotPrefab, graphContainer);
RectTransform dotRect = dot.GetComponent<RectTransform>();
dotRect.anchoredPosition = new Vector2(xPos, yPos);
Image dotImage = dot.GetComponent<Image>();
if (dotImage != null)
{
dotImage.color = Color.Lerp(Color.red, Color.green,
(fitnessHistory[i] - minFitness) / range);
}
graphDots.Add(dot);
if (i > 0)
{
DrawLine(graphDots[i - 1].GetComponent<RectTransform>().anchoredPosition,
dotRect.anchoredPosition, graphLineColor);
}
}
}
void DrawLine(Vector2 start, Vector2 end, Color color)
{
GameObject line = new GameObject("GraphLine");
line.transform.SetParent(graphContainer, false);
Image lineImg = line.AddComponent<Image>();
lineImg.color = color;
RectTransform rect = line.GetComponent<RectTransform>();
Vector2 dir = (end - start).normalized;
float distance = Vector2.Distance(start, end);
rect.sizeDelta = new Vector2(distance, 2f);
rect.anchoredPosition = start + dir * distance * 0.5f;
rect.localEulerAngles = new Vector3(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
graphDots.Add(line);
}
void EmitEvolutionParticles(float fitness)
{
if (evolutionParticles != null)
{
var emission = evolutionParticles.emission;
emission.rateOverTime = Mathf.Lerp(10f, 100f, Mathf.Abs(fitness) / 100f);
var main = evolutionParticles.main;
main.startColor = Color.Lerp(Color.red, Color.cyan,
Mathf.InverseLerp(-100f, 0f, fitness));
}
}
public void ShowBestFoundEffect(Vector3 position)
{
if (bestFoundParticles != null)
{
bestFoundParticles.transform.position = position;
bestFoundParticles.Play();
}
}
public void ShowMigrationEffect(int fromIsland, int toIsland)
{
if (migrationParticles != null)
{
migrationParticles.Play();
}
}
public void VisualizeExpressionTree(ExpressionNode root)
{
if (treeContainer == null || treeNodePrefab == null) return;
foreach (GameObject node in treeNodes)
{
Destroy(node);
}
treeNodes.Clear();
if (root == null) return;
Dictionary<ExpressionNode, Vector2> positions = new Dictionary<ExpressionNode, Vector2>();
CalculateTreePositions(root, positions, 0, 0, 1000f);
foreach (var kvp in positions)
{
GameObject nodeObj = Instantiate(treeNodePrefab, treeContainer);
RectTransform rect = nodeObj.GetComponent<RectTransform>();
rect.anchoredPosition = kvp.Value;
TextMeshProUGUI nodeText = nodeObj.GetComponentInChildren<TextMeshProUGUI>();
if (nodeText != null)
{
nodeText.text = GetNodeLabel(kvp.Key);
nodeText.color = GetNodeColor(kvp.Key.nodeType);
}
treeNodes.Add(nodeObj);
}
}
void CalculateTreePositions(ExpressionNode node, Dictionary<ExpressionNode, Vector2> positions,
float x, float y, float horizontalSpacing)
{
if (node == null) return;
positions[node] = new Vector2(x, y);
float newSpacing = horizontalSpacing / 2f;
if (node.left != null)
{
CalculateTreePositions(node.left, positions, x - newSpacing,
y - levelSpacing, newSpacing);
}
if (node.right != null)
{
CalculateTreePositions(node.right, positions, x + newSpacing,
y - levelSpacing, newSpacing);
}
}
string GetNodeLabel(ExpressionNode node)
{
switch (node.nodeType)
{
case NodeType.Variable: return "X";
case NodeType.Constant: return node.constantValue.ToString("F2");
case NodeType.Add: return "+";
case NodeType.Subtract: return "-";
case NodeType.Multiply: return "×";
case NodeType.Divide: return "÷";
case NodeType.Sin: return "sin";
case NodeType.Cos: return "cos";
case NodeType.Log: return "log";
case NodeType.Exp: return "exp";
case NodeType.Power: return "^";
case NodeType.Sqrt: return "√";
default: return "?";
}
}
Color GetNodeColor(NodeType type)
{
switch (type)
{
case NodeType.Variable: return Color.cyan;
case NodeType.Constant: return Color.yellow;
case NodeType.Add:
case NodeType.Subtract:
case NodeType.Multiply:
case NodeType.Divide:
return Color.green;
case NodeType.Sin:
case NodeType.Cos:
case NodeType.Log:
case NodeType.Exp:
case NodeType.Sqrt:
return Color.magenta;
default:
return Color.white;
}
}
}