-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb53d62
commit c76421d
Showing
7 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Project/Assets/ML-Agents/Examples/CarCatching/Scripts/DataLoder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Project/Assets/ML-Agents/Examples/CarCatching/Scripts/DataLoder.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.