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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
import torch
import torch.nn as nn
import torch.nn.functional as F  # Added for activation functions
import numpy as np
from dataclasses import dataclass, field
import pygame
import gradio as gr
from typing import List, Tuple, Dict, Optional, Set
import random
import colorsys
import pymunk
import time
import threading
from queue import Queue
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from PIL import Image
import io
import logging  # Added for logging

# ==============================
# Logging Configuration
# ==============================
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s [%(levelname)s] %(message)s',
    handlers=[
        logging.FileHandler("simulation.log"),
        logging.StreamHandler()
    ]
)

# ==============================
# Configuration Dataclasses
# ==============================

@dataclass
class SimulationConfig:
    WIDTH: int = 1024
    HEIGHT: int = 768
    TARGET_FPS: int = 60
    MIN_ORGANISMS: int = 5
    MAX_ORGANISMS: int = 50  # Population cap set to 50
    MUTATION_RATE: float = 0.1
    REPRODUCTION_ENERGY: float = 150.0
    INITIAL_ENERGY: float = 100.0
    BRAIN_UPDATE_RATE: int = 10  # Hz
    MAX_NEURONS: int = 1000
    ENERGY_DECAY: float = 0.1

@dataclass
class NeuronState:
    activation: float = 0.0
    connections: int = 0
    energy: float = 100.0
    memory: List[float] = field(default_factory=lambda: [0.0] * 8)

@dataclass
class VisualizationConfig:
    BACKGROUND_COLOR: Tuple[int, int, int] = (10, 10, 30)
    NEURON_COLORS: Dict[str, Tuple[int, int, int]] = field(default_factory=lambda: {
        'active': (255, 255, 0),
        'inactive': (100, 100, 100),
        'connected': (0, 255, 255)
    })
    CONNECTION_COLOR: Tuple[int, int, int, int] = (50, 50, 200, 100)
    ENERGY_COLOR: Tuple[int, int, int] = (0, 255, 0)
    MAX_NEURAL_CONNECTIONS: int = 50

@dataclass
class PhysicsConfig:
    COLLISION_TYPE_ORGANISM: int = 1
    ELASTICITY: float = 0.7
    FRICTION: float = 0.5
    DAMPING: float = 0.9
    INTERACTION_RADIUS: float = 50.0
    FORCE_SCALE: float = 100.0

# ==============================
# Neural Processing System
# ==============================

class FractalNeuron(nn.Module):
    def __init__(self, input_dim=16, output_dim=16, depth=0, max_depth=2):
            super().__init__()
            self.depth = depth
            self.max_depth = max_depth

            # Store dimensions
            self.input_dim = input_dim
            self.output_dim = output_dim
            self.hidden_dim = max(input_dim // 2, 8)  # Add explicit hidden_dim

            # Enhanced neural processing layers with LeakyReLU
            self.synapse = nn.Sequential(
                nn.Linear(input_dim, self.hidden_dim),  # First layer: input_dim to hidden_dim
                nn.LeakyReLU(negative_slope=0.1, inplace=True),
                nn.Linear(self.hidden_dim, output_dim),  # Second layer: hidden_dim to output_dim
                nn.Tanh()
            )

            # Initialize weights using Xavier uniform initialization
            for layer in self.synapse:
                if isinstance(layer, nn.Linear):
                    nn.init.xavier_uniform_(layer.weight)
                    if layer.bias is not None:
                        nn.init.constant_(layer.bias, 0.0)

            # Set to eval mode to prevent BatchNorm issues
            self.eval()

            # State maintenance with bounded values
            self.state = NeuronState()
            self.state.activation = 0.0
            self.state.energy = min(100.0, max(0.0, self.state.energy))

            # Memory processing with correct dimensions
            self.memory_gate = nn.Sequential(
                nn.Linear(output_dim + 8, 8),
                nn.Sigmoid()
            )

            # Initialize memory_gate weights
            for layer in self.memory_gate:
                if isinstance(layer, nn.Linear):
                    nn.init.xavier_uniform_(layer.weight)
                    if layer.bias is not None:
                        nn.init.constant_(layer.bias, 0.0)

            # Child neurons with matching dimensions
            self.sub_neurons = nn.ModuleList([])
            if depth < max_depth:
                branching_factor = max(1, 2 - depth)
                for _ in range(branching_factor):
                    child = FractalNeuron(
                        input_dim=output_dim,  # Child's input_dim matches parent's output_dim
                        output_dim=output_dim,  # Keep output_dim consistent
                        depth=depth + 1,
                        max_depth=max_depth
                    )
                    self.sub_neurons.append(child)

    def forward(self, x):
        """Forward pass for PyTorch module compatibility"""
        return self.process_signal(x)

    def process_signal(self, x, external_input=None):
        """Process input signal through the neuron"""
        try:
            with torch.no_grad():
                # Ensure we're in eval mode
                self.eval()

                # Reshape input for processing
                if len(x.shape) == 1:
                    x = x.unsqueeze(0)  # Add batch dimension

                # Check for NaNs in input
                if torch.isnan(x).any():
                    logging.warning("NaN detected in input tensor. Returning zero tensor.")
                    return torch.zeros(self.output_dim)

                # Add external input if provided
                if external_input is not None:
                    if len(external_input.shape) == 1:
                        external_input = external_input.unsqueeze(0)
                    x = torch.cat([x, external_input], dim=-1)

                # Process through synapse with proper shapes
                x = x.to(torch.float32)  # Ensure float32 dtype
                
                try:
                    x = self.synapse(x)
                except RuntimeError as e:
                    logging.error(f"Error in synapse processing: {e}")
                    return torch.zeros(self.output_dim)
                
                # Update memory with bounds checking
                try:
                    memory_tensor = torch.tensor(self.state.memory).to(torch.float32)
                    if len(x.shape) == 1:
                        x_for_memory = x.unsqueeze(0)
                    else:
                        x_for_memory = x
                    memory_input = torch.cat([x_for_memory, memory_tensor.unsqueeze(0)], dim=-1)
                    new_memory = self.memory_gate(memory_input)
                    new_memory = torch.clamp(new_memory, 0.0, 1.0)
                    if not torch.isnan(new_memory).any():
                        self.state.memory = new_memory[0].tolist()
                except Exception as e:
                    logging.error(f"Error updating memory: {e}")

                # Update activation with bounded value
                activation = float(torch.clamp(x.mean(), -1.0, 1.0))
                
                if not np.isnan(activation):
                    self.state.activation = activation
                
                # Process through children with error handling
                if self.sub_neurons:
                    child_outputs = []
                    for child in self.sub_neurons:
                        try:
                            # Ensure x has correct shape before passing to child
                            child_input = x.squeeze(0) if len(x.shape) == 2 else x
                            # Ensure input matches child's expected input dimension
                            if child_input.shape[-1] != child.input_dim:
                                child_input = child_input[:child.input_dim]
                            child_out = child.process_signal(child_input)
                            if not torch.isnan(child_out).any():
                                # Ensure child output has correct shape for stacking
                                if len(child_out.shape) == 1:
                                    child_out = child_out.unsqueeze(0)
                                child_outputs.append(child_out)
                        except Exception as e:
                            logging.error(f"Error in child neuron processing: {e}")
                            continue
                    
                    if child_outputs:
                        child_outputs = torch.stack(child_outputs)
                        x = torch.mean(child_outputs, dim=0)
                        x = torch.clamp(x, -1.0, 1.0)

                # Update energy with bounds
                energy_cost = 0.1 * self.depth
                self.state.energy = max(0.0, min(100.0, self.state.energy - energy_cost))

                # Remove batch dimension if it was added
                if len(x.shape) == 2:
                    x = x.squeeze(0)

                return x

        except Exception as e:
            logging.error(f"Error in process_signal: {e}")
            return torch.zeros(self.output_dim)
        
    def interact_with(self, other_neuron, strength=0.5):
        """Interact with another neuron"""
        try:
            # Bound strength value
            strength = max(0.0, min(1.0, strength))

            # Share neural states with bounds
            shared_activation = (self.state.activation + other_neuron.state.activation) / 2
            shared_activation = float(shared_activation)

            if np.isnan(shared_activation):
                logging.warning("NaN detected in shared activation. Using default value.")
                shared_activation = 0.0
            
            self.state.activation = shared_activation
            other_neuron.state.activation = shared_activation

            # Share memories with bounds checking
            shared_memory = []
            for a, b in zip(self.state.memory, other_neuron.state.memory):
                shared_value = (float(a) + float(b)) / 2
                shared_value = max(0.0, min(1.0, shared_value))
                shared_memory.append(shared_value)

            self.state.memory = shared_memory
            other_neuron.state.memory = shared_memory

            # Update connections with bounds
            max_connections = 100
            self.state.connections = min(self.state.connections + 1, max_connections)
            other_neuron.state.connections = min(other_neuron.state.connections + 1, max_connections)

            return shared_activation
        except Exception as e:
            logging.error(f"Error in interact_with: {e}")
            return 0.0

    def save_state(self):
        """Save the current state of the neuron"""
        return {
            'activation': self.state.activation,
            'connections': self.state.connections,
            'energy': self.state.energy,
            'memory': self.state.memory.copy()
        }

    def load_state(self, state_dict):
        """Load a previously saved state"""
        try:
            self.state.activation = state_dict['activation']
            self.state.connections = state_dict['connections']
            self.state.energy = state_dict['energy']
            self.state.memory = state_dict['memory'].copy()
        except Exception as e:
            logging.error(f"Error loading neuron state: {e}")

    def clone(self):
        """Create a deep copy of the neuron"""
        try:
            new_neuron = FractalNeuron(
                input_dim=self.input_dim,
                output_dim=self.output_dim,
                depth=self.depth,
                max_depth=self.max_depth
            )
            new_neuron.load_state(self.save_state())
            return new_neuron
        except Exception as e:
            logging.error(f"Error cloning neuron: {e}")
            return None

    def mutate(self, mutation_rate=0.1):
        """Apply random mutations to the neuron"""
        try:
            with torch.no_grad():
                # Mutate weights
                for layer in self.synapse:
                    if isinstance(layer, nn.Linear):
                        mask = torch.rand_like(layer.weight) < mutation_rate
                        mutations = torch.randn_like(layer.weight) * 0.1
                        layer.weight.data[mask] += mutations[mask]
                        
                        if layer.bias is not None:
                            mask = torch.rand_like(layer.bias) < mutation_rate
                            mutations = torch.randn_like(layer.bias) * 0.1
                            layer.bias.data[mask] += mutations[mask]

                # Mutate memory gate
                for layer in self.memory_gate:
                    if isinstance(layer, nn.Linear):
                        mask = torch.rand_like(layer.weight) < mutation_rate
                        mutations = torch.randn_like(layer.weight) * 0.1
                        layer.weight.data[mask] += mutations[mask]
                        
                        if layer.bias is not None:
                            mask = torch.rand_like(layer.bias) < mutation_rate
                            mutations = torch.randn_like(layer.bias) * 0.1
                            layer.bias.data[mask] += mutations[mask]

                # Recursively mutate child neurons
                for child in self.sub_neurons:
                    child.mutate(mutation_rate)

        except Exception as e:
            logging.error(f"Error mutating neuron: {e}")

class FractalBrain:
    def __init__(self, input_dim=32, hidden_dim=64, max_neurons=1000):
        self.input_dim = min(input_dim, 32)  # Limit input dimension
        self.hidden_dim = min(hidden_dim, 64)  # Limit hidden dimension
        self.max_neurons = min(max_neurons, 1000)  # Limit maximum neurons

        # Core neural network components with reduced complexity
        self.visual_cortex = FractalNeuron(self.input_dim, self.hidden_dim, max_depth=2)
        self.thought_processor = FractalNeuron(self.hidden_dim, self.hidden_dim, max_depth=2)
        self.action_generator = FractalNeuron(self.hidden_dim, self.input_dim, max_depth=2)

        # State tracking with bounds
        self.total_neurons = self.count_neurons()
        self.total_energy = 100.0  # Reduced initial energy
        self.memories = []
        self.current_vision = None

    def get_vitals(self):
        """Get vital statistics of the brain with safety checks"""
        try:
            # Calculate average activation safely
            activations = []
            for neuron in [self.visual_cortex, self.thought_processor, self.action_generator]:
                try:
                    activation = float(neuron.state.activation)
                    if not np.isnan(activation) and not np.isinf(activation):
                        activations.append(activation)
                except (AttributeError, ValueError, TypeError):
                    activations.append(0.0)
            
            avg_activation = sum(activations) / max(len(activations), 1)
            avg_activation = max(-1.0, min(1.0, avg_activation))

            # Get connection counts safely
            connections = []
            for neuron in [self.visual_cortex, self.thought_processor, self.action_generator]:
                try:
                    conn_count = int(neuron.state.connections)
                    if not np.isnan(conn_count) and not np.isinf(conn_count):
                        connections.append(conn_count)
                except (AttributeError, ValueError, TypeError):
                    connections.append(0)
            
            total_connections = sum(connections)

            return {
                'neurons': min(self.total_neurons, self.max_neurons),
                'energy': max(0.0, min(1000.0, float(self.total_energy))),
                'connections': max(0, min(1000, total_connections)),
                'activation': avg_activation
            }
        except Exception as e:
            logging.error(f"Exception in get_vitals: {e}. Returning default vitals.")
            # Return safe default values if anything goes wrong
            return {
                'neurons': 1,
                'energy': 0.0,
                'connections': 0,
                'activation': 0.0
            }

    def process_vision(self, visual_input):
        try:
            with torch.no_grad():
                # Ensure input is valid and properly shaped
                visual_input = visual_input.clone().detach()
                if len(visual_input.shape) == 1:
                    visual_input = visual_input.unsqueeze(0)  # Add batch dimension

                if torch.isnan(visual_input).any():
                    logging.warning("NaN detected in visual_input. Replacing with zeros.")
                    visual_input = torch.zeros_like(visual_input)
                
                visual_input = torch.clamp(visual_input, -10.0, 10.0)

                # Process through neural components with shape handling
                try:
                    visual_features = self.visual_cortex.process_signal(visual_input)
                    if len(visual_features.shape) == 1:
                        visual_features = visual_features.unsqueeze(0)
                except Exception as e:
                    logging.error(f"Exception in visual_cortex.process_signal: {e}. Using zero tensor.")
                    visual_features = torch.zeros((1, self.hidden_dim))

                try:
                    thoughts = self.thought_processor.process_signal(visual_features)
                    if len(thoughts.shape) == 1:
                        thoughts = thoughts.unsqueeze(0)
                except Exception as e:
                    logging.error(f"Exception in thought_processor.process_signal: {e}. Using zero tensor.")
                    thoughts = torch.zeros((1, self.hidden_dim))

                try:
                    actions = self.action_generator.process_signal(thoughts)
                except Exception as e:
                    logging.error(f"Exception in action_generator.process_signal: {e}. Using zero tensor.")
                    actions = torch.zeros(self.input_dim)

                # Remove batch dimension from final output if present
                if len(actions.shape) > 1:
                    actions = actions.squeeze(0)

                # Ensure outputs are bounded
                actions = torch.clamp(actions, -1.0, 1.0)

                # Energy consumption with bounds
                self.total_energy = max(0.0, min(1000.0, self.total_energy - 0.1))

                return actions
            
        except Exception as e:
            logging.error(f"Exception in process_vision: {e}. Returning zero actions.")
            return torch.zeros(self.input_dim)
        
    def interact_with(self, other_brain, strength=0.5):
        try:
            # Bound strength value
            strength = max(0.0, min(1.0, strength))

            # Neural interactions with error handling
            shared_visual = self.visual_cortex.interact_with(other_brain.visual_cortex, strength)
            shared_thoughts = self.thought_processor.interact_with(other_brain.thought_processor, strength)
            shared_actions = self.action_generator.interact_with(other_brain.action_generator, strength)

            # Energy transfer with bounds
            energy_diff = self.total_energy - other_brain.total_energy
            transfer = max(-10.0, min(10.0, energy_diff * 0.1))
            
            self.total_energy = max(0.0, min(1000.0, self.total_energy - transfer))
            other_brain.total_energy = max(0.0, min(1000.0, other_brain.total_energy + transfer))

            return shared_visual, shared_thoughts, shared_actions
        except Exception as e:
            logging.error(f"Exception in interact_with: {e}. Returning zeros.")
            return 0.0, 0.0, 0.0

    def count_neurons(self):
        """Safely count neurons with error handling"""
        try:
            def count_recursive(module):
                count = 1
                if hasattr(module, 'sub_neurons'):
                    for child in module.sub_neurons:
                        count += count_recursive(child)
                return min(count, self.max_neurons)  # Limit total count

            total = sum(count_recursive(x) for x in [
                self.visual_cortex,
                self.thought_processor,
                self.action_generator
            ])
            return min(total, self.max_neurons)
        except Exception as e:
            logging.error(f"Exception in count_neurons: {e}. Returning 1.")
            return 1  # Return minimum count if counting fails

    def can_grow(self):
        """Check if brain can grow new neurons"""
        return (self.total_neurons < self.max_neurons and
                self.total_energy > 100.0)

# ==============================
# Organism Definition and Behavior
# ==============================

class FractalOrganism:
    def __init__(self, x, y, size=20, feature_dim=32, max_neurons=1000):
        # Physical properties
        self.pos = pygame.math.Vector2(x, y)
        self.vel = pygame.math.Vector2(0, 0)
        self.acc = pygame.math.Vector2(0, 0)
        self.size = size
        self.mass = size * 0.1

        # Neural system
        self.brain = FractalBrain(input_dim=feature_dim, hidden_dim=feature_dim*2, max_neurons=max_neurons)
        self.feature_dim = feature_dim
        self.features = torch.randn(feature_dim)

        # Visual properties with validation
        self.color = self._features_to_color()
        self.pattern_type = self._determine_pattern_type()
        self.pattern_intensity = self._determine_pattern_intensity()
        self.shape_points = self._generate_shape()

        # Life properties
        self.alive = True
        self.age = 0
    def _validate_color_component(self, value):
        """Ensure color component is a valid integer between 0 and 255"""
        try:
            value = int(value)
            return max(0, min(255, value))
        except (ValueError, TypeError):
            return 0

    def update(self, screen_width, screen_height, organisms):
            """Update organism state"""
            if not self.alive:
                return

            try:
                # Physics integration
                # Update velocity with acceleration
                self.vel += self.acc
                
                # Apply friction/damping
                self.vel *= 0.98  # Slight damping to prevent infinite movement
                
                # Update position with velocity
                self.pos += self.vel
                
                # Clear acceleration for next frame
                self.acc.x = 0
                self.acc.y = 0

                # Get visual input and process through brain
                visual_input = self._get_visual_input(organisms)
                actions = self.brain.process_vision(visual_input)

                # Apply neural network outputs as forces if valid
                if isinstance(actions, torch.Tensor) and not torch.isnan(actions).any():
                    self._apply_action_forces(actions)

                # Wrap around screen edges
                self.pos.x = self.pos.x % screen_width
                self.pos.y = self.pos.y % screen_height

                # Update life properties
                self.age += 1
                vitals = self.brain.get_vitals()

                # Death conditions
                if vitals['energy'] <= 0 or self.age > 1000:
                    self.alive = False

            except Exception as e:
                logging.error(f"Error updating organism {id(self)}: {e}")
                logging.debug(f"Organism state - Age: {self.age}, Alive: {self.alive}")

    def _get_visual_input(self, organisms):
        """Create visual input tensor from surroundings"""
        try:
            visual_input = torch.zeros(self.feature_dim)

            # Add self-perception (first 3 features are color)
            color_tensor = torch.tensor([c/255.0 for c in self.color])
            visual_input[:3] = color_tensor[:3]

            # Add velocity perception (helps with movement learning)
            if hasattr(self, 'vel'):
                velocity_magnitude = np.sqrt(self.vel.x**2 + self.vel.y**2)
                velocity_direction = np.arctan2(self.vel.y, self.vel.x) / np.pi
                if 3 < len(visual_input):
                    visual_input[3] = float(velocity_magnitude) / 10.0  # Normalize velocity
                if 4 < len(visual_input):
                    visual_input[4] = float(velocity_direction)

            # Add perception of nearby organisms
            for other in organisms:
                if other != self and other.alive:
                    distance = self.pos.distance_to(other.pos)
                    if distance < 100:  # Visual range
                        direction = (other.pos - self.pos)
                        if direction.length() > 0:
                            direction = direction.normalize()
                            angle = np.arctan2(direction.y, direction.x)
                            
                            # Map angle to feature index
                            idx = int((angle + np.pi) / (2 * np.pi) * (self.feature_dim - 5)) + 5
                            idx = min(max(5, idx), self.feature_dim - 1)
                            
                            # Set feature value based on distance and target's properties
                            intensity = 1.0 - min(1.0, distance / 100)
                            visual_input[idx] = intensity

                            # Add information about target's energy level if visible
                            if idx + 1 < self.feature_dim:
                                target_energy = float(other.brain.total_energy) / 1000.0
                                if not np.isnan(target_energy):
                                    visual_input[idx + 1] = target_energy


            return visual_input

        except Exception as e:
            logging.error(f"Error in _get_visual_input: {e}")
            return torch.zeros(self.feature_dim)


    def _apply_action_forces(self, actions):
            """Convert neural actions to physical forces with better control"""
            try:
                if not isinstance(actions, torch.Tensor):
                    return

                # Get first two action dimensions for movement control
                if len(actions) >= 2:
                    # Scale force based on neural network activation
                    activation = float(self.brain.visual_cortex.state.activation)
                    force_scale = 20.0  # Increased for more visible movement

                    # Convert actions to directional movement
                    force_x = float(actions[0].item()) * force_scale * (1 + abs(activation))
                    force_y = float(actions[1].item()) * force_scale * (1 + abs(activation))
                    
                    # Add some randomness for exploration when activation is low
                    if abs(activation) < 0.2:
                        force_x += random.uniform(-2.0, 2.0)  # Increased random movement
                        force_y += random.uniform(-2.0, 2.0)

                    # Clamp forces but allow for stronger movement
                    max_force = 40.0  # Increased maximum force
                    force_x = max(-max_force, min(max_force, force_x))
                    force_y = max(-max_force, min(max_force, force_y))

                    # Apply the forces
                    self.apply_force((force_x, force_y))

                    # Additional actions for other behaviors
                    if len(actions) >= 4:
                        try:
                            # Action 3: Energy usage control
                            energy_control = float(actions[2].item())
                            if energy_control > 0.8:
                                self.brain.total_energy += energy_control * 0.1
                            
                            # Action 4: Interaction strength
                            interaction_strength = max(0, min(1, float(actions[3].item())))
                            if not hasattr(self, 'interaction_strength'):
                                self.__dict__['interaction_strength'] = interaction_strength
                            else:
                                self.interaction_strength = interaction_strength

                        except Exception as e:
                            logging.error(f"Error processing additional actions: {e}")

            except Exception as e:
                logging.error(f"Error in _apply_action_forces: {e}")

    def apply_force(self, force):
        """Apply physics force with validation"""
        try:
            if isinstance(force, (tuple, list)) and len(force) >= 2:
                fx = float(force[0])
                fy = float(force[1])
                
                # Check for NaN
                if np.isnan(fx) or np.isnan(fy):
                    return
                    
                # Limit maximum force
                max_force = 10.0
                fx = max(-max_force, min(max_force, fx))
                fy = max(-max_force, min(max_force, fy))
                
                force = pygame.math.Vector2(fx, fy)
                
                # Validate acceleration before applying
                new_acc = force / self.mass
                if not (np.isnan(new_acc.x) or np.isnan(new_acc.y)):
                    self.acc.update(new_acc.x, new_acc.y)
                    
                    # Clamp acceleration
                    max_acc = 5.0
                    self.acc.x = max(-max_acc, min(max_acc, self.acc.x))
                    self.acc.y = max(-max_acc, min(max_acc, self.acc.y))
        except Exception as e:
            logging.error(f"Error in apply_force: {e}")


    def _features_to_color(self):
        """Convert feature vector to RGB color with validation"""
        try:
            r = self._validate_color_component((self.features[0].item() + 1) / 2 * 255)
            g = self._validate_color_component((self.features[1].item() + 1) / 2 * 255)
            b = self._validate_color_component((self.features[2].item() + 1) / 2 * 255)
            return (r, g, b)
        except (IndexError, AttributeError) as e:
            logging.error(f"Error in _features_to_color: {e}. Defaulting to (100, 100, 100).")
            return (100, 100, 100)

    def _determine_pattern_type(self):
        """Determine pattern type based on specific features"""
        try:
            # Use features 3 and 4 to determine pattern type safely
            if len(self.features) >= 5:
                feature_sum = float(self.features[3].item() + self.features[4].item())
                if feature_sum > 1:
                    return 'stripes'
                elif feature_sum < -1:
                    return 'spots'
                else:
                    return 'gradient'
            return 'gradient'  # Default pattern
        except (IndexError, AttributeError, ValueError) as e:
            logging.error(f"Error in _determine_pattern_type: {e}. Defaulting to 'gradient'.")
            return 'gradient'  # Fallback pattern

    def _determine_pattern_intensity(self):
        """Determine pattern intensity based on specific features"""
        try:
            if len(self.features) >= 6:
                intensity = (float(self.features[5].item()) + 1) / 2
                return max(0.0, min(1.0, intensity))
            return 0.5  # Default intensity
        except (IndexError, AttributeError, ValueError) as e:
            logging.error(f"Error in _determine_pattern_intensity: {e}. Defaulting to 0.5.")
            return 0.5  # Fallback intensity

    def _generate_shape(self):
        """Generate a polygon shape based on the pattern type"""
        try:
            points = []
            if self.pattern_type == 'stripes':
                # Generate a star-like shape with protrusions
                for angle in range(0, 360, 30):
                    rad = np.radians(angle)
                    x = self.size * np.cos(rad)
                    y = self.size * np.sin(rad)
                    # Alternate between outer and inner points for stripes
                    if (angle // 30) % 2 == 0:
                        points.append((x * 1.2, y * 1.2))
                    else:
                        points.append((x * 0.8, y * 0.8))
            elif self.pattern_type == 'spots':
                # Generate a more circular, smooth shape with bulges
                for angle in range(0, 360, 45):
                    rad = np.radians(angle)
                    x = self.size * (1 + 0.3 * np.sin(4 * rad)) * np.cos(rad)
                    y = self.size * (1 + 0.3 * np.sin(4 * rad)) * np.sin(rad)
                    points.append((x, y))
            else:  # 'gradient' or other patterns
                # Simple regular polygon
                for angle in range(0, 360, 60):
                    rad = np.radians(angle)
                    x = self.size * np.cos(rad)
                    y = self.size * np.sin(rad)
                    points.append((x, y))

            # Validate points and ensure we have at least a triangle
            if len(points) < 3:
                # Fallback to basic triangle
                points = [
                    (-self.size, -self.size),
                    (self.size, -self.size),
                    (0, self.size)
                ]
            return points
        except Exception as e:
            logging.error(f"Error in _generate_shape: {e}. Defaulting to basic triangle.")
            # Fallback to basic triangle if anything goes wrong
            return [
                (-self.size, -self.size),
                (self.size, -self.size),
                (0, self.size)
            ]

    def reproduce(self, mate, mutation_rate=0.1):
        """Reproduce with another organism to create a child organism with possible mutations"""
        try:
            # Check reproduction energy requirements
            if not hasattr(self.brain, 'REPRODUCTION_ENERGY'):
                self.brain.REPRODUCTION_ENERGY = 150.0  # Default value if not set
                
            if self.brain.total_energy < self.brain.REPRODUCTION_ENERGY or mate.brain.total_energy < mate.brain.REPRODUCTION_ENERGY:
                return None

            # Deduct energy for reproduction
            self.brain.total_energy -= 50.0
            mate.brain.total_energy -= 50.0

            # Blend features
            child_features = (self.features + mate.features) / 2

            # Apply mutations
            for i in range(len(child_features)):
                if random.random() < mutation_rate:
                    child_features[i] += random.uniform(-0.1, 0.1)

            # Clamp mutated features to prevent extreme values
            child_features = torch.clamp(child_features, -1.0, 1.0)

            # Create child organism
            child = FractalOrganism(
                x=(self.pos.x + mate.pos.x) / 2 + random.uniform(-10, 10),
                y=(self.pos.y + mate.pos.y) / 2 + random.uniform(-10, 10),
                size=self.size,
                feature_dim=self.feature_dim,
                max_neurons=self.brain.max_neurons
            )
            child.features = child_features
            child.color = child._features_to_color()
            child.pattern_type = child._determine_pattern_type()
            child.pattern_intensity = child._determine_pattern_intensity()
            child.shape_points = child._generate_shape()
            child.brain = self._mutate_brain(mate.brain, mutation_rate)

            return child
        except Exception as e:
            logging.error(f"Error in reproduction: {e}")
            return None

    def _mutate_brain(self, brain, mutation_rate):
        """Mutate the brain's neurons"""
        try:
            # For simplicity, we can randomly add connections or adjust activation
            # Here, we'll randomly adjust activation levels
            brain.visual_cortex.state.activation += random.uniform(-0.1, 0.1)
            brain.thought_processor.state.activation += random.uniform(-0.1, 0.1)
            brain.action_generator.state.activation += random.uniform(-0.1, 0.1)
            
            # Ensure activations stay in valid range
            brain.visual_cortex.state.activation = max(-1.0, min(1.0, brain.visual_cortex.state.activation))
            brain.thought_processor.state.activation = max(-1.0, min(1.0, brain.thought_processor.state.activation))
            brain.action_generator.state.activation = max(-1.0, min(1.0, brain.action_generator.state.activation))
            
            return brain
        except Exception as e:
            logging.error(f"Error in brain mutation: {e}. Returning unmutated brain.")
            return brain

    def interact_with(self, other):
        """Interact with another organism"""
        try:
            distance = self.pos.distance_to(other.pos)
            if distance < self.size + other.size:
                # Neural interaction
                interaction_strength = 1.0 - distance / (self.size + other.size)
                self.brain.interact_with(other.brain, interaction_strength)

                # Physical interaction (simple collision)
                direction = (self.pos - other.pos).normalize()
                force = direction * interaction_strength * 5
                self.apply_force(force)
                other.apply_force(-force)

                return True
            return False
        except Exception as e:
            logging.error(f"Error in organism interaction: {e}")
            return False

    def _blend_patterns(self, pattern1: str, pattern2: str) -> str:
        """Blend two pattern types to create a new pattern type"""
        try:
            if pattern1 == pattern2:
                return pattern1
            else:
                # Simple blending logic: randomly choose one of the parent patterns or a new pattern
                return random.choice([pattern1, pattern2, 'stripes', 'spots', 'gradient'])
        except Exception as e:
            logging.error(f"Error in _blend_patterns: {e}. Defaulting to 'gradient'.")
            return 'gradient'  # Default pattern if anything goes wrong

# ==============================
# Physics and Interaction Handling
# ==============================

class PhysicsEngine:
    def __init__(self, width: int, height: int, config: PhysicsConfig):
        self.config = config
        # Initialize pymunk space
        self.space = pymunk.Space()
        self.space.damping = self.config.DAMPING

        # Create boundaries
        self.create_boundaries(width, height)

        # Collision handler for organisms
        handler = self.space.add_collision_handler(
            self.config.COLLISION_TYPE_ORGANISM,
            self.config.COLLISION_TYPE_ORGANISM
        )
        handler.begin = self.handle_collision

        # Track interactions
        self.current_interactions: Set[tuple] = set()

        # Store dimensions
        self.width = width
        self.height = height

    def update(self, dt: float):
        """Update physics simulation"""
        try:
            # Pymunk works best with a fixed time step
            fixed_dt = 1.0 / 60.0
            steps = max(1, min(4, int(dt / fixed_dt)))  # Limit max steps to prevent spiral

            for _ in range(steps):
                self.space.step(fixed_dt)

            # Update organism positions from physics bodies
            for body in self.space.bodies:
                if hasattr(body, 'organism'):
                    try:
                        organism = body.organism
                        
                        # Validate positions
                        if not (np.isnan(body.position.x) or np.isnan(body.position.y)):
                            new_x = float(body.position.x % self.width)
                            new_y = float(body.position.y % self.height)
                            # Update pygame Vector2 position
                            organism.pos.update(new_x, new_y)
                        else:
                            # Reset to center if NaN
                            body.position = self.width/2, self.height/2
                            organism.pos.update(self.width/2, self.height/2)
                        
                        # Validate velocities
                        if not (np.isnan(body.velocity.x) or np.isnan(body.velocity.y)):
                            max_velocity = 200.0
                            vx = max(-max_velocity, min(max_velocity, body.velocity.x))
                            vy = max(-max_velocity, min(max_velocity, body.velocity.y))
                            # Update pygame Vector2 velocity
                            organism.vel.update(vx, vy)
                        else:
                            body.velocity = (0, 0)
                            organism.vel.update(0, 0)

                    except Exception as e:
                        logging.error(f"Error updating organism physics state: {e}")
                        # Reset body to safe state
                        body.position = self.width/2, self.height/2
                        body.velocity = (0, 0)
                        try:
                            organism.pos.update(self.width/2, self.height/2)
                            organism.vel.update(0, 0)
                        except:
                            pass

        except Exception as e:
            logging.error(f"Error updating physics: {e}")

    def create_boundaries(self, width: int, height: int):
        """Create screen boundaries"""
        try:
            walls = [
                [(0, 0), (width, 0)],      # Top
                [(width, 0), (width, height)],  # Right
                [(width, height), (0, height)],  # Bottom
                [(0, height), (0, 0)]       # Left
            ]

            for wall in walls:
                shape = pymunk.Segment(self.space.static_body, wall[0], wall[1], 0)
                shape.elasticity = self.config.ELASTICITY
                shape.friction = self.config.FRICTION
                self.space.add(shape)
        except Exception as e:
            logging.error(f"Error creating boundaries: {e}")

    def add_organism(self, organism: FractalOrganism) -> pymunk.Body:
        """Add organism to physics space"""
        try:
            # Validate mass
            mass = max(0.1, organism.mass)  # Ensure positive mass
            moment = pymunk.moment_for_circle(mass, 0, organism.size)
            
            body = pymunk.Body(mass, moment)
            
            # Validate initial position and velocity
            body.position = (
                float(organism.pos.x % self.width),
                float(organism.pos.y % self.height)
            )
            
            # Clamp initial velocity
            max_initial_velocity = 50.0
            vel_x = max(-max_initial_velocity, min(max_initial_velocity, organism.vel.x))
            vel_y = max(-max_initial_velocity, min(max_initial_velocity, organism.vel.y))
            body.velocity = (vel_x, vel_y)

            # Validate shape points and create polygon
            valid_vertices = []
            for x, y in organism.shape_points:
                if not (np.isnan(x) or np.isnan(y)):
                    valid_vertices.append((float(x), float(y)))

            # If insufficient valid vertices, create default circle shape
            if len(valid_vertices) < 3:
                logging.warning(f"Insufficient valid vertices for organism {id(organism)}, using circle shape")
                shape = pymunk.Circle(body, organism.size)
            else:
                shape = pymunk.Poly(body, valid_vertices)

            shape.elasticity = self.config.ELASTICITY
            shape.friction = self.config.FRICTION
            shape.collision_type = self.config.COLLISION_TYPE_ORGANISM

            # Store reference to organism
            shape.organism = organism
            body.organism = organism

            self.space.add(body, shape)
            return body
        except Exception as e:
            logging.error(f"Error adding organism to physics: {e}")
            return None

    def handle_collision(self, arbiter, space, data):
        """Handle collision between organisms"""
        try:
            # Get colliding organisms
            shape_a, shape_b = arbiter.shapes
            org_a, org_b = shape_a.organism, shape_b.organism

            # Add to interaction set
            interaction_pair = tuple(sorted([id(org_a), id(org_b)]))
            self.current_interactions.add(interaction_pair)

            # Calculate collision response with validation
            restitution = max(0, min(1, self.config.ELASTICITY))
            j = -(1 + restitution) * arbiter.total_ke / 2

            # Validate impulse
            if not np.isnan(j):
                # Clamp impulse to prevent extreme values
                max_impulse = 1000.0
                j = max(-max_impulse, min(max_impulse, j))

                body_a = shape_a.body
                body_b = shape_b.body
                
                normal = arbiter.normal
                point = arbiter.contact_point_set.points[0].point_a

                # Apply impulse along the collision normal
                body_a.apply_impulse_at_world_point(j * normal, point)
                body_b.apply_impulse_at_world_point(-j * normal, point)

            return True
        except Exception as e:
            logging.error(f"Error handling collision: {e}")
            return False

    def process_interactions(self, organisms: List[FractalOrganism]):
        """Process all current interactions"""
        try:
            # Process collision-based interactions
            for org_a_id, org_b_id in self.current_interactions:
                org_a = next((org for org in organisms if id(org) == org_a_id), None)
                org_b = next((org for org in organisms if id(org) == org_b_id), None)

                if org_a and org_b and org_a.alive and org_b.alive:
                    # Neural interaction
                    shared_thoughts = org_a.brain.interact_with(org_b.brain)

                    # Energy transfer based on neural activity
                    energy_diff = org_a.brain.total_energy - org_b.brain.total_energy
                    transfer = max(-10.0, min(10.0, energy_diff * 0.1))  # Limit transfer amount
                    org_a.brain.total_energy = max(0, org_a.brain.total_energy - transfer)
                    org_b.brain.total_energy = max(0, org_b.brain.total_energy + transfer)

            # Clear interactions for next frame
            self.current_interactions.clear()

            # Process proximity-based interactions
            for i, org_a in enumerate(organisms):
                if not org_a.alive:
                    continue

                for org_b in organisms[i+1:]:
                    if not org_b.alive:
                        continue

                    # Calculate distance with validation
                    dx = org_b.pos.x - org_a.pos.x
                    dy = org_b.pos.y - org_a.pos.y
                    
                    if np.isnan(dx) or np.isnan(dy):
                        continue
                        
                    distance = np.sqrt(dx*dx + dy*dy)

                    if distance < self.config.INTERACTION_RADIUS:
                        # Calculate interaction strength based on distance
                        strength = 1.0 - (distance / self.config.INTERACTION_RADIUS)

                        # Neural field effect with reduced strength
                        field_interaction = strength * 0.5

                        # Calculate force with validation
                        force_magnitude = field_interaction * self.config.FORCE_SCALE
                        force_angle = np.arctan2(dy, dx)

                        if not (np.isnan(force_magnitude) or np.isnan(force_angle)):
                            force_x = np.cos(force_angle) * force_magnitude
                            force_y = np.sin(force_angle) * force_magnitude

                            # Clamp forces
                            max_force = 100.0
                            force_x = max(-max_force, min(max_force, force_x))
                            force_y = max(-max_force, min(max_force, force_y))

                            # Apply forces through physics bodies
                            body_a = next((body for body in self.space.bodies 
                                         if hasattr(body, 'organism') and body.organism == org_a), None)
                            body_b = next((body for body in self.space.bodies 
                                         if hasattr(body, 'organism') and body.organism == org_b), None)

                            if body_a and body_b:
                                body_a.apply_force_at_local_point((-force_x, -force_y), (0, 0))
                                body_b.apply_force_at_local_point((force_x, force_y), (0, 0))

                            # Apply direct forces to organisms as well
                            org_a.apply_force((-force_x, -force_y))
                            org_b.apply_force((force_x, force_y))

        except Exception as e:
            logging.error(f"Error processing interactions: {e}")

# ==============================
# Visualization with PyGame
# ==============================

class NeuralVisualizer:
    def __init__(self, width: int, height: int, config: VisualizationConfig):
        self.width = width
        self.height = height
        self.config = config
        self.neuron_surface = pygame.Surface((width, height), pygame.SRCALPHA)
        self.connection_surface = pygame.Surface((width, height), pygame.SRCALPHA)

    def _apply_pattern_overlay(self, organism, surface):
        """Apply visual pattern overlay based on organism type"""
        try:
            if not organism.alive:
                return

            pattern_alpha = int(255 * organism.pattern_intensity)
            pattern_color = (
                255 - organism.color[0],
                255 - organism.color[1],
                255 - organism.color[2],
                pattern_alpha
            )

            # Create pattern surface
            pattern_surface = pygame.Surface((self.width, self.height), pygame.SRCALPHA)

            if organism.pattern_type == 'stripes':
                # Draw alternating stripes
                stride = max(5, int(organism.size * 0.5))
                x, y = int(organism.pos.x), int(organism.pos.y)
                for i in range(-2, 3):
                    offset = i * stride
                    pygame.draw.line(pattern_surface, pattern_color,
                                (x - organism.size, y + offset),
                                (x + organism.size, y + offset), 2)

            elif organism.pattern_type == 'spots':
                # Draw spots in a circular pattern
                x, y = int(organism.pos.x), int(organism.pos.y)
                spot_size = max(2, int(organism.size * 0.2))
                for angle in range(0, 360, 45):
                    spot_x = x + int(np.cos(np.radians(angle)) * organism.size * 0.7)
                    spot_y = y + int(np.sin(np.radians(angle)) * organism.size * 0.7)
                    pygame.draw.circle(pattern_surface, pattern_color,
                                    (spot_x, spot_y), spot_size)

            else:  # gradient
                # Draw radial gradient
                x, y = int(organism.pos.x), int(organism.pos.y)
                max_radius = int(organism.size * 1.2)
                for radius in range(max_radius, 0, -2):
                    alpha = int((radius / max_radius) * pattern_alpha)
                    current_color = (*pattern_color[:3], alpha)
                    pygame.draw.circle(pattern_surface, current_color,
                                    (x, y), radius, 1)

            # Blend pattern with surface
            surface.blit(pattern_surface, (0, 0), special_flags=pygame.BLEND_ALPHA_SDL2)

        except Exception as e:
            logging.error(f"Error applying pattern overlay: {e}")

    def draw_brain_state(self, organism: FractalOrganism, surface: pygame.Surface):
        """Draw neural activity visualization with patterns and NaN handling"""
        try:
            # Clear previous state
            self.neuron_surface.fill((0, 0, 0, 0))
            self.connection_surface.fill((0, 0, 0, 0))

            # Get brain vitals with NaN check
            vitals = organism.brain.get_vitals()
            if any(np.isnan(value) for value in vitals.values() if isinstance(value, (int, float))):
                logging.warning(f"NaN detected in vitals for organism {id(organism)}. Marking for death.")
                organism.alive = False
                return

            # Calculate neural positions based on fractal pattern
            def plot_neural_layer(center, radius, neurons, depth=0):
                if depth >= 3 or not neurons:
                    return
                
                # Convert neurons to list if it's a ModuleList
                if hasattr(neurons, 'sub_neurons'):
                    neurons = neurons.sub_neurons
                
                if not neurons or len(neurons) == 0:
                    return

                angle_step = 2 * np.pi / len(neurons)
                for i, neuron in enumerate(neurons):
                    try:
                        angle = i * angle_step
                        x = center[0] + radius * np.cos(angle)
                        y = center[1] + radius * np.sin(angle)

                        # NaN check for coordinates
                        if np.isnan(x) or np.isnan(y):
                            logging.warning(f"NaN coordinates detected for neuron {i}. Skipping.")
                            continue

                        # Draw neuron
                        activation = float(neuron.state.activation)
                        connections = int(neuron.state.connections)

                        # Check for NaNs in neuron state
                        if np.isnan(activation) or np.isnan(connections):
                            logging.warning(f"NaN detected in neuron state. Marking organism for death.")
                            organism.alive = False
                            return

                        # Ensure coordinates are valid integers
                        x_pos = int(np.clip(x, 0, self.width))
                        y_pos = int(np.clip(y, 0, self.height))

                        color = self._get_neuron_color(activation, connections)
                        pygame.draw.circle(self.neuron_surface, color, (x_pos, y_pos), 5)

                        # Draw connections with safety checks
                        if connections > 0:
                            alpha = int(255 * min(connections / self.config.MAX_NEURAL_CONNECTIONS, 1))
                            if not np.isnan(alpha):
                                connection_color = (*self.config.CONNECTION_COLOR[:3], alpha)
                                pygame.draw.line(
                                    self.connection_surface,
                                    connection_color,
                                    (x_pos, y_pos),
                                    (int(center[0]), int(center[1])),
                                    2
                                )

                        # Recursively draw sub-neurons
                        if hasattr(neuron, 'sub_neurons') and neuron.sub_neurons:
                            child_radius = radius * 0.5
                            child_center = (x, y)
                            plot_neural_layer(child_center, child_radius, neuron.sub_neurons, depth + 1)
                    except Exception as e:
                        logging.error(f"Error plotting neuron {i}: {e}")
                        continue

            # Draw neural network
            try:
                center = (organism.pos.x, organism.pos.y)
                if not (np.isnan(center[0]) or np.isnan(center[1])):
                    plot_neural_layer(center, organism.size * 2,
                                    [organism.brain.visual_cortex,
                                    organism.brain.thought_processor,
                                    organism.brain.action_generator])
            except Exception as e:
                logging.error(f"Error plotting neural network: {e}")

            # Apply pattern overlay with safety checks
            try:
                self._apply_pattern_overlay(organism, surface)
            except Exception as e:
                logging.error(f"Error applying pattern overlay: {e}")

            # Combine surfaces with alpha blending
            surface.blit(self.connection_surface, (0, 0))
            surface.blit(self.neuron_surface, (0, 0))

        except Exception as e:
            logging.error(f"Error in draw_brain_state: {e}")

    def _get_neuron_color(self, activation: float, connections: int) -> Tuple[int, int, int]:
        """Generate color based on neuron state"""
        try:
            # Use HSV color space for smooth transitions
            hue = (activation + 1) / 2  # Map -1,1 to 0,1
            saturation = min(connections / self.config.MAX_NEURAL_CONNECTIONS, 1)
            value = 0.8 + 0.2 * activation

            # Convert to RGB
            rgb = colorsys.hsv_to_rgb(hue, saturation, value)
            return tuple(int(255 * x) for x in rgb)
        except Exception as e:
            logging.error(f"Error in _get_neuron_color: {e}. Defaulting to gray.")
            return (100, 100, 100)

class SimulationVisualizer:
    def __init__(self, width: int, height: int, config: VisualizationConfig):
        pygame.init()
        self.width = width
        self.height = height
        self.config = config
        
        # Enable double buffering and vsync
        self.screen = pygame.display.set_mode(
            (width, height), 
            pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.SCALED, 
            vsync=1
        )
        pygame.display.set_caption("Fractal Life Simulation")
        
        # Create off-screen surfaces for double buffering
        self.buffer = pygame.Surface((width, height), pygame.SRCALPHA)
        self.neural_viz = NeuralVisualizer(width, height, config)
        self.background = pygame.Surface((width, height))
        self.background.fill(config.BACKGROUND_COLOR)
        
        # Additional surfaces for layered rendering
        self.organism_surface = pygame.Surface((width, height), pygame.SRCALPHA)
        self.interaction_surface = pygame.Surface((width, height), pygame.SRCALPHA)
        self.stats_surface = pygame.Surface((width, height), pygame.SRCALPHA)

    def _validate_color(self, color):
        """Validate and ensure color values are proper RGB integers"""
        try:
            if len(color) >= 3:
                return (
                    max(0, min(255, int(color[0]))),
                    max(0, min(255, int(color[1]))),
                    max(0, min(255, int(color[2])))
                )
            return (100, 100, 100)  # Default fallback color
        except Exception as e:
            logging.error(f"Error validating color: {e}")
            return (100, 100, 100)



    def draw_frame(self, organisms: List[FractalOrganism], stats: Dict):
        """Draw complete frame with all visualizations"""
        try:
            # Clear all off-screen surfaces
            self.organism_surface.fill((0, 0, 0, 0))
            self.interaction_surface.fill((0, 0, 0, 0))
            self.stats_surface.fill((0, 0, 0, 0))

            # Draw organisms and their neural states
            for organism in organisms:
                if organism.alive:
                    self._draw_organism(organism, self.organism_surface)
                    self.neural_viz.draw_brain_state(organism, self.interaction_surface)

            # Draw statistics
            self._draw_stats(stats, self.stats_surface)

            # Blit off-screen surfaces to the main display surface
            self.screen.blit(self.background, (0, 0))  # Background layer
            self.screen.blit(self.organism_surface, (0, 0))
            self.screen.blit(self.interaction_surface, (0, 0))
            self.screen.blit(self.stats_surface, (0, 0))

            # Flip display buffers to avoid flickering
            pygame.display.flip()
        except Exception as e:
            logging.error(f"Error in draw_frame: {e}")
            
    def _draw_organism(self, organism: FractalOrganism, surface: pygame.Surface):
        """Draw organism body with color and pattern"""
        try:
            # Validate color before drawing
            safe_color = self._validate_color(organism.color)
            
            # Draw shape with validated color
            points = []
            for x, y in organism.shape_points:
                try:
                    px = organism.pos.x + x
                    py = organism.pos.y + y
                    if not (np.isnan(px) or np.isnan(py)):
                        points.append((px, py))
                    else:
                        logging.warning(f"NaN detected in shape points for organism {id(organism)}. Skipping point.")
                except Exception as e:
                    logging.error(f"Error processing shape points for organism {id(organism)}: {e}")
                    continue

            if len(points) >= 3:
                pygame.draw.polygon(surface, safe_color, points)
            else:
                logging.warning(f"Insufficient valid points to draw organism {id(organism)}. Skipping drawing.")

            # Draw energy bar
            energy_percentage = min(max(organism.brain.total_energy / 1000.0, 0), 1)
            bar_width = organism.size * 2
            bar_height = 4
            bar_pos = (organism.pos.x - bar_width / 2, organism.pos.y - organism.size - 10)
            pygame.draw.rect(surface, (50, 50, 50), (*bar_pos, bar_width, bar_height))
            pygame.draw.rect(surface, (0, 255, 0),  # Using direct color value for energy bar
                             (*bar_pos, bar_width * energy_percentage, bar_height))
        except Exception as e:
            logging.error(f"Error in _draw_organism: {e}")

    def _draw_stats(self, stats: Dict, surface: pygame.Surface):
        """Draw simulation statistics"""
        try:
            font = pygame.font.Font(None, 24)
            y_pos = 10
            for key, value in stats.items():
                text = font.render(f"{key.capitalize()}: {value}", True, (255, 255, 255))
                surface.blit(text, (10, y_pos))
                y_pos += 25
        except Exception as e:
            logging.error(f"Error in _draw_stats: {e}")

    def cleanup(self):
        """Clean up pygame resources"""
        try:
            pygame.quit()
        except Exception as e:
            logging.error(f"Error during pygame cleanup: {e}")

# ==============================
# Interaction Field (Optional Enhancement)
# ==============================

class InteractionField:
    def __init__(self, width: int, height: int, resolution: int = 50):
        self.width = max(1, width)
        self.height = max(1, height)
        self.resolution = max(10, min(resolution, 100))  # Bound resolution

        # Create field grid with safe dimensions
        self.grid_w = max(1, self.width // self.resolution)
        self.grid_h = max(1, self.height // self.resolution)
        self.field = np.zeros((self.grid_w, self.grid_h, 3))

    def update(self, organisms: List[FractalOrganism]):
        """Update field based on organism neural activity with safety checks"""
        try:
            # Decay field
            self.field *= 0.9
            np.clip(self.field, 0, 1, out=self.field)

            for org in organisms:
                if not org.alive:
                    continue

                try:
                    # Safe position to grid conversion
                    pos_x = max(0, min(float(org.pos.x), self.width))
                    pos_y = max(0, min(float(org.pos.y), self.height))
                    
                    grid_x = int((pos_x / self.width) * (self.grid_w - 1))
                    grid_y = int((pos_y / self.height) * (self.grid_h - 1))

                    # Get neural activity with safety checks
                    vitals = org.brain.get_vitals()
                    activity_color = np.array([
                        max(0.0, min(1.0, float(vitals['activation']))),
                        max(0.0, min(1.0, float(vitals['energy']) / 1000.0)),
                        max(0.0, min(1.0, float(vitals['connections']) / 100.0))
                    ])

                    # Apply to field with falloff
                    for dx in range(-2, 3):
                        for dy in range(-2, 3):
                            x = (grid_x + dx) % self.grid_w
                            y = (grid_y + dy) % self.grid_h
                            
                            distance = np.sqrt(dx * dx + dy * dy)
                            if distance < 3:
                                intensity = max(0.0, min(1.0, (3 - distance) / 3))
                                self.field[x, y] += activity_color * intensity

                except (ValueError, TypeError, ZeroDivisionError) as e:
                    logging.error(f"Error updating field for organism {id(org)}: {e}")
                    continue  # Skip problematic organisms

            # Ensure field values stay in valid range
            np.clip(self.field, 0, 1, out=self.field)
        except Exception as e:
            logging.error(f"Error in InteractionField.update: {e}")

    def get_field_at(self, x: float, y: float) -> np.ndarray:
        """Safely get field value at position"""
        try:
            x = max(0, min(float(x), self.width))
            y = max(0, min(float(y), self.height))
            
            grid_x = int((x / self.width) * (self.grid_w - 1))
            grid_y = int((y / self.height) * (self.grid_h - 1))
            
            return self.field[grid_x, grid_y]
        except (ValueError, TypeError, IndexError) as e:
            logging.error(f"Error in get_field_at for position ({x}, {y}): {e}")
            return np.zeros(3)

# ==============================
# Simulation State Tracking
# ==============================

class SimulationState:
    """Tracks the current state of the simulation"""
    def __init__(self):
        self.running = False
        self.paused = False
        self.step_count = 0
        self.stats = {
            'population': 0,
            'avg_energy': 0.0,
            'avg_neurons': 0.0,
            'avg_connections': 0.0,
            'total_interactions': 0
        }
        self.selected_organism: Optional[FractalOrganism] = None

# ==============================
# Main Simulation Class
# ==============================

class FractalLifeSimulation:
    def __init__(self, config: SimulationConfig, shared_data: Dict):
        self.config = config
        self.state = SimulationState()

        # Shared data for Gradio interface
        self.shared_data = shared_data
        self.shared_lock = threading.Lock()

        # Initialize systems
        visualization_config = VisualizationConfig()
        self.visualizer = SimulationVisualizer(config.WIDTH, config.HEIGHT, visualization_config)
        self.physics = PhysicsEngine(config.WIDTH, config.HEIGHT, PhysicsConfig())
        self.field = InteractionField(config.WIDTH, config.HEIGHT, resolution=50)

        # Event queues for thread communication
        self.event_queue = Queue()
        self.stats_queue = Queue()

        # Initialize organisms
        self.organisms: List[FractalOrganism] = []
        self._init_organisms()

        # Pygame threading control
        self.running = False
        self.thread = None

    def _init_organisms(self):
        """Initialize starting organisms"""
        try:
            for _ in range(self.config.MIN_ORGANISMS):
                x = random.uniform(0, self.config.WIDTH)
                y = random.uniform(0, self.config.HEIGHT)
                organism = FractalOrganism(
                    x=x, y=y,
                    feature_dim=32,
                    max_neurons=self.config.MAX_NEURONS
                )
                self.organisms.append(organism)
                self.physics.add_organism(organism)
        except Exception as e:
            logging.error(f"Error initializing organisms: {e}")

    def _process_reproduction(self):
        """Handle organism reproduction"""
        try:
            new_organisms = []
            for org in self.organisms:
                if not org.alive or len(self.organisms) + len(new_organisms) >= self.config.MAX_ORGANISMS:
                    continue

                if org.brain.total_energy > self.config.REPRODUCTION_ENERGY:
                    # Find a mate (simple random selection for demonstration)
                    potential_mates = [o for o in self.organisms if o != org and o.alive and o.brain.total_energy > self.config.REPRODUCTION_ENERGY]
                    if potential_mates:
                        mate = random.choice(potential_mates)
                        child = org.reproduce(mate, mutation_rate=self.config.MUTATION_RATE)
                        if child:
                            new_organisms.append(child)
                            self.physics.add_organism(child)

            self.organisms.extend(new_organisms)
        except Exception as e:
            logging.error(f"Error processing reproduction: {e}")

    def _update_stats(self):
        """Update simulation statistics"""
        try:
            living_organisms = [org for org in self.organisms if org.alive]
            population = len(living_organisms)

            if population > 0:
                self.state.stats.update({
                    'population': population,
                    'avg_energy': sum(org.brain.total_energy for org in living_organisms) / population,
                    'avg_neurons': sum(org.brain.total_neurons for org in living_organisms) / population,
                    'avg_connections': sum(sum(n.state.connections for n in [org.brain.visual_cortex,
                                                                            org.brain.thought_processor,
                                                                            org.brain.action_generator])
                                       for org in living_organisms) / population,
                    'total_interactions': len(self.physics.current_interactions)
                })
            else:
                self.state.stats.update({
                    'population': 0,
                    'avg_energy': 0.0,
                    'avg_neurons': 0.0,
                    'avg_connections': 0.0,
                    'total_interactions': 0
                })

            with self.shared_lock:
                self.shared_data['stats'] = self.state.stats.copy()
        except Exception as e:
            logging.error(f"Error updating statistics: {e}")

    def _main_loop(self):
        """Main simulation loop"""
        try:
            clock = pygame.time.Clock()

            while self.running:
                dt = clock.tick(self.config.TARGET_FPS) / 1000.0  # Delta time in seconds

                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        self.stop()

                # Process external events
                while not self.event_queue.empty():
                    event = self.event_queue.get()
                    self._handle_event(event)

                if not self.state.paused:
                    # Update physics
                    self.physics.update(dt)

                    # Update neural field
                    self.field.update(self.organisms)

                    # Update organisms
                    for org in self.organisms:
                        if org.alive:
                            # Update organism state
                            org.update(self.config.WIDTH, self.config.HEIGHT, self.organisms)

                            # Process field interactions (placeholder for actual implementation)
                            field_value = self.field.get_field_at(org.pos.x, org.pos.y)
                            # org.process_field_input(field_value)  # Implement if needed

                            # Energy decay
                            org.brain.total_energy = max(0.0, org.brain.total_energy - self.config.ENERGY_DECAY)

                    # Process physical interactions
                    self.physics.process_interactions(self.organisms)

                    # Handle reproduction
                    self._process_reproduction()

                    # Remove dead organisms
                    self.organisms = [org for org in self.organisms if org.alive]

                    # Maintain minimum population
                    while len(self.organisms) < self.config.MIN_ORGANISMS and len(self.organisms) < self.config.MAX_ORGANISMS:
                        x = random.uniform(0, self.config.WIDTH)
                        y = random.uniform(0, self.config.HEIGHT)
                        organism = FractalOrganism(
                            x=x, y=y,
                            feature_dim=32,
                            max_neurons=self.config.MAX_NEURONS
                        )
                        self.organisms.append(organism)
                        self.physics.add_organism(organism)

                    # Update statistics
                    self._update_stats()

                    # Draw frame
                    self.visualizer.draw_frame(self.organisms, self.state.stats)
        except Exception as e:
            logging.error(f"Exception in main loop: {e}")
        finally:
            # Cleanup when simulation stops
            self.visualizer.cleanup()

    def start(self):
        """Start simulation in separate thread"""
        if not self.running:
            self.running = True
            self.thread = threading.Thread(target=self._main_loop)
            self.thread.start()
            logging.info("Simulation started.")

    def stop(self):
        """Stop simulation"""
        self.running = False
        if self.thread and self.thread.is_alive():
            self.thread.join()
            logging.info("Simulation stopped.")

    def pause(self):
        """Pause/unpause simulation"""
        self.state.paused = not self.state.paused
        logging.info(f"Simulation {'paused' if self.state.paused else 'resumed'}.")

    def _handle_event(self, event: Dict):
        """Handle external events"""
        try:
            if event['type'] == 'select_organism':
                organism_id = event['organism_id']
                self.state.selected_organism = next(
                    (org for org in self.organisms if id(org) == organism_id),
                    None
                )
                logging.info(f"Organism {organism_id} selected.")
            elif event['type'] == 'add_energy':
                if self.state.selected_organism:
                    self.state.selected_organism.brain.total_energy += event['amount']
                    logging.info(f"Added {event['amount']} energy to organism {id(self.state.selected_organism)}.")
            elif event['type'] == 'modify_neurons':
                if self.state.selected_organism:
                    # Placeholder for neuron modification logic
                    logging.info(f"Modify neurons event received for organism {id(self.state.selected_organism)}.")
        except Exception as e:
            logging.error(f"Error handling event {event}: {e}")

# ==============================
# Gradio Interface
# ==============================

class FractalLifeInterface:
    def __init__(self):
        self.simulation: Optional[FractalLifeSimulation] = None
        self.history = {
            'population': [],
            'avg_energy': [],
            'avg_neurons': [],
            'time': []
        }
        self.selected_organism_id = None
        self.frame_image = None
        self.DEFAULT_WIDTH = 1024
        self.DEFAULT_HEIGHT = 768
        
        self.shared_data = {
            'stats': {
                'population': 0,
                'avg_energy': 0.0,
                'avg_neurons': 0.0,
                'avg_connections': 0.0,
                'total_interactions': 0
            }
        }

    def get_frame(self):
        """Retrieve the latest frame from Pygame."""
        try:
            if self.simulation and self.simulation.running:
                with self.simulation.shared_lock:
                    # Get pygame surface
                    screen = self.simulation.visualizer.screen
                    
                    # Get the size of the screen
                    width = screen.get_width()
                    height = screen.get_height()
                    
                    # Create a new surface with alpha channel
                    surf_alpha = pygame.Surface((width, height), pygame.SRCALPHA)
                    surf_alpha.blit(screen, (0, 0))
                    
                    # Convert Pygame surface to PIL Image
                    data_string = pygame.image.tostring(surf_alpha, 'RGBA')
                    image = Image.frombytes('RGBA', (width, height), data_string)
                    
                    # Convert to RGB
                    image = image.convert('RGB')
                    
                    return image
            else:
                # Return a blank image
                return Image.new('RGB', (self.DEFAULT_WIDTH, self.DEFAULT_HEIGHT), (0, 0, 0))
                
        except Exception as e:
            logging.error(f"Error in get_frame: {e}")
            # Return a fallback image in case of error
            return Image.new('RGB', (self.DEFAULT_WIDTH, self.DEFAULT_HEIGHT), (0, 0, 0))
        
    def update_display(self):
        """Update display by retrieving the latest frame and statistics."""
        try:
            # Get the frame as PIL Image
            image = self.get_frame()
            
            # Update statistics only if simulation is running
            if self.simulation and self.simulation.running:
                with self.simulation.shared_lock:
                    stats = self.shared_data['stats']
                    # Update history
                    self.history['time'].append(self.simulation.state.step_count)
                    self.history['population'].append(stats['population'])
                    self.history['avg_energy'].append(stats['avg_energy'])
                    self.history['avg_neurons'].append(stats['avg_neurons'])
                    
                    # Limit history length to prevent memory issues
                    max_history = 1000
                    if len(self.history['time']) > max_history:
                        for key in self.history:
                            self.history[key] = self.history[key][-max_history:]
            else:
                stats = self.shared_data['stats']

            # Update plots
            stats_fig = self._create_stats_plot()
            neural_fig = self._create_neural_plot()

            # Update organism list only if simulation is running
            organism_list = []
            if self.simulation and self.simulation.running:
                organism_list = [
                    (f"Organism {id(org)}", id(org))
                    for org in self.simulation.organisms
                    if org.alive
                ]

            # Update selected organism vitals
            vitals = None
            if self.selected_organism_id and self.simulation and self.simulation.running:
                org = next(
                    (org for org in self.simulation.organisms
                     if id(org) == self.selected_organism_id),
                    None
                )
                if org:
                    vitals = {
                        'energy': org.brain.total_energy,
                        'neurons': org.brain.total_neurons,
                        'age': org.age,
                        'connections': sum(n.state.connections for n in
                                        [org.brain.visual_cortex,
                                         org.brain.thought_processor,
                                         org.brain.action_generator])
                    }

            # Create new Dropdown choices instead of using update
            dropdown = gr.Dropdown(
                choices=organism_list,
                label="Select Organism",
                interactive=True
            )

            return [
                image,  # Return PIL Image directly
                stats_fig,
                neural_fig,
                dropdown,
                vitals
            ]
            
        except Exception as e:
            logging.error(f"Error in update_display: {e}")
            # Return default values in case of error
            blank_image = Image.new('RGB', (self.DEFAULT_WIDTH, self.DEFAULT_HEIGHT), (0, 0, 0))
            empty_fig = go.Figure()
            empty_dropdown = gr.Dropdown(choices=[])
            return [blank_image, empty_fig, empty_fig, empty_dropdown, None]

    def create_interface(self):
        with gr.Blocks(title="Fractal Life Simulator") as interface:
            gr.Markdown("# 🧬 Fractal Life Simulator")

            with gr.Row():
                # Main simulation view and controls
                with gr.Column(scale=2):
                    canvas = gr.Image(label="Simulation View", interactive=False)

                    with gr.Row():
                        start_btn = gr.Button("Start Simulation", variant="primary")
                        pause_btn = gr.Button("Pause")
                        stop_btn = gr.Button("Stop")

                    with gr.Row():
                        population_slider = gr.Slider(
                            minimum=5, maximum=100,  # Increased maximum for flexibility
                            value=10, step=1,
                            label="Initial Population"
                        )
                        mutation_rate = gr.Slider(
                            minimum=0, maximum=1, value=0.1, step=0.01,
                            label="Mutation Rate"
                        )
                        max_population_slider = gr.Slider(
                            minimum=50, maximum=500, value=50, step=10,
                            label="Max Population"
                        )

                # Statistics and organism details
                with gr.Column(scale=1):
                    stats_plot = gr.Plot(label="Population Statistics")
                    neural_plot = gr.Plot(label="Neural Activity")

                    with gr.Group():
                        gr.Markdown("### Selected Organism")
                        organism_dropdown = gr.Dropdown(
                            label="Select Organism",
                            choices=[],
                            interactive=True
                        )
                        vitals_json = gr.JSON(label="Organism Vitals")

                        with gr.Row():
                            add_energy_btn = gr.Button("Add Energy")
                            add_neurons_btn = gr.Button("Add Neurons")

            # Advanced settings tab
            with gr.Tab("Advanced Settings"):
                with gr.Row():
                    with gr.Column():
                        brain_update_rate = gr.Slider(
                            minimum=1, maximum=30, value=10, step=1,
                            label="Brain Update Rate (Hz)"
                        )
                        max_neurons = gr.Slider(
                            minimum=100, maximum=5000, value=1000, step=100,
                            label="Max Neurons per Brain"
                        )
                        energy_decay = gr.Slider(
                            minimum=0, maximum=1, value=0.1, step=0.01,
                            label="Energy Decay Rate"
                        )

                    with gr.Column():
                        interaction_strength = gr.Slider(
                            minimum=0, maximum=1, value=0.5, step=0.01,
                            label="Interaction Strength"
                        )
                        field_resolution = gr.Slider(
                            minimum=10, maximum=100, value=50, step=5,
                            label="Field Resolution"
                        )

            # Event handlers
            def start_simulation(initial_population, mutation_rate_val, max_population_val, 

                            brain_update_rate_val, max_neurons_val, energy_decay_val, 

                            interaction_strength_val, field_resolution_val):
                try:
                    if self.simulation is None:
                        config = SimulationConfig()
                        config.MIN_ORGANISMS = int(initial_population)
                        config.MUTATION_RATE = mutation_rate_val
                        config.MAX_ORGANISMS = int(max_population_val)
                        config.BRAIN_UPDATE_RATE = int(brain_update_rate_val)
                        config.MAX_NEURONS = int(max_neurons_val)
                        config.ENERGY_DECAY = energy_decay_val

                        self.simulation = FractalLifeSimulation(config, self.shared_data)
                        self.simulation.start()
                        logging.info("Simulation started via interface.")
                        return "Simulation started"
                    else:
                        logging.warning("Simulation is already running.")
                        return "Simulation is already running."
                except Exception as e:
                    logging.error(f"Error starting simulation: {e}")
                    return "Failed to start simulation."

            def pause_simulation():
                try:
                    if self.simulation:
                        self.simulation.pause()
                        status = "paused" if self.simulation.state.paused else "resumed"
                        logging.info(f"Simulation {status} via interface.")
                        return f"Simulation {status}"
                    logging.warning("No simulation running to pause/resume.")
                    return "No simulation running"
                except Exception as e:
                    logging.error(f"Error pausing simulation: {e}")
                    return "Failed to pause simulation."

            def stop_simulation():
                try:
                    if self.simulation:
                        self.simulation.stop()
                        self.simulation = None
                        logging.info("Simulation stopped via interface.")
                        return "Simulation stopped"
                    logging.warning("No simulation running to stop.")
                    return "No simulation running"
                except Exception as e:
                    logging.error(f"Error stopping simulation: {e}")
                    return "Failed to stop simulation."

            def select_organism(organism_id):
                try:
                    self.selected_organism_id = organism_id
                    if self.simulation:
                        self.simulation.event_queue.put({
                            'type': 'select_organism',
                            'organism_id': organism_id
                        })
                        logging.info(f"Organism {organism_id} selected via interface.")
                except Exception as e:
                    logging.error(f"Error selecting organism: {e}")

            def add_energy_to_organism():
                try:
                    if self.simulation and self.selected_organism_id:
                        self.simulation.event_queue.put({
                            'type': 'add_energy',
                            'amount': 50.0
                        })
                        logging.info(f"Added energy to organism {self.selected_organism_id} via interface.")
                        return "Added energy to selected organism"
                    logging.warning("No organism selected or simulation not running to add energy.")
                    return "No organism selected or simulation not running"
                except Exception as e:
                    logging.error(f"Error adding energy to organism: {e}")
                    return "Failed to add energy"

            def add_neurons_to_organism():
                try:
                    if self.simulation and self.selected_organism_id:
                        self.simulation.event_queue.put({
                            'type': 'modify_neurons',
                            'amount': 10
                        })
                        logging.info(f"Added neurons to organism {self.selected_organism_id} via interface.")
                        return "Added neurons to selected organism"
                    logging.warning("No organism selected or simulation not running to add neurons.")
                    return "No organism selected or simulation not running"
                except Exception as e:
                    logging.error(f"Error adding neurons to organism: {e}")
                    return "Failed to add neurons"

            # Create a bound method for update_display
            def bound_update_display():
                return self.update_display()

            # Connect events
            start_btn.click(
                start_simulation, 
                inputs=[population_slider, mutation_rate, max_population_slider, 
                    brain_update_rate, max_neurons, energy_decay, 
                    interaction_strength, field_resolution], 
                outputs=gr.Textbox()
            )
            pause_btn.click(pause_simulation, outputs=gr.Textbox())
            stop_btn.click(stop_simulation, outputs=gr.Textbox())

            organism_dropdown.change(select_organism, inputs=[organism_dropdown], outputs=None)
            add_energy_btn.click(add_energy_to_organism, outputs=gr.Textbox())
            add_neurons_btn.click(add_neurons_to_organism, outputs=gr.Textbox())

            # Periodic update using Gradio's update mechanism
            interface.load(
                fn=bound_update_display,
                inputs=[],
                outputs=[canvas, stats_plot, neural_plot, organism_dropdown, vitals_json],
                every=1/30,  # 30 FPS updates
                queue=True
            )

            return interface

    def _create_stats_plot(self):
        """Create statistics plot using plotly"""
        try:
            fig = make_subplots(rows=3, cols=1, shared_xaxes=True,
                                subplot_titles=("Population", "Average Energy", "Average Neurons"))

            fig.add_trace(
                go.Scatter(x=self.history['time'], y=self.history['population'],
                           name="Population"),
                row=1, col=1
            )

            fig.add_trace(
                go.Scatter(x=self.history['time'], y=self.history['avg_energy'],
                           name="Avg Energy"),
                row=2, col=1
            )

            fig.add_trace(
                go.Scatter(x=self.history['time'], y=self.history['avg_neurons'],
                           name="Avg Neurons"),
                row=3, col=1
            )

            fig.update_layout(height=600, showlegend=True)
            return fig
        except Exception as e:
            logging.error(f"Error creating stats plot: {e}")
            return go.Figure()

    def _create_neural_plot(self):
        """Create neural activity plot"""
        try:
            if self.selected_organism_id and self.simulation and self.simulation.running:
                org = next(
                    (org for org in self.simulation.organisms
                     if id(org) == self.selected_organism_id),
                    None
                )
                if org:
                    # Create neural activity heatmap
                    neurons = [org.brain.visual_cortex,
                            org.brain.thought_processor,
                            org.brain.action_generator]

                    activities = []
                    for neuron in neurons:
                        layer_activities = [child.state.activation for child in neuron.sub_neurons] if hasattr(neuron, 'sub_neurons') and neuron.sub_neurons else [neuron.state.activation]
                        activities.append(layer_activities)

                    activities = np.array(activities)

                    fig = go.Figure(data=go.Heatmap(z=activities, colorscale='Viridis'))
                    fig.update_layout(
                        title="Neural Activity",
                        xaxis_title="Neuron Index",
                        yaxis_title="Layer"
                    )
                    return fig
            return go.Figure()
        except Exception as e:
            logging.error(f"Error creating neural plot: {e}")
            return go.Figure()

# ==============================
# Entry Point
# ==============================

if __name__ == "__main__":
    interface = FractalLifeInterface()
    gr_interface = interface.create_interface()
    # Enable queue and allow for public access
    gr_interface.queue().launch(
        server_name="0.0.0.0", 
        server_port=7860,
        share=True  # This creates a public link
    )