Skip to content

Commit

Permalink
fixed command logger, added guess string check, updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 9, 2024
1 parent a4f9ab5 commit 91e02d4
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 18 deletions.
11 changes: 9 additions & 2 deletions Assets/CommandTerminal/CommandLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace CommandTerminal
{
Expand All @@ -16,13 +17,18 @@ public CommandLogger(string fileName = "command_log.txt", int bufferSize = 100)
{
string _desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
_logFilePath = Path.Combine(_desktopPath, fileName);
_streamWriter = new StreamWriter(_logFilePath, true);
_streamWriter = new StreamWriter(new FileStream(_logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite), Encoding.UTF8);
_logBuffer = new List<string>();
_bufferSize = bufferSize;
}

public void Log(string message)
{
if (_disposed)
{
throw new ObjectDisposedException("CommandLogger", "Cannot log to a disposed CommandLogger.");
}

lock (_logBuffer)
{
long _epochTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
Expand Down Expand Up @@ -50,7 +56,6 @@ public void FlushBuffer()
}
catch (Exception ex)
{
// Handle the exception, e.g., log it to a separate file or rethrow
Console.WriteLine($"An error occurred while flushing the buffer: {ex.Message}");
}
}
Expand All @@ -66,6 +71,8 @@ public void Dispose()
{
if (!_disposed)
{
FlushBuffer(); // Ensure buffer is flushed before closing
_streamWriter?.Close(); // Close the stream before disposing
_streamWriter?.Dispose();
_disposed = true;
}
Expand Down
49 changes: 49 additions & 0 deletions Assets/Resources/Prefabs/EchoAgent.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1189533486204883621}
- component: {fileID: 1499401919544391686}
- component: {fileID: 5052090153251849861}
m_Layer: 0
m_Name: EchoAgent
m_TagString: Untagged
Expand All @@ -31,3 +33,50 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1499401919544391686
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7291126267894192705}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5d1c4e0b1822b495aa52bc52839ecb30, type: 3}
m_Name:
m_EditorClassIdentifier:
m_BrainParameters:
VectorObservationSize: 1000
NumStackedVectorObservations: 1
m_ActionSpec:
m_NumContinuousActions: 1001
BranchSizes:
VectorActionSize: e9030000
VectorActionDescriptions: []
VectorActionSpaceType: 1
hasUpgradedBrainParametersWithActionSpec: 1
m_Model: {fileID: 0}
m_InferenceDevice: 0
m_BehaviorType: 1
m_BehaviorName: Dialogos
TeamId: 0
m_UseChildSensors: 1
m_UseChildActuators: 1
m_DeterministicInference: 0
m_ObservableAttributeHandling: 0
--- !u!114 &5052090153251849861
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7291126267894192705}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 91356ebb54747f140a42a1e87c3f5965, type: 3}
m_Name:
m_EditorClassIdentifier:
agentParameters:
maxStep: 0
hasUpgradedFromAgentParameters: 1
MaxStep: 10000
4 changes: 4 additions & 0 deletions Assets/Scripts/Agents/AgentUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public static float CalculateEchoReward(string expectedString, string guessedStr
{
throw new ArgumentException("Expected string must end with '<eos>'.");
}
if (expectedString == guessedString)
{
return 1f;
}

// Calculate Levenshtein distance
int levenshteinDistance = Lexer.LevenshteinDistance(expectedString, guessedString);
Expand Down
35 changes: 21 additions & 14 deletions Assets/Scripts/Agents/EchoAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;

namespace DialogosEngine
{
public class EchoAgent : Agent
{
string _CachedString;
string _ExpectedString = "echo hello <eos>";

public override void OnEpisodeBegin()
{
Expand All @@ -16,32 +18,42 @@ public override void OnEpisodeBegin()

public void FixedUpdate()
{
string expectedString = GetExpectedString();
if (_CachedString != null)
{
float reward = AgentUtils.CalculateEchoReward(expectedString, _CachedString);
float reward = AgentUtils.CalculateEchoReward(_ExpectedString, _CachedString);

// Debug
Terminal.Instance.LogToFile($"[{StepCount}]{reward} | {_CachedString} | {_ExpectedString}");

if (_CachedString.EndsWith(AgentUtils.k_EndOfSequence))
{
_CachedString = _CachedString.Replace(AgentUtils.k_EndOfSequence, "");
Terminal.Instance.Shell.Run(_CachedString);
}

SetReward(reward);
_CachedString = null;

// Testing
Terminal.Instance.LogToFile($"{GetCumulativeReward()} | {_CachedString} | {expectedString}");
if (_CachedString == _ExpectedString)
{
_CachedString = null;
SetReward(1f);
EndEpisode();
}
else
{
_CachedString = null;
SetReward(reward);
}
}
}

public override void CollectObservations(VectorSensor sensor)
{
string _buffer = Terminal.Instance.Buffer.GetLastLog();
float[] _vectorizedBuffer = Lexer.VectorizeUTF8(_buffer);
foreach (var obs in _vectorizedBuffer)
int maxObservations = Mathf.Min(_vectorizedBuffer.Length, Lexer.k_MaxBufferLength);

for (int i = 0; i < maxObservations; i++)
{
sensor.AddObservation(obs);
sensor.AddObservation(_vectorizedBuffer[i]);
}
}

Expand All @@ -55,10 +67,5 @@ public override void OnActionReceived(ActionBuffers actions)

_CachedString = Lexer.QuantizeUTF8(_actionArray);
}

private string GetExpectedString()
{
return "echo hello <eos>"; // Testing
}
}
}
1 change: 0 additions & 1 deletion Assets/Scripts/Agents/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Globalization;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace DialogosEngine
Expand Down
1 change: 0 additions & 1 deletion Assets/Scripts/Agents/Transformer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Linq;
using UnityEngine;

Expand Down
17 changes: 17 additions & 0 deletions Tests/AgentUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
[TestFixture]
public class AgentUtilsTests
{
[Test]
public void CalculateEchoReward_ShouldReturnPerfectScore_ForEchoHelloEos()
{
// Arrange
string expected = "echo hello <eos>";
string guessed = "echo hello <eos>";
TestContext.WriteLine($"Testing CalculateEchoReward with expected string: '{expected}' and guessed string: '{guessed}'.");

// Act
float reward = AgentUtils.CalculateEchoReward(expected, guessed);
TestContext.WriteLine($"Calculated reward: {reward}");

// Assert
Assert.That(reward, Is.EqualTo(1f), "The reward should be 1.0f when the expected and guessed strings match exactly for 'echo hello <eos>'.");
TestContext.WriteLine("Test passed: The calculated reward is 1.0f as expected for 'echo hello <eos>'.");
}

[Test]
public void CalculateEchoReward_ShouldReturnPerfectScore_WhenStringsMatchExactly()
{
Expand Down

0 comments on commit 91e02d4

Please sign in to comment.