Example #1
0
        void SetUpNeurons()
        {
            if (Brain == null)
            {
                return;
            }

            int i = 0;

            EyeR_I        = GetListSliceAsArray(i, i += EyeNeuronsAmount, Brain.Inputs);
            EyeG_I        = GetListSliceAsArray(i, i += EyeNeuronsAmount, Brain.Inputs);
            EyeB_I        = GetListSliceAsArray(i, i += EyeNeuronsAmount, Brain.Inputs);
            EyeDistance_I = GetListSliceAsArray(i, i += EyeNeuronsAmount, Brain.Inputs);

            random_I = Brain.Inputs[i++];
            clock_I  = Brain.Inputs[i++];
            energy_I = Brain.Inputs[i++];

            i        = 0;
            rotate_O = Brain.Outputs[i++];
            move_O   = Brain.Outputs[i++];
            eat_O    = Brain.Outputs[i++];
            mate_O   = Brain.Outputs[i++];
            attack_O = Brain.Outputs[i++];

            i = 0;
            //BodyGenes
        }
Example #2
0
        public void ApplyInputs_WithValidNetwork_PropogatesNetworkInputs()
        {
            // Arrange
            var network = new DFFNeuralNetwork(1, 1, 2, 2);

            var inputs = new List <INetworkInput>()
            {
                new NetworkInput()
                {
                    ActivationLevel = .75
                }
            };

            network.RandomizeNetwork();

            IInputNeuron        input1            = network.Layers.OfType <IInputLayer>().First().Neurons.First() as IInputNeuron;
            IOutgoingConnection input1Hidden1Conn = input1.Connections.OfType <IOutgoingConnection>().First();
            IOutgoingConnection input1Hidden2Conn = input1.Connections.OfType <IOutgoingConnection>().ToList()[1];

            IHiddenNeuron       hidden1            = input1Hidden1Conn.ToNeuron as IHiddenNeuron;
            IOutgoingConnection hidden1Output1Conn = hidden1.Connections.OfType <IOutgoingConnection>().First();
            IOutgoingConnection hidden1Output2Conn = hidden1.Connections.OfType <IOutgoingConnection>().ToList()[1];

            IHiddenNeuron       hidden2            = input1Hidden2Conn.ToNeuron as IHiddenNeuron;
            IOutgoingConnection hidden2Output1Conn = hidden2.Connections.OfType <IOutgoingConnection>().First();
            IOutgoingConnection hidden2Output2Conn = hidden2.Connections.OfType <IOutgoingConnection>().ToList()[1];

            IOutputNeuron output1 = hidden1Output1Conn.ToNeuron as IOutputNeuron;
            IOutputNeuron output2 = hidden1Output2Conn.ToNeuron as IOutputNeuron;

            // Act
            network.ApplyInputs(inputs);

            var           outputLayer  = network.Layers.OfType <IOutputLayer>().First();
            IOutputNeuron output1After = outputLayer.Neurons.First() as IOutputNeuron;
            IOutputNeuron output2After = outputLayer.Neurons.ToList()[1] as IOutputNeuron;

            // sigmoid function : (1.0 / (1 + Math.Exp(-1.0 * value)));

            var h1Activation = ApplyActivationFunction((.75 * input1Hidden1Conn.Weight) + hidden1.Bias);
            var h2Activation = ApplyActivationFunction((.75 * input1Hidden2Conn.Weight) + hidden2.Bias);
            var o1Activation = ApplyActivationFunction((h1Activation * hidden1Output1Conn.Weight) + (h2Activation * hidden2Output1Conn.Weight) + output1.Bias);
            var o2Activation = ApplyActivationFunction((h1Activation * hidden1Output2Conn.Weight) + (h2Activation * hidden2Output2Conn.Weight) + output2.Bias);

            // Assert
            Assert.IsTrue(o1Activation == output1After.ActivationLevel);
            Assert.IsTrue(o2Activation == output2After.ActivationLevel);
        }
        /// <summary>
        /// Creates the incoming and outgoing connections between this neuron and a output neuron.
        /// </summary>
        /// <param name="neuron">The output neuron to connect this neuron to.</param>
        public void GenerateConnectionsWith(IOutputNeuron neuron)
        {
            if (neuron == null)
            {
                throw new ArgumentNullException("neuron");
            }

            // Create connection only if this neuron doesn't already have an outgoing connection to the output neuron.
            if (!GetOutgoingConnections().Any(c => c.ToNeuron == neuron))
            {
                Connections.Add(new OutgoingConnection(neuron));
            }

            // Create connection only if the output neuron doesn't already have an incoming connection from this neuron.
            if (!neuron.GetIncomingConnections().Any(c => c.FromNeuron == this))
            {
                neuron.Connections.Add(new IncomingConnection(this));
            }
        }
Example #4
0
        public override void Load(BinaryReader r, uint id)
        {
            base.Load(r, id);

            Brain          = ReadNullableObject <INeuralNet>(r);
            CreatureGenome = ReadNullableObject <Genome>(r);

            EyeNeuronsAmount = r.ReadInt32();
            EyeSpan          = r.ReadDouble();
            ViewDistance     = r.ReadInt32();
            Speed            = r.ReadDouble();
            RotationSpeed    = r.ReadDouble();
            Energy           = DoubleMinMax.Read(r);

            CanSeeCreatures = r.ReadBoolean();

            EnergyLossByTick     = r.ReadDouble();
            EnergyLossByRotation = r.ReadDouble();
            EnergyLossByMove     = r.ReadDouble();
            EnergyLossByEating   = r.ReadDouble();
            EnergyLossByMating   = r.ReadDouble();
            EatingBiteSize       = r.ReadDouble();

            MatingCandidate       = BinarySerializable.GetObject <Creature>(r);
            CyclesInMating        = r.ReadInt32();
            CyclesNeededForMating = r.ReadInt32();

            MatingCooldownCyclesLeft    = r.ReadInt32();
            MatingCooldown              = r.ReadInt32();
            AttackingCooldownCyclesLeft = r.ReadInt32();
            AttackingCooldown           = r.ReadInt32();
            AttackMultiplier            = r.ReadDouble();

            MaxAge = r.ReadInt32();

            PixelsRotatedLeft    = r.ReadDouble();
            PixelsRotatedRight   = r.ReadDouble();
            PixelsMovedForwards  = r.ReadDouble();
            PixelsMovedBackwards = r.ReadDouble();
            TimesHaveEaten       = r.ReadInt32();
            TimesMated           = r.ReadInt32();

            LoseEnergy       = r.ReadBoolean();
            IsThinking       = r.ReadBoolean();
            DisableMating    = r.ReadBoolean();
            DisableAttacking = r.ReadBoolean();

            AlwaysMate = r.ReadBoolean();
            AlwaysEat  = r.ReadBoolean();

            if (Brain != null)
            {
                EyeR_I        = LoadArrayInfo <IInputNeuron>(r);
                EyeG_I        = LoadArrayInfo <IInputNeuron>(r);
                EyeB_I        = LoadArrayInfo <IInputNeuron>(r);
                EyeDistance_I = LoadArrayInfo <IInputNeuron>(r);

                random_I = BinarySerializable.GetObject <IInputNeuron>(r);
                clock_I  = BinarySerializable.GetObject <IInputNeuron>(r);
                energy_I = BinarySerializable.GetObject <IInputNeuron>(r);

                rotate_O = BinarySerializable.GetObject <IOutputNeuron>(r);
                move_O   = BinarySerializable.GetObject <IOutputNeuron>(r);
                eat_O    = BinarySerializable.GetObject <IOutputNeuron>(r);
                mate_O   = BinarySerializable.GetObject <IOutputNeuron>(r);
                attack_O = BinarySerializable.GetObject <IOutputNeuron>(r);
            }
        }
Example #5
0
 public void Initialize()
 {
     _inputNeuron  = new InputNeuron();
     _hiddenNeuron = new HiddenNeuron();
     _outputNeuron = new OutputNeuron();
 }