Beispiel #1
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);
        }
Beispiel #2
0
        public static Genome CreateDefault(NeatEvaluator evaluator)
        {
            var g = new Genome(evaluator);

            g.GenomeId = evaluator.GetNextGenomeId();
            g.InitializeDefaultNodes();
            return(g);
        }
Beispiel #3
0
        public static Genome CreateFromCrossover(NeatEvaluator evaluator, IEnumerable <NodeGene> nodes, IEnumerable <ConnectionGene> connections)
        {
            var g = new Genome(evaluator);

            g.GenomeId = evaluator.GetNextGenomeId();
            g.InitializeDefaultNodes();
            g.InitializeFromCrossover(nodes, connections);
            return(g);
        }
Beispiel #4
0
        public static Genome Crossover(NeatEvaluator evaluator, Genome genome1, Genome genome2)
        {
            float disableInheritChance  = evaluator.NeatConfig.DisableInheritChance;
            float fitnessEqualTolerance = evaluator.NeatConfig.FitnessEqualTolerance;

            var  result       = CompareGenomes(genome1, genome2);
            var  fitParent    = genome1.Fitness > genome2.Fitness ? genome1 : genome2;
            bool fitnessEqual = System.Math.Abs(genome1.Fitness - genome2.Fitness) < fitnessEqualTolerance;

            List <ConnectionGene> crossoverConnections = new List <ConnectionGene>();
            List <NodeGene>       nodesToTake          = new List <NodeGene>();

            foreach (var con in result.MatchingGenes)
            {
                var geneToTake = GetMatchingGene(evaluator, con.connection1, con.connection2, disableInheritChance);

                crossoverConnections.Add(geneToTake);
            }

            foreach (var con in result.DisjointGenes)
            {
                var geneToTake = GetGeneFromFitterParent(evaluator, fitParent, fitnessEqual, con.connection, con.genome);
                if (geneToTake != null)
                {
                    crossoverConnections.Add(geneToTake);
                }
            }

            foreach (var con in result.ExcessGenes)
            {
                var geneToTake = GetGeneFromFitterParent(evaluator, fitParent, fitnessEqual, con.connection, con.genome);
                if (geneToTake != null)
                {
                    crossoverConnections.Add(geneToTake);
                }
            }

            nodesToTake.AddRange(genome1.NodeCollection.NodeList.Where(x => x.Type != NodeGeneType.Hidden));

            return(Genome.CreateFromCrossover(evaluator, nodesToTake, crossoverConnections));
        }
Beispiel #5
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);
        }
Beispiel #6
0
 public Genome(NeatEvaluator evaluator)
 {
     Evaluator = evaluator;
 }
Beispiel #7
0
 public Species(NeatEvaluator evaluator)
 {
     Evaluator = evaluator;
 }
Beispiel #8
0
 public SpeciesCollection(NeatEvaluator evaluator)
 {
     Evaluator = evaluator;
 }