Exemplo n.º 1
0
 public CommonSlice(int numberOfNeurons)
 {
     for (var i = 0; i < numberOfNeurons; i++)
     {
         Neurons.Add(new CommonBiasNeuron());
     }
 }
Exemplo n.º 2
0
 public void InitDelta(List <double> idealOutput)
 {
     foreach (var(neuron, output) in Neurons.Cast <OutputNeuron>().Zip(idealOutput, (neuron, output) => (neuron, output)))
     {
         neuron.InitDelta(output);
     }
 }
Exemplo n.º 3
0
        static void Main()
        {
            //given
            const int stringLength = 32;
            //Input : N numbers , while NOT negative
            List <string> inputStrings = new List <string>();
            long          toAdd;

            while (true)
            {
                toAdd = long.Parse(Console.ReadLine());
                if (toAdd < 0)
                {
                    break;
                }

                inputStrings.Add(Convert.ToString(toAdd, 2).PadLeft(stringLength, '0'));
            }
            //Create Neurons Grid
            Neurons[,] neuronGrid
                = new Neurons[inputStrings.Count, stringLength];
            //check each bit
            for (int currRow = 0;
                 currRow < inputStrings.Count; currRow++)
            {
                for (int currCol = 0; currCol < stringLength; currCol++)
                {
                    bool boundRight = false;
                    bool boundTop   = false;
                    bool boundBot   = false;
                }
            }
        }
 public void AddLink(CPPNNetworkNeuron from, CPPNNetworkNeuron to, Complex weight)
 {
     if (Neurons.Contains(from) && Neurons.Contains(to))
     {
         (to as CPPNOutputNeuron).AddChild(from, weight);
     }
 }
Exemplo n.º 5
0
        public override void InitNetwork()
        {
            // Creating neurons
            for (int ii = 0; ii < NeuronCount; ii++)
            {
                Neurons.Add(new Neuron(ii));
            }
            // Adding link btween neurons
            int jj = 0;

            // input to hidden layer
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[0], Neurons[2 + kk], jj++);
            }
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[1], Neurons[2 + kk], jj++);
            }
            // reccurant links
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[2 + kk], jj++);
            }
            // hidden layer to input
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[NeuronCount - 2], jj++);
            }
            for (int kk = 0; kk < HIDDEN_NEURON_COUNT; kk++)
            {
                CreateSynapse(Neurons[2 + kk], Neurons[NeuronCount - 1], jj++);
            }
        }
Exemplo n.º 6
0
 public void Calculate()
 {
     for (int layer = 0; layer < TotalLayers; layer++)
     {
         Neurons.FindAll(n => n.NetworkLevel == layer).ForEach(n => n.Calculate());
     }
 }
 public NeuronLayer Clone()
 {
     return(new NeuronLayer
     {
         Neurons = Neurons.Select(n => n.Clone()).ToList()
     });
 }
Exemplo n.º 8
0
    private void setBravery(Neurons n)
    {
        //Neurons n = victim.GetComponent ("Neurons") as Neurons;
        int bravery = (level * 10);

        n.setBravery(bravery);
    }
Exemplo n.º 9
0
        public double[] Activate(double[] inputs)
        {
            if (inputs.Length != Inputs)
            {
                throw new System.IndexOutOfRangeException("Input length invalid, should be: " + Inputs.ToString());
            }

            Neurons.Sort(new Node.NodeSorter());

            for (int i = 0; i < Inputs; i++)
            {
                Neurons[i].AddValue(inputs[i]);
            }

            foreach (Node n in Neurons)
            {
                n.Fire();
            }

            var output = new double[Outputs];

            for (int o = 0; o < Outputs; o++)
            {
                output[o] = Neurons[o + Inputs].GetValue();
            }

            return(output);
        }
Exemplo n.º 10
0
    public void damage(GameObject Player)
    {
        health -= 50;

        if (isMonster && health <= 100)
        {
            loseForm();
        }


        Neurons n = gameObject.GetComponent("Neurons") as Neurons;

        if (!isPlayer)
        {
            Brain      b      = gameObject.GetComponent("Brain") as Brain;
            Neurons    neuron = gameObject.GetComponent("Neurons") as Neurons;
            GameObject canSee = b.visiblePlayer();
            if (canSee == null && neuron.canBeStealthKilled())
            {
                health -= 50;
            }
        }

        if (health < 0)
        {
            health = 0;
        }

        checkLife();

        if (n != null)
        {
            n.seePlayer(Player);
        }
    }
Exemplo n.º 11
0
        /// <summary>
        /// Randomly mutates the neural network by changing weights, adding connections, and/or adding neurons.
        /// </summary>
        public void RandomMutation()
        {
            double connectionWeightsMutation = rand.NextDouble();
            double connectionMutation        = rand.NextDouble();
            double NeuronMutation            = rand.NextDouble();

            if (connectionWeightsMutation < connectionWeightsMutationChance)
            {
                for (int outputNeuronIdx = NumInputs; outputNeuronIdx < NumInputs + NumOutputs; outputNeuronIdx++)
                {
                    Neurons.TryGetValue(outputNeuronIdx, out Neuron outputNeuron);
                    MutateConnections(outputNeuron);
                }
            }

            if (connectionMutation < connectionMutationChance)
            {
                MutateLink();
            }

            if (NeuronMutation < NeuronMutationChance)
            {
                MutateNeuron();
            }
        }
Exemplo n.º 12
0
        internal void Add(NeuronTypes type)
        {
            int arrayIndex = 0;

            for (int i = 0; i < NeuronsInfo.Count; i++)
            {
                arrayIndex += Convert.ToInt32(type == NeuronsInfo[i].neuronType);
            }
            NeuronsInfo.Add(new NeuronInfo(arrayIndex, type));
            switch (type)
            {
            case NeuronTypes.feedForward:
                Neurons.Add(new Neuron(previousLayerLenght));
                break;

            case NeuronTypes.lSTM:
                LSTMCells.Add(new LSTMCell(previousLayerLenght));
                break;

            case NeuronTypes.recurrent:
                RecurrentNeurons.Add(new RecurrentNeuron(previousLayerLenght));
                break;

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 13
0
        public void FeedForward(double[] input)
        {
            foreach (Neuron n in Neurons)
            {
                n.Net = input[Neurons.IndexOf(n)];
            }

            if (LayerAfter != null)
            {
                double[] newInput = new double[LayerAfter.Neurons.Count];

                foreach (Neuron n in LayerAfter.Neurons)
                {
                    double netSum = 0;
                    foreach (Connection c in n.Connections)
                    {
                        double neuronValue = c.FromNeuron.Out;
                        netSum += c.Weight * neuronValue;
                    }
                    newInput[LayerAfter.Neurons.IndexOf(n)] = netSum;
                }

                LayerAfter.FeedForward(newInput);
            }
        }
Exemplo n.º 14
0
 public void ResetNeuronsOfLayer(double minWeight, double maxWeight)
 {
     for (int neuronNo = 0; neuronNo < Neurons.Count; neuronNo++)
     {
         Neurons.ElementAt(neuronNo).ResetNeuron();
     }
 }
Exemplo n.º 15
0
 public void ResetNeuronsOfLayer()
 {
     for (int neuronNo = 0; neuronNo < Neurons.Count; neuronNo++)
     {
         Neurons.ElementAt(neuronNo).ResetNeuron();
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// Makes the initial connections frome every inout to every output with random weights.
 /// </summary>
 public void InitializeRandom()
 {
     for (int i = NumInputs; i < NumInputs + NumOutputs; i++)
     {
         Neurons.TryGetValue(i, out Neuron outputNeuron);
         for (int j = 1; j < NumInputs; j++)
         {
             Neurons.TryGetValue(j, out Neuron inputNeuron);
             Tuple <int, int> fromToPair = new Tuple <int, int>(inputNeuron.ID, outputNeuron.ID);
             Connection       conn;
             if (InnovationNumbers.ContainsKey(fromToPair))
             {
                 lock (InnovationNumbers)
                 {
                     InnovationNumbers.TryGetValue(fromToPair, out int innoNumber);
                     conn = new Connection(innoNumber, inputNeuron, rand.NextDouble() * 4 - 2);
                 }
             }
             else
             {
                 lock (InnovationNumbers)
                 {
                     conn = new Connection(InnovationNumbers.Count(), inputNeuron, rand.NextDouble() * 4 - 2);
                     InnovationNumbers.Add(new Tuple <int, int>(inputNeuron.ID, outputNeuron.ID), InnovationNumbers.Count());
                 }
             }
             Connections.Add(fromToPair);
             outputNeuron.AddConnection(conn.ID, conn);
         }
     }
 }
        /// <summary>
        /// Сгенерировать нейроны сети.
        /// </summary>
        /// <param name="attributes">Количество параметров входящего вектора.</param>
        /// <param name="neuronsCount">Количество нейронов.</param>
        public void GenerateNeurons(List <NetworkAttribute> attributes, int neuronsCount)
        {
            Neurons.Clear();
            Weights.Clear();
            InputAttributes.Clear();

            InputAttributes.AddRange(attributes.Select(a => new InputAttributeBase
            {
                InputAttributeNumber = a.OrderNumber,
                Name = a.Name
            })
                                     .ToList());

            for (int i = 0; i < neuronsCount; i++)
            {
                Neurons.Add(new NeuronBase
                {
                    NeuronNumber = i
                });
            }

            foreach (var inputAttribute in InputAttributes)
            {
                foreach (var neuron in Neurons)
                {
                    var randomWeight = NormalizationType.GetNeuronWeight(attributes.Count);
                    Weights.Add(new WeightBase
                    {
                        InputAttributeNumber = inputAttribute.InputAttributeNumber,
                        NeuronNumber         = neuron.NeuronNumber,
                        Value = randomWeight
                    });
                }
            }
        }
Exemplo n.º 18
0
 public InputLayer(int numberOfNeurons, IActivationFunction activationFunction, IInputFunction inputFunction)
 {
     for (int i = 0; i < numberOfNeurons; i++)
     {
         Neurons.Add(new InputNeuron(activationFunction, inputFunction));
     }
 }
 public void FillLayer()
 {
     for (var i = 0; i < Size; i++)
     {
         Neurons.Add(new Neuron(Index, i));
     }
 }
Exemplo n.º 20
0
 public void AssignInput(List <double> input)
 {
     foreach (var(neuron, d) in Neurons.Zip(input, (neuron, d) => (neuron, d)))
     {
         neuron.Value = d;
     }
 }
Exemplo n.º 21
0
        /// <summary>
        /// mutates a random link in the neural network
        /// </summary>
        public void MutateNeuron()
        {
            while (true)
            {
                int neuronId = rand.Next(NumInputs, NumInputs + NumOutputs);
                if (Neurons.TryGetValue(neuronId, out Neuron farNeuron) && farNeuron.Connections.Count > 0)
                {
                    Connection conn;
                    int        connectionId;
                    do
                    {
                        connectionId = rand.Next(farNeuron.Connections.Count());
                        conn         = farNeuron.Connections.Values.ElementAt(connectionId);
                    } while (conn.From.ID == 0);

                    Neuron newNeuron = new Neuron(Neurons.Count);

                    Tuple <int, int> nearToMidTuple = new Tuple <int, int>(conn.From.ID, newNeuron.ID);
                    Connection       nearToMid;
                    lock (InnovationNumbers)
                    {
                        if (InnovationNumbers.TryGetValue(nearToMidTuple, out int innoNumber))
                        {
                            nearToMid = new Connection(innoNumber, conn.From, 1);
                        }
                        else
                        {
                            nearToMid = new Connection(InnovationNumbers.Count(), conn.From, 1);
                            InnovationNumbers.Add(nearToMidTuple, InnovationNumbers.Count());
                        }
                    }
                    Connection       midToFar;
                    Tuple <int, int> midToFarTuple = new Tuple <int, int>(newNeuron.ID, farNeuron.ID);
                    lock (InnovationNumbers)
                    {
                        if (InnovationNumbers.TryGetValue(midToFarTuple, out int innoNumber2))
                        {
                            midToFar = new Connection(innoNumber2, newNeuron, conn.Weight);
                        }
                        else
                        {
                            midToFar = new Connection(InnovationNumbers.Count(), newNeuron, conn.Weight);
                            InnovationNumbers.Add(midToFarTuple, InnovationNumbers.Count());
                        }
                    }

                    Neurons.Add(newNeuron.ID, newNeuron);
                    newNeuron.AddConnection(nearToMid.ID, nearToMid);
                    Connection oldConnection = farNeuron.Connections.Values.ElementAt(connectionId);
                    conn.EnableDisable();
                    Connections.Add(nearToMidTuple);
                    Connections.Add(midToFarTuple);

                    farNeuron.AddConnection(midToFar.ID, midToFar);
                    NumNeurons++;
                    return;
                }
            }
        }
Exemplo n.º 22
0
 public override void Forward(bool isTraining)
 {
     Neurons.ForEach((N, i) =>
     {
         N.InVal  = N.InSynapses[0].InNeuron.OutVal;
         N.OutVal = N.InVal;
     });
 }
Exemplo n.º 23
0
 public void copyTo(Network n1)
 {
     for (int layer = Synapsis.Length; layer-- > 0;)
     {
         Synapsis[layer].CopyTo(n1.Synapsis[layer], 0);
     }
     Neurons.CopyTo(n1.Neurons, 0);
 }
Exemplo n.º 24
0
 public Neuron GetWinner(Vector <double> input)
 {
     return(Neurons
            .Select((n, i) =>
                    new { neuron = n, value = DistanceCalculator.CalculateDistance(n.CurrentWeights, input) })
            .OrderBy(item => item.value)
            .First().neuron);
 }
Exemplo n.º 25
0
        public Matrix[] Compute(Matrix[] input)
        {
            var outputs = Neurons.AsParallel().Select(n => n.Compute(input)).ToArray();

            Outputs = outputs;

            return(outputs);
        }
Exemplo n.º 26
0
 public void Run()
 {
     Neurons.ForEach(x => x.Run());
     if (NextLayer != null)
     {
         NextLayer.Run();
     }
 }
        public double[] Compute(double[] inputs)
        {
            var outputs = Neurons.AsParallel().Select(n => n.Compute(inputs)).ToArray();

            Outputs = outputs;

            return(outputs);
        }
Exemplo n.º 28
0
        public void Optimize(double delta)
        {
            Weight += LearningRate * delta;

            Debug.WriteLine($"LR = {LearningRate} Delta = {delta} Weight = {Weight}");

            Neurons.ToList().ForEach(x => x.UpdateWeights(Weight));
        }
Exemplo n.º 29
0
        public NeuralLayer(int neuronCount, double initialWeight, string name)
        {
            Weight = initialWeight;
            Name   = name;

            Enumerable.Range(0, neuronCount).ToList()
            .ForEach(elem => Neurons.Add(new Neuron()));
        }
Exemplo n.º 30
0
 public void Populate(int size, AbstractInputFunction inputFunction = null, AbstractActivationFunction activationFunction = null)
 {
     for (int toFill = size - Neurons.Count(); toFill > 0; toFill--)
     {
         Neurons.Add(new Neuron(inputFunction, activationFunction));
     }
     Size = Neurons.Count();
 }
Exemplo n.º 31
0
 public Network(int inputsCount, int layersCount, List<int> neuronsCountInHiddenLayers,
     Functions.IActivationFunction function, Neurons.INeuronInitilizer initializer )
 {
     NetworkInit(inputsCount, layersCount);
     if (neuronsCountInHiddenLayers.Count != layersCount)
         throw new ApplicationException("Number of layers and provided layer's neuron count do not match!");
     layers[0] = new Layer(inputsCount, inputsCount, function, initializer);
     for(int i = 1; i < layers.Length; i++)
         layers[i] = new Layer(neuronsCountInHiddenLayers[i-1], neuronsCountInHiddenLayers[i], function, initializer);
 }