Exemple #1
0
 public int GetIndexNeuron(Neuron neuron)
 {
     int index = -1;
     for (int i = 0; i < NeuronsCount; i++)
     {
         if (neurons[i] == neuron)
             return i;
     }
     return index;
 }
Exemple #2
0
        public NeuroNet(int numb_input_neurons, int numb_output_neurons, int[] numbNeuronsInLayers,
            bool[,] _connections, double[,] _weights, ActivateFunction af)
        {
            if(_connections.GetLength(0) != _connections.GetLength(1) ||
                _weights.GetLength(0) != _weights.GetLength(1) ||
                _connections.GetLength(0) != _weights.GetLength(0))
            {
                throw new Exception("Invalid dimensions of connection or weight matrices");
            }

            int countNeurons = _connections.GetLength(0);
            if (numb_input_neurons <= 0 || numb_input_neurons >= countNeurons)
                throw new Exception("Number of input neurons is out of range");
            if (numb_output_neurons <= 0 || numb_output_neurons >= countNeurons)
                throw new Exception("Number of output neurons is out of range");
            if(af == null)
            {
                throw new Exception("Not initialized activate function");
            }
            if (numbNeuronsInLayers.GetLength(0) < 2 || numbNeuronsInLayers.GetLength(0) > countNeurons)
                throw new Exception("Invalid count of layers");

            int sum = 0;
            foreach (int item in numbNeuronsInLayers)
            {
                if (item <= 0)
                    throw new Exception("Must be at least one neuron in each layer");
                sum += item;
            }
            if(sum != countNeurons)
                throw new Exception("Invalid count of neurons in layers");

            neuronsInLayers = numbNeuronsInLayers;

            input_neurons = new InputNeuron[numb_input_neurons];
            neurons = new Neuron[countNeurons];
            output_neurons = new OutputNeuron[numb_output_neurons];
            topology = new bool[countNeurons, countNeurons];
            weights = new double[countNeurons, countNeurons];

            evaluation_machine = new Queue<Queue<Neuron>>();

            for (int i = 0; i < numb_input_neurons; i++)
            {
                input_neurons[i] = new InputNeuron(af);
                neurons[i] = input_neurons[i];
            }
            for (int i = numb_input_neurons; i < countNeurons - numb_output_neurons; i++)
            {
                neurons[i] = new Neuron(af);
            }
            for (int i = 0; i < numb_output_neurons; i++)
            {
                output_neurons[i] = new OutputNeuron(af);
                neurons[i + countNeurons - numb_output_neurons] = output_neurons[i];
            }
            for (int i = 0; i < countNeurons; i++)
            {
                Neuron cur = neurons[i];
                for (int j = 0; j < countNeurons; j++)
                {
                    topology[j, i] = false;
                    if (_connections[j, i] == true)
                    {
                        topology[j, i] = true;
                        weights[j, i] = _weights[j, i];
                    }
                }
            }
            setInputConnections();

            isIterationsFinished = true;
            isWaveCameToOutputNeuron = false;
        }
        private NeuronLocation getNeuronLocation(Neuron neuron)
        {
            int index = learned_net.GetIndexNeuron(neuron);
            if (index >= 0)
            {
                int layer = 1;
                int number = 1;

                int curInd = 0;
                for (int i = 0; i < learned_net.NeuronsInLayers.GetLength(0); i++)
                {
                    int nextInd = curInd + learned_net.NeuronsInLayers[i];
                    if (index < nextInd && index >= curInd)
                    {
                        number = index - curInd + 1;
                        break;
                    }
                    curInd = nextInd;
                    layer++;
                }

                return new NeuronLocation(layer, number);
            }
            else
            {
                return null;
            }
        }