示例#1
0
    /// Contains the logic for the initializtation of the socket.
    public void InitializeCommunicator()
    {
        //Application.logMessageReceived += HandleLog;
        //logPath = Path.GetFullPath(".") + "/unity-environment.log";
        //logWriter = new StreamWriter(logPath, false);
        //logWriter.WriteLine(System.DateTime.Now.ToString());
        //logWriter.WriteLine(" ");
        //logWriter.Close();
        messageHolder = new byte[messageLength];
        lengthHolder  = new byte[4];

        // Create a TCP/IP  socket.
        sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        UnityEngine.Debug.Log("Connecting to : localhost:" + comPort);
        sender.Connect("localhost", comPort);

        var accParamerters = new AcademyParameters();

        accParamerters.brainParameters    = new List <BrainParameters>();
        accParamerters.brainNames         = new List <string>();
        accParamerters.externalBrainNames = new List <string>();
        accParamerters.apiNumber          = _version_;
        accParamerters.logPath            = logPath;
        foreach (Brain b in brains)
        {
            accParamerters.brainParameters.Add(b.brainParameters);
            accParamerters.brainNames.Add(b.gameObject.name);
            if (b.brainType == BrainType.External)
            {
                accParamerters.externalBrainNames.Add(b.gameObject.name);
            }
        }
        accParamerters.AcademyName     = academy.gameObject.name;
        accParamerters.resetParameters = academy.resetParameters;

        SendParameters(accParamerters);

        sMessage                       = new StepMessage();
        sMessage.agents                = new List <int>(defaultNumAgents);
        sMessage.vectorObservations    = new List <float>(defaultNumAgents * defaultNumObservations);
        sMessage.rewards               = new List <float>(defaultNumAgents);
        sMessage.memories              = new List <float>(defaultNumAgents * defaultNumObservations);
        sMessage.dones                 = new List <bool>(defaultNumAgents);
        sMessage.previousVectorActions = new List <float>(defaultNumAgents * defaultNumObservations);
        sMessage.previousTextActions   = new List <string>(defaultNumAgents);
        sMessage.maxes                 = new List <bool>(defaultNumAgents);
        sMessage.textObservations      = new List <string>(defaultNumAgents);

        // Initialize the list of brains the Communicator must listen to
        // Issue : This assumes all brains are broadcasting.
        foreach (string k in accParamerters.brainNames)
        {
            current_agents[k] = new List <Agent>(defaultNumAgents);
            hasSentState[k]   = false;
            triedSendState[k] = false;
        }
    }
示例#2
0
        private StepMessage GetStepMessage(string messagetext)
        {
            StepMessage message = new StepMessage();

            message.StepNumber = 1;
            message.StepName   = string.Format("Step:{0}", message.StepNumber);
            message.Message    = messagetext;
            return(message);
        }
示例#3
0
/// <summary>
/// Delete Step
/// </summary>
/// <param name="request">step request.</param>
/// <returns>Step message.</returns>
        public StepMessage DeleteStep(StepRequest request)
        {
            StepMessage message = new StepMessage();

            try
            {
                serviceStep.DeleteStep(request.ToPivot());
                message.OperationSuccess = true;
            }
            catch (Exception e)
            {
                message.ErrorType    = ErrorType.TechnicalError;
                message.ErrorMessage = e.Message;
            }
            return(message);
        }
示例#4
0
/// <summary>
/// Get list of Step
/// </summary>
/// <returns>Step message.</returns>
        public StepMessage GetAllStep()
        {
            StepMessage message = new StepMessage();

            try
            {
                message = serviceStep.GetAllStep().ToMessage();
                message.OperationSuccess = true;
            }
            catch (Exception e)
            {
                message.ErrorType    = ErrorType.TechnicalError;
                message.ErrorMessage = e.Message;
            }
            return(message);
        }
示例#5
0
        public IHttpActionResult UpdateStep(StepRequest request)
        {
            StepMessage   message = new StepMessage();
            List <string> errors  = ValidateUpdateStep(request);

            if (errors != null && errors.Any())
            {
                message.ErrorMessage     = StepMessageResource.ValidationErrors;
                message.ErrorType        = ErrorType.ValidationError;
                message.Errors           = new List <string>();
                message.OperationSuccess = false;
                message.Errors.AddRange(errors);
            }
            else
            {
                message = _serviceStepClient.UpdateStep(request);
            }
            return(Json(message));
        }
示例#6
0
        private void SaveStepMessage(StepMessage message)
        {
            //XmlSerializer ser = new XmlSerializer(typeof(StepMessage));
            //bool bAppend = true ;

            //TextWriter writer = new StreamWriter(string.Format(@"{0}\message.xml",LogFolder), bAppend);

            //ser.Serialize(writer, message);

            //writer.Close();
            //string sb = string.Format("{1}", message.Message);
            if (!File.Exists(string.Format(@"{0}\message.csv", LogFolder)))
            {
                StreamWriter w = File.CreateText(string.Format(@"{0}\message.csv", LogFolder));
                w.WriteLine(message.Message);
                w.Close();
            }
            else
            {
                StreamWriter w = File.AppendText(string.Format(@"{0}\message.csv", LogFolder));
                w.WriteLine(message.Message);
                w.Close();
            }
        }
示例#7
0
    /// Collects the information from the brains and sends it accross the socket
    public void giveBrainInfo(Brain brain)
    {
        string brainName = brain.gameObject.name;

        current_agents[brainName] = new List <int>(brain.agents.Keys);
        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();

        foreach (int id in current_agents[brainName])
        {
            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();
        }
        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)));
        Receive();
        int i = 0;

        foreach (resolution res in brain.brainParameters.cameraResolutions)
        {
            foreach (int id in current_agents[brainName])
            {
                sender.Send(AppendLength(TexToByteArray(brain.ObservationToTex(collectedObservations[id][i], res.width, res.height))));
                Receive();
            }
            i++;
        }

        hasSentState[brainName] = true;

        if (hasSentState.Values.All(x => x))
        {
            // if all the brains listed have sent their state
            sender.Send(Encoding.ASCII.GetBytes((academy.done ? "True" : "False")));
            List <string> brainNames = hasSentState.Keys.ToList();
            foreach (string k in brainNames)
            {
                hasSentState[k] = false;
            }
        }
    }
示例#8
0
        public IHttpActionResult FindStep(StepRequest request)
        {
            StepMessage message = serviceStepClient.FindStep(request);

            return(Json(message));
        }
示例#9
0
        public IHttpActionResult GetAllStep()
        {
            StepMessage message = serviceStepClient.GetAllStep();

            return(Json(message));
        }