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); }
public static Genome CreateDefault(NeatEvaluator evaluator) { var g = new Genome(evaluator); g.GenomeId = evaluator.GetNextGenomeId(); g.InitializeDefaultNodes(); return(g); }
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); }
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)); }
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); }
public Genome(NeatEvaluator evaluator) { Evaluator = evaluator; }
public Species(NeatEvaluator evaluator) { Evaluator = evaluator; }
public SpeciesCollection(NeatEvaluator evaluator) { Evaluator = evaluator; }