Spaces:
Running
Running
File size: 73,346 Bytes
c136415 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced AI Flocking Evolution Simulator</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
background: #000;
}
#ui {
position: absolute;
top: 10px;
left: 10px;
color: white;
background-color: rgba(0,0,0,0.9);
padding: 15px;
border-radius: 8px;
z-index: 100;
font-size: 14px;
min-width: 200px;
}
#controls {
position: absolute;
top: 10px;
right: 10px;
color: white;
background-color: rgba(0,0,0,0.9);
padding: 15px;
border-radius: 8px;
z-index: 100;
}
button {
background-color: #4CAF50;
border: none;
color: white;
padding: 8px 16px;
margin: 5px;
cursor: pointer;
border-radius: 4px;
font-size: 12px;
}
button:hover {
background-color: #45a049;
}
#stats {
position: absolute;
bottom: 10px;
left: 10px;
color: white;
background-color: rgba(0,0,0,0.9);
padding: 15px;
border-radius: 8px;
z-index: 100;
font-size: 12px;
min-width: 200px;
}
#flockingStats {
position: absolute;
bottom: 10px;
right: 10px;
color: white;
background-color: rgba(0,0,0,0.9);
padding: 15px;
border-radius: 8px;
z-index: 100;
font-size: 12px;
min-width: 180px;
}
#aiStats {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
color: white;
background-color: rgba(0,0,0,0.9);
padding: 15px;
border-radius: 8px;
z-index: 100;
font-size: 12px;
min-width: 180px;
}
.highlight { color: #ffcc00; font-weight: bold; }
.success { color: #00ff00; font-weight: bold; }
.flocking { color: #00aaff; }
.solo { color: #ff8800; }
.leader { color: #ff00ff; font-weight: bold; }
.explorer { color: #00ffff; }
.follower { color: #88ff88; }
.species-0 { color: #ff6b6b; }
.species-1 { color: #4ecdc4; }
.species-2 { color: #45b7d1; }
.species-3 { color: #96ceb4; }
.species-4 { color: #ffd93d; }
.progress-bar {
width: 100%;
height: 10px;
background-color: #333;
border-radius: 5px;
overflow: hidden;
margin: 5px 0;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1);
transition: width 0.3s ease;
}
</style>
</head>
<body>
<div id="ui">
<div class="highlight">Enhanced AI Evolution Simulator</div>
<div>Epoch: <span id="epoch">1</span></div>
<div>Time: <span id="epochTime">60</span>s</div>
<div class="progress-bar"><div class="progress-fill" id="timeProgress"></div></div>
<div>Population: <span id="population">100</span></div>
<div>Species: <span id="speciesCount">1</span></div>
<div>Best Fitness: <span id="bestFitness">0</span></div>
<div>Avg IQ: <span id="avgIQ">50</span></div>
<div>Innovation: <span id="innovationCount">0</span></div>
</div>
<div id="controls">
<button id="pauseBtn">Pause</button>
<button id="resetBtn">Reset</button>
<button id="speedBtn">Speed: 1x</button>
<button id="viewBtn">View: Follow</button>
<button id="flockBtn">Flocks: ON</button>
<button id="adaptiveBtn">Adaptive: ON</button>
<button id="challengeBtn">Challenge: Normal</button>
</div>
<div id="stats">
<div><span class="highlight">Top Performers:</span></div>
<div id="topPerformers"></div>
<div style="margin-top: 10px;"><span class="highlight">Generation Stats:</span></div>
<div>Crashes: <span id="crashCount">0</span></div>
<div>Total Distance: <span id="totalDistance">0</span></div>
<div>Exploration: <span id="explorationBonus">0</span></div>
<div>Cooperation: <span id="cooperationScore">0</span></div>
<div>Road Mastery: <span id="roadMastery">0</span>%</div>
</div>
<div id="flockingStats">
<div><span class="highlight">Flocking Dynamics:</span></div>
<div><span class="leader">Leaders:</span> <span id="leaderCount">0</span></div>
<div><span class="flocking">Followers:</span> <span id="followerCount">0</span></div>
<div><span class="explorer">Explorers:</span> <span id="explorerCount">0</span></div>
<div><span class="solo">Solo:</span> <span id="soloCount">0</span></div>
<div>Largest Flock: <span id="largestFlock">0</span></div>
<div>Avg Coordination: <span id="avgCoordination">0</span>%</div>
<div>Group Efficiency: <span id="groupEfficiency">0</span>%</div>
</div>
<div id="aiStats">
<div><span class="highlight">AI Intelligence:</span></div>
<div>Neural Complexity: <span id="neuralComplexity">100</span></div>
<div>Decision Quality: <span id="decisionQuality">50</span>%</div>
<div>Learning Rate: <span id="learningRate">1.0</span></div>
<div>Memory Usage: <span id="memoryUsage">0</span>%</div>
<div style="margin-top: 10px;"><span class="highlight">Behaviors:</span></div>
<div>Predictive: <span id="predictiveBehavior">0</span>%</div>
<div>Adaptive: <span id="adaptiveBehavior">0</span>%</div>
<div>Emergent: <span id="emergentBehavior">0</span>%</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Global variables
let scene, camera, renderer, clock;
let world = {
roads: [],
intersections: [],
buildings: [],
jumpRamps: [],
flockLines: [],
dynamicObstacles: [],
targets: []
};
// Enhanced evolution system
let epoch = 1;
let epochTime = 60;
let timeLeft = 60;
let population = [];
let species = [];
let populationSize = 100;
let bestFitness = 0;
let totalDistance = 0;
let groupDistance = 0;
let crashCount = 0;
let paused = false;
let speedMultiplier = 1;
let cameraMode = 'follow';
let showFlockLines = true;
let adaptiveEnvironment = true;
let challengeLevel = 'normal'; // normal, hard, extreme
let innovationCounter = 0;
let globalMemory = new Map();
// Enhanced AI parameters
const NEIGHBOR_RADIUS = 30;
const SEPARATION_RADIUS = 10;
const LEADERSHIP_RADIUS = 40;
const MEMORY_SIZE = 10;
const SPECIES_THRESHOLD = 3.0;
const TARGET_SPECIES = 5;
// Dynamic challenge system
let dynamicChallenges = {
obstacles: [],
targets: [],
weather: 'clear',
timeOfDay: 'day'
};
// Enhanced Neural Network with memory and multiple layers
class EnhancedNeuralNetwork {
constructor() {
this.inputSize = 24; // Expanded sensory inputs
this.hiddenLayers = [32, 24, 16]; // Multi-layer deep network
this.outputSize = 8; // More nuanced outputs
this.memorySize = MEMORY_SIZE;
// Initialize all weight matrices and biases
this.weights = [];
this.biases = [];
this.memory = new Array(this.memorySize).fill(0);
this.memoryPointer = 0;
// Build network layers
let prevSize = this.inputSize + this.memorySize;
for (let i = 0; i < this.hiddenLayers.length; i++) {
this.weights.push(this.randomMatrix(prevSize, this.hiddenLayers[i]));
this.biases.push(this.randomArray(this.hiddenLayers[i]));
prevSize = this.hiddenLayers[i];
}
// Output layer
this.weights.push(this.randomMatrix(prevSize, this.outputSize));
this.biases.push(this.randomArray(this.outputSize));
// Specialized modules
this.attentionWeights = this.randomArray(this.inputSize);
this.innovationGenes = this.randomArray(10);
this.personalityTraits = {
leadership: Math.random(),
exploration: Math.random(),
cooperation: Math.random(),
caution: Math.random(),
adaptability: Math.random()
};
}
randomMatrix(rows, cols) {
let matrix = [];
for (let i = 0; i < rows; i++) {
matrix[i] = [];
for (let j = 0; j < cols; j++) {
matrix[i][j] = (Math.random() - 0.5) * 2;
}
}
return matrix;
}
randomArray(size) {
return Array(size).fill().map(() => (Math.random() - 0.5) * 2);
}
// Advanced activation with attention mechanism
activate(inputs) {
// Apply attention mechanism to inputs
const attentionScores = inputs.map((input, i) =>
input * this.sigmoid(this.attentionWeights[i])
);
// Combine inputs with memory
let currentInput = [...attentionScores, ...this.memory];
// Forward pass through hidden layers
for (let layer = 0; layer < this.hiddenLayers.length; layer++) {
currentInput = this.forwardLayer(currentInput, this.weights[layer], this.biases[layer]);
}
// Output layer
const outputs = this.forwardLayer(currentInput,
this.weights[this.weights.length - 1],
this.biases[this.biases.length - 1]);
// Update memory with current state
this.updateMemory(inputs, outputs);
return outputs;
}
forwardLayer(inputs, weights, biases) {
const outputs = new Array(weights[0].length).fill(0);
for (let i = 0; i < outputs.length; i++) {
for (let j = 0; j < inputs.length; j++) {
outputs[i] += inputs[j] * weights[j][i];
}
outputs[i] += biases[i];
outputs[i] = this.advancedActivation(outputs[i]);
}
return outputs;
}
// Advanced activation function combining sigmoid and tanh
advancedActivation(x) {
const clampedX = Math.max(-10, Math.min(10, x));
return (this.sigmoid(clampedX) + Math.tanh(clampedX)) / 2;
}
sigmoid(x) {
return 1 / (1 + Math.exp(-Math.max(-500, Math.min(500, x))));
}
updateMemory(inputs, outputs) {
// Store important environmental information
const importance = Math.max(...inputs.slice(0, 8)); // Obstacle sensor max
this.memory[this.memoryPointer] = importance;
this.memoryPointer = (this.memoryPointer + 1) % this.memorySize;
}
// Advanced mutation with adaptive rates
mutate(baseRate = 0.1, innovation = false) {
const adaptiveRate = baseRate * (1 + this.personalityTraits.adaptability);
// Mutate weights
this.weights.forEach(weightMatrix => {
this.mutateMatrix(weightMatrix, adaptiveRate);
});
// Mutate biases
this.biases.forEach(biasArray => {
this.mutateArray(biasArray, adaptiveRate);
});
// Mutate attention weights
this.mutateArray(this.attentionWeights, adaptiveRate * 0.5);
// Mutate personality traits
Object.keys(this.personalityTraits).forEach(trait => {
if (Math.random() < adaptiveRate) {
this.personalityTraits[trait] += (Math.random() - 0.5) * 0.2;
this.personalityTraits[trait] = Math.max(0, Math.min(1, this.personalityTraits[trait]));
}
});
// Innovation mutations
if (innovation) {
this.mutateArray(this.innovationGenes, adaptiveRate * 2);
innovationCounter++;
}
}
mutateMatrix(matrix, rate) {
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
if (Math.random() < rate) {
const mutationStrength = 0.5 * (1 + Math.random());
matrix[i][j] += (Math.random() - 0.5) * mutationStrength;
matrix[i][j] = Math.max(-5, Math.min(5, matrix[i][j])); // Clamp weights
}
}
}
}
mutateArray(array, rate) {
for (let i = 0; i < array.length; i++) {
if (Math.random() < rate) {
const mutationStrength = 0.5 * (1 + Math.random());
array[i] += (Math.random() - 0.5) * mutationStrength;
array[i] = Math.max(-5, Math.min(5, array[i])); // Clamp values
}
}
}
// Crossover with compatibility checking
crossover(other) {
const child = new EnhancedNeuralNetwork();
// Blend weights and biases
for (let layer = 0; layer < this.weights.length; layer++) {
for (let i = 0; i < this.weights[layer].length; i++) {
for (let j = 0; j < this.weights[layer][i].length; j++) {
child.weights[layer][i][j] = Math.random() < 0.5 ?
this.weights[layer][i][j] : other.weights[layer][i][j];
}
}
for (let i = 0; i < this.biases[layer].length; i++) {
child.biases[layer][i] = Math.random() < 0.5 ?
this.biases[layer][i] : other.biases[layer][i];
}
}
// Blend personality traits
Object.keys(this.personalityTraits).forEach(trait => {
child.personalityTraits[trait] = (this.personalityTraits[trait] + other.personalityTraits[trait]) / 2;
});
return child;
}
copy() {
const newNN = new EnhancedNeuralNetwork();
// Deep copy all components
newNN.weights = this.weights.map(matrix =>
matrix.map(row => [...row])
);
newNN.biases = this.biases.map(bias => [...bias]);
newNN.attentionWeights = [...this.attentionWeights];
newNN.memory = [...this.memory];
newNN.memoryPointer = this.memoryPointer;
newNN.innovationGenes = [...this.innovationGenes];
newNN.personalityTraits = {...this.personalityTraits};
return newNN;
}
// Calculate network complexity for visualization
getComplexity() {
let totalConnections = 0;
this.weights.forEach(matrix => {
totalConnections += matrix.length * matrix[0].length;
});
return totalConnections;
}
}
// Enhanced AI Car with advanced behaviors
class EnhancedAICar {
constructor(x = 0, z = 0) {
this.brain = new EnhancedNeuralNetwork();
this.mesh = this.createCarMesh();
this.mesh.position.set(x, 1, z);
// Enhanced movement properties
this.velocity = new THREE.Vector3(
(Math.random() - 0.5) * 10, 0, (Math.random() - 0.5) * 10
);
this.acceleration = new THREE.Vector3();
this.maxSpeed = 25;
this.minSpeed = 3;
this.accelerationForce = 0.6;
this.turnSpeed = 0.1;
// Advanced flocking and behavior
this.neighbors = [];
this.role = 'follower'; // leader, follower, explorer, scout
this.flockId = -1;
this.speciesId = 0;
this.leadership = this.brain.personalityTraits.leadership;
this.exploration = this.brain.personalityTraits.exploration;
this.cooperation = this.brain.personalityTraits.cooperation;
// Enhanced fitness and metrics
this.fitness = 0;
this.rawFitness = 0;
this.adjustedFitness = 0;
this.distanceTraveled = 0;
this.explorationBonus = 0;
this.cooperationScore = 0;
this.leadershipScore = 0;
this.innovationScore = 0;
this.decisionQuality = 50;
this.predictiveAccuracy = 0;
// State tracking
this.timeAlive = 100;
this.crashed = false;
this.lastPosition = new THREE.Vector3(x, 1, z);
this.visitedAreas = new Set();
this.decisions = [];
this.predictions = [];
// Enhanced sensors
this.sensors = Array(12).fill(0); // More sensors
this.environmentSensors = Array(4).fill(0);
this.socialSensors = Array(8).fill(0);
this.sensorRays = [];
this.flockLines = [];
this.createSensorRays();
this.createFlockVisualization();
this.initializeMovement();
}
createCarMesh() {
const group = new THREE.Group();
// Enhanced car body with role-based styling
const bodyGeometry = new THREE.BoxGeometry(1.5, 0.8, 3);
this.bodyMaterial = new THREE.MeshLambertMaterial({
color: new THREE.Color().setHSL(Math.random(), 0.8, 0.6)
});
const body = new THREE.Mesh(bodyGeometry, this.bodyMaterial);
body.position.y = 0.4;
body.castShadow = true;
group.add(body);
// Role indicator
const indicatorGeometry = new THREE.SphereGeometry(0.2, 8, 6);
this.roleIndicator = new THREE.Mesh(indicatorGeometry,
new THREE.MeshLambertMaterial({ color: 0xffffff }));
this.roleIndicator.position.set(0, 1.5, 0);
group.add(this.roleIndicator);
// Intelligence indicator (size based on neural complexity)
const complexity = this.brain.getComplexity();
const brainSize = 0.1 + (complexity / 10000) * 0.4;
const brainGeometry = new THREE.SphereGeometry(brainSize, 6, 4);
this.brainIndicator = new THREE.Mesh(brainGeometry,
new THREE.MeshLambertMaterial({
color: 0x00ffff,
transparent: true,
opacity: 0.7
}));
this.brainIndicator.position.set(0, 1.8, 0);
group.add(this.brainIndicator);
// Enhanced wheels with rotation
const wheelGeometry = new THREE.CylinderGeometry(0.3, 0.3, 0.2, 8);
const wheelMaterial = new THREE.MeshLambertMaterial({ color: 0x333333 });
this.wheels = [];
const wheelPositions = [
[-0.8, 0, 1.2], [0.8, 0, 1.2],
[-0.8, 0, -1.2], [0.8, 0, -1.2]
];
wheelPositions.forEach((pos, i) => {
const wheel = new THREE.Mesh(wheelGeometry, wheelMaterial);
wheel.position.set(...pos);
wheel.rotation.z = Math.PI / 2;
this.wheels.push(wheel);
group.add(wheel);
});
return group;
}
createSensorRays() {
const sensorMaterial = new THREE.LineBasicMaterial({
color: 0xff0000,
transparent: true,
opacity: 0.3
});
// 12 sensors for comprehensive environment detection
for (let i = 0; i < 12; i++) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, 8)
]);
const ray = new THREE.Line(geometry, sensorMaterial);
this.sensorRays.push(ray);
this.mesh.add(ray);
}
}
createFlockVisualization() {
const flockMaterial = new THREE.LineBasicMaterial({
color: 0x00ff00,
transparent: true,
opacity: 0.3
});
for (let i = 0; i < 8; i++) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 2, 0),
new THREE.Vector3(0, 2, 0)
]);
const line = new THREE.Line(geometry, flockMaterial);
this.flockLines.push(line);
if (showFlockLines) scene.add(line);
}
}
initializeMovement() {
this.mesh.rotation.y = Math.random() * Math.PI * 2;
this.velocity.set(
Math.sin(this.mesh.rotation.y) * (8 + Math.random() * 7),
0,
Math.cos(this.mesh.rotation.y) * (8 + Math.random() * 7)
);
}
updateEnhancedSensors() {
const maxDistance = 8;
const raycaster = new THREE.Raycaster();
// 12-direction sensor array
const sensorAngles = [];
for (let i = 0; i < 12; i++) {
sensorAngles.push((i * Math.PI * 2) / 12);
}
sensorAngles.forEach((angle, i) => {
const direction = new THREE.Vector3(
Math.sin(angle), 0, Math.cos(angle)
);
direction.applyQuaternion(this.mesh.quaternion);
raycaster.set(this.mesh.position, direction);
const intersects = raycaster.intersectObjects(this.getObstacles(), true);
if (intersects.length > 0 && intersects[0].distance <= maxDistance) {
this.sensors[i] = 1 - (intersects[0].distance / maxDistance);
} else {
this.sensors[i] = 0;
}
// Update visual ray
const endDistance = intersects.length > 0 ?
Math.min(intersects[0].distance, maxDistance) : maxDistance;
const rayEnd = direction.clone().multiplyScalar(endDistance);
this.sensorRays[i].geometry.setFromPoints([
new THREE.Vector3(0, 0, 0), rayEnd
]);
});
// Environment sensors
this.updateEnvironmentSensors();
}
updateEnvironmentSensors() {
const pos = this.mesh.position;
// Road detection with direction
this.environmentSensors[0] = this.detectRoadPosition();
// Obstacle density in area
let nearbyObstacles = 0;
population.forEach(other => {
if (other !== this && !other.crashed) {
const dist = pos.distanceTo(other.mesh.position);
if (dist < 20) nearbyObstacles++;
}
});
this.environmentSensors[1] = Math.min(nearbyObstacles / 5, 1);
// Target/goal direction (if any targets exist)
this.environmentSensors[2] = this.getTargetDirection();
// Exploration potential
this.environmentSensors[3] = this.getExplorationPotential();
}
updateAdvancedFlocking() {
this.neighbors = [];
this.socialSensors.fill(0);
let separation = new THREE.Vector3();
let alignment = new THREE.Vector3();
let cohesion = new THREE.Vector3();
let leadership = new THREE.Vector3();
let neighborCount = 0;
let leaderInfluence = 0;
population.forEach(other => {
if (other !== this && !other.crashed) {
const distance = this.mesh.position.distanceTo(other.mesh.position);
if (distance < NEIGHBOR_RADIUS && distance > 0) {
this.neighbors.push(other);
// Traditional flocking forces
cohesion.add(other.mesh.position);
alignment.add(other.velocity);
// Leadership dynamics
if (other.role === 'leader' && distance < LEADERSHIP_RADIUS) {
const influence = other.leadership * (1 - distance / LEADERSHIP_RADIUS);
leadership.add(other.velocity.clone().multiplyScalar(influence));
leaderInfluence += influence;
}
neighborCount++;
}
if (distance < SEPARATION_RADIUS && distance > 0) {
const diff = this.mesh.position.clone().sub(other.mesh.position);
diff.normalize().divideScalar(distance);
separation.add(diff);
}
}
});
// Finalize flocking forces
if (neighborCount > 0) {
cohesion.divideScalar(neighborCount).sub(this.mesh.position).normalize();
alignment.divideScalar(neighborCount).normalize();
this.cooperationScore += neighborCount * 0.1;
}
if (leaderInfluence > 0) {
leadership.normalize();
}
// Update social sensors
this.socialSensors[0] = Math.min(neighborCount / 10, 1); // Neighbor density
this.socialSensors[1] = separation.length(); // Separation strength
this.socialSensors[2] = alignment.length(); // Alignment strength
this.socialSensors[3] = cohesion.length(); // Cohesion strength
this.socialSensors[4] = leadership.length(); // Leadership influence
this.socialSensors[5] = this.leadership; // Own leadership
this.socialSensors[6] = this.cooperation; // Cooperation tendency
this.socialSensors[7] = this.role === 'leader' ? 1 : 0; // Role indicator
// Store forces for neural network
this.flockingForces = { separation, alignment, cohesion, leadership };
// Update role based on behavior
this.updateRole();
}
updateRole() {
const neighborCount = this.neighbors.length;
if (this.leadership > 0.7 && neighborCount > 2) {
this.role = 'leader';
this.leadershipScore += 1;
} else if (this.exploration > 0.8 && neighborCount < 2) {
this.role = 'explorer';
this.explorationBonus += this.velocity.length() * 0.1;
} else if (neighborCount > 0) {
this.role = 'follower';
} else {
this.role = 'scout';
}
// Update visual indicator
const colors = {
leader: 0xff00ff,
explorer: 0x00ffff,
follower: 0x88ff88,
scout: 0xffff00
};
this.roleIndicator.material.color.setHex(colors[this.role]);
}
getEnhancedInputs() {
// Comprehensive input vector
return [
...this.sensors, // 12 obstacle sensors
...this.environmentSensors, // 4 environment sensors
...this.socialSensors, // 8 social sensors
];
}
makeDecision(inputs, outputs) {
// Enhanced decision making with prediction
const decision = {
timestamp: Date.now(),
inputs: [...inputs],
outputs: [...outputs],
prediction: this.makePrediction(inputs),
confidence: this.calculateConfidence(outputs)
};
this.decisions.push(decision);
if (this.decisions.length > 20) {
this.decisions.shift();
}
// Update decision quality based on outcomes
this.updateDecisionQuality();
return outputs;
}
makePrediction(inputs) {
// Simple prediction: where will I be in 5 steps?
const prediction = this.mesh.position.clone().add(
this.velocity.clone().multiplyScalar(5)
);
this.predictions.push({
timestamp: Date.now(),
predicted: prediction,
actual: null // Will be filled later
});
return prediction;
}
calculateConfidence(outputs) {
// Confidence based on output certainty
const variance = outputs.reduce((sum, val) => sum + Math.pow(val - 0.5, 2), 0);
return Math.min(variance * 2, 1);
}
updateDecisionQuality() {
// Evaluate prediction accuracy
let accuracy = 0;
let validPredictions = 0;
this.predictions.forEach(pred => {
if (pred.actual) {
const error = pred.predicted.distanceTo(pred.actual);
accuracy += Math.max(0, 1 - error / 50); // Normalize error
validPredictions++;
}
});
if (validPredictions > 0) {
this.predictiveAccuracy = accuracy / validPredictions;
this.decisionQuality = this.predictiveAccuracy * 100;
}
}
detectRoadPosition() {
const pos = this.mesh.position;
const roadWidth = 12;
const roadSpacing = 150;
const nearestHorizontalRoad = Math.round(pos.z / roadSpacing) * roadSpacing;
const distToHorizontalRoad = Math.abs(pos.z - nearestHorizontalRoad);
const onHorizontalRoad = distToHorizontalRoad <= roadWidth / 2;
const nearestVerticalRoad = Math.round(pos.x / roadSpacing) * roadSpacing;
const distToVerticalRoad = Math.abs(pos.x - nearestVerticalRoad);
const onVerticalRoad = distToVerticalRoad <= roadWidth / 2;
if (onHorizontalRoad || onVerticalRoad) {
return Math.max(
onHorizontalRoad ? 1 - (distToHorizontalRoad / (roadWidth / 2)) : 0,
onVerticalRoad ? 1 - (distToVerticalRoad / (roadWidth / 2)) : 0
);
}
return 0;
}
getTargetDirection() {
// Find nearest unexplored area or target
if (world.targets.length > 0) {
const nearest = world.targets.reduce((closest, target) => {
const dist = this.mesh.position.distanceTo(target.position);
return dist < closest.distance ? { target, distance: dist } : closest;
}, { distance: Infinity });
if (nearest.distance < 100) {
const direction = nearest.target.position.clone()
.sub(this.mesh.position).normalize();
return (direction.dot(this.velocity.clone().normalize()) + 1) / 2;
}
}
return 0.5;
}
getExplorationPotential() {
// Calculate exploration potential based on visited areas
const currentArea = `${Math.floor(this.mesh.position.x / 50)},${Math.floor(this.mesh.position.z / 50)}`;
return this.visitedAreas.has(currentArea) ? 0.2 : 0.8;
}
update(deltaTime) {
if (this.crashed) return;
this.timeAlive -= deltaTime;
if (this.timeAlive <= 0) {
this.crashed = true;
return;
}
// Update all sensors and behaviors
this.updateEnhancedSensors();
this.updateAdvancedFlocking();
this.updateVisuals();
// Get comprehensive neural network inputs
const inputs = this.getEnhancedInputs();
// Get brain decision
const outputs = this.brain.activate(inputs);
// Process decision with prediction
const processedOutputs = this.makeDecision(inputs, outputs);
// Apply enhanced movement
this.applyEnhancedMovement(processedOutputs, deltaTime);
// Update fitness with advanced metrics
this.updateAdvancedFitness(deltaTime);
// Track exploration
this.trackExploration();
this.lastPosition.copy(this.mesh.position);
this.checkCollisions();
this.keepInBounds();
}
applyEnhancedMovement(outputs, deltaTime) {
// Enhanced output interpretation
const [
forwardForce, turnLeft, turnRight, brake,
emergencyStop, boost, preciseTurn, formation
] = outputs;
// Turning with precision control
const baseTurn = (turnRight - turnLeft) * this.turnSpeed;
const precisionTurn = (preciseTurn - 0.5) * this.turnSpeed * 0.5;
const totalTurn = (baseTurn + precisionTurn) * deltaTime;
this.mesh.rotation.y += totalTurn;
// Advanced acceleration
const forward = new THREE.Vector3(0, 0, 1);
forward.applyQuaternion(this.mesh.quaternion);
let acceleration = this.accelerationForce;
// Boost behavior
if (boost > 0.7) {
acceleration *= 1.5;
this.maxSpeed = 30;
} else {
this.maxSpeed = 25;
}
// Emergency stop
if (emergencyStop > 0.8) {
this.velocity.multiplyScalar(0.8);
} else if (forwardForce > 0.1) {
this.acceleration.add(forward.multiplyScalar(acceleration * forwardForce * deltaTime));
}
// Braking
if (brake > 0.5) {
this.velocity.multiplyScalar(1 - brake * deltaTime * 2);
}
// Apply flocking forces
if (this.flockingForces) {
const flockingStrength = formation * 0.5;
this.acceleration.add(this.flockingForces.separation.multiplyScalar(0.3));
this.acceleration.add(this.flockingForces.alignment.multiplyScalar(0.2 * flockingStrength));
this.acceleration.add(this.flockingForces.cohesion.multiplyScalar(0.2 * flockingStrength));
this.acceleration.add(this.flockingForces.leadership.multiplyScalar(0.4 * (1 - this.leadership)));
}
// Apply acceleration and velocity
this.velocity.add(this.acceleration);
this.acceleration.multiplyScalar(0.1); // Decay acceleration
// Speed limits
const currentSpeed = this.velocity.length();
if (currentSpeed > this.maxSpeed) {
this.velocity.normalize().multiplyScalar(this.maxSpeed);
} else if (currentSpeed < this.minSpeed) {
this.velocity.normalize().multiplyScalar(this.minSpeed);
}
// Apply movement
this.mesh.position.add(this.velocity.clone().multiplyScalar(deltaTime));
// Wheel rotation animation
this.wheels.forEach(wheel => {
wheel.rotation.x += this.velocity.length() * deltaTime * 0.1;
});
}
updateAdvancedFitness(deltaTime) {
const distance = this.mesh.position.distanceTo(this.lastPosition);
this.distanceTraveled += distance;
// Multi-objective fitness function
const roadBonus = this.detectRoadPosition() * distance * 3;
const groupBonus = Math.min(this.neighbors.length, 8) * distance * 2;
const roleBonus = this.getRoleBonus() * deltaTime;
const innovationBonus = this.innovationScore * 0.5;
const efficiencyBonus = this.getEfficiencyBonus();
this.rawFitness = this.distanceTraveled +
roadBonus +
groupBonus +
roleBonus +
this.explorationBonus +
this.cooperationScore +
innovationBonus +
efficiencyBonus;
// Update predictions
this.predictions.forEach(pred => {
if (!pred.actual && Date.now() - pred.timestamp > 5000) {
pred.actual = this.mesh.position.clone();
}
});
}
getRoleBonus() {
switch (this.role) {
case 'leader': return this.leadershipScore * 0.5;
case 'explorer': return this.explorationBonus * 0.3;
case 'follower': return this.cooperationScore * 0.2;
case 'scout': return this.distanceTraveled * 0.1;
default: return 0;
}
}
getEfficiencyBonus() {
// Reward efficient decision making
return this.decisionQuality * 0.1 + this.predictiveAccuracy * 50;
}
trackExploration() {
const area = `${Math.floor(this.mesh.position.x / 25)},${Math.floor(this.mesh.position.z / 25)}`;
if (!this.visitedAreas.has(area)) {
this.visitedAreas.add(area);
this.explorationBonus += 10;
}
}
updateVisuals() {
// Update car color based on role and performance
this.updateCarColor();
this.updateFlockVisualization();
// Brain indicator pulsing based on activity
const brainActivity = this.brain.memory.reduce((sum, val) => sum + Math.abs(val), 0);
this.brainIndicator.material.opacity = 0.5 + (brainActivity * 0.1);
}
updateCarColor() {
const hue = this.speciesId * 0.2;
let saturation = 0.8;
let lightness = 0.6;
// Role-based color modifications
switch (this.role) {
case 'leader':
saturation = 1.0;
lightness = 0.7;
break;
case 'explorer':
saturation = 0.9;
lightness = 0.8;
break;
case 'follower':
saturation = 0.7;
lightness = 0.5;
break;
}
// Performance-based brightness
const performanceBonus = Math.min(this.rawFitness / 1000, 0.3);
lightness += performanceBonus;
this.bodyMaterial.color.setHSL(hue, saturation, lightness);
}
updateFlockVisualization() {
if (!showFlockLines) return;
const nearestNeighbors = this.neighbors
.sort((a, b) => {
const distA = this.mesh.position.distanceTo(a.mesh.position);
const distB = this.mesh.position.distanceTo(b.mesh.position);
return distA - distB;
})
.slice(0, 8);
for (let i = 0; i < this.flockLines.length; i++) {
if (i < nearestNeighbors.length) {
const start = this.mesh.position.clone();
start.y = 2;
const end = nearestNeighbors[i].mesh.position.clone();
end.y = 2;
this.flockLines[i].geometry.setFromPoints([start, end]);
this.flockLines[i].visible = true;
// Color based on relationship
if (nearestNeighbors[i].role === 'leader') {
this.flockLines[i].material.color.setHex(0xff00ff);
} else {
this.flockLines[i].material.color.setHex(0x00ff00);
}
} else {
this.flockLines[i].visible = false;
}
}
}
getObstacles() {
let obstacles = [];
population.forEach(car => {
if (car !== this && !car.crashed) {
obstacles.push(car.mesh);
}
});
world.buildings.forEach(building => {
obstacles.push(building.mesh);
});
world.dynamicObstacles.forEach(obstacle => {
obstacles.push(obstacle.mesh);
});
return obstacles;
}
checkCollisions() {
const carBox = new THREE.Box3().setFromObject(this.mesh);
// Enhanced collision detection
population.forEach(otherCar => {
if (otherCar !== this && !otherCar.crashed) {
const otherBox = new THREE.Box3().setFromObject(otherCar.mesh);
if (carBox.intersectsBox(otherBox)) {
// Soft collision - reduce speed instead of crash
const collisionForce = new THREE.Vector3()
.subVectors(this.mesh.position, otherCar.mesh.position)
.normalize()
.multiplyScalar(5);
this.velocity.add(collisionForce);
otherCar.velocity.sub(collisionForce);
// Small fitness penalty
this.rawFitness -= 10;
otherCar.rawFitness -= 10;
}
}
});
// Building collisions
world.buildings.forEach(building => {
const buildingBox = new THREE.Box3().setFromObject(building.mesh);
if (carBox.intersectsBox(buildingBox)) {
this.crashed = true;
crashCount++;
}
});
}
keepInBounds() {
const bounds = 400;
if (Math.abs(this.mesh.position.x) > bounds ||
Math.abs(this.mesh.position.z) > bounds) {
if (Math.abs(this.mesh.position.x) > bounds) {
this.mesh.position.x = Math.sign(this.mesh.position.x) * bounds;
this.velocity.x *= -0.7;
}
if (Math.abs(this.mesh.position.z) > bounds) {
this.mesh.position.z = Math.sign(this.mesh.position.z) * bounds;
this.velocity.z *= -0.7;
}
this.rawFitness -= 5; // Boundary penalty
}
}
destroy() {
this.flockLines.forEach(line => {
if (line.parent) scene.remove(line);
});
if (this.mesh.parent) {
scene.remove(this.mesh);
}
}
}
// Enhanced speciation system
function calculateCompatibility(brain1, brain2) {
let weightDiff = 0;
let totalWeights = 0;
// Compare all weight matrices
for (let layer = 0; layer < brain1.weights.length; layer++) {
for (let i = 0; i < brain1.weights[layer].length; i++) {
for (let j = 0; j < brain1.weights[layer][i].length; j++) {
weightDiff += Math.abs(brain1.weights[layer][i][j] - brain2.weights[layer][i][j]);
totalWeights++;
}
}
}
// Compare personality traits
let traitDiff = 0;
Object.keys(brain1.personalityTraits).forEach(trait => {
traitDiff += Math.abs(brain1.personalityTraits[trait] - brain2.personalityTraits[trait]);
});
return (weightDiff / totalWeights) + (traitDiff / 5);
}
function speciate() {
species = [];
population.forEach(individual => {
let foundSpecies = false;
for (let s of species) {
if (s.members.length > 0) {
const representative = s.members[0];
const compatibility = calculateCompatibility(individual.brain, representative.brain);
if (compatibility < SPECIES_THRESHOLD) {
s.members.push(individual);
individual.speciesId = s.id;
foundSpecies = true;
break;
}
}
}
if (!foundSpecies) {
const newSpecies = {
id: species.length,
members: [individual],
avgFitness: 0,
staleness: 0,
bestFitness: 0
};
species.push(newSpecies);
individual.speciesId = newSpecies.id;
}
});
// Calculate species fitness
species.forEach(s => {
if (s.members.length > 0) {
s.avgFitness = s.members.reduce((sum, ind) => sum + ind.rawFitness, 0) / s.members.length;
s.bestFitness = Math.max(...s.members.map(ind => ind.rawFitness));
// Adjust individual fitness by species size (fitness sharing)
s.members.forEach(ind => {
ind.adjustedFitness = ind.rawFitness / s.members.length;
});
}
});
// Remove empty species
species = species.filter(s => s.members.length > 0);
}
function init() {
// Enhanced scene setup
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB);
scene.fog = new THREE.Fog(0x87CEEB, 400, 1200);
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.set(0, 120, 120);
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x001122);
document.body.appendChild(renderer.domElement);
// Enhanced lighting
const ambientLight = new THREE.AmbientLight(0x404040, 0.6);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(100, 100, 50);
directionalLight.castShadow = true;
directionalLight.shadow.mapSize.width = 2048;
directionalLight.shadow.mapSize.height = 2048;
scene.add(directionalLight);
// Create enhanced world
createEnhancedWorld();
createInitialPopulation();
clock = new THREE.Clock();
// Event listeners
window.addEventListener('resize', onWindowResize);
setupEventListeners();
animate();
}
function createEnhancedWorld() {
// Enhanced ground with texture variation
const groundGeometry = new THREE.PlaneGeometry(1000, 1000);
const groundMaterial = new THREE.MeshLambertMaterial({
color: 0x228B22,
transparent: true,
opacity: 0.9
});
const ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
createRoadNetwork();
createObstacles();
createDynamicEnvironment();
}
function createRoadNetwork() {
const roadMaterial = new THREE.MeshLambertMaterial({ color: 0x444444 });
for (let i = -300; i <= 300; i += 150) {
// Horizontal roads
const hRoadGeometry = new THREE.PlaneGeometry(600, 12);
const hRoad = new THREE.Mesh(hRoadGeometry, roadMaterial);
hRoad.rotation.x = -Math.PI / 2;
hRoad.position.set(0, 0.1, i);
scene.add(hRoad);
// Vertical roads
const vRoadGeometry = new THREE.PlaneGeometry(12, 600);
const vRoad = new THREE.Mesh(vRoadGeometry, roadMaterial);
vRoad.rotation.x = -Math.PI / 2;
vRoad.position.set(i, 0.1, 0);
scene.add(vRoad);
}
}
function createObstacles() {
world.buildings = [];
const buildingMaterial = new THREE.MeshLambertMaterial({ color: 0x666666 });
for (let i = 0; i < 20; i++) {
const x = (Math.random() - 0.5) * 700;
const z = (Math.random() - 0.5) * 700;
const width = 12 + Math.random() * 25;
const height = 8 + Math.random() * 35;
const depth = 12 + Math.random() * 25;
const buildingGeometry = new THREE.BoxGeometry(width, height, depth);
const building = new THREE.Mesh(buildingGeometry, buildingMaterial);
building.position.set(x, height / 2, z);
building.castShadow = true;
scene.add(building);
world.buildings.push({ mesh: building });
}
}
function createDynamicEnvironment() {
// Create exploration targets
world.targets = [];
for (let i = 0; i < 8; i++) {
const target = {
position: new THREE.Vector3(
(Math.random() - 0.5) * 600,
5,
(Math.random() - 0.5) * 600
),
discovered: false
};
// Visual target
const targetGeometry = new THREE.SphereGeometry(3, 8, 6);
const targetMaterial = new THREE.MeshLambertMaterial({
color: 0x00ff00,
transparent: true,
opacity: 0.7
});
target.mesh = new THREE.Mesh(targetGeometry, targetMaterial);
target.mesh.position.copy(target.position);
scene.add(target.mesh);
world.targets.push(target);
}
}
function createInitialPopulation() {
population = [];
for (let i = 0; i < populationSize; i++) {
const angle = (i / populationSize) * Math.PI * 2;
const radius = 40 + Math.random() * 60;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const car = new EnhancedAICar(x, z);
population.push(car);
scene.add(car.mesh);
}
speciate();
}
function evolvePopulation() {
speciate();
// Advanced evolution with speciation
const totalAdjustedFitness = population.reduce((sum, ind) => sum + ind.adjustedFitness, 0);
const newPopulation = [];
// Determine offspring allocation per species
species.forEach(s => {
if (s.members.length === 0) return;
const speciesFitness = s.members.reduce((sum, ind) => sum + ind.adjustedFitness, 0);
const offspringCount = Math.floor((speciesFitness / totalAdjustedFitness) * populationSize);
// Sort species members by fitness
s.members.sort((a, b) => b.adjustedFitness - a.adjustedFitness);
// Elite selection
const eliteCount = Math.max(1, Math.floor(offspringCount * 0.2));
for (let i = 0; i < eliteCount && i < s.members.length; i++) {
const elite = s.members[i];
const angle = Math.random() * Math.PI * 2;
const radius = 40 + Math.random() * 60;
const newCar = new EnhancedAICar(
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
newCar.brain = elite.brain.copy();
newCar.speciesId = s.id;
newPopulation.push(newCar);
}
// Crossover and mutation
while (newPopulation.filter(car => car.speciesId === s.id).length < offspringCount) {
const parent1 = tournamentSelection(s.members);
const parent2 = tournamentSelection(s.members);
const angle = Math.random() * Math.PI * 2;
const radius = 40 + Math.random() * 60;
const child = new EnhancedAICar(
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
if (Math.random() < 0.7) {
child.brain = parent1.brain.crossover(parent2.brain);
} else {
child.brain = parent1.brain.copy();
}
// Adaptive mutation
const mutationRate = 0.05 + (s.staleness * 0.01);
child.brain.mutate(mutationRate, Math.random() < 0.1);
child.speciesId = s.id;
newPopulation.push(child);
}
});
// Fill any remaining slots
while (newPopulation.length < populationSize) {
const randomSpecies = species[Math.floor(Math.random() * species.length)];
if (randomSpecies.members.length > 0) {
const parent = randomSpecies.members[0];
const angle = Math.random() * Math.PI * 2;
const radius = 40 + Math.random() * 60;
const child = new EnhancedAICar(
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
child.brain = parent.brain.copy();
child.brain.mutate(0.3, true); // High mutation for diversity
child.speciesId = parent.speciesId;
newPopulation.push(child);
}
}
// Clean up old population
population.forEach(car => car.destroy());
// Replace population
population = newPopulation;
population.forEach(car => scene.add(car.mesh));
// Update epoch
epoch++;
timeLeft = epochTime;
bestFitness = Math.max(bestFitness, ...population.map(car => car.rawFitness));
crashCount = 0;
console.log(`Epoch ${epoch}: ${species.length} species, best fitness: ${bestFitness.toFixed(1)}`);
}
function tournamentSelection(individuals, tournamentSize = 3) {
let best = null;
let bestFitness = -1;
for (let i = 0; i < tournamentSize; i++) {
const candidate = individuals[Math.floor(Math.random() * individuals.length)];
if (candidate.adjustedFitness > bestFitness) {
best = candidate;
bestFitness = candidate.adjustedFitness;
}
}
return best;
}
function animate() {
requestAnimationFrame(animate);
if (!paused) {
const deltaTime = Math.min(clock.getDelta() * speedMultiplier, 0.1);
// Update timer
timeLeft -= deltaTime;
if (timeLeft <= 0) {
evolvePopulation();
}
// Update population
updatePopulation(deltaTime);
updateCamera();
updateUI();
updateDynamicEnvironment(deltaTime);
}
renderer.render(scene, camera);
}
function updatePopulation(deltaTime) {
let stats = {
alive: 0,
leaders: 0,
followers: 0,
explorers: 0,
scouts: 0,
totalVelocity: 0,
totalCooperation: 0,
totalExploration: 0,
totalDecisionQuality: 0,
totalNeuralComplexity: 0,
maxGroupSize: 0
};
population.forEach(car => {
car.update(deltaTime);
if (!car.crashed) {
stats.alive++;
stats.totalVelocity += car.velocity.length();
stats.totalDecisionQuality += car.decisionQuality;
stats.totalNeuralComplexity += car.brain.getComplexity();
stats.maxGroupSize = Math.max(stats.maxGroupSize, car.neighbors.length + 1);
switch (car.role) {
case 'leader': stats.leaders++; break;
case 'follower': stats.followers++; break;
case 'explorer': stats.explorers++; break;
case 'scout': stats.scouts++; break;
}
stats.totalCooperation += car.cooperationScore;
stats.totalExploration += car.explorationBonus;
}
});
// Store stats for UI
window.populationStats = stats;
}
function updateCamera() {
if (cameraMode === 'follow') {
// Follow the best performing car or largest flock
let target = population.reduce((best, car) => {
if (car.crashed) return best;
return !best || car.rawFitness > best.rawFitness ? car : best;
}, null);
if (target) {
const targetPos = target.mesh.position.clone();
targetPos.y += 50;
targetPos.add(target.velocity.clone().normalize().multiplyScalar(30));
camera.position.lerp(targetPos, 0.02);
camera.lookAt(target.mesh.position);
}
} else {
camera.position.lerp(new THREE.Vector3(0, 200, 200), 0.02);
camera.lookAt(0, 0, 0);
}
}
function updateUI() {
const stats = window.populationStats || {};
// Main UI
document.getElementById('epoch').textContent = epoch;
document.getElementById('epochTime').textContent = Math.ceil(timeLeft);
document.getElementById('population').textContent = stats.alive || 0;
document.getElementById('speciesCount').textContent = species.length;
document.getElementById('bestFitness').textContent = Math.round(bestFitness);
document.getElementById('innovationCount').textContent = innovationCounter;
// Progress bar
const progress = ((epochTime - timeLeft) / epochTime) * 100;
document.getElementById('timeProgress').style.width = `${progress}%`;
// AI Stats
if (stats.alive > 0) {
document.getElementById('avgIQ').textContent = Math.round(stats.totalDecisionQuality / stats.alive);
document.getElementById('neuralComplexity').textContent = Math.round(stats.totalNeuralComplexity / stats.alive);
document.getElementById('decisionQuality').textContent = Math.round(stats.totalDecisionQuality / stats.alive);
document.getElementById('avgCoordination').textContent = Math.round((stats.totalCooperation / stats.alive) * 10);
}
// Flocking stats
document.getElementById('leaderCount').textContent = stats.leaders || 0;
document.getElementById('followerCount').textContent = stats.followers || 0;
document.getElementById('explorerCount').textContent = stats.explorers || 0;
document.getElementById('soloCount').textContent = stats.scouts || 0;
document.getElementById('largestFlock').textContent = stats.maxGroupSize || 0;
// Generation stats
const totalDistance = population.reduce((sum, car) => sum + car.distanceTraveled, 0);
const totalExploration = population.reduce((sum, car) => sum + car.explorationBonus, 0);
const totalCooperation = population.reduce((sum, car) => sum + car.cooperationScore, 0);
document.getElementById('totalDistance').textContent = Math.round(totalDistance);
document.getElementById('explorationBonus').textContent = Math.round(totalExploration);
document.getElementById('cooperationScore').textContent = Math.round(totalCooperation);
document.getElementById('crashCount').textContent = crashCount;
// Top performers
updateTopPerformers();
}
function updateTopPerformers() {
const sorted = [...population]
.filter(car => !car.crashed)
.sort((a, b) => b.rawFitness - a.rawFitness)
.slice(0, 5);
const topPerformersDiv = document.getElementById('topPerformers');
topPerformersDiv.innerHTML = '';
sorted.forEach((car, i) => {
const div = document.createElement('div');
const roleIcon = {
leader: 'π',
explorer: 'π',
follower: 'π€',
scout: 'ποΈ'
}[car.role] || 'π';
div.innerHTML = `${i + 1}. ${roleIcon} S${car.speciesId} | IQ:${Math.round(car.decisionQuality)} | F:${Math.round(car.rawFitness)}`;
div.className = `species-${car.speciesId % 5}`;
topPerformersDiv.appendChild(div);
});
}
function updateDynamicEnvironment(deltaTime) {
// Update targets
world.targets.forEach(target => {
// Pulsing animation
target.mesh.scale.setScalar(1 + Math.sin(Date.now() * 0.005) * 0.1);
// Check if discovered
population.forEach(car => {
if (!car.crashed && target.mesh.position.distanceTo(car.mesh.position) < 10) {
if (!target.discovered) {
target.discovered = true;
car.explorationBonus += 50;
target.mesh.material.color.setHex(0xffff00);
}
}
});
});
}
function setupEventListeners() {
document.getElementById('pauseBtn').addEventListener('click', togglePause);
document.getElementById('resetBtn').addEventListener('click', resetSimulation);
document.getElementById('speedBtn').addEventListener('click', toggleSpeed);
document.getElementById('viewBtn').addEventListener('click', toggleView);
document.getElementById('flockBtn').addEventListener('click', toggleFlockLines);
document.getElementById('adaptiveBtn').addEventListener('click', toggleAdaptive);
document.getElementById('challengeBtn').addEventListener('click', toggleChallenge);
}
function togglePause() {
paused = !paused;
document.getElementById('pauseBtn').textContent = paused ? 'Resume' : 'Pause';
if (!paused) clock.start();
}
function resetSimulation() {
epoch = 1;
timeLeft = epochTime;
bestFitness = 0;
crashCount = 0;
innovationCounter = 0;
population.forEach(car => car.destroy());
createInitialPopulation();
}
function toggleSpeed() {
speedMultiplier = speedMultiplier === 1 ? 2 : speedMultiplier === 2 ? 5 : 1;
document.getElementById('speedBtn').textContent = `Speed: ${speedMultiplier}x`;
}
function toggleView() {
cameraMode = cameraMode === 'follow' ? 'overview' : 'follow';
document.getElementById('viewBtn').textContent = `View: ${cameraMode === 'follow' ? 'Follow' : 'Overview'}`;
}
function toggleFlockLines() {
showFlockLines = !showFlockLines;
document.getElementById('flockBtn').textContent = `Flocks: ${showFlockLines ? 'ON' : 'OFF'}`;
}
function toggleAdaptive() {
adaptiveEnvironment = !adaptiveEnvironment;
document.getElementById('adaptiveBtn').textContent = `Adaptive: ${adaptiveEnvironment ? 'ON' : 'OFF'}`;
}
function toggleChallenge() {
const levels = ['normal', 'hard', 'extreme'];
const currentIndex = levels.indexOf(challengeLevel);
challengeLevel = levels[(currentIndex + 1) % levels.length];
document.getElementById('challengeBtn').textContent = `Challenge: ${challengeLevel}`;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
init();
</script>
</body>
</html> |