Dictionary < string , Dictionary < int , float [ ] > > storedMemories ;
Dictionary < string , Dictionary < int , float > > storedValues ;
// For Messages
List < float > concatenatedStates = new List < float > ( 1 0 2 4 ) ;
List < float > concatenatedRewards = new List < float > ( 3 2 ) ;
List < float > concatenatedMemories = new List < float > ( 1 0 2 4 ) ;
List < bool > concatenatedDones = new List < bool > ( 3 2 ) ;
List < float > concatenatedActions = new List < float > ( 1 0 2 4 ) ;
private int comPort ;
Socket sender ;
byte [ ] messageHolder ;
const string api = "API-2" ;
private class StepMessage
/// Placeholder for state information to send.
[System.Serializable]
public struct StepMessage
public string brain_name { get ; set ; }
public List < int > agents { get ; set ; }
public List < float > states { get ; set ; }
public List < float > rewards { get ; set ; }
public List < float > actions { get ; set ; }
public string brain_name ;
public List < int > agents ;
public List < float > states ;
public List < float > rewards ;
public List < float > actions ;
public List < float > memories ;
public List < bool > dones ;
}
public List < float > memories { get ; set ; }
StepMessage sMessage ;
string sMessageString ;
public List < bool > dones { get ; set ; }
}
string rMessage ;
private class AgentMessage
/// Placeholder for returned message.
struct AgentMessage
private class ResetParametersMessage
/// Placeholder for reset parameter message
struct ResetParametersMessage
public bool train_model { get ; set ; }
}
hasSentState [ brain . gameObject . name ] = false ;
}
public bool CommunicatorHandShake ( ) {
/// Attempts to make handshake with external API.
public bool CommunicatorHandShake ( )
{
try
{
ReadArgs ( ) ;
sender = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
sender . Connect ( "localhost" , comPort ) ;
AcademyParameters accParamerters = new AcademyParameters ( ) ;
var accParamerters = new AcademyParameters ( ) ;
accParamerters . brainParameters = new List < BrainParameters > ( ) ;
accParamerters . brainNames = new List < string > ( ) ;
accParamerters . externalBrainNames = new List < string > ( ) ;
accParamerters . resetParameters = academy . resetParameters ;
SendParameters ( accParamerters ) ;
sMessage = new StepMessage ( ) ;
void HandleLog ( string logString , string stackTrace , LogType type )
{
void HandleLog ( string logString , string stackTrace , LogType type )
{
}
}
/// Listens to the socket for a command and returns the corresponding
/// External Command.
public Dictionary < string , float > GetResetParameters ( )
{
sender . Send ( Encoding . ASCII . GetBytes ( "CONFIG_REQUEST" ) ) ;
ResetParametersMessage resetParams = JsonConvert . DeserializeObject < ResetParametersMessage > ( Receive ( ) ) ;
Receive ( ) ;
var resetParams = JsonConvert . DeserializeObject < ResetParametersMessage > ( rMessage ) ;
academy . isInference = ! resetParams . train_model ;
return resetParams . parameters ;
}
private void ReadArgs ( )
{
string [ ] args = System . Environment . GetCommandLineArgs ( ) ;
string inputPort = "" ;
var inputPort = "" ;
for ( int i = 0 ; i < args . Length ; i + + )
{
if ( args [ i ] = = "--port" )
}
/// Receives messages from external agent
private string Receive ( )
private void Receive ( )
string message = Encoding . ASCII . GetString ( messageHolder , 0 , location ) ;
return message ;
rMessage = Encoding . ASCII . GetString ( messageHolder , 0 , location ) ;
/// Ends connection and closes environment
private void OnApplicationQuit ( )
return bytes ;
}
private byte [ ] AppendLength ( byte [ ] input ) {
private byte [ ] AppendLength ( byte [ ] input )
{
byte [ ] newArray = new byte [ input . Length + 4 ] ;
input . CopyTo ( newArray , 4 ) ;
System . BitConverter . GetBytes ( input . Length ) . CopyTo ( newArray , 0 ) ;
/// Collects the information from the brains and sends it accross the socket
public void giveBrainInfo ( Brain brain )
{
string brainName = brain . gameObject . name ;
var brainName = brain . gameObject . name ;
List < float > concatenatedStates = new List < float > ( ) ;
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 , List < Camera > > collectedObservations = brain . CollectObservations ( ) ;
Dictionary < int , List < float > > collectedStates = brain . CollectStates ( ) ;
Dictionary < int , float > collectedRewards = brain . CollectRewards ( ) ;
Dictionary < int , float [ ] > collectedMemories = brain . CollectMemories ( ) ;
Dictionary < int , bool > collectedDones = brain . CollectDones ( ) ;
Dictionary < int , float [ ] > collectedActions = brain . CollectActions ( ) ;
brain . CollectEverything ( ) ;
concatenatedStates . Clear ( ) ;
concatenatedRewards . Clear ( ) ;
concatenatedMemories . Clear ( ) ;
concatenatedDones . Clear ( ) ;
concatenatedActions . Clear ( ) ;
concatenatedStates = concatenatedStates . Concat ( collectedStates [ id ] ) . ToList ( ) ;
concatenatedRewards . Add ( collectedRewards [ id ] ) ;
concatenatedMemories = concatenatedMemories . Concat ( collectedMemories [ id ] . ToList ( ) ) . ToList ( ) ;
concatenatedDones . Add ( collectedDones [ id ] ) ;
concatenatedActions = concatenatedActions . Concat ( collectedActions [ id ] . ToList ( ) ) . ToList ( ) ;
concatenatedStates . AddRange ( brain . currentStates [ id ] ) ;
concatenatedRewards . Add ( brain . currentRewards [ id ] ) ;
concatenatedMemories . AddRange ( brain . currentMemories [ id ] . ToList ( ) ) ;
concatenatedDones . Add ( brain . currentDones [ id ] ) ;
concatenatedActions . AddRange ( brain . currentActions [ id ] . ToList ( ) ) ;
StepMessage message = new StepMessage ( )
{
brain_name = brainName ,
agents = current_agents [ brainName ] ,
states = concatenatedStates ,
rewards = concatenatedRewards ,
actions = concatenatedActions ,
memories = concatenatedMemories ,
dones = concatenatedDones
} ;
string envMessage = JsonConvert . SerializeObject ( message , Formatting . Indented ) ;
sender . Send ( AppendLength ( Encoding . ASCII . GetBytes ( envMessage ) ) ) ;
sMessage . brain_name = brainName ;
sMessage . agents = current_agents [ brainName ] ;
sMessage . states = concatenatedStates ;
sMessage . rewards = concatenatedRewards ;
sMessage . actions = concatenatedActions ;
sMessage . memories = concatenatedMemories ;
sMessage . dones = concatenatedDones ;
sMessageString = JsonUtility . ToJson ( sMessage ) ;
sender . Send ( AppendLength ( Encoding . ASCII . GetBytes ( sMessageString ) ) ) ;
Receive ( ) ;
int i = 0 ;
foreach ( resolution res in brain . brainParameters . cameraResolutions )
sender . Send ( AppendLength ( TexToByteArray ( brain . ObservationToTex ( collectedObservations [ id ] [ i ] , res . width , res . height ) ) ) ) ;
sender . Send ( AppendLength ( TexToByteArray ( brain . ObservationToTex ( brain . currentCameras [ id ] [ i ] , res . width , res . height ) ) ) ) ;
Receive ( ) ;
}
i + + ;
{
// TO MODIFY --------------------------------------------
sender . Send ( Encoding . ASCII . GetBytes ( "STEPPING" ) ) ;
string a = Receive ( ) ;
AgentMessage agentMessage = JsonConvert . DeserializeObject < AgentMessage > ( a ) ;
Receive ( ) ;
var agentMessage = JsonConvert . DeserializeObject < AgentMessage > ( rMessage ) ;
string brainName = brain . gameObject . name ;
var brainName = brain . gameObject . name ;
Dictionary < int , float [ ] > actionDict = new Dictionary < int , float [ ] > ( ) ;
var actionDict = new Dictionary < int , float [ ] > ( ) ;
var memoryDict = new Dictionary < int , float [ ] > ( ) ;
var valueDict = new Dictionary < int , float > ( ) ;
for ( int i = 0 ; i < current_agents [ brainName ] . Count ; i + + )
{
if ( brain . brainParameters . actionSpaceType = = StateType . continuous )
actionDict . Add ( current_agents [ brainName ] [ i ] ,
agentMessage . action [ brainName ] . GetRange ( i , 1 ) . ToArray ( ) ) ;
}
}
storedActions [ brainName ] = actionDict ;
Dictionary < int , float [ ] > memoryDict = new Dictionary < int , float [ ] > ( ) ;
for ( int i = 0 ; i < current_agents [ brainName ] . Count ; i + + )
{
agentMessage . memory [ brainName ] . GetRange ( i * brain . brainParameters . memorySize , brain . brainParameters . memorySize ) . ToArray ( ) ) ;
agentMessage . memory [ brainName ] . GetRange ( i * brain . brainParameters . memorySize , brain . brainParameters . memorySize ) . ToArray ( ) ) ;
valueDict . Add ( current_agents [ brainName ] [ i ] ,
agentMessage . value [ brainName ] [ i ] ) ;
storedActions [ brainName ] = actionDict ;
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 ] ) ;
}
}
}