Exemplo n.º 1
0
        public void NormalRandNominalBehavior()
        {
            List <float> list = new List <float>();

            for (int i = 0; i < 1000; i++)
            {
                list.Add(ThreadSafeRandom.NormalRand());
            }

            List <List <float> > ranged = new List <List <float> >();

            for (float n = -3; n < 3; n = n + 0.1f)
            {
                var r = new List <float>();

                foreach (var l in list)
                {
                    if (n < l && l < n + 0.2f)
                    {
                        r.Add(l);
                    }
                }

                ranged.Add(r);
            }


            foreach (var r in ranged)
            {
                if (r.Any())
                {
                    Console.Write($"\t{r[^1]} : {r[0]} ; {r.Count}\t");
Exemplo n.º 2
0
        /// <summary>
        /// Initialize a <see cref="Network"/> from a given size
        /// </summary>
        /// <param name="Sizes"></param>
        private void InitializeNetwork(int[] Sizes)
        {
            this.Sizes    = Sizes.ToArray();
            NumberOfLayer = Sizes.Length;
            Biases        = new Matrix[Sizes.Length - 1];
            Weights       = new Matrix[Sizes.Length - 1];

            // Initialize Biases
            foreach ((int y, int l) in Sizes.Skip(1).Select((v, i) => (v, i)))
            {
                float[,] biases = new float[y, 1];
                for (int i = 0; i < y; i++)
                {
                    biases[i, 0] = ThreadSafeRandom.NormalRand();
                }
                Biases[l] = biases;
            }

            // Initialize Weights
            foreach (((int x, int y), int l) in Sizes.SkipLast(1).Zip(Sizes.Skip(1)).Select((v, i) => (v, i)))
            {
                // x => number of neuron in previous layer
                // y => number of neuron in actual layer
                float[,] layer = new float[y, x];
                for (int i = 0; i < y; i++)
                {
                    for (int j = 0; j < x; j++)
                    {
                        layer[i, j] = ThreadSafeRandom.NormalRand();
                    }
                }
                Weights[l] = layer;
            }
        }
Exemplo n.º 3
0
        ///<inheritdoc/>
        public void MutateWeightRandom()
        {
            ConnectionGene connection = Connections.RandomElement();

            if (connection != null)
            {
                connection.Weight = ThreadSafeRandom.NormalRand(0, 0.2f) * Constants.WEIGHT_RANDOM_STRENGTH;
            }
        }
Exemplo n.º 4
0
        ///<inheritdoc/>
        public void MutateNode()
        {
            ConnectionGene connection = Connections.RandomElement();

            if (connection == null)
            {
                return;
            }

            NodeGene from = connection.In;
            NodeGene to   = connection.Out;

            int replaceIndex = Neat.GetReplaceIndex(from, to);

            NodeGene middle;

            if (replaceIndex == 0)
            {
                ActivationEnumeration a = ActivationEnumeration.Random();
                middle                = Neat.CreateNode();
                middle.X              = (from.X + to.X) / 2;
                middle.Y              = ((from.Y + to.Y) / 2) + (ThreadSafeRandom.NormalRand(0, 0.02f) / 2);
                middle.Activation     = a.Activation;
                middle.ActivationName = a.Name;
                Neat.SetReplaceIndex(from, to, middle.InnovationNumber);
            }
            else
            {
                middle = Neat.GetNode(replaceIndex);
            }

            ConnectionGene connection1 = Neat.GetConnection(from, middle);
            ConnectionGene connection2 = Neat.GetConnection(middle, to);

            connection1.Weight  = 1;
            connection2.Weight  = connection.Weight;
            connection2.Enabled = connection.Enabled;

            connection.Enabled = false;
            Connections.Add(connection1);
            Connections.Add(connection2);

            Nodes.Add(middle);
        }
Exemplo n.º 5
0
        ///<inheritdoc/>
        public void MutateLink()
        {
            for (int i = 0; i < 100; i++)
            {
                NodeGene a = Nodes.RandomElement();
                NodeGene b = Nodes.RandomElement();

                if (a == null || b == null)
                {
                    continue;
                }
                if (a.X.Equals(b.X))
                {
                    continue;
                }

                ConnectionGene connection;
                if (a.X < b.X)
                {
                    connection = new ConnectionGene(a, b);
                }
                else
                {
                    connection = new ConnectionGene(b, a);
                }

                if (Connections.Contains(connection))
                {
                    continue;
                }

                connection         = Neat.GetConnection(connection.In, connection.Out);
                connection.Weight += ThreadSafeRandom.NormalRand(0, 0.2f) * Constants.WEIGHT_SHIFT_STRENGTH;

                AddSorted(connection);

                return;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// TODO : move this to unit tests
        /// </summary>
        /// <returns></returns>
        public RandomList <Client> CheckEvolutionProcess()
        {
            Neat neat = new Neat();

            double[] inputs = new double[Constants.InputSize];
            for (int i = 0; i < Constants.InputSize; i++)
            {
                inputs[i] = ThreadSafeRandom.NormalRand();
            }

            for (int i = 0; i < 100; i++)
            {
                foreach (Client c in neat.AllClients)
                {
                    c.Score = c.Calculate(inputs)[0];
                }
                neat.Evolve();
                //neat.TraceClients();
                neat.TraceSpecies();
            }

            return(neat.AllClients);
        }