Skip to content

Commit

Permalink
implemented prefab load command and added reward signal to echo agent
Browse files Browse the repository at this point in the history
  • Loading branch information
p3nGu1nZz committed Apr 7, 2024
1 parent ddf1a72 commit f32a9b0
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Assets/CommandTerminal/CommandArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public struct CommandArg
{
private string _value;

public CommandArg(string value)
{
_value = value;
}

public string Value
{
get { return _value; }
Expand Down
8 changes: 8 additions & 0 deletions Assets/Resources/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
33 changes: 33 additions & 0 deletions Assets/Resources/Prefabs/EchoAgent.prefab
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}
7 changes: 7 additions & 0 deletions Assets/Resources/Prefabs/EchoAgent.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
68 changes: 45 additions & 23 deletions Assets/Scripts/Agents/EchoAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,91 @@
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;
using UnityEngine;

namespace DialogosEngine
{
public class EchoAgent : Agent
{
char _GuessedChar;
string _CachedGuessedString;
const string k_EndOfSequence = "<eos>";

public override void OnEpisodeBegin()
{
ClearConsole();

//
}

public void FixedUpdate()
{
char expectedChar = GetExpectedChar();
float reward = CalculateReward(expectedChar, _GuessedChar);
SetReward(reward);
string expectedString = GetExpectedString();
if (_CachedGuessedString != null)
{
float _reward;
if (_CachedGuessedString.EndsWith(k_EndOfSequence))
{
_reward = CalculateReward(expectedString, _CachedGuessedString);
_CachedGuessedString = _CachedGuessedString.Replace(k_EndOfSequence, "");
Terminal.Instance.Shell.Run(_CachedGuessedString);
if(Terminal.Instance.IssuedError)
{
_reward -= 0.5f; // Penalize for bad commands
}
}
else
{
_reward = -1f;
}
SetReward(_reward);
_CachedGuessedString = null;
}
}

public override void CollectObservations(VectorSensor sensor)
{
string buffer = GetConsoleBuffer();
float[] vectorizedBuffer = Lexer.VectorizeUTF8(buffer);
foreach (var obs in vectorizedBuffer)
string _buffer = GetConsoleBuffer();
float[] _vectorizedBuffer = Lexer.VectorizeUTF8(_buffer);
foreach (var obs in _vectorizedBuffer)
{
sensor.AddObservation(obs);
}
}

public override void OnActionReceived(ActionBuffers actions)
{
float[] actionArray = new float[1] { actions.ContinuousActions[0] };
_GuessedChar = Lexer.QuantizeUTF8(actionArray)[0];
HandleGuessedCharacter(_GuessedChar);
float[] _actionArray = actions.ContinuousActions.Array;
_CachedGuessedString = Lexer.QuantizeUTF8(_actionArray);
}

private void ClearConsole()
{
Terminal.Instance.Buffer.Reset();
}

private float CalculateReward(char expectedChar, char guessedChar)
private float CalculateReward(string expectedString, string guessedString)
{
// Implementation to calculate the reward based on the guessed character
return 0;
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]

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]

float combinedScore = (0.5f * similarityScore) + (0.5f * lengthMatchScore);
return Mathf.Clamp(combinedScore, -1f, 1f); // Ensure final score is within [-1, 1]
}


private string GetConsoleBuffer()
{
return Terminal.Instance.Buffer.GetLastLog();
}

private void HandleGuessedCharacter(char guessedChar)
{
// Implementation to handle the guessed character
}

private char GetExpectedChar()
private string GetExpectedString()
{
// Implementation to get the expected character for the current step
return new char();
// Implementation to get the expected string for the current step
return "";
}
}
}
37 changes: 37 additions & 0 deletions Assets/Scripts/Agents/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,43 @@ public static string QuantizeUTF8(float[] vector)
return new string(chars);
}

public static int LevenshteinDistance(string a, string b)
{
if (string.IsNullOrEmpty(a))
{
return string.IsNullOrEmpty(b) ? 0 : b.Length;
}

if (string.IsNullOrEmpty(b))
{
return a.Length;
}

int[] codePointsA = a.ToCharArray().Select(c => char.ConvertToUtf32(a, a.IndexOf(c))).ToArray();
int[] codePointsB = b.ToCharArray().Select(c => char.ConvertToUtf32(b, b.IndexOf(c))).ToArray();

int lengthA = codePointsA.Length;
int lengthB = codePointsB.Length;
var distances = new int[lengthA + 1, lengthB + 1];

for (int i = 0; i <= lengthA; distances[i, 0] = i++) { }
for (int j = 0; j <= lengthB; distances[0, j] = j++) { }

for (int i = 1; i <= lengthA; i++)
{
for (int j = 1; j <= lengthB; j++)
{
int cost = codePointsB[j - 1] == codePointsA[i - 1] ? 0 : 1;
distances[i, j] = Mathf.Min(
Mathf.Min(distances[i - 1, j] + 1, distances[i, j - 1] + 1),
distances[i - 1, j - 1] + cost);
}
}

return distances[lengthA, lengthB];
}


public static float CalculateWhitespace(string[] text)
{
int _totalWhitespace = text.Sum(line => line.Count(char.IsWhiteSpace));
Expand Down
17 changes: 17 additions & 0 deletions Assets/Scripts/Commands/PrefabCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public static void PrefabCommand(CommandArg[] args)
case "load":
LoadPrefab(prefabName);
break;
case "destroy":
DestroyPrefab(prefabName);
break;
default:
Terminal.Instance.LogError($"[PREFAB] Unknown mode: {mode}");
break;
Expand All @@ -40,5 +43,19 @@ private static void LoadPrefab(string prefabName)
Terminal.Instance.LogError($"[PREFAB] Could not find prefab: {prefabName}");
}
}

private static void DestroyPrefab(string prefabName)
{
GameObject prefabObject = GameObject.Find($"{prefabName}(Clone)");
if (prefabObject != null)
{
Object.Destroy(prefabObject);
Terminal.Instance.Log($"[PREFAB] {prefabName} destroyed successfully.");
}
else
{
Terminal.Instance.LogError($"[PREFAB] Could not find object: {prefabName}");
}
}
}
}
Loading

0 comments on commit f32a9b0

Please sign in to comment.