private bool LoadSeedInit() { NetworkGenome genome = NetworkGenome.LoadFromFile(this.Init.Seed.file); if (genome == null) { return(false); } genome.Fitness = 0.0; genome.Age = 0; genome.Species = 0; genome.Id = 0; this.Current.Genomes.Add(genome); this.species.Add(new Species(0, 0, 0.0, genome)); this.Init.MaxLayers = Enumerable.First <NodeGene>((IEnumerable <NodeGene>)genome.Nodes, (Func <NodeGene, bool>)(g => g.Type == NodeType.Output)).Layer + 1; this.Init.Inputs = Enumerable.Count <NodeGene>((IEnumerable <NodeGene>)genome.Nodes, (Func <NodeGene, bool>)(g => g.Type == NodeType.Input)); this.Init.Outputs = Enumerable.Count <NodeGene>((IEnumerable <NodeGene>)genome.Nodes, (Func <NodeGene, bool>)(g => g.Type == NodeType.Output)); NetworkGenome.NextId = 1; foreach (LinkGene LinkGenes in genome.Links) { LinkGene.NextId = Math.Max(LinkGene.NextId, LinkGenes.Id); } foreach (NodeGene NodeGenes in genome.Nodes) { NodeGene.NextId = Math.Max(NodeGene.NextId, NodeGenes.Id); } while (this.Current.Genomes.Count < this.Init.PopulationSize) { NetworkGenome networkGenomes = genome.Clone() as NetworkGenome; networkGenomes.Id = NetworkGenome.NextId; networkGenomes.Parent1 = 0; networkGenomes.Parent2 = -1; this.RandomizeWeights(networkGenomes); double num1 = 1000000.0; int index1 = 0; for (int index2 = 0; index2 < this.species.Count; ++index2) { double num2 = this.species[index2].GenomeCompatibility(networkGenomes); if (num2 < num1) { num1 = num2; index1 = index2; } } if (num1 <= this.Speciate.Threshold) { this.species[index1].Genomes.Add(networkGenomes); networkGenomes.Species = index1; } else { this.species.Add(new Species(0, 0, 0.0, networkGenomes)); networkGenomes.Species = this.species.Count - 1; } this.Current.Genomes.Add(networkGenomes); } return(true); }
private bool FreshInit() { NetworkGenome NetworkGenomes1 = new NetworkGenome(-1, 0, -1, -1, -1); NetworkGenomes1.Nodes.Add(new NodeGene("Identity", NodeGene.NextId, 0, NodeType.Bias)); for (int index = 0; index < this.Init.Inputs; ++index) { NetworkGenomes1.Nodes.Add(new NodeGene("Identity", NodeGene.NextId, 0, NodeType.Input)); } for (int index = 0; index < this.Init.Outputs; ++index) { NetworkGenomes1.Nodes.Add(new NodeGene("BipolarSigmoid", NodeGene.NextId, this.Init.MaxLayers - 1, NodeType.Output)); } while (this.Current.Genomes.Count < this.Init.PopulationSize) { NetworkGenome NetworkGenomes2 = NetworkGenomes1.Clone() as NetworkGenome; NetworkGenomes2.Id = NetworkGenome.NextId; this.InitLinks(NetworkGenomes2); double num1 = 1000000.0; int index1 = 0; for (int index2 = 0; index2 < this.species.Count; ++index2) { double num2 = this.species[index2].GenomeCompatibility(NetworkGenomes2); if (num2 < num1) { num1 = num2; index1 = index2; } } if (num1 <= this.Speciate.Threshold) { this.species[index1].Genomes.Add(NetworkGenomes2); NetworkGenomes2.Species = index1; } else { this.species.Add(new Species(0, 0, 0.0, NetworkGenomes2)); NetworkGenomes2.Species = this.species.Count - 1; } this.Current.Genomes.Add(NetworkGenomes2); } return(true); }
private NetworkGenome CreateOneOffspringFromSpecies(List <NetworkGenome> list) { NetworkGenome parent1 = this.SelectGenome(list); NetworkGenome temp; if (Utilities.Rnd.NextDouble() < this.Reprod.AsexualProbability || list.Count < 2) { temp = parent1.Clone() as NetworkGenome; temp.Age = 0; temp.Fitness = 0.0; temp.Id = NetworkGenome.NextId; temp.Parent1 = parent1.Id; temp.Parent2 = -1; this.MutateGenome(temp); } else { NetworkGenome parent2 = this.SelectGenome(list, parent1); temp = this.Recombine(parent1, parent2); } this.DetermineSpecies(temp); return(temp); }
public static DecodedNetworks DecodeGenome(NetworkGenome genome) { DecodedNetworks decodedNetwork = new DecodedNetworks(); NetworkGenome NetworkGenome = genome.Clone() as NetworkGenome; NetworkGenome.Nodes.Sort(); for (int index = 0; index < NetworkGenome.Nodes.Count; ++index) { if (NetworkGenome.Nodes[index].Id != index) { foreach (LinkGene LinkGenes in NetworkGenome.Links) { if (LinkGenes.Source == NetworkGenome.Nodes[index].Id) { LinkGenes.Source = index; } if (LinkGenes.Target == NetworkGenome.Nodes[index].Id) { LinkGenes.Target = index; } } } NetworkGenome.Nodes[index].Id = index; } foreach (NodeGene NodeGenes in NetworkGenome.Nodes) { DecodedNetworks.Node node = new DecodedNetworks.Node(); node.activation = ActivationFunctions.GetFunction(NodeGenes.Function); foreach (LinkGene LinkGenes in NetworkGenome.Links) { if (LinkGenes.Target == NodeGenes.Id) { node.links.Add(new DecodedNetworks.Link() { sourceNode = LinkGenes.Source, weight = LinkGenes.Weight }); } } if (NodeGenes.Type == NodeType.Bias) { node.signal = 1.0; decodedNetwork.Neurons.Add(node); } else if (NodeGenes.Type == NodeType.Input) { decodedNetwork.Neurons.Add(node); decodedNetwork.Inputs.Add(node); } else if (NodeGenes.Type == NodeType.Hidden) { decodedNetwork.Neurons.Add(node); decodedNetwork.ActivatingNodes.Add(node); } else { decodedNetwork.Neurons.Add(node); decodedNetwork.ActivatingNodes.Add(node); decodedNetwork.Outputs.Add(node); } } decodedNetwork.OutputArray = new double[decodedNetwork.Outputs.Count]; return(decodedNetwork); }