Esempio n. 1
0
        public static CSNeuroNet FromByteArray(byte[] array)
        {
            int i        = 0;
            int inputsc  = BitConverter.ToInt32(array, i); i += 4;
            int outputsc = BitConverter.ToInt32(array, i); i += 4;

            CSNeuroNet result = new CSNeuroNet(inputsc, outputsc);

            int nodesC = BitConverter.ToInt32(array, i); i += 4;

            for (int k = 0; k < nodesC; ++k)
            {
                Node n = new Node(BitConverter.ToInt32(array, i)); i += 4;
                n.Value = BitConverter.ToDouble(array, i); i += 8;
                result.AddNode(n);
            }

            int connectionsCount = BitConverter.ToInt32(array, i); i += 4;

            for (int k = 0; k < connectionsCount; ++k)
            {
                int    id     = BitConverter.ToInt32(array, i); i += 4;
                int    from   = BitConverter.ToInt32(array, i); i += 4;
                int    to     = BitConverter.ToInt32(array, i); i += 4;
                double weight = BitConverter.ToDouble(array, i); i += 8;

                Connection c = new Connection(id, from, to, weight);
                result.AddConnection(c);
            }
            return(result);
        }
Esempio n. 2
0
        public byte[] ToByteArray()
        {
            CSNeuroNet copy = new CSNeuroNet(this);
            //copy.Optimize();
            List <byte> result = new List <byte>();

            result.AddRange(BitConverter.GetBytes(copy.Inputs));
            result.AddRange(BitConverter.GetBytes(copy.Outputs));

            result.AddRange(BitConverter.GetBytes(copy.Nodes.Count - Inputs - Outputs));
            foreach (Node n in copy.Nodes)
            {
                if (n.ID > Inputs + Outputs - 1)
                {
                    result.AddRange(BitConverter.GetBytes(n.ID));
                    result.AddRange(BitConverter.GetBytes(n.Value));
                }
            }
            result.AddRange(BitConverter.GetBytes(copy.Connections.Count));
            foreach (Connection c in copy.Connections)
            {
                result.AddRange(BitConverter.GetBytes(c.ID));
                result.AddRange(BitConverter.GetBytes(c.From));
                result.AddRange(BitConverter.GetBytes(c.To));
                result.AddRange(BitConverter.GetBytes(c.Weight));
            }

            return(result.ToArray());
        }
Esempio n. 3
0
        // Use this for initialization
        public void Start(int passes)
        {
            BestFromPasses = new Dictionary <int, Tuple <double, CSNeuroNet> >();
            ET.Init();

            for (int i = 0; i < passes; ++i)
            {
                bool wasP = false;
                Console.WriteLine("Starting pass {0}", i);
                ET.PassGeneration();

                ET.LastResult.Sort((x, y) =>
                {
                    if (x.a > y.a)
                    {
                        return(1);
                    }
                    else if (x.a < y.a)
                    {
                        return(-1);
                    }
                    else
                    {
                        return(0);
                    }
                });
                var max = ET.LastResult[ET.LastResult.Count - 1];

                BestFromPasses.Add(i, new Tuple <double, CSNeuroNet>(max.a, max.b));
                Console.WriteLine("Minimum is: {0:F5}, Maximum: {1:F5}, medium: {2:F5}", ET.LastResult.Min(x => x.a), max.a, ET.LastResult.ConvertAll(x => x.a).Average(x => x));
                Console.WriteLine("Best net: {0}", max.b);
                Console.WriteLine("Pass {0} have been passed", i);

                if (pause)
                {
                    Console.WriteLine("Pausing...");
                    wasP = true;
                }
                while (pause)
                {
                    Thread.Sleep(100);
                }
                if (wasP)
                {
                    Console.WriteLine("Continueing");
                }
            }

            neuro = ET[ET.Individuals - 1];
            while (true)
            {
            }
        }
Esempio n. 4
0
        public CSNeuroNet(CSNeuroNet copy)
        {
            Copies  = copy.Copies;
            Nodes   = new List <Node>();
            outputs = new List <Node>();
            inputs  = new List <Node>();
            Inputs  = copy.Inputs;
            Outputs = copy.Outputs;
            copy.Nodes.ForEach(x =>
            {
                Node n = new Node(x);



                Nodes.Add(n);
                if (x.ID < Inputs)
                {
                    inputs.Add(n);
                }
                else if (x.ID < Inputs + Outputs)
                {
                    outputs.Add(n);
                }
            });

            Connections = new List <Connection>();


            copy.Connections.ForEach(x =>
            {
                Connection c = new Connection(x);


                Connections.Add(c);
            });
        }
Esempio n. 5
0
        public double Test(CSNeuroNet nn)
        {
            Vector2 pos        = new Vector2(0.0, 10.0);
            Vector2 vel        = new Vector2((double)((0.5) * 5), 0.0);
            Vector2 player     = new Vector2(0.0, 0.0);
            Vector2 grav       = new Vector2(0.0, -30.0);
            Vector2 controller = new Vector2(10.0, 0.0);
            double  time       = 0.04;

            nn.Flush();

            double overall = 0.0;



            int hj = 0;

            int max = 75;

            while (hj++ < max)
            {
                Random r   = new Random(hj + 4723);
                Random rad = new Random(hj + 1234);
                Func <double, double, double> randF = (x, y) =>
                {
                    return((double)(rad.NextDouble() * ((double)y - x) + x));
                };

                double fr  = randF(-2, 2);
                double fr2 = randF(-2, 2);

                controller = new Vector2(17.0, 0.0);
                pos        = new Vector2(randF(-5, 5), randF(3, 20));

                int    diff    = hj / 4;
                int    maxDiff = max / 4;
                double diffC   = (diff / (double)maxDiff);


                vel    = new Vector2(randF(-5, 5) + randF(-1, 1) * diffC * controller.x, randF(-1, 1) + randF(-4, 0) * diffC);
                player = new Vector2(0.0, 0.0);
                grav   = new Vector2(0.0, -35.0);
                double curTim = 0;
                while (true)
                {
                    var res = nn.Calculate(new double[] { player.x, player.y, pos.x, pos.y, vel.x, vel.y, time, controller.x, grav.y, -10.0, 10.0, -0.5, 0.5 });


                    if (res[0] > 0.6)
                    {
                        player -= controller * time;
                    }
                    if (res[1] > 0.6)
                    {
                        player += controller * time;
                    }
                    if (player.x > 10.0)
                    {
                        player.x = 10.0;
                    }
                    else if (player.x < -10.0)
                    {
                        player.x = -10.0;
                    }

                    vel  = vel + grav * time;
                    pos += vel * time;
                    if (pos.x > 10.0)
                    {
                        pos.x = 10.0;
                        vel.x = -vel.x;
                    }
                    else if (pos.x < -10.0)
                    {
                        pos.x = -10.0;
                        vel.x = -vel.x;
                    }

                    if (pos.y < 0.0)
                    {
                        if ((pos.x < player.x + 0.5) && (pos.x > player.x - 0.5))
                        {
                            pos.y = 0.0;
                            vel.y = -vel.y;
                            vel.x = vel.x + (pos.x - player.x) + (double)((r.NextDouble() - 0.5) * 2);
                        }
                        else
                        {
                            break;
                        }
                    }



                    curTim += time;
                    if ((pos.x < player.x + 0.5) && (pos.x > player.x - 0.5))
                    {
                        curTim += time * 2;
                    }
                    if (curTim > 40.0)
                    {
                        break;
                    }
                }
                overall += curTim * diffC;
            }


            if (overall > record)
            {
                record = overall;
                Console.WriteLine("New record! {0:F5} by net: {1}", record, nn);
            }

            return(overall);
        }