Browse Source

Change CozmoAgent to give him custom memory size ad other model brain

Development
Tobi 5 years ago
parent
commit
d96e23e8c2

+ 1
- 1
Assets/Brains/CozmoLearningBrain.asset View File

vectorActionDescriptions: vectorActionDescriptions:
- -
vectorActionSpaceType: 0 vectorActionSpaceType: 0
model: {fileID: 11400000, guid: 24baaa8c605c1a74785880885cf04847, type: 3}
model: {fileID: 11400000, guid: 7f8f0f4568060e84bb933886fd122e7e, type: 3}
inferenceDevice: 0 inferenceDevice: 0

BIN
Assets/Brains/CozmoLearningBrain_cozmo_training-0.nn View File


+ 10
- 0
Assets/Brains/CozmoLearningBrain_cozmo_training-0.nn.meta View File

fileFormatVersion: 2
guid: 7f8f0f4568060e84bb933886fd122e7e
ScriptedImporter:
fileIDToRecycleName:
11400000: Assets/Brains/CozmoLearningBrain_cozmo_training-0.nn
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 83221ad3db87f4b3b91b041047cb2bc5, type: 3}

+ 16
- 0
Assets/Scenes/CozmoTraining.unity View File

- {fileID: 11400000, guid: 0d9d5ce547064a648b466ad7f208cf3d, type: 2} - {fileID: 11400000, guid: 0d9d5ce547064a648b466ad7f208cf3d, type: 2}
_brainsToControl: _brainsToControl:
- {fileID: 0} - {fileID: 0}
- {fileID: 11400000, guid: 0d9d5ce547064a648b466ad7f208cf3d, type: 2}
maxSteps: 1500 maxSteps: 1500
trainingConfiguration: trainingConfiguration:
width: 80 width: 80
propertyPath: timeBetweenDecisionsAtInference propertyPath: timeBetweenDecisionsAtInference
value: 0.04 value: 0.04
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 7570006596986120125, guid: 0f97dac5215d69a4795763340d82925d,
type: 3}
propertyPath: maxStoredMovementStates
value: 5
objectReference: {fileID: 0}
- target: {fileID: 891713638, guid: 0f97dac5215d69a4795763340d82925d, type: 3} - target: {fileID: 891713638, guid: 0f97dac5215d69a4795763340d82925d, type: 3}
propertyPath: m_TagString propertyPath: m_TagString
value: Untagged value: Untagged
propertyPath: m_Enabled propertyPath: m_Enabled
value: 1 value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 7570006596986120128, guid: 0f97dac5215d69a4795763340d82925d,
type: 3}
propertyPath: m_TurnSpeed
value: 35
objectReference: {fileID: 0}
- target: {fileID: 7570006596986120128, guid: 0f97dac5215d69a4795763340d82925d,
type: 3}
propertyPath: m_Speed
value: 0.0535
objectReference: {fileID: 0}
m_RemovedComponents: [] m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 0f97dac5215d69a4795763340d82925d, type: 3} m_SourcePrefab: {fileID: 100100000, guid: 0f97dac5215d69a4795763340d82925d, type: 3}
--- !u!1 &7570006596986120123 stripped --- !u!1 &7570006596986120123 stripped

+ 70
- 25
Assets/Scripts/ML Cozmo/CozmoAgent.cs View File

using MLAgents; using MLAgents;
using OpenCvSharp; using OpenCvSharp;
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;


namespace Cozmo namespace Cozmo
[Tooltip("Reference to the CozmoMovement script")] [Tooltip("Reference to the CozmoMovement script")]
public CozmoMovementController movementController; public CozmoMovementController movementController;
public float timeBetweenDecisionsAtInference; public float timeBetweenDecisionsAtInference;
public int maxStoredMovementStates = 1;


private Academy academy; // CozmoAcademy private Academy academy; // CozmoAcademy
private float timeSinceDecision; // time since last decision private float timeSinceDecision; // time since last decision
private int centerOfImageX = 0; // Middle of the image in x direction private int centerOfImageX = 0; // Middle of the image in x direction
private MovementState lastChosenMovement = MovementState.Stop; // The last action/movement that was executed private MovementState lastChosenMovement = MovementState.Stop; // The last action/movement that was executed


private Queue<MovementState> lastActions = new Queue<MovementState>();

private double startTime = Time.time; private double startTime = Time.time;


private void Start() private void Start()
// Set ActionMask for training // Set ActionMask for training
private void SetMask() private void SetMask()
{ {
switch (lastChosenMovement)
// Do not allow stop decision after a stop
if (lastChosenMovement == MovementState.Stop)
{ {
// Do not allow stop decision after a stop
case (MovementState.Stop):
SetActionMask(STOP);
break;
// Do not allow stop after forward
case (MovementState.Forward):
SetActionMask(STOP);
break;
// Do not allow stop & left after right
case (MovementState.Right):
SetActionMask(STOP);
SetActionMask(LEFT);
break;
// Do not allow stop & right after left
case (MovementState.Left):
SetActionMask(STOP);
SetActionMask(RIGHT);
break;
default:
throw new ArgumentException("Invalid MovementState.");
SetActionMask(STOP);
}

// Do not allow left decision if right was in the last actions
if (lastActions.Contains(MovementState.Right))
{
SetActionMask(LEFT);
} }

// Do not allow right decision if left was in the last actions
if (lastActions.Contains(MovementState.Left))
{
SetActionMask(RIGHT);
}

//switch (lastChosenMovement)
//{
// // Do not allow stop decision after a stop
// case (MovementState.Stop):
// SetActionMask(STOP);
// break;
// // Do not allow stop after forward
// case (MovementState.Forward):
// //SetActionMask(STOP);
// break;
// // Do not allow stop & left after right
// case (MovementState.Right):
// //SetActionMask(STOP);
// if (lastActions.Contains(MovementState.))
// SetActionMask(LEFT);
// break;
// // Do not allow stop & right after left
// case (MovementState.Left):
// //SetActionMask(STOP);
// SetActionMask(RIGHT);
// break;
// default:
// throw new ArgumentException("Invalid MovementState.");
//}
} }


// to be implemented by the developer // to be implemented by the developer
movementController.currentMovementState = MovementState.Stop; movementController.currentMovementState = MovementState.Stop;
lastChosenMovement = MovementState.Stop; lastChosenMovement = MovementState.Stop;
//Test //Test
SetReward(-0.1f);
SetReward(-0.02f);
break; break;
case FORWARD: case FORWARD:
movementController.currentMovementState = MovementState.Forward; movementController.currentMovementState = MovementState.Forward;
lastChosenMovement = MovementState.Forward; lastChosenMovement = MovementState.Forward;
//Test //Test
SetReward(0.01f);
SetReward(0.02f);
break; break;
case RIGHT: case RIGHT:
movementController.currentMovementState = MovementState.Right; movementController.currentMovementState = MovementState.Right;
lastChosenMovement = MovementState.Right; lastChosenMovement = MovementState.Right;
//Test //Test
SetReward(-0.02f);
SetReward(0.01f);
break; break;
case LEFT: case LEFT:
movementController.currentMovementState = MovementState.Left; movementController.currentMovementState = MovementState.Left;
lastChosenMovement = MovementState.Left; lastChosenMovement = MovementState.Left;
//Test //Test
SetReward(-0.02f);
SetReward(0.01f);
break; break;
default: default:
//movement.Move(0); //movement.Move(0);
throw new ArgumentException("Invalid action value. Stop movement."); throw new ArgumentException("Invalid action value. Stop movement.");
} }


CollectLastMovementStates(lastChosenMovement);

// Render new image after movement in order to update the centerOfGravity // Render new image after movement in order to update the centerOfGravity
if (renderCamera != null) if (renderCamera != null)
{ {
SetReward(reward); SetReward(reward);
} }


// Store the last movementStates in a Queue
private void CollectLastMovementStates(MovementState movementState)
{
// Check if Queue exists and values should be stored
if ((lastActions != null) && (maxStoredMovementStates > 0))
{
// maxStoredMovementStates is reached
if (lastActions.Count >= maxStoredMovementStates)
{
// deque first value(s) when maxStoredMovementStates is reached
for (int i = 0; i <= (lastActions.Count - maxStoredMovementStates); i++)
{
lastActions.Dequeue();
}
}
// add last action to queue
lastActions.Enqueue(movementState);
}
}

// to be implemented by the developer // to be implemented by the developer
public override void AgentReset() public override void AgentReset()
{ {

Loading…
Cancel
Save