public NeuronNEAT(int nodeID, GeneNodeNEAT.GeneNodeType type, TransferFunctions.TransferFunction function) {
     id = nodeID;
     nodeType = type;
     currentValue = new float[1];
     incomingConnectionsList = new List<ConnectionNEAT>();
     activationFunction = function;
 }
Beispiel #2
0
 public NeuronNEAT(int nodeID, GeneNodeNEAT.GeneNodeType type, TransferFunctions.TransferFunction function)
 {
     id                      = nodeID;
     nodeType                = type;
     currentValue            = new float[1];
     incomingConnectionsList = new List <ConnectionNEAT>();
     activationFunction      = function;
 }
 public GeneNodeNEAT(int id, GeneNodeType nodeType, TransferFunctions.TransferFunction function, int inno, int recurse, bool mirror, int channelNum) {
     this.id = id;
     this.nodeType = nodeType;
     this.activationFunction = function;
     sourceAddonInno = inno;
     sourceAddonRecursionNum = recurse;
     if (mirror) {
         sourceAddonRecursionNum += 10;
     }
     sourceAddonChannelNum = channelNum;
 }
 public GeneNodeNEAT(int id, GeneNodeType nodeType, TransferFunctions.TransferFunction function, int inno, int recurse, bool mirror, int channelNum)
 {
     this.id                 = id;
     this.nodeType           = nodeType;
     this.activationFunction = function;
     sourceAddonInno         = inno;
     sourceAddonRecursionNum = recurse;
     if (mirror)
     {
         sourceAddonRecursionNum += 10;
     }
     sourceAddonChannelNum = channelNum;
 }
    public override Genome InitializeRandomBrain(int[] layerSizes)
    {
        // Validate the input data

        Genome genome = new Genome();
        genome.layerSizes = layerSizes; // save the brain layer dimensions inside the agent's genome
        int biasGenomeLength = 0;
        int weightGenomeLength = 0;
        int biasIndex = 0;
        int weightIndex = 0;

        // Initialize network layers
        layerCount = layerSizes.Length - 1;  // doesn't include input layer, so one less than layerSizes
        inputSize = layerSizes[0];  // set size of input layer

        layerSize = new int[layerCount]; // keeps track of the number of nodes in each layer
        for (int i = 0; i < layerCount; i++) // transferring layer sizes function input (which included input layer) into new variable that does not include input layer nodes
            layerSize[i] = layerSizes[i + 1];

        // OG transfer function code:
        //transferFunction = new TransferFunction[layerCount];
        //for (int i = 0; i < layerCount; i++) // transferring transferFunction function input (which included input layer) into new variable that does not include input layer
        //	transferFunction[i] = transferFunctions[i + 1];

        // Start dimensioning arrays
        bias = new float[layerCount][];  // setting length of first array, which specifies number of 'real' layers
        previousBiasDelta = new float[layerCount][];
        delta = new float[layerCount][];
        layerOutput = new float[layerCount][];
        layerInput = new float[layerCount][];
        transferFunctions = new TransferFunctions.TransferFunction[layerCount][];

        weight = new float[layerCount][][]; // setting length of first array, which specifies number of 'real' layers
        previousWeightDelta = new float[layerCount][][];

        // Fill 2 dimensional arrays
        for (int l = 0; l < layerCount; l++) // iterate through second arrays and set number of nodes (second array)
        {
            bias[l] = new float[layerSize[l]];
            biasGenomeLength += layerSize[l];  // keep track of bias Genome Length

            previousBiasDelta[l] = new float[layerSize[l]];
            delta[l] = new float[layerSize[l]];
            layerOutput[l] = new float[layerSize[l]];
            layerInput[l] = new float[layerSize[l]];
            transferFunctions[l] = new TransferFunctions.TransferFunction[layerSize[l]];

            weight[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];  // if l (layer) is 0 (first hidden layer), then it needs weights fromt he input layer, which is not included in layer sizes
            previousWeightDelta[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];  //.... so it uses the inputSize variable instead, which holds the nubmer of nodes int he input layer
            // .... else it uses the layerSize of the previous layer

            // need to set the size of the current layer's nodes that we're concerned with (third array)
            // iterates through the (second array) previous layer's nodes and sets the target nodes in the current layer
            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++)  // if l = 0, previous layer is inputLayer, else: layer - 1
            {
                weight[l][i] = new float[layerSize[l]];
                weightGenomeLength += layerSize[l];

                previousWeightDelta[l][i] = new float[layerSize[l]];
            }
        }

        //Debug.Log ("BiasGenomeLength= " + biasGenomeLength.ToString () + ", weightGenomeLength= " + weightGenomeLength.ToString ());
        genome.genomeBiases = new float[biasGenomeLength];
        genome.geneFunctions = new TransferFunctions.TransferFunction[biasGenomeLength];
        genome.genomeWeights = new float[weightGenomeLength];

        // Initialize the weights
        for (int l = 0; l < layerCount; l++)  // iterate through the 'real' layers
        {
            for (int j = 0; j < layerSize[l]; j++)  // for 2-dimensional arrays, iterate through current layer
            {
                bias[l][j] = Gaussian.GetRandomGaussian();  // normally distributed
                //bias[l][j] = 0f;  // start zeroed
                genome.genomeBiases[biasIndex] = bias[l][j];
                transferFunctions[l][j] = TransferFunctions.TransferFunction.RationalSigmoid;
                genome.geneFunctions[biasIndex] = transferFunctions[l][j];
                biasIndex++;

                previousBiasDelta[l][j] = 0.0f;   // init to 0
                layerOutput[l][j] = 0.0f;
                layerInput[l][j] = 0.0f;
                delta[l][j] = 0.0f;

            }

            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++) // for 3-dimensional arrays, iterate through nodes in previous layer (second array)
            {
                for (int j = 0; j < layerSize[l]; j++)  // iterate through nodes in current layer
                {
                    weight[l][i][j] = Gaussian.GetRandomGaussian(); // init connective weights to standard distribution around 0.0
                    genome.genomeWeights[weightIndex] = weight[l][i][j];
                    weightIndex++;

                    previousWeightDelta[l][i][j] = 0.0f;
                }
            }
        }
        return genome;
    }
 public GeneNodeNEAT(int id, GeneNodeType nodeType, TransferFunctions.TransferFunction function)
 {
     this.id                 = id;
     this.nodeType           = nodeType;
     this.activationFunction = function;
 }
    public override void InitializeBrainFromGenome(Genome genome)
    {
        int genomeBiasIndex    = 0;
        int genomeWeightIndex  = 0;
        int biasGenomeLength   = 0;
        int weightGenomeLength = 0;

        // Initialize network layers
        layerCount = genome.layerSizes.Length - 1; // doesn't include input layer, so one less than layerSizes
        inputSize  = genome.layerSizes[0];         // set size of input layer
        layerSize  = new int[layerCount];          // keeps track of the number of nodes in each layer
        for (int i = 0; i < layerCount; i++)       // transferring layer sizes function input (which included input layer) into new variable that does not include input layer nodes
        {
            layerSize[i] = genome.layerSizes[i + 1];
        }

        // Start dimensioning arrays
        bias = new float[layerCount][];          // setting length of first array, which specifies number of 'real' layers
        previousBiasDelta   = new float[layerCount][];
        delta               = new float[layerCount][];
        layerOutput         = new float[layerCount][];
        layerInput          = new float[layerCount][];
        transferFunctions   = new TransferFunctions.TransferFunction[layerCount][];
        weight              = new float[layerCount][][]; // setting length of first array, which specifies number of 'real' layers
        previousWeightDelta = new float[layerCount][][];
        // Fill 2 dimensional arrays
        for (int l = 0; l < layerCount; l++)         // iterate through second arrays and set number of nodes (second array)
        {
            bias[l]           = new float[layerSize[l]];
            biasGenomeLength += layerSize[l];              // keep track of bias Genome Length

            previousBiasDelta[l] = new float[layerSize[l]];
            delta[l]             = new float[layerSize[l]];
            layerOutput[l]       = new float[layerSize[l]];
            layerInput[l]        = new float[layerSize[l]];
            transferFunctions[l] = new TransferFunctions.TransferFunction[layerSize[l]];

            weight[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];              // if l (layer) is 0 (first hidden layer), then it needs weights fromt he input layer, which is not included in layer sizes
            previousWeightDelta[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][]; //.... so it uses the inputSize variable instead, which holds the nubmer of nodes int he input layer
            // .... else it uses the layerSize of the previous layer

            // need to set the size of the current layer's nodes that we're concerned with (third array)
            // iterates through the (second array) previous layer's nodes and sets the target nodes in the current layer
            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++)              // if l = 0, previous layer is inputLayer, else: layer - 1
            {
                weight[l][i]        = new float[layerSize[l]];
                weightGenomeLength += layerSize[l];

                previousWeightDelta[l][i] = new float[layerSize[l]];
            }
        }

        //genome.genomeBiases = new float[biasGenomeLength];
        genome.geneFunctions = new TransferFunctions.TransferFunction[biasGenomeLength];          // REVISIT HOW TO SAVE TRANSFER FUNCTIONS (static class cna't be saved)
        //genome.genomeWeights = new float[weightGenomeLength];

        // Initialize the weights
        for (int l = 0; l < layerCount; l++)                                                  // iterate through the 'real' layers
        {
            for (int j = 0; j < layerSize[l]; j++)                                            // for 2-dimensional arrays, iterate through current layer
            {
                bias[l][j] = genome.genomeBiases[genomeBiasIndex];                            // normally distributed
                //transferFunctions[l][j] = genome.geneFunctions[genomeBiasIndex];
                transferFunctions[l][j] = TransferFunctions.TransferFunction.RationalSigmoid; // REVISIT HOW TO SAVE TRANSFER FUNCTIONS (static class cna't be saved)
                genomeBiasIndex        += 1;

                //previousBiasDelta[l][j] = 0.0;   // init to 0
                //layerOutput[l][j] = 0.0;
                //layerInput[l][j] = 0.0;
                //delta[l][j] = 0.0;
            }

            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++)     // for 3-dimensional arrays, iterate through nodes in previous layer (second array)
            {
                for (int j = 0; j < layerSize[l]; j++)                            // iterate through nodes in current layer
                {
                    weight[l][i][j]    = genome.genomeWeights[genomeWeightIndex]; // init connective weights to standard distribution around 0.0
                    genomeWeightIndex += 1;
                    //previousWeightDelta[l][i][j] = 0.0;
                }
            }
        }
    }
    public override Genome InitializeBlankBrain(int[] layerSizes)
    {
        // Validate the input data


        Genome genome = new Genome();

        genome.layerSizes = layerSizes;         // save the brain layer dimensions inside the agent's genome
        int biasGenomeLength   = 0;
        int weightGenomeLength = 0;
        int biasIndex          = 0;
        int weightIndex        = 0;

        // Initialize network layers
        layerCount = layerSizes.Length - 1;  // doesn't include input layer, so one less than layerSizes
        inputSize  = layerSizes[0];          // set size of input layer

        layerSize = new int[layerCount];     // keeps track of the number of nodes in each layer
        for (int i = 0; i < layerCount; i++) // transferring layer sizes function input (which included input layer) into new variable that does not include input layer nodes
        {
            layerSize[i] = layerSizes[i + 1];
        }

        // OG transfer function code:
        //transferFunction = new TransferFunction[layerCount];
        //for (int i = 0; i < layerCount; i++) // transferring transferFunction function input (which included input layer) into new variable that does not include input layer
        //	transferFunction[i] = transferFunctions[i + 1];

        // Start dimensioning arrays
        bias = new float[layerCount][];          // setting length of first array, which specifies number of 'real' layers
        previousBiasDelta = new float[layerCount][];
        delta             = new float[layerCount][];
        layerOutput       = new float[layerCount][];
        layerInput        = new float[layerCount][];
        transferFunctions = new TransferFunctions.TransferFunction[layerCount][];

        weight = new float[layerCount][][];         // setting length of first array, which specifies number of 'real' layers
        previousWeightDelta = new float[layerCount][][];

        // Fill 2 dimensional arrays
        for (int l = 0; l < layerCount; l++)         // iterate through second arrays and set number of nodes (second array)
        {
            bias[l]           = new float[layerSize[l]];
            biasGenomeLength += layerSize[l];              // keep track of bias Genome Length

            previousBiasDelta[l] = new float[layerSize[l]];
            delta[l]             = new float[layerSize[l]];
            layerOutput[l]       = new float[layerSize[l]];
            layerInput[l]        = new float[layerSize[l]];
            transferFunctions[l] = new TransferFunctions.TransferFunction[layerSize[l]];

            weight[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];              // if l (layer) is 0 (first hidden layer), then it needs weights fromt he input layer, which is not included in layer sizes
            previousWeightDelta[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][]; //.... so it uses the inputSize variable instead, which holds the nubmer of nodes int he input layer
            // .... else it uses the layerSize of the previous layer

            // need to set the size of the current layer's nodes that we're concerned with (third array)
            // iterates through the (second array) previous layer's nodes and sets the target nodes in the current layer
            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++)              // if l = 0, previous layer is inputLayer, else: layer - 1
            {
                weight[l][i]        = new float[layerSize[l]];
                weightGenomeLength += layerSize[l];

                previousWeightDelta[l][i] = new float[layerSize[l]];
            }
        }

        //Debug.Log ("BiasGenomeLength= " + biasGenomeLength.ToString () + ", weightGenomeLength= " + weightGenomeLength.ToString ());
        genome.genomeBiases  = new float[biasGenomeLength];
        genome.geneFunctions = new TransferFunctions.TransferFunction[biasGenomeLength];
        genome.genomeWeights = new float[weightGenomeLength];

        // Initialize the weights
        for (int l = 0; l < layerCount; l++)          // iterate through the 'real' layers
        {
            for (int j = 0; j < layerSize[l]; j++)    // for 2-dimensional arrays, iterate through current layer
            {
                //bias[l][j] = Gaussian.GetRandomGaussian();  // normally distributed
                bias[l][j] = 0f;                  // start zeroed
                genome.genomeBiases[biasIndex]  = bias[l][j];
                transferFunctions[l][j]         = TransferFunctions.TransferFunction.RationalSigmoid;
                genome.geneFunctions[biasIndex] = transferFunctions[l][j];
                biasIndex++;

                previousBiasDelta[l][j] = 0.0f;                   // init to 0
                layerOutput[l][j]       = 0.0f;
                layerInput[l][j]        = 0.0f;
                delta[l][j]             = 0.0f;
            }

            for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++) // for 3-dimensional arrays, iterate through nodes in previous layer (second array)
            {
                for (int j = 0; j < layerSize[l]; j++)                        // iterate through nodes in current layer
                {
                    //weight[l][i][j] = Gaussian.GetRandomGaussian(); // init connective weights to standard distribution around 0.0
                    weight[l][i][j] = 0f;
                    genome.genomeWeights[weightIndex] = weight[l][i][j];
                    weightIndex++;

                    previousWeightDelta[l][i][j] = 0.0f;
                }
            }
        }
        return(genome);
    }
	public override void InitializeBrainFromGenome(Genome genome)
	{
		int genomeBiasIndex = 0;
		int genomeWeightIndex = 0;
		int biasGenomeLength = 0;
		int weightGenomeLength = 0;
		
		// Initialize network layers
		layerCount = genome.layerSizes.Length - 1;  // doesn't include input layer, so one less than layerSizes
		inputSize = genome.layerSizes[0];  // set size of input layer		
		layerSize = new int[layerCount]; // keeps track of the number of nodes in each layer
		for (int i = 0; i < layerCount; i++) // transferring layer sizes function input (which included input layer) into new variable that does not include input layer nodes
			layerSize[i] = genome.layerSizes[i + 1];

		// Start dimensioning arrays
		bias = new float[layerCount][];  // setting length of first array, which specifies number of 'real' layers
		previousBiasDelta = new float[layerCount][];
		delta = new float[layerCount][];
		layerOutput = new float[layerCount][];
		layerInput = new float[layerCount][];
		transferFunctions = new TransferFunctions.TransferFunction[layerCount][];
		weight = new float[layerCount][][]; // setting length of first array, which specifies number of 'real' layers
		previousWeightDelta = new float[layerCount][][];
		// Fill 2 dimensional arrays
		for (int l = 0; l < layerCount; l++) // iterate through second arrays and set number of nodes (second array)
		{
			bias[l] = new float[layerSize[l]];
			biasGenomeLength += layerSize[l];  // keep track of bias Genome Length
			
			previousBiasDelta[l] = new float[layerSize[l]];
			delta[l] = new float[layerSize[l]];
			layerOutput[l] = new float[layerSize[l]];
			layerInput[l] = new float[layerSize[l]];
			transferFunctions[l] = new TransferFunctions.TransferFunction[layerSize[l]];
			
			weight[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];  // if l (layer) is 0 (first hidden layer), then it needs weights fromt he input layer, which is not included in layer sizes 
			previousWeightDelta[l] = new float[l == 0 ? inputSize : layerSize[l - 1]][];  //.... so it uses the inputSize variable instead, which holds the nubmer of nodes int he input layer
			// .... else it uses the layerSize of the previous layer
			
			// need to set the size of the current layer's nodes that we're concerned with (third array)
			// iterates through the (second array) previous layer's nodes and sets the target nodes in the current layer
			for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++)  // if l = 0, previous layer is inputLayer, else: layer - 1
			{
				weight[l][i] = new float[layerSize[l]];
				weightGenomeLength += layerSize[l];
				
				previousWeightDelta[l][i] = new float[layerSize[l]];
			}
		}

		//genome.genomeBiases = new float[biasGenomeLength];
		genome.geneFunctions = new TransferFunctions.TransferFunction[biasGenomeLength];  // REVISIT HOW TO SAVE TRANSFER FUNCTIONS (static class cna't be saved)
		//genome.genomeWeights = new float[weightGenomeLength];

		// Initialize the weights
		for (int l = 0; l < layerCount; l++)  // iterate through the 'real' layers
		{
			for (int j = 0; j < layerSize[l]; j++)  // for 2-dimensional arrays, iterate through current layer
			{
				bias[l][j] = genome.genomeBiases[genomeBiasIndex];  // normally distributed
				//transferFunctions[l][j] = genome.geneFunctions[genomeBiasIndex];
				transferFunctions[l][j] = TransferFunctions.TransferFunction.RationalSigmoid; // REVISIT HOW TO SAVE TRANSFER FUNCTIONS (static class cna't be saved)
				genomeBiasIndex += 1;
				
				//previousBiasDelta[l][j] = 0.0;   // init to 0
				//layerOutput[l][j] = 0.0;
				//layerInput[l][j] = 0.0;
				//delta[l][j] = 0.0;
			}
			
			for (int i = 0; i < (l == 0 ? inputSize : layerSize[l - 1]); i++) // for 3-dimensional arrays, iterate through nodes in previous layer (second array)
			{
				for (int j = 0; j < layerSize[l]; j++)  // iterate through nodes in current layer
				{
					weight[l][i][j] = genome.genomeWeights[genomeWeightIndex]; // init connective weights to standard distribution around 0.0
					genomeWeightIndex += 1;
					//previousWeightDelta[l][i][j] = 0.0;
				}
			}
		}
	}
 public GeneNodeNEAT(int id, GeneNodeType nodeType, TransferFunctions.TransferFunction function) {
     this.id = id;
     this.nodeType = nodeType;
     this.activationFunction = function;
 }