2019-04-10 13:39:36 +02:00
|
|
|
|
using MLAgents;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
using OpenCvSharp;
|
|
|
|
|
using System;
|
2019-04-10 13:39:36 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class CozmoAgent : Agent
|
|
|
|
|
{
|
2019-05-17 18:23:24 +02:00
|
|
|
|
// Possible Actions
|
|
|
|
|
private const int STOP = 0;
|
|
|
|
|
private const int FORWARD = 1;
|
|
|
|
|
private const int RIGHT = 2;
|
|
|
|
|
private const int LEFT = 3;
|
2019-04-10 13:39:36 +02:00
|
|
|
|
|
2019-05-17 18:23:24 +02:00
|
|
|
|
// Used to determine different areas in the image (near to the center, far away)
|
2019-05-23 21:04:03 +02:00
|
|
|
|
private const float NEAR_AREA_PERCENTAGE_OFFSET = 0.3f;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
|
|
|
|
[Tooltip("The virtual Cozmo camera")]
|
2019-05-15 12:07:27 +02:00
|
|
|
|
public Camera renderCamera;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
[Tooltip("Reference to the CozmoMovement script")]
|
2019-05-23 17:28:37 +02:00
|
|
|
|
public CozmoMovementController movementController;
|
2019-05-15 12:07:27 +02:00
|
|
|
|
public float timeBetweenDecisionsAtInference;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
|
|
|
|
private Academy academy; // CozmoAcademy
|
|
|
|
|
private float timeSinceDecision; // time since last decision
|
2019-05-23 17:28:37 +02:00
|
|
|
|
private ImageProcessor imageProcessor; // reference to the ImageProcessor
|
2019-05-17 18:23:24 +02:00
|
|
|
|
private int nearAreaLimit = 0; // X coordinate limit for the near to the imagecenter area
|
2019-05-23 21:04:03 +02:00
|
|
|
|
private int centerOfImageX = 0; // Middle of the image in x direction
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
academy = FindObjectOfType(typeof(CozmoAcademy)) as CozmoAcademy;
|
2019-05-23 17:28:37 +02:00
|
|
|
|
imageProcessor = renderCamera.GetComponent<ImageProcessor>();
|
2019-05-17 18:23:24 +02:00
|
|
|
|
nearAreaLimit = (int)(renderCamera.targetTexture.width / 2 * NEAR_AREA_PERCENTAGE_OFFSET);
|
2019-05-23 21:04:03 +02:00
|
|
|
|
centerOfImageX = renderCamera.targetTexture.width / 2;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
}
|
2019-05-15 12:07:27 +02:00
|
|
|
|
|
2019-05-23 17:28:37 +02:00
|
|
|
|
|
2019-05-15 12:07:27 +02:00
|
|
|
|
public void FixedUpdate()
|
|
|
|
|
{
|
|
|
|
|
WaitTimeInference();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
|
|
|
|
// to be implemented by the developer
|
|
|
|
|
public override void AgentAction(float[] vectorAction, string textAction)
|
|
|
|
|
{
|
|
|
|
|
int action = Mathf.FloorToInt(vectorAction[0]);
|
2019-05-23 17:28:37 +02:00
|
|
|
|
Point centerOfGravity = imageProcessor.CenterOfGravity;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
|
|
|
|
AddReward(-0.01f);
|
|
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
|
{
|
|
|
|
|
case STOP:
|
2019-05-23 17:28:37 +02:00
|
|
|
|
movementController.currentMovementState = MovementState.Stop;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
break;
|
|
|
|
|
case FORWARD:
|
2019-05-23 17:28:37 +02:00
|
|
|
|
movementController.currentMovementState = MovementState.Forward;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
break;
|
|
|
|
|
case RIGHT:
|
2019-05-23 17:28:37 +02:00
|
|
|
|
movementController.currentMovementState = MovementState.Right;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
break;
|
|
|
|
|
case LEFT:
|
2019-05-23 17:28:37 +02:00
|
|
|
|
movementController.currentMovementState = MovementState.Left;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2019-05-19 21:18:13 +02:00
|
|
|
|
//movement.Move(0);
|
2019-05-17 18:23:24 +02:00
|
|
|
|
throw new ArgumentException("Invalid action value. Stop movement.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Render new image after movement in order to update the centerOfGravity
|
|
|
|
|
if (renderCamera != null)
|
|
|
|
|
{
|
|
|
|
|
renderCamera.Render();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 17:28:37 +02:00
|
|
|
|
RewardAgent();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 21:04:03 +02:00
|
|
|
|
// Set the reward for the agent based on how far away the center of gravity is from the center of the image
|
2019-05-23 17:28:37 +02:00
|
|
|
|
private void RewardAgent()
|
|
|
|
|
{
|
|
|
|
|
float centerOfGravityX = imageProcessor.CenterOfGravity.X;
|
|
|
|
|
float reward = 0;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
2019-05-23 21:04:03 +02:00
|
|
|
|
// Center of gravity is far away from the center (left)
|
|
|
|
|
if (centerOfGravityX <= centerOfImageX - nearAreaLimit)
|
2019-05-17 18:23:24 +02:00
|
|
|
|
{
|
2019-05-23 21:04:03 +02:00
|
|
|
|
float range = centerOfImageX - nearAreaLimit;
|
|
|
|
|
reward = -(1 - (centerOfGravityX / range));
|
|
|
|
|
// Clamp the reward to max -1 in order to handle rewards if the center of gravity is outside of the image
|
|
|
|
|
reward = Mathf.Clamp(reward, -1, 0);
|
2019-05-17 18:23:24 +02:00
|
|
|
|
}
|
2019-05-23 17:28:37 +02:00
|
|
|
|
// Center of gravity is near left of the center
|
|
|
|
|
else if (centerOfGravityX <= centerOfImageX)
|
|
|
|
|
{
|
|
|
|
|
float range = centerOfImageX - (centerOfImageX - nearAreaLimit);
|
|
|
|
|
float distanceToLeftFarBorder = centerOfGravityX - (centerOfImageX - nearAreaLimit);
|
|
|
|
|
reward = (distanceToLeftFarBorder / range);
|
|
|
|
|
}
|
2019-05-23 21:04:03 +02:00
|
|
|
|
// Center of gravity is far away from the center (right)
|
2019-05-23 17:28:37 +02:00
|
|
|
|
else if (centerOfGravityX >= centerOfImageX + nearAreaLimit)
|
|
|
|
|
{
|
2019-05-23 21:04:03 +02:00
|
|
|
|
float range = renderCamera.targetTexture.width - (centerOfImageX + nearAreaLimit);
|
|
|
|
|
reward = -(((centerOfGravityX - (centerOfImageX + nearAreaLimit)) / range));
|
|
|
|
|
// Clamp the reward to max -1 in order to handle rewards if the center of gravity is outside of the image
|
|
|
|
|
reward = Mathf.Clamp(reward, -1, 0);
|
2019-05-23 17:28:37 +02:00
|
|
|
|
}
|
|
|
|
|
// Center of gravity is near right of the center
|
|
|
|
|
else if (centerOfGravityX >= centerOfImageX)
|
2019-05-17 18:23:24 +02:00
|
|
|
|
{
|
2019-05-23 17:28:37 +02:00
|
|
|
|
float range = (centerOfImageX + nearAreaLimit) - centerOfImageX;
|
2019-05-23 21:04:03 +02:00
|
|
|
|
float distanceToCenterOfImage = centerOfGravityX - centerOfImageX;
|
|
|
|
|
reward = (1 - distanceToCenterOfImage / range);
|
2019-05-17 18:23:24 +02:00
|
|
|
|
}
|
2019-05-23 15:28:26 +02:00
|
|
|
|
|
2019-05-23 17:28:37 +02:00
|
|
|
|
SetReward(reward);
|
2019-05-17 18:23:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// to be implemented by the developer
|
|
|
|
|
public override void AgentReset()
|
|
|
|
|
{
|
|
|
|
|
academy.AcademyReset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-05-17 19:12:07 +02:00
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
|
{
|
|
|
|
|
if (other.transform.CompareTag("Goal"))
|
|
|
|
|
{
|
|
|
|
|
Done();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 12:07:27 +02:00
|
|
|
|
private void WaitTimeInference()
|
|
|
|
|
{
|
|
|
|
|
if (renderCamera != null)
|
|
|
|
|
{
|
|
|
|
|
renderCamera.Render();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!academy.GetIsInference())
|
|
|
|
|
{
|
|
|
|
|
RequestDecision();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (timeSinceDecision >= timeBetweenDecisionsAtInference)
|
|
|
|
|
{
|
|
|
|
|
timeSinceDecision = 0f;
|
|
|
|
|
RequestDecision();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
timeSinceDecision += Time.fixedDeltaTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-10 13:39:36 +02:00
|
|
|
|
}
|