123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- 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<ImageProcessor>();
- 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.7f);
- print("Reward: -0.7");
- }
- 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;
- }
- }
- }
-
- }
|