Advertisement
JontePonte

bad pong

Apr 27th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. using UnityEngine;
  2. using GeneticAlgorithm;
  3. using NeuralNetwork;
  4.  
  5. public class AgentManager : MonoBehaviour
  6. {
  7. public int numAgents;
  8. public float pongInstanceSize;
  9. public float gameLengthSeconds;
  10. public GameObject pongPrefab;
  11.  
  12. [Range(0, 1)]
  13. public float mutationChance;
  14. public int mutationStrength;
  15.  
  16. public float timeScale;
  17.  
  18. public int currentGeneration;
  19.  
  20. GeneticAlgorithm.GeneticAlgorithm geneticAlgorithm;
  21.  
  22. PongAgent[] pongAgents;
  23. Agent[] geneticAgents;
  24.  
  25. float timer;
  26.  
  27. void Start()
  28. {
  29. pongAgents = new PongAgent[numAgents];
  30.  
  31. for (int i = 0; i < pongAgents.Length; i++)
  32. {
  33. pongAgents[i] = new PongAgent();
  34.  
  35. GameObject pongObj = Instantiate(pongPrefab);
  36. pongObj.transform.parent = transform;
  37. pongPrefab.transform.position = Vector2.right * (i + 1) * pongInstanceSize;
  38.  
  39. pongAgents[i].pongGame = pongObj.GetComponent<PongGame>();
  40. }
  41.  
  42. geneticAlgorithm = new GeneticAlgorithm.GeneticAlgorithm();
  43. geneticAlgorithm.mutationChance = mutationChance;
  44. geneticAlgorithm.mutationStrength = mutationStrength;
  45.  
  46. geneticAgents = new Agent[numAgents];
  47. geneticAlgorithm.SetAgents(geneticAgents);
  48.  
  49. for (int i = 0; i < geneticAgents.Length; i++)
  50. {
  51. geneticAgents[i] = new Agent();
  52. }
  53.  
  54. }
  55.  
  56. void Update()
  57. {
  58. Time.timeScale = timeScale;
  59.  
  60. timer += Time.deltaTime;
  61.  
  62. if (timer > gameLengthSeconds)
  63. {
  64. int scoreSum = 0;
  65.  
  66. // Update genetic agents
  67. for (int i = 0; i < pongAgents.Length; i++)
  68. {
  69. string geneticCode = NeuralNetworkSerializer.SerializeToBitString(pongAgents[i].neuralNetwork);
  70.  
  71. geneticAgents[i].geneticCode = geneticCode;
  72. geneticAgents[i].score = pongAgents[i].pongGame.score.score;
  73. scoreSum += pongAgents[i].pongGame.score.score;
  74. pongAgents[i].pongGame.score.ResetScore();
  75. }
  76.  
  77. int averageScore = scoreSum / pongAgents.Length;
  78.  
  79. geneticAlgorithm.NewGeneration();
  80. Debug.Log("New Generation! Average score: " + averageScore);
  81. currentGeneration++;
  82.  
  83. // Update pong agents' neural networks
  84. for (int i = 0; i < pongAgents.Length; i++)
  85. {
  86. NeuralNetworkSerializer.DeserializeFromBitString(geneticAgents[i].geneticCode, pongAgents[i].neuralNetwork);
  87. }
  88.  
  89. timer = 0;
  90. }
  91.  
  92. foreach (var agent in pongAgents)
  93. {
  94. agent.Evaluate(Time.deltaTime);
  95. }
  96.  
  97. }
  98. }
  99.  
  100. public class PongAgent
  101. {
  102. public PongGame pongGame;
  103. public NeuralNetwork.NeuralNetwork neuralNetwork;
  104.  
  105. public PongAgent()
  106. {
  107. NeuralNetworkParams networkParams = new NeuralNetworkParams
  108. {
  109. numInputs = 3,
  110. hiddenLayersNodes = new int[] { 5, 5 },
  111. outputLayerNodes = 2
  112. };
  113.  
  114. neuralNetwork = new NeuralNetwork.NeuralNetwork(networkParams);
  115. neuralNetwork.SetRandomParameters();
  116. }
  117.  
  118. // Should be called every frame
  119. public void Evaluate(float deltaTime)
  120. {
  121. // Move paddle based on balls and paddles position using network
  122.  
  123. Vector2 ballPosition = pongGame.ball.transform.localPosition;
  124. Vector2 ballPositionNormalized = new Vector2(ballPosition.x / 6.5f, ballPosition.y / 4.9f);
  125. float paddleHeightNormalized = pongGame.paddle.transform.position.y / 4.3f;
  126.  
  127. neuralNetwork.SetInputs(new float[] { ballPositionNormalized.x, ballPositionNormalized.y, paddleHeightNormalized });
  128. float[] output = neuralNetwork.Evaluate();
  129.  
  130. if (output[0] > output[1])
  131. {
  132. pongGame.paddle.MoveUp(deltaTime);
  133. }
  134. else
  135. {
  136. pongGame.paddle.MoveDown(deltaTime);
  137. }
  138. }
  139. }
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement