示例#1
0
        public static bool Load(ref NeuronHandler theNeuronArray, string fileName)
        {
            FileStream file = File.Open(fileName, FileMode.Open);

            XmlSerializer reader1 = new XmlSerializer(typeof(NA));//, GetModuleTypes());

            aNeuronArray = (NA)reader1.Deserialize(file);
            file.Close();

            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList neuronNodes;
            FileStream  fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            xmldoc.Load(fs);
            fs.Close();

            int arraySize = aNeuronArray.arraySize;

            theNeuronArray = new NeuronHandler();
            theNeuronArray.Initialize(arraySize);
            NeuronPartial n1 = theNeuronArray.GetPartialNeuron(1);

            neuronNodes = xmldoc.GetElementsByTagName("Neuron");

            for (int i = 0; i < neuronNodes.Count; i++)
            {
                //this is the hard way...we could parse by hand
                XmlDocument tempDoc = new XmlDocument();

                XmlElement neuronNode = (XmlElement)neuronNodes[i];
                XmlNode    importNode = tempDoc.ImportNode(neuronNode, true);
                tempDoc.AppendChild(importNode);

                MemoryStream stream = new MemoryStream();
                tempDoc.Save(stream);
                stream.Flush();
                stream.Position = 0;

                XmlSerializer reader = new XmlSerializer(typeof(N));
                //reader.UnknownAttribute += Reader_UnknownAttribute;
                //reader.UnknownElement += Reader_UnknownElement;
                //reader.UnknownNode += Reader_UnknownNode;
                //reader.UnreferencedObject += Reader_UnreferencedObject;
                N n = (N)reader.Deserialize(stream);
                if (n != null)
                {
                    int id = n.Id;
                    theNeuronArray.SetNeuronCurrentCharge(id, n.CurrentCharge);
                    if (n.Label != "")
                    {
                        theNeuronArray.SetNeuronLabel(id, n.Label);
                    }
                    if (n.LeakRate != 0.1f)
                    {
                        theNeuronArray.SetNeuronLeakRate(id, n.LeakRate);
                    }
                    if (n.Model != modelType.Std)
                    {
                        theNeuronArray.SetNeuronModel(id, (int)n.Model);
                    }
                    foreach (S s in n.Synapses)
                    {
                        theNeuronArray.AddSynapse(id, s.TargetNeuron, s.Weight, s.IsHebbian, false);
                    }
                }

                //stream.Position = 0;
                //StreamReader sr = new StreamReader(stream);
                //string x = sr.ReadToEnd();
            }
            return(true);
        }
示例#2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int neuronCount       = 1000000;
            int synapsesPerNeuron = 1000;

            MessageBox.Show("Starting array allocation");
            theNeuronArray = new NeuronHandler();
            MessageBox.Show("any existing array removed");
            theNeuronArray.Initialize(neuronCount);
            MessageBox.Show("array allocation complete");
            int test    = theNeuronArray.GetArraySize();
            int threads = theNeuronArray.GetThreadCount();

            theNeuronArray.SetThreadCount(16);
            threads = theNeuronArray.GetThreadCount();

            theNeuronArray.SetNeuronCurrentCharge(1, 1.4f);
            theNeuronArray.SetNeuronCurrentCharge(2, 0.9f);
            theNeuronArray.Fire(); //should transfer current chargest to last
            float a = theNeuronArray.GetNeuronLastCharge(1);
            float b = theNeuronArray.GetNeuronLastCharge(2);

            string s0 = theNeuronArray.GetNeuronLabel(1);

            theNeuronArray.SetNeuronLabel(1, "Fred");
            string s1 = theNeuronArray.GetNeuronLabel(1);

            theNeuronArray.SetNeuronLabel(1, "George");
            string s2 = theNeuronArray.GetNeuronLabel(1);

            theNeuronArray.AddSynapse(2, 4, .75f, 1, false);
            List <Synapse> synapses2 = theNeuronArray.GetSynapsesList(2);

            theNeuronArray.AddSynapse(1, 2, .5f, 0, false);
            List <Synapse> synapses1 = theNeuronArray.GetSynapsesList(1);

            theNeuronArray.AddSynapse(1, 3, .6f, 0, false);
            synapses1 = theNeuronArray.GetSynapsesList(1);
            theNeuronArray.AddSynapse(1, 4, .75f, 1, false);
            synapses1 = theNeuronArray.GetSynapsesList(1);
            theNeuronArray.AddSynapse(2, 4, .75f, 1, false);
            long           count     = theNeuronArray.GetTotalSynapses();
            List <Synapse> synapses0 = theNeuronArray.GetSynapsesList(0);

            synapses1 = theNeuronArray.GetSynapsesList(1);
            List <Synapse> synapsesFrom = theNeuronArray.GetSynapsesFromList(4);

            NeuronPartial n = theNeuronArray.GetPartialNeuron(1);

            theNeuronArray.Fire();
            long          gen = theNeuronArray.GetGeneration();
            NeuronPartial n1  = theNeuronArray.GetPartialNeuron(1);
            NeuronPartial n2  = theNeuronArray.GetPartialNeuron(2);
            NeuronPartial n3  = theNeuronArray.GetPartialNeuron(3);

            theNeuronArray.DeleteSynapse(1, 3);
            theNeuronArray.DeleteSynapse(1, 2);


            MessageBox.Show("allocating synapses");
            Parallel.For(0, neuronCount, x =>
            {
                //for (int x = 0; x < neuronCount; x++)
                //{
                for (int j = 0; j < synapsesPerNeuron; j++)
                {
                    int target = x + j;
                    if (target >= theNeuronArray.GetArraySize())
                    {
                        target -= theNeuronArray.GetArraySize();
                    }
                    if (target < 0)
                    {
                        target += theNeuronArray.GetArraySize();
                    }
                    theNeuronArray.AddSynapse(x, target, 1.0f, 0, true);
                }
            });
            for (int i = 0; i < neuronCount / 100; i++)
            {
                theNeuronArray.SetNeuronCurrentCharge(100 * i, 1);
            }
            MessageBox.Show("synapses and charge complete");
            Stopwatch sw  = new Stopwatch();
            string    msg = "";

            for (int i = 0; i < 10; i++)
            {
                sw.Start();
                theNeuronArray.Fire();
                sw.Stop();
                msg += "Gen: " + theNeuronArray.GetGeneration() + "  FireCount: " + theNeuronArray.GetFiredCount() + " time: " + sw.Elapsed.Milliseconds.ToString() + "\n";
                sw.Reset();
            }
            sw.Stop();
            MessageBox.Show("Done firing 10x\n" + msg);
        }
示例#3
0
        public static bool Save(ref NeuronHandler theNeuronArray, string fileName)
        {
            string     tempFile = System.IO.Path.GetTempFileName();
            FileStream file     = File.Create(tempFile);

            XmlSerializer writer = new XmlSerializer(typeof(NA));//, GetModuleTypes());

            writer.Serialize(file, aNeuronArray);
            file.Position = 0;;

            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(file);

            XmlElement root        = xmldoc.DocumentElement;
            XmlNode    neuronsNode = xmldoc.CreateNode("element", "Neurons", "");

            root.AppendChild(neuronsNode);

            for (int i = 0; i < theNeuronArray.GetArraySize(); i++)
            {
                NeuronPartial n = theNeuronArray.GetPartialNeuron(i);
                if (n.inUse && n.id != 0)
                {
                    string         label       = theNeuronArray.GetNeuronLabel(n.id);
                    List <Synapse> theSynapses = theNeuronArray.GetSynapsesList(i);
                    //this is needed bacause inUse is true if any synapse points to this neuron--we don't need to bother with
                    if (theSynapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f ||
                        n.model != modelType.Std)

                    {
                        XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", "");
                        neuronsNode.AppendChild(neuronNode);

                        XmlNode attrNode = xmldoc.CreateNode("element", "Id", "");
                        attrNode.InnerText = n.id.ToString();
                        neuronNode.AppendChild(attrNode);

                        if (n.lastCharge != 0)
                        {
                            attrNode           = xmldoc.CreateNode("element", "LastCharge", "");
                            attrNode.InnerText = n.lastCharge.ToString();
                            neuronNode.AppendChild(attrNode);
                        }
                        if (n.leakRate != 0.1f)
                        {
                            attrNode           = xmldoc.CreateNode("element", "LeakRate", "");
                            attrNode.InnerText = n.leakRate.ToString();
                            neuronNode.AppendChild(attrNode);
                        }
                        if (n.model != modelType.Std)
                        {
                            attrNode           = xmldoc.CreateNode("element", "Model", "");
                            attrNode.InnerText = n.model.ToString();
                            neuronNode.AppendChild(attrNode);
                        }
                        if (label != "")
                        {
                            attrNode           = xmldoc.CreateNode("element", "Label", "");
                            attrNode.InnerText = label;
                            neuronNode.AppendChild(attrNode);
                        }
                        if (theSynapses.Count > 0)
                        {
                            XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", "");
                            neuronNode.AppendChild(synapsesNode);
                            foreach (Synapse s in theSynapses)
                            {
                                XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", "");
                                synapsesNode.AppendChild(synapseNode);

                                if (s.weight != 1)
                                {
                                    attrNode           = xmldoc.CreateNode("element", "Weight", "");
                                    attrNode.InnerText = s.weight.ToString();
                                    synapseNode.AppendChild(attrNode);
                                }

                                attrNode           = xmldoc.CreateNode("element", "TargetNeuron", "");
                                attrNode.InnerText = s.target.ToString();
                                synapseNode.AppendChild(attrNode);

                                if (s.isHebbian)
                                {
                                    attrNode           = xmldoc.CreateNode("element", "IsHebbian", "");
                                    attrNode.InnerText = "true";
                                    synapseNode.AppendChild(attrNode);
                                }
                            }
                        }
                    }
                }
            }
            file.Position = 0;
            xmldoc.Save(file);
            file.Close();
            File.Copy(tempFile, fileName, true);
            File.Delete(tempFile);

            return(true);
        }