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)
|
|
|
|
|
private const float NEAR_AREA_PERCENTAGE_OFFSET = 0.2f;
|
|
|
|
|
private const float FAR_AREA_PERCENTAGE_OFFSET = 0.3f;
|
|
|
|
|
|
|
|
|
|
[Tooltip("The virtual Cozmo camera")]
|
2019-05-15 12:07:27 +02:00
|
|
|
|
public Camera renderCamera;
|
2019-05-23 17:28:37 +02:00
|
|
|
|
//[Tooltip("Final cropped and scaled rendertexture")]
|
|
|
|
|
//public RenderTexture renderTextureScaled;
|
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
|
|
|
|
|
private int farAreaLimit = 0; // X coordinate limit for the far away to the imagecenter area
|
|
|
|
|
|
2019-05-23 17:28:37 +02:00
|
|
|
|
|
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);
|
|
|
|
|
farAreaLimit = (int)(renderCamera.targetTexture.width / 2 * FAR_AREA_PERCENTAGE_OFFSET);
|
|
|
|
|
}
|
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)
|
|
|
|
|
{
|
2019-05-19 21:18:13 +02:00
|
|
|
|
//print("Action before FloorToInt: " + vectorAction[0]);
|
2019-05-17 18:23:24 +02:00
|
|
|
|
int action = Mathf.FloorToInt(vectorAction[0]);
|
2019-05-23 17:28:37 +02:00
|
|
|
|
Point centerOfGravity = imageProcessor.CenterOfGravity;
|
2019-05-19 21:18:13 +02:00
|
|
|
|
//Vector3 targetPos = transform.position;
|
|
|
|
|
|
|
|
|
|
//print("Action after FloorToInt: " + action);
|
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();
|
|
|
|
|
imageProcessor.enabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// TODO: Cleanup code
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RewardAgent()
|
|
|
|
|
{
|
|
|
|
|
float centerOfImageX = renderCamera.targetTexture.width / 2;
|
|
|
|
|
float centerOfGravityX = imageProcessor.CenterOfGravity.X;
|
|
|
|
|
float reward = 0;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
|
2019-05-23 17:28:37 +02:00
|
|
|
|
// Center of gravity is far left of the center
|
|
|
|
|
if (centerOfGravityX <= centerOfImageX - farAreaLimit)
|
2019-05-17 18:23:24 +02:00
|
|
|
|
{
|
2019-05-23 17:28:37 +02:00
|
|
|
|
reward = -1;
|
2019-05-17 18:23:24 +02:00
|
|
|
|
}
|
2019-05-23 17:28:37 +02:00
|
|
|
|
// Center of gravity is between far and near left of the center
|
|
|
|
|
else if (centerOfGravityX <= centerOfImageX - nearAreaLimit)
|
2019-05-17 18:23:24 +02:00
|
|
|
|
{
|
2019-05-23 17:28:37 +02:00
|
|
|
|
float range = (centerOfImageX - nearAreaLimit) - (centerOfImageX - farAreaLimit);
|
|
|
|
|
float distanceToLeftFarBorder = centerOfGravityX - (centerOfImageX - farAreaLimit);
|
|
|
|
|
reward = -(1 - (distanceToLeftFarBorder / range));
|
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);
|
|
|
|
|
}
|
|
|
|
|
// Center of gravity is far right of the center
|
|
|
|
|
else if (centerOfGravityX >= centerOfImageX + farAreaLimit)
|
|
|
|
|
{
|
|
|
|
|
reward = -1;
|
|
|
|
|
}
|
|
|
|
|
// Center of gravity is between far and near right of the center
|
|
|
|
|
else if (centerOfGravityX >= centerOfImageX + nearAreaLimit)
|
|
|
|
|
{
|
|
|
|
|
float range = (centerOfImageX + farAreaLimit) - (centerOfImageX + nearAreaLimit);
|
|
|
|
|
float distanceToLeftFarBorder = centerOfGravityX - (centerOfImageX + nearAreaLimit);
|
|
|
|
|
reward = -(distanceToLeftFarBorder / range);
|
|
|
|
|
}
|
|
|
|
|
// 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;
|
|
|
|
|
float distanceToLeftFarBorder = centerOfGravityX - centerOfImageX;
|
|
|
|
|
reward = (1 - distanceToLeftFarBorder / 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"))
|
|
|
|
|
{
|
|
|
|
|
print("Collission");
|
|
|
|
|
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
|
|
|
|
}
|