using MLAgents; using OpenCvSharp; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class CozmoAgent : Agent { // Possible Actions private const int STOP = 0; private const int FORWARD = 1; private const int RIGHT = 2; private const int LEFT = 3; // Used to determine different areas in the image (near to the center, far away) private const float NEAR_AREA_PERCENTAGE_OFFSET = 0.2f; private const float FAR_AREA_PERCENTAGE_OFFSET = 0.3f; [Tooltip("The virtual Cozmo camera")] public Camera renderCamera; [Tooltip("Reference to the CozmoMovement script")] public CozmoMovement movement; public float timeBetweenDecisionsAtInference; private Academy academy; // CozmoAcademy private float timeSinceDecision; // time since last decision private ImageProcessor onRenderImageTest; // reference to the ImageProcessor private int nearAreaLimit = 0; // X coordinate limit for the near to the imagecenter area private int farAreaLimit = 0; // X coordinate limit for the far away to the imagecenter area // for testing //private float[] floats = { 1.0f, 2.0f, 3.0f }; private void Start() { academy = FindObjectOfType(typeof(CozmoAcademy)) as CozmoAcademy; onRenderImageTest = renderCamera.GetComponent(); nearAreaLimit = (int)(renderCamera.targetTexture.width / 2 * NEAR_AREA_PERCENTAGE_OFFSET); farAreaLimit = (int)(renderCamera.targetTexture.width / 2 * FAR_AREA_PERCENTAGE_OFFSET); } public void FixedUpdate() { WaitTimeInference(); // for testing //AgentAction(floats, "ActionText"); } // to be implemented by the developer public override void AgentAction(float[] vectorAction, string textAction) { //print("Action before FloorToInt: " + vectorAction[0]); int action = Mathf.FloorToInt(vectorAction[0]); Point centerOfGravity = onRenderImageTest.CenterOfGravity; //Vector3 targetPos = transform.position; //print("Action after FloorToInt: " + action); AddReward(-0.01f); switch (action) { case STOP: movement.Move(0); break; case FORWARD: movement.Move(1); break; case RIGHT: movement.Turn(1); break; case LEFT: movement.Turn(-1); break; default: //movement.Move(0); throw new ArgumentException("Invalid action value. Stop movement."); } // Render new image after movement in order to update the centerOfGravity if (renderCamera != null) { renderCamera.Render(); } // If centerOfGravity lies near to the center of the image horizontally if (centerOfGravity.X > renderCamera.targetTexture.width / 2 - nearAreaLimit && centerOfGravity.X < renderCamera.targetTexture.width / 2 + nearAreaLimit) { SetReward(1); print("Reward: +1"); } else if (centerOfGravity.X > renderCamera.targetTexture.width / 2 - farAreaLimit && centerOfGravity.X < renderCamera.targetTexture.width / 2 + farAreaLimit) { SetReward(-0.3f); print("Reward: -0.3"); } else { SetReward(-1); print("Reward: -1"); } } // to be implemented by the developer public override void AgentReset() { academy.AcademyReset(); } private void OnTriggerEnter(Collider other) { if (other.transform.CompareTag("Goal")) { print("Collission"); Done(); } } private void WaitTimeInference() { if (renderCamera != null) { renderCamera.Render(); } if (!academy.GetIsInference()) { RequestDecision(); } else { if (timeSinceDecision >= timeBetweenDecisionsAtInference) { timeSinceDecision = 0f; RequestDecision(); } else { timeSinceDecision += Time.fixedDeltaTime; } } } }