Skip to content

Commit

Permalink
add: data.json, DataLoader class
Browse files Browse the repository at this point in the history
  • Loading branch information
colourfulspring committed Jan 18, 2024
1 parent bb53d62 commit c76421d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class CarAgent : Agent
// to this range
public int decisionRangeRadius;

private NavMeshAgent m_AgentNavMeshAgent; //cached on initialization

protected override void Awake()
{
base.Awake();
Expand All @@ -34,6 +36,12 @@ void Start()
// GetComponent<NavMeshAgent>().SetDestination(navigationGoal);
}

public override void Initialize()
{
base.Initialize();
m_AgentNavMeshAgent = GetComponent<NavMeshAgent>();
}

/// <summary>
/// Collects vector observations for each agent.
/// </summary>
Expand Down Expand Up @@ -104,7 +112,7 @@ public override void OnActionReceived(ActionBuffers actionBuffers)

Debug.Log(this.transform.parent.gameObject.name +
", " + this.name + "onActionReceived: navigationGoal " + navigationGoal);
GetComponent<NavMeshAgent>().SetDestination(navigationGoal);
m_AgentNavMeshAgent.SetDestination(navigationGoal);

// Move the agent using the action.
// MoveAgent(actionBuffers.DiscreteActions);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using Unity.MLAgents;
using UnityEngine;
using Random = UnityEngine.Random;

public class CarCatchingEnvController : MonoBehaviour
{
Expand Down Expand Up @@ -46,13 +43,16 @@ public class CarInfo

private CarCatchingSettings m_CarCatchingSettings;

public CapturePosMap CapturePosMap;

// private int m_ResetTimer;

protected void Awake()
{
// Get the ground's bounds
areaBounds = ground.GetComponent<Collider>().bounds;
m_CarCatchingSettings = FindObjectOfType<CarCatchingSettings>();
CapturePosMap = new CapturePosMap("data.json");
}

void Start()
Expand Down
74 changes: 74 additions & 0 deletions Project/Assets/ML-Agents/Examples/CarCatching/Scripts/DataLoder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;

public class CapturePosMap
{
// Read the JSON file content
string jsonContent;

Dictionary<string, double[,]> resultMap;

public CapturePosMap(string relativePath)
{
// Use Application.streamingAssetsPath for files in the StreamingAssets folder
string fullPath = Path.Combine(Application.streamingAssetsPath, relativePath);
Debug.Log("Json file at: " + fullPath);

if (File.Exists(fullPath))
{
jsonContent = File.ReadAllText(fullPath);
}
else
{
Debug.Log("File not found: " + fullPath);
}

Debug.Log(jsonContent);

// Deserialize the JSON content into a Dictionary<string, int[][]>
resultMap = JsonConvert.DeserializeObject<Dictionary<string, double[,]>>(jsonContent);
}

// Here 'width' and 'height' mentioned below means the longer edge and the shorter edge of the scene, respectively.
// In json file, a capturing point or an enemy point [a, b] means the height axis coordination 'a' and the width axis coordination 'b'.
// However, in C# Astar class, a point [a, b] represents the width axis coordination 'a' and the height axis coordination 'b'.
// Since the order of the above two formats are different, so it is necessary to change the coordination order of each point here,
// including enemy point (constructing key) and capturing point (generating return value).
// Moreover, Unity scene's origin is lower left corner and Python image library usually treat upper left corner as origin,
// so suppose enemy point in costmap is [a, b], another transform to [a, 100-b] should be applied on it.
// In conclusion, this function's input point [x, y] should be transformed to [100-y, x] first before querying
// the json file to retrieve the corresponding capturing point. After querying, the capturing point [x, y] should also be
// transformed to [y, 100-x]. 100 is the height of costmap.

public double[,] getCapturePosFromEnemyPos(int enemyPosX, int enemyPosY)
{
(enemyPosX, enemyPosY) = (100 - enemyPosY, enemyPosX);
string key = "(" + enemyPosX + ", " + enemyPosY + ")";
// Debug.Log("getValueFromKey: " + key);
double[,] posList;
if (resultMap.ContainsKey(key))
{
// A new copy of the value in the dictionary should be created because the swap operation mentioned below
// are not supposed to edit the value in the Dictionary container.
posList = new double[3, 2];
// .Length property is the total length of 2-dim array.
Array.Copy(resultMap[key], posList, resultMap[key].Length);

for (int i = 0; i < resultMap[key].GetLength(0); ++i)
{
(posList[i, 0], posList[i, 1]) = (posList[i, 1], 100 - posList[i, 0]);
}
}
else
{
posList = new double[0, 0];
Console.WriteLine($"Key '{key}' not found in the dictionary.");
}

// Debug.Log("getValueFromKey End");
return posList;
}
}

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

8 changes: 8 additions & 0 deletions Project/Assets/StreamingAssets.meta

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

1 change: 1 addition & 0 deletions Project/Assets/StreamingAssets/data.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Project/Assets/StreamingAssets/data.json.meta

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

0 comments on commit c76421d

Please sign in to comment.