Esempio n. 1
0
    public void FromIntList(IEnumerator <int> list)
    {
        subnetNeurons = list.Current;
        list.MoveNext();
        layers = list.Current;
        list.MoveNext();
        baseConnNum = list.Current;
        list.MoveNext();
        inputOutputConnNum = list.Current;
        list.MoveNext();
        EmotionState = list.Current;
        list.MoveNext();

        Init();

        subNets = new NeuronCluster[list.Current];
        list.MoveNext();
        for (int i = 0; i < subNets.Length; i++)
        {
            subNets[i] = NeuronCluster.FromIntList(list);
        }
        connections = new NNConnection[list.Current];
        list.MoveNext();
        for (int i = 0; i < connections.Length; i++)
        {
            connections[i] = NNConnection.FromIntList(list);
        }
    }
Esempio n. 2
0
        public NeuralNetwork Read()
        {
            NeuralNetwork net;

            using (BinaryReader reader = new BinaryReader(new FileStream(fileName, FileMode.Open)))
            {
                reader.BaseStream.Seek(0x18600, SeekOrigin.Begin);

                double learningRate = reader.ReadDouble();
                net = new NeuralNetwork(learningRate);

                int     layersCount = reader.ReadInt32();
                NNLayer layer       = null;
                for (int i = 0; i < layersCount; i++)
                {
                    string layerName = ReadString(reader);
                    layer = new NNLayer(layer);
                    net.Layers.Add(layer);

                    int neuronsCount = reader.ReadInt32();
                    int weightsCount = reader.ReadInt32();

                    for (int j = 0; j < neuronsCount; j++)
                    {
                        string   neuronName = ReadString(reader);
                        NNNeuron neuron     = new NNNeuron();
                        layer.Neurons.Add(neuron);

                        int connectionsCount = reader.ReadInt32();

                        for (int k = 0; k < connectionsCount; k++)
                        {
                            uint         neuronIndex = reader.ReadUInt32();
                            uint         weightIndex = reader.ReadUInt32();
                            NNConnection connection  = new NNConnection(neuronIndex, weightIndex);
                            neuron.Connections.Add(connection);
                        }
                    }

                    for (int j = 0; j < weightsCount; j++)
                    {
                        string weightName = ReadString(reader);
                        double value      = reader.ReadDouble();

                        NNWeight weight = new NNWeight {
                            Value = value
                        };
                        layer.Weights.Add(weight);
                    }
                }
            }

            return(net);
        }
Esempio n. 3
0
        public static NNConnection FromIntList(IEnumerator <int> list)
        {
            NNConnection result = new NNConnection();

            result.srcNet = list.Current;
            list.MoveNext();
            result.srcIdx = list.Current;
            list.MoveNext();
            result.destNet = list.Current;
            list.MoveNext();
            result.destIdx = list.Current;
            list.MoveNext();
            return(result);
        }
Esempio n. 4
0
    public void DoTimestep()
    {
        if (EmotionState > 0)
        {
            EmotionState--;
        }
        else if (EmotionState < 0)
        {
            EmotionState++;
        }

        int finalEmotionState = (EmotionState > EMOTION_MAGNITUDE_THRESH ? 1 : (EmotionState < -EMOTION_MAGNITUDE_THRESH ? -1 : 0));

        // First, copy inputs / connection values into all subnets
        for (int i = 0; i < connections.Length; i++)
        {
            NNConnection  conn   = connections[i];
            NeuronCluster srcNet = subNets[conn.srcNet];
            // destNet can be -1, which means access the IO ports
            if (conn.destNet >= 0)
            {
                NeuronCluster destNet = subNets[conn.destNet];
                srcNet.SetExternalInput(conn.srcIdx, destNet.GetExternalOutput(conn.destIdx));
                destNet.SetExternalInput(conn.destIdx, srcNet.GetExternalOutput(conn.srcIdx));
            }
            else
            {
                srcNet.SetExternalInput(conn.srcIdx, inputVector[conn.destIdx]);
                // copy outputs after processing
                // outputVector[conn.destIdx] = srcNet.GetExternalOutput(conn.srcIdx);
            }
        }
        // Second, subnets process based on emotional state
        for (int i = 0; i < subNets.Length; i++)
        {
            subNets[i].FullTimestep(finalEmotionState);
        }
        // Third, set outputs
        for (int i = 0; i < connections.Length; i++)
        {
            NNConnection conn = connections[i];
            // destNet must be -1
            if (conn.destNet != -1)
            {
                continue;
            }
            NeuronCluster srcNet = subNets[conn.srcNet];
            outputVector[conn.destIdx] = srcNet.GetExternalOutput(conn.srcIdx);
        }
    }