private void RandomizeWeights(NetworkGenome temp) { foreach (LinkGene LinkGenes in temp.Links) { LinkGenes.Weight = Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0); } }
private void DetermineSpecies(NetworkGenome temp) { int index1 = -1; double num1 = 100000.0; for (int index2 = 0; index2 < this.species.Count; ++index2) { if (!this.species[index2].Extinct) { double num2 = this.species[index2].GenomeCompatibility(temp); if (num2 < num1) { num1 = num2; index1 = index2; } } } if (num1 <= this.Speciate.Threshold || (double)Enumerable.Count <Species>((IEnumerable <Species>) this.species, (Func <Species, bool>)(g => !g.Extinct)) > (double)this.Speciate.SpeciesCount * 1.5) { temp.Species = index1; this.species[index1].Genomes.Add(temp); } else { temp.Species = this.species.Count; this.species.Add(new Species(this.Generation, this.Generation, 0.0, temp)); } }
private NetworkGenome SelectGenome(List <NetworkGenome> list, NetworkGenome parent1) { List <NetworkGenome> list1 = new List <NetworkGenome>((IEnumerable <NetworkGenome>)list); list1.Remove(parent1); return(this.SelectGenome(list1)); }
private bool AddNode(NetworkGenome temp) { List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks); if (list.Count < 1) { return(false); } LinkGene split = list[(int)(Utilities.Rnd.NextDouble() * (double)list.Count)]; NodeGene NodeGenes1 = temp.Nodes.Find((Predicate <NodeGene>)(g => g.Id == split.Source)); NodeGene NodeGenes2 = temp.Nodes.Find((Predicate <NodeGene>)(g => g.Id == split.Target)); if (Math.Abs(NodeGenes1.Layer - NodeGenes2.Layer) <= 1) { return(false); } NodeGene NodeGenes3 = new NodeGene("Identity", 0, Utilities.Rnd.Next(Math.Min(NodeGenes1.Layer, NodeGenes2.Layer) + 1, Math.Max(NodeGenes1.Layer, NodeGenes2.Layer)), NodeType.Hidden); NodeGenes3.Id = HistoricalMarkings.GetNodeMarking(split.Id, NodeGenes3.Layer, this.Generation); NodeGenes3.Function = this.SelectFunction(); LinkGene LinkGenes1 = new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes1.Id, NodeGenes3.Id, this.Generation), NodeGenes1.Id, NodeGenes3.Id, split.Weight); LinkGene LinkGenes2 = new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes3.Id, NodeGenes2.Id, this.Generation), NodeGenes3.Id, NodeGenes2.Id, Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, Utilities.Rnd.NextGuassian(0.0, 0.2) * this.Mutate.WeightRange / 2.0))); temp.Nodes.Add(NodeGenes3); temp.Links.Add(LinkGenes1); temp.Links.Add(LinkGenes2); if (Utilities.Rnd.NextDouble() > this.Mutate.KeepLinkOnAddNode) { temp.Links.Remove(split); } return(true); }
private void Save() { this.Current.Genomes.Sort((Comparison <NetworkGenome>)((b, a) => a.Fitness.CompareTo(b.Fitness))); NetworkGenome genome = this.Current.Genomes[0]; this.Champions.Add(genome); this.Current.Genomes.Sort((Comparison <NetworkGenome>)((a, b) => a.Species.CompareTo(b.Species))); if (this.Saves.Champions.save && this.Generation % this.Saves.Champions.inteval == 0) { NetworkGenome.SaveToFile(genome, this.Saves.Champions.path + "G" + this.Generation.ToString("0000") + "Champ.xml"); } if (this.Saves.Populations.save && this.Generation % this.Saves.Populations.inteval == 0) { Population.SaveToXml(this.Saves.Populations.path + "G" + this.Generation.ToString("0000") + "Pop.xml", this.Current); } if (this.Saves.PopulationStatistics.save && (this.Generation % this.Saves.PopulationStatistics.inteval == 0 || this.Generation == 1)) { this.PopulationStatsFile.WriteLine((string)(object)this.Current.Generation + (object)"\t" + (string)(object)this.Current.MeanFitness + "\t" + (string)(object)this.Current.MaxFitness); } if (this.Saves.Species.save && this.Generation % this.Saves.Species.inteval == 0) { for (int index = 0; index < this.species.Count; ++index) { Species.SaveToXml(this.Saves.Species.path + "G" + this.Generation.ToString("0000") + "Species" + index.ToString("0000") + ".xml", this.species[index]); } } if (!this.Saves.SpeciesStatistics.save || this.Generation % this.Saves.SpeciesStatistics.inteval != 0) { return; } this.WriteSpeciesStats(); }
public static bool SaveToFile(NetworkGenome genome, string file) { using (StreamWriter streamWriter = new StreamWriter(file)) { NetworkGenome.serializer.Serialize((TextWriter)streamWriter, (object)genome); return(true); } }
public static void subXor(NetworkGenome cppn) { DecodedNetworks decodedNetwork = (DecodedNetworks)null; double[] inputs = new double[4]; bool flag1 = true; inputs[0] = 1.0; inputs[1] = 0.0; inputs[2] = 0.0; inputs[3] = 0.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(2); double[] outputs1 = decodedNetwork.GetOutputs(); cppn.Fitness = 0.0; cppn.Fitness += Math.Min(1.0, Math.Max(0.0, 1.0 - outputs1[0])); bool flag2 = flag1 && outputs1[0] < 0.5; decodedNetwork.Flush(); inputs[0] = 0.0; inputs[1] = 0.0; inputs[2] = 0.0; inputs[3] = 1.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(2); double[] outputs2 = decodedNetwork.GetOutputs(); cppn.Fitness += Math.Min(1.0, Math.Max(0.0, 1.0 - outputs2[0])); bool flag3 = flag2 && outputs2[0] < 0.5; decodedNetwork.Flush(); inputs[0] = 0.0; inputs[1] = 1.0; inputs[2] = 0.0; inputs[3] = 0.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(2); double[] outputs3 = decodedNetwork.GetOutputs(); cppn.Fitness += Math.Min(1.0, Math.Max(0.0, outputs3[0])); bool flag4 = flag3 && outputs3[0] >= 0.5; decodedNetwork.Flush(); inputs[0] = 0.0; inputs[1] = 0.0; inputs[2] = 1.0; inputs[3] = 0.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(2); double[] outputs4 = decodedNetwork.GetOutputs(); cppn.Fitness += Math.Min(1.0, Math.Max(0.0, outputs4[0])); bool flag5 = flag4 && outputs4[0] >= 0.5; decodedNetwork.Flush(); if (!flag5) { return; } cppn.Fitness = 4.0; }
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); }
public int CompareTo(object obj) { NetworkGenome networkGenome = obj as NetworkGenome; if (networkGenome == null) { throw new InvalidOperationException("Object being compared to is not a Network Genome!"); } return(this.Id.CompareTo(networkGenome.Id)); }
private bool DelectConnection(NetworkGenome temp) { List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks); if (list.Count < 1) { return(false); } LinkGene LinkGenes = list[(int)(Utilities.Rnd.NextDouble() * (double)list.Count)]; temp.Links.Remove(LinkGenes); return(true); }
public static void Xor(object target) { NetworkGenome genome = target as NetworkGenome; double[] inputs = new double[2]; bool flag1 = true; DecodedNetworks decodedNetwork = DecodedNetworks.DecodeGenome(genome); inputs[0] = 0.0; inputs[1] = 0.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(6); double[] outputs1 = decodedNetwork.GetOutputs(); genome.Fitness = 0.0; genome.Fitness += Math.Min(1.0, Math.Max(0.0, 1.0 - outputs1[0])); bool flag2 = flag1 && outputs1[0] < 0.5; decodedNetwork.Flush(); inputs[0] = 1.0; inputs[1] = 1.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(6); double[] outputs2 = decodedNetwork.GetOutputs(); genome.Fitness += Math.Min(1.0, Math.Max(0.0, 1.0 - outputs2[0])); bool flag3 = flag2 && outputs2[0] < 0.5; decodedNetwork.Flush(); inputs[0] = 0.0; inputs[1] = 1.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(6); double[] outputs3 = decodedNetwork.GetOutputs(); genome.Fitness += Math.Min(1.0, Math.Max(0.0, outputs3[0])); bool flag4 = flag3 && outputs3[0] >= 0.5; decodedNetwork.Flush(); inputs[0] = 1.0; inputs[1] = 0.0; decodedNetwork.SetInputs(inputs); decodedNetwork.ActivateNetwork(6); double[] outputs4 = decodedNetwork.GetOutputs(); genome.Fitness += Math.Min(1.0, Math.Max(0.0, outputs4[0])); bool flag5 = flag4 && outputs4[0] >= 0.5; decodedNetwork.Flush(); if (!flag5) { return; } genome.Fitness = 4.0; }
//by phillip static void MapWeights(NetworkGenome genome, DecodedNetworks cppn) { double[] coords = new double[4]; double[] output; LinkGene temp; SubstrateNodes source; SubstrateNodes target; int linkId = 0; for (int i = 1; i < genome.Links.Count; i++) { temp = genome.Links[i]; source = (SubstrateNodes)genome.Nodes[temp.Source]; target = (SubstrateNodes)genome.Nodes[temp.Target]; if (temp.Source != 0) { coords[0] = source.Coordinate[0]; coords[1] = source.Coordinate[1]; } else { coords[0] = target.Coordinate[0]; coords[1] = target.Coordinate[1]; } coords[2] = target.Coordinate[0]; coords[3] = target.Coordinate[1]; cppn.Flush(); cppn.SetInputs(coords); cppn.ActivateNetwork(5); output = cppn.GetOutputs(); if (source.Id != 0) { temp.Weight = output[0]; } else { temp.Weight = output[1]; } } if (saveSub) { NetworkGenome.SaveToFile(genome, "Sub.xml"); saveSub = false; } }
public NetworkGenome(NetworkGenome copy) : this(copy.Id, copy.Age, copy.Parent1, copy.Parent2, copy.Species, copy.Fitness, copy.Novelty, copy.RealFitness, copy.Genotypic_Diversity) { foreach (NodeGene nodeGene in copy.Nodes) { this.Nodes.Add(nodeGene.Clone() as NodeGene); } foreach (LinkGene linkGene in copy.Links) { this.Links.Add(linkGene.Clone() as LinkGene); } this.Complexity = this.Links.Count + this.Nodes.Count; //this.BehaviorType = copy.BehaviorType; //this.GenomeDiversity = copy.GenomeDiversity; }
private bool ChangeNode(NetworkGenome temp) { List <NodeGene> list = new List <NodeGene>(temp.MutatableNodes); if (list.Count < 1) { return(false); } NodeGene NodeGenes = list[(int)(Utilities.Rnd.NextDouble() * (double)list.Count)]; string str = NodeGenes.Function; for (int index = 0; NodeGenes.Function == str && index < 7; ++index) { NodeGenes.Function = this.SelectFunction(); } return(!(NodeGenes.Function == str)); }
private NetworkGenome SelectGenome(List <NetworkGenome> list) { if (list.Count < 2) { return(list[0]); } if (list.Count < 3) { NetworkGenome NetworkGenomes1 = list[0].Fitness > list[1].Fitness ? list[0] : list[1]; NetworkGenome NetworkGenomes2 = list[0].Fitness > list[1].Fitness ? list[1] : list[0]; if (Utilities.Rnd.NextDouble() < this.Reprod.SelectFitterGenome) { return(NetworkGenomes1); } return(NetworkGenomes2); } List <NetworkGenome> list1 = new List <NetworkGenome>(3); int index1 = (int)(Utilities.Rnd.NextDouble() * (double)list.Count); list1.Add(list[index1]); int index2 = (int)(Utilities.Rnd.NextDouble() * (double)list.Count); if (list1.Contains(list[index2])) { index2 = (index2 + 1) % list.Count; } list1.Add(list[index2]); int index3 = (int)(Utilities.Rnd.NextDouble() * (double)list.Count); while (list1.Contains(list[index3])) { index3 = (index3 + 1) % list.Count; } list1.Add(list[index3]); list1.Sort((Comparison <NetworkGenome>)((g, e) => e.Fitness.CompareTo(g.Fitness))); if (Utilities.Rnd.NextDouble() < this.Reprod.SelectFitterGenome) { return(list[0]); } if (Utilities.Rnd.NextDouble() > this.Reprod.SelectFitterGenome) { return(list[1]); } return(list[2]); }
/// <summary> /// Computes the genome average novelty /// based on k nearest neighbors and against the archive /// </summary> /// <param name="neatGenome"></param> /// <param name="genomeList"></param> /// <returns></returns> public double computeNovelty(NetworkGenome neatGenome, List <NetworkGenome> genomeList) { config = KeepawayConfig.Load("KeepawayConfig.xml"); double novelty = 0.0; //double dist = 0.0; List <NetworkGenome> Genomes = genomeList; int len = Genomes.Count + archive.Count; NetworkGenome neatgenomes; List <Pair <double, NetworkGenome> > noveltyList = new List <Pair <double, NetworkGenome> >(); //if (genomeList.Count == 0) novelty = 0.002; foreach (NetworkGenome genome in genomeList) { //add a list of behaviors if (neatGenome != genome) { noveltyList.Add(new Pair <double, NetworkGenome>(Behavior.BehaviorDistance(((NetworkGenome)genome).BehaviorType, neatGenome.BehaviorType), ((NetworkGenome)genome))); } } //noveltyList.Sort(CompareFirst); //easy break tires noveltyList = noveltyList.OrderBy(o => o.First).ThenBy(o => o.Second.Fitness).ToList(); int neighbors = config.nearestNeighbors; if (noveltyList.Count < config.nearestNeighbors) { neighbors = noveltyList.Count; } //test against k nearest neighbors for (int i = 0; i < neighbors; i++) { novelty += noveltyList[i].First; neatGenome.nearestNeighbors++; } //test against the archive foreach (NetworkGenome inArchive in archive) { novelty += Behavior.BehaviorDistance(neatGenome.BehaviorType, inArchive.BehaviorType); } //average novelty of genome x novelty /= (neighbors + archive.Count()); return(novelty); }
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 void InitLinks(NetworkGenome temp) { foreach (NodeGene NodeGenes1 in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes => { if (nodes.Type != NodeType.Bias) { return(nodes.Type == NodeType.Input); } return(true); }))) { foreach (NodeGene NodeGenes2 in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes => nodes.Type == NodeType.Output))) { if (Utilities.Rnd.NextDouble() < this.Init.InitalConnectionPercentage) { temp.Links.Add(new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes1.Id, NodeGenes2.Id, 0), NodeGenes1.Id, NodeGenes2.Id, Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0))); } } } }
/// <summary> /// returns the genome diversity /// considers the k-nearest neighbors and the archive /// </summary> /// <param name="GenomesList"></param> /// <param name="genomes"></param> /// <returns></returns> public double DiversityDistance(List <NetworkGenome> GenomesList, NetworkGenome genomes) { config = KeepawayConfig.Load("KeepawayConfig.xml"); List <NetworkGenome> Genomes = GenomesList; double diversity_dist = 0; List <Pair <double, NetworkGenome> > diversityList = new List <Pair <double, NetworkGenome> >(); foreach (NetworkGenome networkGenomes in Genomes) { if (networkGenomes != genomes) { diversityList.Add(new Pair <double, NetworkGenome>(this.GenomeCompatibility(genomes, networkGenomes), networkGenomes)); } } //diversityList.Sort(CompareFirstFirst); //sort order diversityList = diversityList.OrderBy(o => o.First).ThenBy(o => o.Second.Fitness).ToList(); int neighbors = config.nearestNeighbors; //if list less than k nearestneighbors if (diversityList.Count < config.nearestNeighbors) { neighbors = diversityList.Count; } //compute a sum of the first k nearestneighbors distances of genome x for (int i = 0; i < neighbors; i++) { diversity_dist += diversityList[i].First; genomes.nearestNeighbors++; } //test against the archive foreach (NetworkGenome inArchive in archiveGD) { diversity_dist += this.GenomeCompatibility(genomes, inArchive); } //compute average diversity genotype_Diversity = diversity_dist / ((neighbors + archiveGD.Count)); return(genotype_Diversity); }
private bool TestInit() { this.TestRun = true; if (this.Tests.Directory.test) { DirectoryInfo directoryInfo = new DirectoryInfo(this.Tests.Directory.path); foreach (FileSystemInfo fileSystemInfo in directoryInfo.GetFiles("*.xml")) { NetworkGenome networkGenomes = NetworkGenome.LoadFromFile(fileSystemInfo.FullName); if (networkGenomes != null) { this.Current.Genomes.Add(networkGenomes); } } } else { this.Current.Genomes.Add(NetworkGenome.LoadFromFile(this.Tests.Genome.path)); } return(true); }
public static void XorCreate(NetworkGenome sub, DecodedNetworks cppn) { SubstrateNodes substrateNode1 = sub.Nodes.Find((Predicate <NodeGene>)(n => n.Type == NodeType.Output)) as SubstrateNodes; double[] inputs = new double[4]; int id = 0; for (int index = 0; index < sub.Nodes.Count - 1; ++index) { SubstrateNodes substrateNode2 = sub.Nodes[index] as SubstrateNodes; cppn.Flush(); inputs[0] = substrateNode2.Coordinate[0]; inputs[1] = substrateNode2.Coordinate[1]; inputs[2] = substrateNode1.Coordinate[0]; inputs[3] = substrateNode1.Coordinate[1]; cppn.SetInputs(inputs); cppn.ActivateNetwork(5); double[] outputs = cppn.GetOutputs(); sub.Links.Add(new LinkGene(id, substrateNode2.Id, substrateNode1.Id, outputs[0])); } }
private bool AddConnection(NetworkGenome temp) { List <NodeGene> list1 = new List <NodeGene>((IEnumerable <NodeGene>)temp.Nodes); NodeGene source = list1[(int)(Utilities.Rnd.NextDouble() * (double)list1.Count)]; List <NodeGene> list2 = new List <NodeGene>(Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes => { if (nodes.Type == NodeType.Bias || nodes.Type == NodeType.Input) { return(false); } if (!this.Mutate.Recurrence) { return(nodes.Layer > source.Layer); } return(true); }))); if (list2.Count < 1) { return(false); } NodeGene target = list2[(int)(Utilities.Rnd.NextDouble() * (double)list2.Count)]; if (Enumerable.Any <LinkGene>((IEnumerable <LinkGene>)temp.Links, (Func <LinkGene, bool>)(l => { if (l.Source == source.Id) { return(l.Target == target.Id); } return(false); }))) { return(false); } temp.Links.Add(new LinkGene(0, source.Id, target.Id, Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, Utilities.Rnd.NextGuassian(0.0, 0.1) * this.Mutate.WeightRange / 2.0))) { Id = HistoricalMarkings.GetLinkMarking(source.Id, target.Id, this.Generation) }); return(true); }
//phillip static void LinkCreation(NetworkGenome sub) { LinkGene temp; int linkId = 0; for (int i = 0; i < sub.Nodes.Count; i++) { for (int j = 0; j < sub.Nodes.Count && sub.Nodes[i].Layer > sub.Nodes[j].Layer; j++) { if (sub.Nodes[i].Layer - sub.Nodes[j].Layer == 1) { temp = new LinkGene(); temp.Id = linkId; linkId++; temp.Source = sub.Nodes[j].Id; temp.Target = sub.Nodes[i].Id; temp.Weight = 0; sub.Links.Add(temp); } } } }
private void MutateGenome(NetworkGenome temp) { double num1 = Utilities.Rnd.NextDouble() * (this.Mutate.AddConnection + this.Mutate.AddNode + this.Mutate.ChangeNodeFunction + this.Mutate.DeleteConnection + this.Mutate.MutateWeights) - this.Mutate.DeleteConnection; if (num1 <= 0.0 && this.DelectConnection(temp)) { return; } double num2 = num1 - this.Mutate.ChangeNodeFunction; if (num2 <= 0.0 && this.ChangeNode(temp)) { return; } double num3 = num2 - this.Mutate.AddNode; if (num3 <= 0.0 && this.AddNode(temp) || num3 - this.Mutate.AddConnection <= 0.0 && this.AddConnection(temp)) { return; } this.MutateWeights(temp); }
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); }
private void PerformTest() { if (this.Tests.Directory.test || this.Tests.Genome.test) { int num = this.Evaluator(this.Current.Genomes, true) ? 1 : 0; if (this.Tests.Genome.test) { NetworkGenome.SaveToFile(this.Current.Genomes[0], this.Tests.Genome.path); } else { using (StreamWriter streamWriter = new StreamWriter(this.Tests.Directory.path + "Results.txt")) { for (int index = 0; index < this.Current.Genomes.Count; ++index) { streamWriter.WriteLine(this.Current.Genomes[index].Fitness); } } } } else { if (!this.Tests.ChampionsAtEnd.test) { return; } int num = this.Evaluator(this.Champions, true) ? 1 : 0; using (StreamWriter streamWriter = new StreamWriter(this.Tests.ChampionsAtEnd.path + "ChampResults.txt")) { for (int index = 0; index < this.Champions.Count; ++index) { streamWriter.WriteLine(this.Champions[index].Fitness); } } } }
private NetworkGenome Recombine(NetworkGenome parent1, NetworkGenome parent2) { NetworkGenome NetworkGenomes1 = parent1; if (parent1.Fitness < parent2.Fitness) { parent1 = parent2; parent2 = NetworkGenomes1; } NetworkGenome NetworkGenomes2 = new NetworkGenome(); NetworkGenomes2.Id = NetworkGenome.NextId; NetworkGenomes2.Parent1 = parent1.Id; NetworkGenomes2.Parent2 = parent2.Id; List <NodeGene> list1 = new List <NodeGene>(Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)parent1.Nodes, (Func <NodeGene, bool>)(genes => parent2.Nodes.Contains(genes)))); list1.Sort(); List <NodeGene> list2 = new List <NodeGene>(Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)parent2.Nodes, (Func <NodeGene, bool>)(genes => parent1.Nodes.Contains(genes)))); list2.Sort(); List <LinkGene> list3 = new List <LinkGene>(Enumerable.Where <LinkGene>((IEnumerable <LinkGene>)parent1.Links, (Func <LinkGene, bool>)(genes => parent2.Links.Contains(genes)))); list3.Sort(); List <LinkGene> list4 = new List <LinkGene>(Enumerable.Where <LinkGene>((IEnumerable <LinkGene>)parent2.Links, (Func <LinkGene, bool>)(genes => parent1.Links.Contains(genes)))); list4.Sort(); bool flag = Utilities.Rnd.NextDouble() < this.Reprod.RecombineExcessFromLessFit; NetworkGenome NetworkGenomes3 = parent1; NetworkGenome notChoice = parent2; if (flag) { NetworkGenomes3 = parent2; notChoice = parent1; } List <NodeGene> list5 = new List <NodeGene>(Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)NetworkGenomes3.Nodes, (Func <NodeGene, bool>)(genes => !notChoice.Nodes.Contains(genes)))); list5.Sort(); List <LinkGene> list6 = new List <LinkGene>(Enumerable.Where <LinkGene>((IEnumerable <LinkGene>)NetworkGenomes3.Links, (Func <LinkGene, bool>)(genes => !notChoice.Links.Contains(genes)))); list6.Sort(); for (int index = 0; index < list1.Count; ++index) { if (Utilities.Rnd.NextDouble() < this.Reprod.SelectFitterGene) { NetworkGenomes2.Nodes.Add(list1[index]); } else { NetworkGenomes2.Nodes.Add(list2[index]); } } for (int index = 0; index < list3.Count; ++index) { if (Utilities.Rnd.NextDouble() < this.Reprod.SelectFitterGene) { NetworkGenomes2.Links.Add(list3[index]); } else { NetworkGenomes2.Links.Add(list4[index]); } } NetworkGenomes2.Nodes.AddRange((IEnumerable <NodeGene>)list5); NetworkGenomes2.Links.AddRange((IEnumerable <LinkGene>)list6); return(NetworkGenomes2); }
/// <summary> /// compute the distance measure /// </summary> /// <param name="genome1"></param> /// <param name="genome2"></param> /// <returns></returns> internal double GenomeCompatibility(NetworkGenome genome1, NetworkGenome genome2) { double num1 = 0.0; List <LinkGene> list1 = new List <LinkGene>(); List <NodeGene> list2 = new List <NodeGene>(); List <LinkGene> list3 = new List <LinkGene>(); List <NodeGene> list4 = new List <NodeGene>(); genome1.Nodes.Sort(); genome1.Links.Sort(); int num3 = 0; genome2.Nodes.Sort(); genome2.Links.Sort(); list1.Clear(); list2.Clear(); list3.Clear(); list4.Clear(); int index1 = 0; int index2 = 0; //finding the last nodes ids while (index1 < genome1.Nodes.Count && index2 < genome2.Nodes.Count) { if (genome1.Nodes[index1].Id == genome2.Nodes[index2].Id) // common nodes between genomes { list2.Add(genome1.Nodes[index1]); //add nodes of genome 1 to list2 list4.Add(genome2.Nodes[index2]); // add nodes of genome 2 to list4 ++index1; //increase index of nodes of genome 1 ++index2; //increase index of nodes of genome 2 } else if (genome1.Nodes[index1].Id > genome2.Nodes[index2].Id) //genome 1 id greater than genome 2 id , checking if its a disjoint or excess { ++num3; ++index2; } else { ++num3; ++index1; } } num1 += Species.Compatible.Disjoint * (double)(num3 + genome1.Nodes.Count + genome2.Nodes.Count - (index1 + index2)); int index3 = 0; int index4 = 0; int num4 = 0; while (index3 < genome1.Links.Count && index4 < genome2.Links.Count) { if (genome1.Links[index3].Id == genome2.Links[index4].Id) { list1.Add(genome1.Links[index3]); list3.Add(genome2.Links[index4]); ++index3; ++index4; } else if (genome1.Links[index3].Id > genome2.Links[index4].Id) { ++num4; ++index4; } else { ++num4; ++index3; } } num1 += Species.Compatible.Disjoint * (double)(num4 + genome1.Links.Count + genome2.Links.Count - (index3 + index4)); for (int index5 = 0; index5 < list1.Count; ++index5) { num1 += Math.Abs(list1[index5].Weight - list3[index5].Weight) * Species.Compatible.Weight; } for (int index5 = 0; index5 < list2.Count; ++index5) { num1 += list2[index5].Function == list4[index5].Function ? 0.0 : Species.Compatible.Function; } return(num1); }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } NetworkGenome networkGenome = NetworkGenome.LoadFromFile(openFileDialog.FileName); Graph graph = new Graph("Genome: " + (object)networkGenome.Id); graph.GraphAttr.LayerDirection = LayerDirection.BT; graph.Directed = true; graph.GraphAttr.LayerSep = 10.0; graph.BuildNodeHierarchy = true; graph.GraphAttr.OptimizeLabelPositions = true; graph.GraphAttr.NodeAttr.Padding = 1.0; int num = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Type == NodeType.Output)).Layer; ArrayList arrayList1 = new ArrayList(); for (int i = 0; i < num + 1; ++i) { ArrayList arrayList2 = new ArrayList(); foreach (NodeGene nodeGene in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)networkGenome.Nodes, (Func <NodeGene, bool>)(node => node.Layer == i))) { arrayList2.Add((object)nodeGene.Id.ToString()); } graph.AddSameLayer((IEnumerable)arrayList2); } graph.GraphAttr.Orientation = Microsoft.Glee.Drawing.Orientation.Landscape; graph.NeedCalculateLayout = true; graph.GraphAttr.Fontsize = 8; graph.GraphAttr.EdgeAttr.Fontsize = 7; graph.Cluster = true; graph.GraphAttr.LabelFloat = LabelFloat.Float; using (List <LinkGene> .Enumerator enumerator = networkGenome.Links.GetEnumerator()) { while (enumerator.MoveNext()) { LinkGene link = enumerator.Current; if (link.Weight != 0.0 && link.Source != -1) { //Edge edge = graph.AddEdge(link.Source.ToString(), link.Id.ToString(), link.Target.ToString()); Edge edge = graph.AddEdge(link.Source.ToString(), link.Weight.ToString("0.##"), link.Target.ToString()); edge.Attr.Fontsize = 7; edge.EdgeAttr.Fontsize = 7; if (link.Weight <= 0.0) { edge.Attr.Color = Microsoft.Glee.Drawing.Color.Red; } NodeGene nodeGene1 = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Source)); NodeGene nodeGene2 = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Target)); edge.Attr.Weight = Math.Max(nodeGene1.Layer - nodeGene2.Layer, 0); edge.UserData = (object)link; } } } for (int index = 0; index < networkGenome.Nodes.Count; ++index) { (graph.FindNode(networkGenome.Nodes[index].Id.ToString()) ?? graph.AddNode(networkGenome.Nodes[index].Id.ToString())).UserData = (object)networkGenome.Nodes[index]; } foreach (NodeGene nodeGene in networkGenome.Nodes) { if (nodeGene.Layer == 0) { Visualizers.CreateSourceNode(graph.FindNode(nodeGene.Id.ToString())); } else if (nodeGene.Layer == num) { this.CreateTargetNode(graph.FindNode(nodeGene.Id.ToString())); } else { this.CreateHiddenNode(graph.FindNode(nodeGene.Id.ToString())); } } this.gViewer.Graph = graph; }
private void MutateWeights(NetworkGenome temp) { double num1 = 0.0; for (int index = 0; index < this.Mutate.WeightProbabilities.Count; ++index) { num1 += this.Mutate.WeightProbabilities[index].Probability; } double num2 = Utilities.Rnd.NextDouble() * num1; Mutation.WeightMutation weightMutation = new Mutation.WeightMutation(); for (int index = 0; index < this.Mutate.WeightProbabilities.Count; ++index) { num2 -= this.Mutate.WeightProbabilities[index].Probability; if (num2 <= 0.0) { weightMutation = this.Mutate.WeightProbabilities[index]; break; } } if (weightMutation.Proportional) { List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks); for (int index = 0; index < list.Count; ++index) { if (Utilities.Rnd.NextDouble() < weightMutation.Amount) { if (weightMutation.Delta) { list[index].Weight += weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 10.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 10.0, this.Mutate.WeightRange / 10.0); } else { list[index].Weight = weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 2.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0); } list[index].Weight = Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, list[index].Weight)); } } } else { for (int index1 = 0; (double)index1 < weightMutation.Amount && Enumerable.Count <LinkGene>(temp.MutatableLinks) > 0; ++index1) { int index2 = Utilities.Rnd.Next(Enumerable.Count <LinkGene>(temp.MutatableLinks)); LinkGene LinkGenes = Enumerable.ElementAt <LinkGene>(temp.MutatableLinks, index2); LinkGenes.Mutated = true; if (weightMutation.Delta) { LinkGenes.Weight += weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 1.0) * this.Mutate.WeightRange / 4.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 8.0, this.Mutate.WeightRange / 8.0); } else { LinkGenes.Weight = weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 2.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0); } LinkGenes.Weight = Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, LinkGenes.Weight)); } for (int index = 0; index < temp.Links.Count; ++index) { temp.Links[index].Mutated = false; } } }