예제 #1
0
 public override void CopyWeightsFromTemplate(FilterAgentConvolve templateAgent)
 {
     for (int i = 0; i < layerWeightsArrayRT.Length; i++)
     {
         int kernelCopy3D = weightMutationComputeShader.FindKernel("CSCopy3D");
         weightMutationComputeShader.SetTexture(kernelCopy3D, "SourceTex3D", templateAgent.layerWeightsArrayRT[i]);
         weightMutationComputeShader.SetTexture(kernelCopy3D, "ResultTex3D", layerWeightsArrayRT[i]);
         // Run compute shader for each output Pixel:
         weightMutationComputeShader.Dispatch(kernelCopy3D, layerWeightsArrayRT[i].width, layerWeightsArrayRT[i].height, layerWeightsArrayRT[i].volumeDepth);
     }
 }
예제 #2
0
    public override void MutateGenome(FilterAgentConvolve templateAgent, float mutationRate, float mutationSize)
    {
        for (int i = 0; i < layerWeightsArrayRT.Length; i++)
        {
            int kernelMutate = weightMutationComputeShader.FindKernel("CSMain");
            weightMutationComputeShader.SetTexture(kernelMutate, "SourceTex3D", templateAgent.layerWeightsArrayRT[i]);
            weightMutationComputeShader.SetTexture(kernelMutate, "ResultTex3D", layerWeightsArrayRT[i]);

            weightMutationComputeShader.SetFloat("_SeedA", UnityEngine.Random.Range(0f, 100f));
            weightMutationComputeShader.SetFloat("_SeedB", UnityEngine.Random.Range(0f, 100f));
            weightMutationComputeShader.SetFloat("_MutationRate", mutationRate);
            weightMutationComputeShader.SetFloat("_MutationSize", mutationSize);
            weightMutationComputeShader.Dispatch(kernelMutate, layerWeightsArrayRT[i].width, layerWeightsArrayRT[i].height, layerWeightsArrayRT[i].volumeDepth);
            //Debug.Log("Mutate " + i.ToString());
        }

        /*int kernelMutate = weightMutationComputeShader.FindKernel("CSMain");
         * weightMutationComputeShader.SetTexture(kernelMutate, "Source", templateAgent.layerWeightsArrayRT[0]);
         * weightMutationComputeShader.SetTexture(kernelMutate, "Result", layerWeightsArrayRT[0]);
         * //weightMutationComputeShader.SetInt("_Gen", curGeneration);
         * weightMutationComputeShader.SetFloat("_SeedA", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_SeedB", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_MutationRate", mutationRate);
         * weightMutationComputeShader.SetFloat("_MutationSize", mutationSize);
         * weightMutationComputeShader.Dispatch(kernelMutate, layerWeightsArrayRT[0].width, layerWeightsArrayRT[0].height, layerWeightsArrayRT[0].volumeDepth);
         *
         * int kernelMutate2 = weightMutationComputeShader.FindKernel("CSMain");
         * weightMutationComputeShader.SetTexture(kernelMutate2, "Source", templateAgent.layerWeightsArrayRT[2]);
         * weightMutationComputeShader.SetTexture(kernelMutate2, "Result", layerWeightsArrayRT[2]);
         * //weightMutationComputeShader.SetInt("_Gen", curGeneration);
         * weightMutationComputeShader.SetFloat("_SeedA", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_SeedB", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_MutationRate", mutationRate);
         * weightMutationComputeShader.SetFloat("_MutationSize", mutationSize);
         * weightMutationComputeShader.Dispatch(kernelMutate2, layerWeightsArrayRT[2].width, layerWeightsArrayRT[2].height, layerWeightsArrayRT[2].volumeDepth);
         *
         * int kernelMutate3 = weightMutationComputeShader.FindKernel("CSMain");
         * weightMutationComputeShader.SetTexture(kernelMutate3, "Source", templateAgent.layerWeightsArrayRT[3]);
         * weightMutationComputeShader.SetTexture(kernelMutate3, "Result", layerWeightsArrayRT[3]);
         * //weightMutationComputeShader.SetInt("_Gen", curGeneration);
         * weightMutationComputeShader.SetFloat("_SeedA", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_SeedB", UnityEngine.Random.Range(0f, 100f));
         * weightMutationComputeShader.SetFloat("_MutationRate", mutationRate);
         * weightMutationComputeShader.SetFloat("_MutationSize", mutationSize);
         * weightMutationComputeShader.Dispatch(kernelMutate3, layerWeightsArrayRT[3].width, layerWeightsArrayRT[3].height, layerWeightsArrayRT[3].volumeDepth);
         */
    }
예제 #3
0
    private void FirstTimeInit()
    {
        //weightsRT = new RenderTexture[populationSize];
        //swapWeightsRT = new RenderTexture[populationSize];

        //generatedTexturesRT = new RenderTexture[populationSize];
        //sourceTexturesRT = new RenderTexture[populationSize];

        //worldDataCB = new ComputeBuffer[populationSize];
        //inputNeuronsCB = new ComputeBuffer[populationSize];
        //outputNeuronsCB = new ComputeBuffer[populationSize];

        // Create Population:
        population     = new FilterAgentConvolve[populationSize];
        tempPopulation = new FilterAgentConvolve[populationSize];

        GenerateReferenceTexture();
        RegenerateSourceNoiseTexture();

        for (int i = 0; i < populationSize; i++)
        {
            // create each agent:

            FilterAgentConvolve filterAgent = new FilterAgentConvolve();
            // Create weight layer textures with initial values
            filterAgent.InitializeGenome(agentBrainComputeShader, weightMutationComputeShader, imageResolution);
            population[i] = filterAgent;


            // Won't be used until selection / reproduction, but will reuse this array to avoid creation/destruction of agents every Generation
            FilterAgentConvolve tempFilterAgent = new FilterAgentConvolve();
            tempFilterAgent.InitializeGenome(agentBrainComputeShader, weightMutationComputeShader, imageResolution);
            tempPopulation[i] = tempFilterAgent;



            //ComputeBuffer inNeuronsCB = new ComputeBuffer(numInputsX * numInputsY, sizeof(float));
            //inputNeuronsCB[i] = inNeuronsCB;
            //ComputeBuffer outNeuronsCB = new ComputeBuffer(numOutputs, sizeof(float));
            //outputNeuronsCB[i] = outNeuronsCB;

            /*RenderTexture weights = new RenderTexture(numInputsX, numInputsY, 1, RenderTextureFormat.ARGBFloat);
             * weights.filterMode = FilterMode.Point;
             * weights.enableRandomWrite = true;
             * weights.autoGenerateMips = true;
             * weights.useMipMap = true;
             * // Initial Weights:
             * Texture2D weightsTex = new Texture2D(numInputsX, numInputsY, TextureFormat.RGBAFloat, true);
             * for (int x = 0; x < numInputsX; x++) {
             *  for (int y = 0; y < numInputsY; y++) {
             *      float randomWeight0 = UnityEngine.Random.Range(-0.1f, 0.1f);
             *      float randomWeight1 = UnityEngine.Random.Range(-0.1f, 0.1f);
             *      float randomWeight2 = UnityEngine.Random.Range(-0.1f, 0.1f);
             *      //randomWeight = 1.0f / 9.0f;
             *      weightsTex.SetPixel(x, y, new Color(randomWeight0, randomWeight1, randomWeight2));
             *  }
             * }
             * weightsTex.Apply();
             * Graphics.Blit(weightsTex, weights);  // initialize Weights matrix
             * weightsRT[i] = weights;
             *
             * RenderTexture swapWeights = new RenderTexture(numInputsX, numInputsY, 1, RenderTextureFormat.ARGBFloat);
             * swapWeights.filterMode = FilterMode.Point;
             * swapWeights.enableRandomWrite = true;
             * swapWeights.autoGenerateMips = true;
             * swapWeights.useMipMap = true;
             * swapWeights.Create();
             * swapWeightsRT[i] = swapWeights;
             *
             * //generatedTexturesRT = new RenderTexture
             * RenderTexture generatedTex = new RenderTexture(imageResX, imageResY, 1, RenderTextureFormat.ARGBFloat);
             * generatedTex.enableRandomWrite = true;
             * generatedTex.filterMode = FilterMode.Bilinear;
             * generatedTex.autoGenerateMips = false;
             * generatedTex.useMipMap = true;
             * generatedTex.Create();
             * generatedTexturesRT[i] = generatedTex;
             *
             * RenderTexture sourceTex = new RenderTexture(imageResX, imageResY, 1, RenderTextureFormat.ARGBFloat);
             * sourceTex.enableRandomWrite = true;
             * sourceTex.filterMode = FilterMode.Bilinear;
             * sourceTex.autoGenerateMips = true;
             * sourceTex.useMipMap = true;
             * //sourceTex.Create();
             * Graphics.Blit(sourceNoiseTexture, sourceTex);
             * sourceTexturesRT[i] = sourceTex;
             */
        }

        CreateDisplayQuads();
        SetTexturesDisplayQuads();


        //testRT = new RenderTexture(imageResX, imageResY, 1, RenderTextureFormat.ARGBFloat);
        //testRT.enableRandomWrite = true;
        //testRT.filterMode = FilterMode.Point;
        //testRT.Create();
        displayMat.SetTexture("_MainTex", sourceNoiseTexture);

        Material bestAgentMat = new Material(displayMat3D);

        displayQuadBestWeights.GetComponent <MeshRenderer>().material = bestAgentMat;
        bestAgentMat.SetTexture("_MainTex", population[0].GetWeights());

        Material refImgMat = new Material(displayMat);

        displayQuadReference.GetComponent <MeshRenderer>().material = refImgMat;
        refImgMat.SetTexture("_MainTex", referenceTexture);


        //Material blitMat = new Material(blitShader);
        //Graphics.Blit(generatedTexturesRT[0], testRT, blitMat);

        //ComputeBuffer tempCB = new ComputeBuffer(1, sizeof(float));
        //float[] tempArray = new float[1];
        //tempArray[0] = 33.7f;
        //tempCB.SetData(tempArray);

        //int kernelFeedForward = agentBrainComputeShader.FindKernel("CSMain");
        //agentBrainComputeShader.SetTexture(kernelFeedForward, "sourceNoiseTex", sourceNoiseTexture);
        //agentBrainComputeShader.SetBuffer(kernelFeedForward, "inputNeuronsCB", inputNeuronsCB[i]);
        //agentBrainComputeShader.SetBuffer(kernelFeedForward, "outputNeuronsCB", outputNeuronsCB[i]);
        //agentBrainComputeShader.SetTexture(kernelFeedForward, "weightsRT", weightsRT[i]);
        //agentBrainComputeShader.SetBuffer(kernelFeedForward, "tempCB", tempCB);
        //agentBrainComputeShader.SetTexture(kernelFeedForward, "ResultRT", testRT);
        //agentBrainComputeShader.Dispatch(kernelFeedForward, imageResX, imageResY, 1);

        //tempCB.GetData(tempArray);
        //Debug.Log("" + tempArray[0].ToString());
        //displayMat.SetTexture("_MainTex", testRT);



        //inputNeuronsCB = new ComputeBuffer(numInputs, sizeof(float));
        //outputNeuronsCB = new ComputeBuffer(numOutputs, sizeof(float));
        //weightsRT = new RenderTexture(numInputs, numOutputs, 1, RenderTextureFormat.ARGBFloat);
        //worldDataCB = new ComputeBuffer(7, sizeof(float)); // agentPos xy + targetPos xy + bias = 5

        /*inputMappingCB = new ComputeBuffer(numInputs, sizeof(int));
         * outputMappingCB = new ComputeBuffer(numOutputs, sizeof(int));
         *
         * int[] inputMappingArray = new int[numInputs];
         * inputMappingArray[0] = 0;  // index of worldDataBuffer to read from
         * inputMappingArray[1] = 1;
         * inputMappingArray[2] = 4;
         * inputMappingArray[3] = 5;
         * inputMappingArray[4] = 6;
         * inputMappingCB.SetData(inputMappingArray);
         *
         * int[] outputMappingArray = new int[numOutputs];
         * outputMappingArray[0] = 2;  // index of worldDataBuffer to write to
         * outputMappingArray[1] = 3;
         * outputMappingCB.SetData(outputMappingArray);
         */

        /*Texture2D weightsTex = new Texture2D(numInputs, numOutputs, TextureFormat.RGBAFloat, false);
         * for(int x = 0; x < numInputs; x++) {
         *  for(int y = 0; y < numOutputs; y++) {
         *      float randomWeight = UnityEngine.Random.Range(-1f, 1f);
         *      weightsTex.SetPixel(x, y, new Color(randomWeight, 0f, 0f));
         *  }
         * }*/
        //weightsTex.Apply();
        //Graphics.Blit(weightsTex, weightsRT);  // initialize Weights matrix
        // weightsRT.filterMode = FilterMode.Point;

        //displayMat.SetTexture("_MainTex", weightsRT);

        // World Data and World <--> Brain Mappings:

        /*float[] worldDataArray = new float[7];
         * worldDataArray[0] = 0f;  // agentPosX
         * worldDataArray[1] = 0f;  // agentPosY
         * worldDataArray[2] = 0f;  // agentThrottleX
         * worldDataArray[3] = 0f;  // agentThrottleY
         * worldDataArray[4] = 1f;  // targetPosX
         * worldDataArray[5] = -1f;  // targetPosY
         * worldDataArray[6] = 1f;  // bias
         * worldDataArray[7] = 0f;  // timestep
         * worldDataArray[8] = 0f; // time of capture
         * worldDataCB.SetData(worldDataArray);*/
    }
예제 #4
0
 public virtual void MutateGenome(FilterAgentConvolve templateAgent, float mutationRate, float mutationSize)
 {
 }
예제 #5
0
 public virtual void CopyWeightsFromTemplate(FilterAgentConvolve templateAgent)
 {
 }