コード例 #1
0
ファイル: movingFood.cs プロジェクト: rmcmrz/unity-rl
    IEnumerator updateAgents()
    {
        yield return(new WaitForEndOfFrame());

        agents = GameObject.FindGameObjectsWithTag("agent");

        Debug.Log("updating total " + agents.Length);

        //update



        for (int i = 0; i < agents.Length; i++)
        {
            AgentBehaviourRemote AgentBehaviourScript = agents[i].GetComponent <AgentBehaviourRemote>();

            //AgentBehaviourScript.resetNet();

            //AgentBehaviourScript.totalScore = 0f;

            //AgentBehaviourScript.totalScoreHalf = 0f;

            //AgentBehaviourScript.moveCount = 0;

            AgentBehaviourScript.loadNet(evolved[i]);
        }
    }
コード例 #2
0
ファイル: NetworkManager.cs プロジェクト: rmcmrz/unity-rl
    void Start()
    {
        DontDestroyOnLoad(gameObject);

        learningAgent = GameObject.Find("GreenAgent");

        agentBehaviourScript = learningAgent.GetComponent <AgentBehaviourRemote>();


        GameObject go = GameObject.Find("SocketIO");

        socket = go.GetComponent <SocketIOComponent>();
        //myscript = go.GetComponent<ectScript>();
        socket.On("open", ServerOpen);
        socket.On("error", ServerError);
        socket.On("close", ServerClose);
        socket.On("loginSuccess", loginSuccess);
        socket.On("loginUnsuccess", loginUnsuccess);
    }
コード例 #3
0
ファイル: movingFood.cs プロジェクト: rmcmrz/unity-rl
    private void loadEvolved()
    {
        string c = ",";

        char separator = c.ToCharArray()[0];

        for (int i = 0; i < agents.Length; i++)
        {
            AgentBehaviourRemote AgentBehaviourScript = agents[i].GetComponent <AgentBehaviourRemote>();


            string weights = readData("network.agent" + i.ToString());

            string[] weightArray;

            List <double> weightList = new List <double>();

            if (weights != "error")
            {
                weightArray = weights.Split(separator);

                foreach (var weight in weightArray)
                {
                    double item;

                    double.TryParse(weight, out item);

                    weightList.Add(item);
                }

                AgentBehaviourScript.loadNet(weightList);

                Debug.Log("loaded weights " + " " + agents[i].name + " " + weightList.Count + " " + weightList.ElementAt(1));
            }
            else
            {
                Debug.Log("cannot load weights");
            }
        }
    }
コード例 #4
0
ファイル: movingFood.cs プロジェクト: rmcmrz/unity-rl
    public void statistics()
    {
        Debug.Log("agents " + agents.Length);


        AgentBehaviourRemote AgentBehaviourScript = agents[0].GetComponent <AgentBehaviourRemote>();

        //List<double> weights = AgentBehaviourScript.downloadNet();

        //writeData(listToCsv(weights), "AgentWeights");



        //**** begin export stats scores

        string rewardString = "";


        List <int> sum = new List <int>();


        string c = ",";

        char comma = c[0];


        string temp2 = agents[0].GetComponent <AgentBehaviourRemote>().rewardString.TrimEnd();

        temp2 = temp2.Substring(0, temp2.Length - 1);

        string[] tempArray2 = temp2.Split(comma);

        for (int i = 0; i < tempArray2.Length; i++)
        {
            sum.Add(0);
        }

        double replayEntropySum = 0f;

        double replayCuriositySum = 0f;

        double rewardSum = 0f;


        List <double> fitnessList = new List <double>();



        foreach (GameObject agent in agents)

        {
            AgentBehaviourScript = agent.GetComponent <AgentBehaviourRemote>();


            string temp = AgentBehaviourScript.rewardString.TrimEnd();

            temp = temp.Substring(0, temp.Length - 1);

            string[] tempArray = temp.Split(comma);

            List <int> integerList = new List <int>();

            for (int i = 0; i < tempArray.Length; i++)
            {
                int item;

                int.TryParse(tempArray[i], out item);

                integerList.Add(item);
            }

            for (int i = 0; i < sum.Count; i++)
            {
                sum[i] = sum[i] + integerList[i];
            }

            fitnessList.Add(AgentBehaviourScript.getFitness());


            Debug.Log(agent.name + " fitness " + AgentBehaviourScript.getFitness());
        }

        double[] result = new double[tempArray2.Length];

        for (int i = 0; i < sum.Count; i++)
        {
            result[i] = (double)sum.ElementAt(i) / agents.Length;
        }

        rewardString = listToCsv(result.ToList());

        List <int> BestIndexes = new List <int>();


        int index = fitnessList.IndexOf(fitnessList.Max());

        BestIndexes.Add(index);

        fitnessList[index] = -999999f;

        index = fitnessList.IndexOf(fitnessList.Max());

        BestIndexes.Add(index);

        fitnessList[index] = -999999f;

        index = fitnessList.IndexOf(fitnessList.Max());

        BestIndexes.Add(index);

        fitnessList[index] = -999999f;


        List <double> firstCandidate = new List <double>();

        List <double> secondCandidate = new List <double>();

        List <double> thirdCandidate = new List <double>();

        for (int i = 0; i < BestIndexes.Count; i++)
        {
            AgentBehaviourScript = agents[BestIndexes[i]].GetComponent <AgentBehaviourRemote>();

            Debug.Log(i + " " + agents[BestIndexes[i]].name + " " + AgentBehaviourScript.getFitness());

            if (i == 0)
            {
                firstCandidate = AgentBehaviourScript.downloadNet();
            }

            if (i == 1)
            {
                secondCandidate = AgentBehaviourScript.downloadNet();
            }

            if (i == 2)
            {
                thirdCandidate = AgentBehaviourScript.downloadNet();
            }


            replayEntropySum += AgentBehaviourScript.getReplayEntropy();

            replayCuriositySum += AgentBehaviourScript.getReplayCuriosity();

            rewardSum += AgentBehaviourScript.getFitness();
        }

        string replayEntropy = (replayEntropySum / 3).ToString();

        string replayCuriosity = (replayCuriositySum / 3).ToString();

        string replayReward = (rewardSum / 3).ToString();



        writeDataAppend((replayEntropy + ",").TrimEnd(), "agents.Entropy");

        writeDataAppend((replayCuriosity + ",").TrimEnd(), "agents.Curiosity");

        writeDataAppend((replayReward + ",").TrimEnd(), "agents.Reward");

        double replayCuriositySumAll = 0f;

        double rewardSumAll = 0f;

        foreach (var a in agents)
        {
            AgentBehaviourScript = a.GetComponent <AgentBehaviourRemote>();

            replayCuriositySumAll += AgentBehaviourScript.getReplayCuriosity();

            rewardSumAll += AgentBehaviourScript.getFitness();
        }


        writeDataAppend((replayCuriositySumAll / agents.Length + ",").TrimEnd(), "agents.CuriosityAll");

        writeDataAppend((rewardSumAll / agents.Length + ",").TrimEnd(), "agents.RewardAll");


        for (int i = 0; i < agents.Length; i++)
        {
            DestroyImmediate(agents[i]);
        }

        for (int i = 0; i < agents.Length; i++)
        {
            GameObject temp = Instantiate(singleAgent, positions[i], Quaternion.identity) as GameObject;

            temp.name = "agent" + i.ToString();

            temp.tag = "agent";

            agents[i] = temp;
        }

        //evolving

        evolved[0] = mating(firstCandidate, secondCandidate);

        evolved[1] = mating(secondCandidate, firstCandidate);

        evolved[2] = mating(firstCandidate, thirdCandidate);

        evolved[3] = mating(secondCandidate, thirdCandidate);

        evolved[4] = mutation(thirdCandidate);

        evolved[5] = mutation(firstCandidate);



        StartCoroutine(updateAgents());

        /*
         *
         * Debug.Log("first candidate");
         *
         * printListDouble(firstCandidate);
         *
         * Debug.Log("second candidate");
         *
         * printListDouble(secondCandidate);
         *
         * Debug.Log("evolved 0");
         *
         * printListDouble(evolved[0]);
         *
         * Debug.Log("evolved 1");
         *
         * printListDouble(evolved[1]);
         *
         * Debug.Log("evolved 2");
         *
         * printListDouble(evolved[2]);
         *
         * Debug.Log("evolved 3");
         *
         * printListDouble(evolved[3]);
         *
         */


        saveEvolved();
    }