Skip to content

Commit

Permalink
handle agent actions
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 11, 2024
1 parent 48a625d commit 366b993
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
6 changes: 3 additions & 3 deletions Assets/Scenes/TrainingSimple.unity
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,12 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
VectorObservationSize: 1
VectorObservationSize: 1000
NumStackedVectorObservations: 1
m_ActionSpec:
m_NumContinuousActions: 1
m_NumContinuousActions: 1001
BranchSizes:
VectorActionSize: 01000000
VectorActionSize: e9030000
VectorActionDescriptions: []
VectorActionSpaceType: 1
hasUpgradedBrainParametersWithActionSpec: 1
Expand Down
53 changes: 44 additions & 9 deletions Assets/Scripts/Agents/WormSimpleAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class WormSimpleAgent : Agent
{
CommandLogger Logger;
bool _IsInitialized = false;
string _ExpectedString = "echo hello <eos>";
string _CachedString;

public override void Initialize()
{
Expand All @@ -25,23 +27,56 @@ public override void OnEpisodeBegin()

public override void CollectObservations(VectorSensor sensor)
{
var obs = Random.value;
sensor.AddObservation(obs);
Logger.Log($"[{StepCount}] CollectObservations: {obs}");
string simulatedTerminalOutput = "echo hello <eos>";
float[] vectorizedOutput = Lexer.VectorizeUTF8(simulatedTerminalOutput);

System.Array.Resize(ref vectorizedOutput, Lexer.k_MaxBufferLength);

// Debug
//System.Text.StringBuilder sb = new System.Text.StringBuilder();

for (int i = 0; i < Lexer.k_MaxBufferLength; i++)
{
float observationValue = i < vectorizedOutput.Length ? vectorizedOutput[i] : 0f;
sensor.AddObservation(observationValue);

// Debug
//sb.Append(observationValue);
//sb.Append(i < Lexer.k_MaxBufferLength - 1 ? ", " : "");
}

// Debug
//Logger.Log($"[{StepCount}] CollectObservations: " + sb.ToString());
}

public override void OnActionReceived(ActionBuffers actionBuffers)
public override void OnActionReceived(ActionBuffers actions)
{
float[] _actionArray = actions.ContinuousActions.Array;
float _lengthControlValue = _actionArray[0];

Logger.Log($"[{StepCount}] Length Control Value: {_lengthControlValue}");

int outputLength = Transformer.RoundMax(ref _lengthControlValue);

Logger.Log($"[{StepCount}] Rounded Output Length: {outputLength}");

for (int i = 1; i < _actionArray.Length; i++)
{
_actionArray[i] = Transformer.Transform(ref _actionArray[i]);
}

AgentUtils.ProcessActionArray(ref _actionArray, outputLength);

//Logger.Log($"[{StepCount}] Processed Action Array: {string.Join(", ", _actionArray)}");

_CachedString = Lexer.QuantizeUTF8(_actionArray);

var i = -1;
var continuousActions = actionBuffers.ContinuousActions;
var output = continuousActions[++i];
Logger.Log($"[{StepCount}] OnActionReceived: {output}");
Logger.Log($"[{StepCount}] Quantized String: {_CachedString}");
}

void FixedUpdate()
{
if(!_IsInitialized)
if (!_IsInitialized)
{
return;
}
Expand Down

0 comments on commit 366b993

Please sign in to comment.