You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CozmoAgent.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MLAgents;
  2. using OpenCvSharp;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. public class CozmoAgent : Agent
  8. {
  9. // Possible Actions
  10. private const int STOP = 0;
  11. private const int FORWARD = 1;
  12. private const int RIGHT = 2;
  13. private const int LEFT = 3;
  14. // Used to determine different areas in the image (near to the center, far away)
  15. private const float NEAR_AREA_PERCENTAGE_OFFSET = 0.2f;
  16. private const float FAR_AREA_PERCENTAGE_OFFSET = 0.3f;
  17. [Tooltip("The virtual Cozmo camera")]
  18. public Camera renderCamera;
  19. [Tooltip("Reference to the CozmoMovement script")]
  20. public CozmoMovement movement;
  21. public float timeBetweenDecisionsAtInference;
  22. private Academy academy; // CozmoAcademy
  23. private float timeSinceDecision; // time since last decision
  24. private ImageProcessor onRenderImageTest; // reference to the ImageProcessor
  25. private int nearAreaLimit = 0; // X coordinate limit for the near to the imagecenter area
  26. private int farAreaLimit = 0; // X coordinate limit for the far away to the imagecenter area
  27. // for testing
  28. //private float[] floats = { 1.0f, 2.0f, 3.0f };
  29. private void Start()
  30. {
  31. academy = FindObjectOfType(typeof(CozmoAcademy)) as CozmoAcademy;
  32. onRenderImageTest = renderCamera.GetComponent<ImageProcessor>();
  33. nearAreaLimit = (int)(renderCamera.targetTexture.width / 2 * NEAR_AREA_PERCENTAGE_OFFSET);
  34. farAreaLimit = (int)(renderCamera.targetTexture.width / 2 * FAR_AREA_PERCENTAGE_OFFSET);
  35. }
  36. public void FixedUpdate()
  37. {
  38. WaitTimeInference();
  39. // for testing
  40. //AgentAction(floats, "ActionText");
  41. }
  42. // to be implemented by the developer
  43. public override void AgentAction(float[] vectorAction, string textAction)
  44. {
  45. //print("Action before FloorToInt: " + vectorAction[0]);
  46. int action = Mathf.FloorToInt(vectorAction[0]);
  47. Point centerOfGravity = onRenderImageTest.CenterOfGravity;
  48. //Vector3 targetPos = transform.position;
  49. //print("Action after FloorToInt: " + action);
  50. AddReward(-0.01f);
  51. switch (action)
  52. {
  53. case STOP:
  54. movement.Move(0);
  55. break;
  56. case FORWARD:
  57. movement.Move(1);
  58. break;
  59. case RIGHT:
  60. movement.Turn(1);
  61. break;
  62. case LEFT:
  63. movement.Turn(-1);
  64. break;
  65. default:
  66. //movement.Move(0);
  67. throw new ArgumentException("Invalid action value. Stop movement.");
  68. }
  69. // Render new image after movement in order to update the centerOfGravity
  70. if (renderCamera != null)
  71. {
  72. renderCamera.Render();
  73. }
  74. // If centerOfGravity lies near to the center of the image horizontally
  75. if (centerOfGravity.X > renderCamera.targetTexture.width / 2 - nearAreaLimit && centerOfGravity.X < renderCamera.targetTexture.width / 2 + nearAreaLimit)
  76. {
  77. SetReward(1);
  78. print("Reward: +1");
  79. }
  80. else if (centerOfGravity.X > renderCamera.targetTexture.width / 2 - farAreaLimit && centerOfGravity.X < renderCamera.targetTexture.width / 2 + farAreaLimit)
  81. {
  82. SetReward(-0.3f);
  83. print("Reward: -0.3");
  84. }
  85. else
  86. {
  87. SetReward(-1);
  88. print("Reward: -1");
  89. }
  90. }
  91. // to be implemented by the developer
  92. public override void AgentReset()
  93. {
  94. academy.AcademyReset();
  95. }
  96. private void OnTriggerEnter(Collider other)
  97. {
  98. if (other.transform.CompareTag("Goal"))
  99. {
  100. print("Collission");
  101. Done();
  102. }
  103. }
  104. private void WaitTimeInference()
  105. {
  106. if (renderCamera != null)
  107. {
  108. renderCamera.Render();
  109. }
  110. if (!academy.GetIsInference())
  111. {
  112. RequestDecision();
  113. }
  114. else
  115. {
  116. if (timeSinceDecision >= timeBetweenDecisionsAtInference)
  117. {
  118. timeSinceDecision = 0f;
  119. RequestDecision();
  120. }
  121. else
  122. {
  123. timeSinceDecision += Time.fixedDeltaTime;
  124. }
  125. }
  126. }
  127. }