Esempio n. 1
0
        public static SimplePerceptron LoadXML(string filename)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(filename);

            XmlNode root     = doc.SelectSingleNode("network");
            int     ninputs  = int.Parse(root.Attributes ["inputs"].Value);
            int     noutputs = int.Parse(root.Attributes ["outputs"].Value);

            XmlNodeList layers = root.SelectNodes("layer");

            int[] hidden = new int[layers.Count - 1];
            for (int i = 0; i < layers.Count - 1; i++)
            {
                hidden [i] = int.Parse(layers [i].Attributes ["size"].Value);
            }

            SimplePerceptron result = new SimplePerceptron("ai4topology.xml");

            for (int i = 0; i < layers.Count; i++)
            {
                int x = int.Parse(layers [i].Attributes ["size"].Value);
                //int y = int.Parse(layers[i].Attributes["ny"].Value);

                string[] words = layers [i].InnerText.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                for (int j = 0; j < x; j++)
                {
                    //Console.WriteLine(words[j]);
                    result.bias [i] [j] = double.Parse(words [j]);
                }
            }

            layers = root.SelectNodes("links");
            for (int k = 0; k < layers.Count; k++)
            {
                int      x     = int.Parse(layers [k].Attributes ["nx"].Value);
                int      y     = int.Parse(layers[k].Attributes["ny"].Value);
                string[] words = layers [k].InnerText.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 0; i < x; i++)
                {
                    for (int j = 0; j < y; j++)
                    {
                        result.links[k][i, j] = double.Parse(words[i * y + j]);
                    }
                }
            }


            return(result);
        }
Esempio n. 2
0
        public static SimplePerceptron Mix(SimplePerceptron mom, SimplePerceptron dad, double mutationrate)
        {
            Random rnd = mom.rnd;             //mom random seed rulez!

            SimplePerceptron son = new SimplePerceptron(mom.topofile);

            son.maxbias = mom.maxbias;
            son.maxlink = mom.maxlink;

            //for each non input layer... copy the biases
            for (int layer = 0; layer < mom.bias.Count; layer++)
            {
                int momlim = rnd.Next(0, mom.bias[layer].Length);
                for (int i = 0; i < mom.bias[layer].Length; i++)
                {
                    son.bias[layer][i] = (i <= momlim)? mom.bias[layer][i] : dad.bias[layer][i];
                    if (dad.rnd.NextDouble() < mutationrate)                    //mutate!
                    {
                        son.bias[layer][i] = 2 * son.maxbias * (rnd.NextDouble() - 0.5);
                    }
                }
                //copy connections
                for (int x = 0; x < son.links[layer].GetLength(0); x++)
                {
                    momlim = rnd.Next(0, son.links[layer].GetLength(1));
                    for (int y = 0; y < son.links[layer].GetLength(1); y++)
                    {
                        son.links[layer][x, y] = (y <= momlim)? mom.links[layer][x, y] : dad.links[layer][x, y];
                        if (dad.rnd.NextDouble() < mutationrate)                        //mutate!
                        {
                            son.links[layer][x, y] = 2 * son.maxlink * (rnd.NextDouble() - 0.5);
                        }
                    }
                }
            }

            return(son);
        }