Example #1
0
        private void DrawConnections(SpriteBatch spriteBatch, Neuron n, float strongestConnection, Neuron mouseNeuron)
        {
            WorkingNeuron wn = (WorkingNeuron)n;

            foreach (Connection c in wn.GetConnections())
            {
                if (mouseNeuron != null && n != mouseNeuron && c.entryNeuron != mouseNeuron)
                {
                    continue;
                }
                Color color = Color.Black;
                float value = c.GetValue();
                float alpha = Mathf.Sqrt(Math.Abs(value) / strongestConnection);
                //TODO
                if (value > 0)
                {
                    color = new Color(0f, alpha, 0f, 1f);
                }
                else
                {
                    color = new Color(alpha, 0f, 0f, 1f);
                }
                RenderHelper.DrawLine(spriteBatch, n.DrawPosition.X, n.DrawPosition.Y, c.entryNeuron.DrawPosition.X, c.entryNeuron.DrawPosition.Y, color, 1);
            }
        }
Example #2
0
        public void CreateHiddenLayer()
        {
            List <Neuron> newLayer   = new List <Neuron>();
            WorkingNeuron newNeuron  = new WorkingNeuron(HiddenLayerCount + 2);
            int           layerIndex = neurons.Count - 1;

            int prevLayerIndex  = layerIndex - 1;
            int connectionCount = 0;

            while (prevLayerIndex >= 0)
            {
                if (layerIndex > 1 && prevLayerIndex == 0)
                {
                    break;
                }
                float weight = 0.0f;
                if (connectionCount < LastHiddenLayer.Count && connectionCount < ((WorkingNeuron)LastHiddenLayer[connectionCount]).GetConnections().Count)
                {
                    weight = ((WorkingNeuron)LastHiddenLayer[0]).GetConnections()[connectionCount].weight;
                }
                for (int prevNeuronIndex = connectionCount; prevNeuronIndex < neurons[prevLayerIndex].Count &&
                     ((!(prevLayerIndex == 0) || connectionCount < FirstHiddenLayer.Count) || layerIndex == 1); ++prevNeuronIndex)
                {
                    newNeuron.AddNeuronConnection(neurons[prevLayerIndex][prevNeuronIndex], weight);
                    connectionCount++;
                }
                --prevLayerIndex;
            }

            for (int outputIndex = 0; outputIndex < OutputNeurons.Count; outputIndex++)
            {
                WorkingNeuron currentOutputNeuron = OutputNeurons[outputIndex] as WorkingNeuron;
                for (int connectionIndex = 0; connectionIndex < currentOutputNeuron.GetConnections().Count; connectionIndex++)
                {
                    if (currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron == LastHiddenLayer[0])
                    {
                        currentOutputNeuron.GetConnections()[connectionIndex].entryNeuron = newNeuron;
                    }
                }
            }
            newLayer.Add(newNeuron);
            AddHiddenLayer(newLayer);
        }
Example #3
0
        public Connection GetConnection(NetworkIndex index)
        {
            WorkingNeuron neuron = neurons[index.LayerIndex][index.NeuronIndex] as WorkingNeuron;

            if (neuron != null)
            {
                return(neuron.GetConnections()[index.ConnectionIndex]);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
 public void RemoveHiddenLayer()
 {
     if (HiddenLayerCount > 1)
     {
         neurons.RemoveAt(neurons.Count - 2);
         for (int outNeuronIndex = 0; outNeuronIndex < OutputNeurons.Count; outNeuronIndex++)
         {
             WorkingNeuron outputNeuron = OutputNeurons[outNeuronIndex] as WorkingNeuron;
             if (outputNeuron.GetConnections().Count > LastHiddenLayer.Count)
             {
                 outputNeuron.GetConnections().RemoveRange(LastHiddenLayer.Count, outputNeuron.GetConnections().Count - LastHiddenLayer.Count);
             }
             for (int connectionIndex = 0; connectionIndex < LastHiddenLayer.Count && connectionIndex < outputNeuron.GetConnections().Count; connectionIndex++)
             {
                 outputNeuron.GetConnections()[connectionIndex].entryNeuron = LastHiddenLayer[connectionIndex];
             }
             for (int connectionIndex = outputNeuron.GetConnections().Count; connectionIndex < LastHiddenLayer.Count; connectionIndex++)
             {
                 outputNeuron.AddNeuronConnection(LastHiddenLayer[connectionIndex], (Simulation.RandomFloat() * 2 * -1));
             }
         }
     }
 }