Exemplo n.º 1
0
        private static ConnectionGene GetGeneFromFitterParent(NeatEvaluator evaluator, Genome fitParent, bool fitnessEqual, ConnectionGene connection, Genome genome)
        {
            ConnectionGene geneToTake = null;

            if (fitnessEqual)
            {
                if (evaluator.GetRandomFloat() < 0.5)
                {
                    geneToTake = connection.Copy(null);
                }
            }
            else
            {
                if (genome == fitParent)
                {
                    geneToTake = connection.Copy(null);
                }
            }

            return(geneToTake);
        }
Exemplo n.º 2
0
 private void DebugConnection(ConnectionGene c)
 {
 }
Exemplo n.º 3
0
        private static ConnectionGene GetMatchingGene(NeatEvaluator evaluator, ConnectionGene connection1, ConnectionGene connection2, float disableInheritChance)
        {
            ConnectionGene geneToTake;

            if (evaluator.GetRandomFloat() < 0.5)
            {
                geneToTake = connection1.Copy(null);
            }
            else
            {
                geneToTake = connection2.Copy(null);
            }

            if (connection1.Enabled || connection2.Enabled)
            {
                if (!connection1.Enabled || !connection2.Enabled)
                {
                    geneToTake.Enabled = true;
                    if (evaluator.GetRandomFloat() < disableInheritChance)
                    {
                        geneToTake.Enabled = false;
                    }
                }
            }

            return(geneToTake);
        }
Exemplo n.º 4
0
        public void MutateConnection()
        {
            bool geneAdded = false;

            while (!geneAdded)
            {
                if (NodeCollection.Nodes.Count == 0)
                {
                    break;
                }

                bool geneValid = true;

                int indexInput  = Evaluator.Random.Next(0, NodeCollection.Count);
                int indexOutput = Evaluator.Random.Next(0, NodeCollection.Count);
                var input       = NodeCollection[indexInput];
                var output      = NodeCollection[indexOutput];

                //if (input.Id >= output.Id || (input.Type & output.Type) == NodeGeneType.Sensor)
                //    geneValid = false;

                if (Config.FeedForwardNetwork && ((input.Type & output.Type) == NodeGeneType.Sensor ||
                                                  (input.Type & output.Type) == NodeGeneType.Output ||
                                                  input.Id == output.Id) ||
                    //(output.Type == NodeGeneType.Output && input.Type == NodeGeneType.Hidden) ||
                    //(input.Type == NodeGeneType.Output && output.Type == NodeGeneType.Sensor) ||
                    input.Type == NodeGeneType.Output || output.Type == NodeGeneType.Sensor)
                {
                    geneValid = false;
                }

                (int, int)key = (input.Id, output.Id);

                if (geneValid)
                {
                    if (ConnectionCollection.Exists(key))
                    {
                        geneValid = false;
                        break;
                    }

                    ConnectionGene connection;
                    if (!Evaluator.Connections.Exists(key))
                    {
                        connection = new ConnectionGene
                        {
                            Enabled          = true,
                            Input            = input.Id,
                            Output           = output.Id,
                            Weight           = Evaluator.GetRandomNumber(-Config.WeightMutationPower, Config.WeightMutationPower),
                            InnovationNumber = Evaluator.GetNextInnovationId(),
                            Genome           = this
                        };
                        Evaluator.AddConnection(connection);
                    }
                    else
                    {
                        // old connection gets copied
                        connection        = Evaluator.Connections.Get(key).Copy(this);
                        connection.Weight = Evaluator.GetRandomNumber(-Config.WeightMutationPower,
                                                                      Config.WeightMutationPower);

                        if (connection.Input != key.Item1 || connection.Output != key.Item2)
                        {
                            throw new InvalidOperationException();
                        }

                        connection.Enabled = true;
                    }

                    geneAdded = true;
                    ConnectionCollection.AddNew(connection);
                }
            }
        }