Skip to content

Commit fc86bc3

Browse files
committed
added neural evolution trainer. Change the databuffer to be rescalable. Use layer definition for RL and SL network.scripables.
1 parent 8e36437 commit fc86bc3

36 files changed

+2389
-236
lines changed

Assets/UnityTensorflow/Common/Utils/DataBuffer.cs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,43 @@ public DataInfo(string name, Type type, int[] dimension)
3030
[Serializable]
3131
protected struct DataContainer
3232
{
33-
public DataContainer(DataInfo info, int maxDataCount)
33+
34+
public DataInfo info;
35+
public Array dataList;
36+
37+
public DataContainer(DataInfo info):this(info,8)
3438
{
39+
}
40+
41+
public DataContainer(DataInfo info, int reservedSize)
42+
{
43+
Debug.Assert(reservedSize > 0, "reservedSize needs to be larger than 0");
44+
reservedSize = Mathf.Max(1, reservedSize);
3545
this.info = info;
36-
dataList = Array.CreateInstance(info.type, (new int[] { maxDataCount }).Concat(info.dimension).ToArray());
46+
dataList = Array.CreateInstance(info.type, (new int[] { reservedSize }).Concat(info.dimension).ToArray());
3747
}
38-
public DataInfo info;
39-
public Array dataList;
48+
49+
50+
51+
52+
53+
public void IncreaseArraySize(int sizeToAdd)
54+
{
55+
Debug.Assert(sizeToAdd > 0, "Size to add needs to be larger than 0");
56+
sizeToAdd = Mathf.Max(1, sizeToAdd);
57+
58+
var newArray = Array.CreateInstance(info.type, (new int[] { dataList.GetLength(0) + sizeToAdd }).Concat(info.dimension).ToArray());
59+
int typeSize = Marshal.SizeOf(info.type);
60+
Buffer.BlockCopy(dataList, 0, newArray, 0, dataList.Length * info.unitLength * typeSize);
61+
dataList = newArray;
62+
63+
}
64+
65+
public int CurrentSize()
66+
{
67+
return dataList.GetLength(0);
68+
}
69+
4070
}
4171

4272

@@ -105,25 +135,38 @@ public void AddData(params ValueTuple<string, Array>[] data)
105135

106136
//feed the data.
107137

108-
//add the episode to the buffer
138+
//calucate numbers for adding the episode to the buffer
109139
int numToAdd = size;
110140
int spaceLeft = MaxCount - nextBufferPointer;
141+
if (MaxCount <= 0)
142+
spaceLeft = Int32.MaxValue;
111143

112144
int appendSize = Mathf.Min(spaceLeft, numToAdd);
113145
int fromStartSize = Mathf.Max(0, numToAdd - spaceLeft);
114146

147+
CurrentCount += numToAdd;
148+
if (MaxCount > 0)
149+
CurrentCount = Mathf.Clamp(CurrentCount, 0, MaxCount);
150+
115151
foreach (var k in data)
116152
{
153+
//resize the data container if needed
154+
while(CurrentCount > dataset[k.Item1].CurrentSize())
155+
{
156+
dataset[k.Item1].IncreaseArraySize(dataset[k.Item1].CurrentSize());
157+
}
158+
117159
DataContainer dd = dataset[k.Item1];
118160
int typeSize = Marshal.SizeOf(dd.info.type);
119161
//Debug.Log(k.Item1);
120162
//Debug.Log("add length " + k.Item2.Length + " copy length " + (appendSize * dd.info.unitLength).ToString());
121163
//Array.Copy(k.Item2, 0, dd.dataList, nextBufferPointer * dd.info.unitLength, appendSize * dd.info.unitLength);
122164
Buffer.BlockCopy(k.Item2, 0, dd.dataList, nextBufferPointer * dd.info.unitLength * typeSize, appendSize * dd.info.unitLength * typeSize);
123165
}
166+
167+
124168
nextBufferPointer += appendSize;
125-
CurrentCount += numToAdd;
126-
CurrentCount = Mathf.Clamp(CurrentCount, 0, MaxCount);
169+
127170
if (fromStartSize > 0)
128171
{
129172
foreach (var k in data)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!114 &11400000
4+
MonoBehaviour:
5+
m_ObjectHideFlags: 0
6+
m_PrefabParentObject: {fileID: 0}
7+
m_PrefabInternal: {fileID: 0}
8+
m_GameObject: {fileID: 0}
9+
m_Enabled: 1
10+
m_EditorHideFlags: 0
11+
m_Script: {fileID: 11500000, guid: d99c8458226607a4bb1b2196d603c1b6, type: 3}
12+
m_Name: 3DBAllNetworkACNE
13+
m_EditorClassIdentifier:
14+
actorNNHidden: 2
15+
actorNNWidth: 32
16+
criticNNHidden: 2
17+
criticNNWidth: 32
18+
hiddenWeightsInitialScale: 1
19+
outputWeightsInitialScale: 0.01

Assets/UnityTensorflow/Examples/3DBall/3DBAllNetworkACNE.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)