|
|
|
|
|
|
private class StepMessage |
|
|
|
{ |
|
|
|
public string brain_name { get; set; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<bool> dones { get; set; } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Dictionary<string, List<float>> value { get; set; } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
public Dictionary<string, float> parameters { get; set; } |
|
|
|
|
|
|
|
public bool train_model { get; set; } |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
throw new UnityAgentsException("One of the brains was set isExternal" + |
|
|
|
" but Unity was unable to read the" + |
|
|
|
" arguments passed at launch"); |
|
|
|
throw new UnityAgentsException("One of the brains was set to " + |
|
|
|
"External mode or broadcast" + |
|
|
|
" but Unity was unable to read the" + |
|
|
|
" arguments passed at launch"); |
|
|
|
} |
|
|
|
|
|
|
|
messageHolder = new byte[messageLength]; |
|
|
|
|
|
|
AcademyParameters accParamerters = new AcademyParameters(); |
|
|
|
accParamerters.brainParameters = new List<BrainParameters>(); |
|
|
|
accParamerters.brainNames = new List<string>(); |
|
|
|
accParamerters.externalBrainNames = new List<string>(); |
|
|
|
if (b.brainType == BrainType.External) |
|
|
|
{ |
|
|
|
accParamerters.externalBrainNames.Add(b.gameObject.name); |
|
|
|
} |
|
|
|
} |
|
|
|
accParamerters.AcademyName = academy.gameObject.name; |
|
|
|
accParamerters.resetParameters = academy.resetParameters; |
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// Sends Academy parameters to external agent
|
|
|
|
private void SendParameters(AcademyParameters envParams) |
|
|
|
private void SendParameters(AcademyParameters envParams) |
|
|
|
{ |
|
|
|
string envMessage = JsonConvert.SerializeObject(envParams, Formatting.Indented); |
|
|
|
sender.Send(Encoding.ASCII.GetBytes(envMessage)); |
|
|
|
|
|
|
List<float> concatenatedRewards = new List<float>(); |
|
|
|
List<float> concatenatedMemories = new List<float>(); |
|
|
|
List<bool> concatenatedDones = new List<bool>(); |
|
|
|
List<float> concatenatedActions = new List<float>(); |
|
|
|
Dictionary<int, float[]> collectedActions = brain.CollectActions(); |
|
|
|
|
|
|
|
foreach (int id in current_agents[brainName]) |
|
|
|
{ |
|
|
|
|
|
|
concatenatedDones.Add(collectedDones[id]); |
|
|
|
concatenatedActions = concatenatedActions.Concat(collectedActions[id].ToList()).ToList(); |
|
|
|
} |
|
|
|
StepMessage message = new StepMessage() |
|
|
|
{ |
|
|
|
|
|
|
rewards = concatenatedRewards, |
|
|
|
//actions = actionDict,
|
|
|
|
actions = concatenatedActions, |
|
|
|
memories = concatenatedMemories, |
|
|
|
dones = concatenatedDones |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
foreach (Brain brain in brains) |
|
|
|
{ |
|
|
|
string brainName = brain.gameObject.name; |
|
|
|
if (brain.brainType == BrainType.External) |
|
|
|
{ |
|
|
|
string brainName = brain.gameObject.name; |
|
|
|
Dictionary<int, float[]> actionDict = new Dictionary<int, float[]>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
{ |
|
|
|
if (brain.brainParameters.actionSpaceType == StateType.continuous) |
|
|
|
Dictionary<int, float[]> actionDict = new Dictionary<int, float[]>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
actionDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.action[brainName].GetRange(i * brain.brainParameters.actionSize, brain.brainParameters.actionSize).ToArray()); |
|
|
|
if (brain.brainParameters.actionSpaceType == StateType.continuous) |
|
|
|
{ |
|
|
|
actionDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.action[brainName].GetRange(i * brain.brainParameters.actionSize, brain.brainParameters.actionSize).ToArray()); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
actionDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.action[brainName].GetRange(i, 1).ToArray()); |
|
|
|
} |
|
|
|
else |
|
|
|
storedActions[brainName] = actionDict; |
|
|
|
|
|
|
|
Dictionary<int, float[]> memoryDict = new Dictionary<int, float[]>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
actionDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.action[brainName].GetRange(i, 1).ToArray()); |
|
|
|
memoryDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.memory[brainName].GetRange(i * brain.brainParameters.memorySize, brain.brainParameters.memorySize).ToArray()); |
|
|
|
} |
|
|
|
storedActions[brainName] = actionDict; |
|
|
|
storedMemories[brainName] = memoryDict; |
|
|
|
Dictionary<int, float[]> memoryDict = new Dictionary<int, float[]>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
{ |
|
|
|
memoryDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.memory[brainName].GetRange(i * brain.brainParameters.memorySize, brain.brainParameters.memorySize).ToArray()); |
|
|
|
} |
|
|
|
storedMemories[brainName] = memoryDict; |
|
|
|
|
|
|
|
Dictionary<int, float> valueDict = new Dictionary<int, float>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
{ |
|
|
|
valueDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.value[brainName][i]); |
|
|
|
Dictionary<int, float> valueDict = new Dictionary<int, float>(); |
|
|
|
for (int i = 0; i < current_agents[brainName].Count; i++) |
|
|
|
{ |
|
|
|
valueDict.Add(current_agents[brainName][i], |
|
|
|
agentMessage.value[brainName][i]); |
|
|
|
} |
|
|
|
storedValues[brainName] = valueDict; |
|
|
|
storedValues[brainName] = valueDict; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|