public static INeatGenome Crossover(INeatGenome genome1, INeatGenome genome2) { var childGenome = new NeatGenome(genome1.NetworkParameters, genome1.MutationParameters); var nodes = genome1.NodeList.Union(genome2.NodeList).ToList(); foreach (var node in nodes) { childGenome._nodeList.Add(new NodeGene(node.NodeType, node.Idx, node.ActivationType)); } var childConnectionList = genome1.ConnectionList.Union(genome2.ConnectionList).ToList(); foreach (var connectionIdx in childConnectionList) { childGenome._connectionList.Add(connectionIdx); } IConnectionGene connectionGene = null; for (int i = 0; i < childConnectionList.Count; i++) { var childConnectionIdx = childConnectionList[i]; if (genome1.Connections.ContainsKey(childConnectionIdx)) { connectionGene = genome1.Connections[childConnectionIdx]; } else if (genome2.Connections.ContainsKey(childConnectionIdx)) { connectionGene = genome2.Connections[childConnectionIdx]; } else { throw new ArgumentOutOfRangeException(nameof(childConnectionList), "Parent genomes don't have child connection"); } childGenome._connections.Add(childConnectionIdx, new ConnectionGene(connectionGene)); } var childVacantConnections = genome1.VacantConnections.Union(genome2.VacantConnections).ToList(); foreach (var vacantConnection in childVacantConnections) { if (childGenome.Connections.Values .FirstOrDefault(c => (c.SourceNodeIdx == vacantConnection.Item1) && (c.TargetNodeIdx == vacantConnection.Item2)) == null) { childGenome._vacantConnections.Add((vacantConnection.Item1, vacantConnection.Item2)); } } return(childGenome); }
/// <summary> /// Dump to Console genome structure /// </summary> /// <param name="genome">Genome</param> public static void PrintGenomeStructure(this INeatGenome genome) { Console.WriteLine("-------------------"); Console.WriteLine("Genome structure"); Console.WriteLine(); Console.WriteLine($"Input node count: {genome.InputNodeCount}"); Console.WriteLine($"Output node count: {genome.OutputNodeCount}"); Console.WriteLine($"Node count: {genome.NodeList.Count}"); Console.WriteLine($"Connections count: {genome.Connections.Count}"); Console.WriteLine(); Console.WriteLine("[Connections]"); foreach (var connection in genome.Connections) { Console.WriteLine($"Id: {connection.Key} {connection.Value.SourceNodeIdx}->{connection.Value.TargetNodeIdx} {connection.Value.IsActive}"); } Console.WriteLine(); Console.WriteLine("-------------------"); }
public XorIndividual(INeatGenome genome) { Genome = genome; Error = double.NaN; }
} // Correct recognized digits public MnistIndividual(INeatGenome genome) { Genome = genome; Error = double.NaN; Fitness = float.NaN; }
public NetworkBuilder(INeatGenome genome) { _genome = genome; _nextNetNodeId = NetworkParameters.BiasNodeCount + _genome.InputNodeCount + _genome.OutputNodeCount; _netMap = new Dictionary <NodeMap, IList <INetConnection> >(_nextNetNodeId); }