-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
…ocraticagent-and-lexer-classes 11 implement basic unit tests for socraticagent and lexer classes
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "EditorTests", | ||
"optionalUnityReferences": [ | ||
"TestAssemblies" | ||
], | ||
"includePlatforms": [ | ||
"Editor" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using NUnit.Framework; | ||
using UnityEngine; | ||
using UnityEngine.TestTools; | ||
|
||
namespace DialogosEngine.Tests | ||
{ | ||
public class NewEditorTests | ||
{ | ||
[Test] | ||
public void NewEditorTestsSimplePasses() | ||
{ | ||
// TODO Use the Assert class to test conditions | ||
} | ||
|
||
[UnityTest] | ||
public IEnumerator NewEditorTestsWithEnumeratorPasses() | ||
{ | ||
// TODO Use the Assert class to test conditions. | ||
yield return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
%YAML 1.1 | ||
%TAG !u! tag:unity3d.com,2011: | ||
--- !u!1 &7291126267894192705 | ||
GameObject: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
serializedVersion: 6 | ||
m_Component: | ||
- component: {fileID: 1189533486204883621} | ||
m_Layer: 0 | ||
m_Name: EchoAgent | ||
m_TagString: Untagged | ||
m_Icon: {fileID: 0} | ||
m_NavMeshLayer: 0 | ||
m_StaticEditorFlags: 0 | ||
m_IsActive: 1 | ||
--- !u!4 &1189533486204883621 | ||
Transform: | ||
m_ObjectHideFlags: 0 | ||
m_CorrespondingSourceObject: {fileID: 0} | ||
m_PrefabInstance: {fileID: 0} | ||
m_PrefabAsset: {fileID: 0} | ||
m_GameObject: {fileID: 7291126267894192705} | ||
serializedVersion: 2 | ||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} | ||
m_LocalPosition: {x: 0, y: 0, z: 0} | ||
m_LocalScale: {x: 1, y: 1, z: 1} | ||
m_ConstrainProportionsScale: 0 | ||
m_Children: [] | ||
m_Father: {fileID: 0} | ||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace DialogosEngine | ||
{ | ||
public static class AgentUtils | ||
{ | ||
public const string k_EndOfSequence = "<eos>"; | ||
|
||
public static float CalculateEchoReward(string expectedString, string guessedString) | ||
{ | ||
// Validate input strings | ||
if (string.IsNullOrEmpty(expectedString) || string.IsNullOrEmpty(guessedString)) | ||
{ | ||
throw new ArgumentException("Expected and guessed strings must not be null or empty."); | ||
} | ||
if (!expectedString.EndsWith(k_EndOfSequence)) | ||
{ | ||
throw new ArgumentException("Expected string must end with '<eos>'."); | ||
} | ||
|
||
// Calculate Levenshtein distance | ||
int levenshteinDistance = Lexer.LevenshteinDistance(expectedString, guessedString); | ||
float maxStringLength = Mathf.Max(expectedString.Length, guessedString.Length); | ||
float similarityScore = 1f - (float)levenshteinDistance / maxStringLength; | ||
similarityScore = (similarityScore * 2f) - 1f; // Normalize to range [-1, 1] | ||
|
||
// Calculate length match score | ||
float lengthDifference = Mathf.Abs(expectedString.Length - guessedString.Length); | ||
float lengthMatchScore = 1f - Mathf.Min(2f * lengthDifference / maxStringLength, 1f); | ||
lengthMatchScore = (lengthMatchScore * 2f) - 1f; // Normalize to range [-1, 1] | ||
|
||
// Combine similarity and length match scores | ||
float combinedScore = (0.5f * similarityScore) + (0.5f * lengthMatchScore); | ||
|
||
// Ensure the final score is within the range [-1, 1] | ||
return Mathf.Clamp(combinedScore, -1f, 1f); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using CommandTerminal; | ||
using Unity.MLAgents; | ||
using Unity.MLAgents.Actuators; | ||
using Unity.MLAgents.Sensors; | ||
|
||
namespace DialogosEngine | ||
{ | ||
public class EchoAgent : Agent | ||
{ | ||
string _CachedString; | ||
|
||
public override void OnEpisodeBegin() | ||
{ | ||
Terminal.Instance.Buffer.Reset(); | ||
} | ||
|
||
public void FixedUpdate() | ||
{ | ||
string expectedString = GetExpectedString(); | ||
if (_CachedString != null) | ||
{ | ||
float reward = AgentUtils.CalculateEchoReward(expectedString, _CachedString); | ||
|
||
if (_CachedString.EndsWith(AgentUtils.k_EndOfSequence)) | ||
{ | ||
_CachedString = _CachedString.Replace(AgentUtils.k_EndOfSequence, ""); | ||
Terminal.Instance.Shell.Run(_CachedString); | ||
} | ||
|
||
SetReward(reward); | ||
_CachedString = null; | ||
} | ||
} | ||
|
||
public override void CollectObservations(VectorSensor sensor) | ||
{ | ||
string _buffer = Terminal.Instance.Buffer.GetLastLog(); | ||
float[] _vectorizedBuffer = Lexer.VectorizeUTF8(_buffer); | ||
foreach (var obs in _vectorizedBuffer) | ||
{ | ||
sensor.AddObservation(obs); | ||
} | ||
} | ||
|
||
public override void OnActionReceived(ActionBuffers actions) | ||
{ | ||
float[] _actionArray = actions.ContinuousActions.Array; | ||
_CachedString = Lexer.QuantizeUTF8(_actionArray); | ||
} | ||
|
||
private string GetExpectedString() | ||
{ | ||
return "echo hello <eos>"; // Testing | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.