public void AddConnection(string connection) { int botId; Match match = constAssignment.Match(connection); if (match.Success) { int binValue = int.Parse(match.Groups[1].Captures[0].ToString()); botId = int.Parse(match.Groups[2].Captures[0].ToString()); var binInput = new BinInput(binValue); GetBot(botId).AssignInput(binInput); return; } match = botAssignment.Match(connection); botId = int.Parse(match.Groups[1].Captures[0].ToString()); bool lowIsOutput = match.Groups[2].Captures[0].ToString() == "output"; int lowOutputId = int.Parse(match.Groups[3].Captures[0].ToString()); var lowBotInput = new BotInput(GetBot(botId), BotInput.OutputType.LOW); bool highIsOutput = match.Groups[4].Captures[0].ToString() == "output"; int highOutputId = int.Parse(match.Groups[5].Captures[0].ToString()); var highBotInput = new BotInput(GetBot(botId), BotInput.OutputType.HIGH); if (lowIsOutput) { outputBins.Add(lowOutputId, lowBotInput); } else { GetBot(lowOutputId).AssignInput(lowBotInput); } if (highIsOutput) { outputBins.Add(highOutputId, highBotInput); } else { GetBot(highOutputId).AssignInput(highBotInput); } }
/// <summary> /// Initializes a neural network before use. /// </summary> /// <param name="inputSize">Amount of inputs for the neural network.</param> /// /// <param name="outputSize">Amount of output for the neural network.</param> /// /// <param name="hiddenLayers">Amount of hidden layers for the neural network.</param> public NeuralNetwork(int inputSize, int outputSize, int[] hiddenLayers, float mutateChance, int memorySlotSize, BinInput input) { inputFunc = input; //debugging if (inputSize <= 0 || outputSize <= 0) { Debug.LogError("In or output size is zero!"); return; } this.mutateChance = mutateChance; int nLength, wLength; #region Neurons neurons = new float[hiddenLayers.Length + 2][]; //init hidden layer neurons for (int layer = 0; layer < hiddenLayers.Length; layer++) { neurons[layer + 1] = new float[hiddenLayers[layer]]; } //init input neurons neurons[0] = new float[inputSize]; /*init output neurons * reusing old variable for different purpose*/ nLength = neurons.Length - 1; neurons[nLength] = new float[outputSize]; #endregion #region Weights //init weights (and set node + weight values) weights = new float[nLength][][]; for (int layer = 0; layer < nLength; layer++) { weights[layer] = new float[neurons[layer].Length][]; } //get each connection for (int layer = 0; layer < nLength; layer++) { for (int neuron = 0; neuron < neurons[layer].Length; neuron++) { //create weights weights[layer][neuron] = new float[neurons[layer + 1].Length]; //randomize base values for weights wLength = weights[layer][neuron].Length; for (int weigth = 0; weigth < wLength; weigth++) { weights[layer][neuron][weigth] = Ran(); } } } #endregion memoryPrefab = new float[memorySlotSize][]; for (int memorySlot = 0; memorySlot < memoryPrefab.Length; memorySlot++) { memoryPrefab[memorySlot] = new float[hiddenLayers[0]]; } RanMemory(); }