Exemple #1
0
        public void CloneFrom(NeuronNetwork other, Random rnd, bool severeMutations, double severity)
        {
            if (!severeMutations)
            {
                CloneRegular(other, rnd);
            }
            else
            {
                CloneSevereRandomValues(other, rnd, severity);
                switch (rnd.Next(4))
                {
                case 0:
                    CloneSevereRandomValues(other, rnd, severity);
                    break;

                case 1:
                    CloneSevereShrinkNetwork(other, rnd, severity);
                    break;

                case 2:
                    CloneSevereExtendNetwork(other, rnd, severity);
                    break;

                case 3:
                    CloneSevereExtendNetworkZeroLevel(other, rnd, severity);
                    break;
                }
            }
        }
Exemple #2
0
        private void CloneSevereShrinkNetwork(NeuronNetwork other, Random rnd, double severity)
        {
            Debug.Assert(other.Neurons.Length == other.InputVector.Length);
            Debug.Assert(other.Neurons.Length == other.OutputVector.Length);

            bool[] keepVector =
                other.Neurons
                .Select(x => rnd.NextDouble() >= severity)
                .ToArray();

            if (keepVector.Count(keep => keep) < AppProperties.MinNetworkSize)
            {
                CloneSevereRandomValues(other, rnd, severity); // failback
                return;
            }

            Neurons = other.Neurons
                      .Where((n, idx) => keepVector[idx])
                      .Select(n => Neuron.CloneFromWithShrink(n, rnd, keepVector))
                      .ToArray();

            UpdateEye();

            InputVector  = other.InputVector.Where((x, idx) => keepVector[idx]).ToArray();
            OutputVector = other.OutputVector.Where((x, idx) => keepVector[idx]).ToArray();

            Debug.Assert(Neurons.Length == InputVector.Length);
            Debug.Assert(Neurons.Length == OutputVector.Length);
#if DEBUG
            foreach (var neuron in Neurons)
            {
                Debug.Assert(neuron.Weights.Length == Neurons.Length);
            }
#endif
        }
Exemple #3
0
        private void CloneRegular(NeuronNetwork other, Random rnd)
        {
            Neurons = other.Neurons
                      .Select(x => Neuron.CloneFrom(x, rnd))
                      .ToArray();

            InputVector  = other.InputVector.Select(x => x).ToArray();
            OutputVector = other.OutputVector.Select(x => x).ToArray();
        }
Exemple #4
0
        public Cell(Random r, int maxX, int maxY)
        {
            LocationX = r.Next(maxX);
            LocationY = r.Next(maxY);
            Rotation  = r.NextDouble() * 2.0 * Math.PI;
            Network   = new NeuronNetwork(AppProperties.InitialNetworkSize, r);

            Random = new Random(r.Next());
        }
Exemple #5
0
 public void CloneFrom(NeuronNetwork other, Random rnd, bool severeMutations, float severity)
 {
     if (!severeMutations)
     {
         CloneRegular(other, rnd);
     }
     else
     {
         CloneSevereRandomValues(other, rnd, severity);
     }
 }
Exemple #6
0
        private void CloneSevereRandomValues(NeuronNetwork other, Random rnd, float severity)
        {
            Neurons = other.Neurons
                      .Select(x =>
                              (rnd.NextDouble() < severity)
                        ? Neuron.CloneFromWithSevereRandom(x, rnd)
                        : Neuron.CloneFrom(x, rnd))
                      .ToArray();

            InputVector  = other.InputVector.Select(x => x).ToArray();
            OutputVector = other.OutputVector.Select(x => x).ToArray();
        }
Exemple #7
0
        private void CloneSevereExtendNetworkZeroLevel(NeuronNetwork other, Random rnd, double severity)
        {
            Debug.Assert(other.Neurons.Length == other.InputVector.Length);
            Debug.Assert(other.Neurons.Length == other.OutputVector.Length);

            bool[] doubleVector =
                other.Neurons
                .Select(x => rnd.NextDouble() >= severity)
                .ToArray();

            if (other.Size + doubleVector.Count(dbl => dbl) > AppProperties.MaxNetworkSize)
            {
                CloneSevereRandomValues(other, rnd, severity); // failback
                return;
            }

            Neurons =
                other.Neurons
                .Concat(other.Neurons.Where((n, idx) => doubleVector[idx]))
                .Select(n => Neuron.CloneFromWithExpansionZeroValues(n, rnd, doubleVector))
                .ToArray();

            UpdateEye();

            InputVector  = other.InputVector.Concat(other.InputVector.Where((x, idx) => doubleVector[idx])).ToArray();
            OutputVector = other.OutputVector.Concat(other.OutputVector.Where((x, idx) => doubleVector[idx])).ToArray();

            Debug.Assert(Neurons.Length == InputVector.Length);
            Debug.Assert(Neurons.Length == OutputVector.Length);
#if DEBUG
            foreach (var neuron in Neurons)
            {
                Debug.Assert(neuron.Weights.Length == Neurons.Length);
            }
#endif
        }