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; }
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; }
public static bool NormalXor(List <NetworkGenome> genomes, bool test) { double[] inputs = new double[2]; bool flag1 = false; foreach (NetworkGenome genome in genomes) { bool flag2 = 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 flag3 = flag2 && 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 flag4 = flag3 && 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 flag5 = flag4 && 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 flag6 = flag5 && outputs4[0] >= 0.5; decodedNetwork.Flush(); if (flag6) { genome.Fitness = 4.0; } flag1 = flag1 || flag6; } return(flag1); }
//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 void CopyLinks(DecodedNetworks net) { DecodedNetworks.Link link = new DecodedNetworks.Link(); link.sourceNode = -1; link.weight = 0.0; for (int index1 = 0; index1 < this.ActivatingNodes.Count && index1 < net.ActivatingNodes.Count; ++index1) { this.ActivatingNodes[index1].links.Clear(); for (int index2 = 0; index2 < net.ActivatingNodes[index1].links.Count && net.ActivatingNodes[index1].links[index2].sourceNode != -1; ++index2) { this.ActivatingNodes[index1].links.Add(net.ActivatingNodes[index1].links[index2]); } this.ActivatingNodes[index1].links.Add(link); } }
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])); } }
public object Clone() { DecodedNetworks decodedNetwork = new DecodedNetworks(); decodedNetwork.Inputs = new List <DecodedNetworks.Node>(this.Inputs.Count); decodedNetwork.Neurons = new List <DecodedNetworks.Node>(this.Neurons.Count); decodedNetwork.ActivatingNodes = new List <DecodedNetworks.Node>(this.ActivatingNodes.Count); decodedNetwork.OutputArray = new double[this.OutputArray.Length]; decodedNetwork.Outputs = new List <DecodedNetworks.Node>(this.Outputs.Count); for (int index1 = 0; index1 < this.Neurons.Count; ++index1) { DecodedNetworks.Node node = new DecodedNetworks.Node(); node.activation = this.Neurons[index1].activation; node.links = new List <DecodedNetworks.Link>(this.Neurons[index1].links.Count); for (int index2 = 0; index2 < this.Neurons[index1].links.Count; ++index2) { node.links.Add(this.Neurons[index1].links[index2]); } node.signal = 0.0; node.tempSignal = 0.0; decodedNetwork.Neurons.Add(node); if (this.Inputs.Contains(this.Neurons[index1])) { decodedNetwork.Inputs.Add(node); } else if (this.Outputs.Contains(this.Neurons[index1])) { decodedNetwork.Outputs.Add(node); decodedNetwork.ActivatingNodes.Add(node); } else if (this.ActivatingNodes.Contains(this.Neurons[index1])) { decodedNetwork.ActivatingNodes.Add(node); } } return((object)decodedNetwork); }
//modified private static double[] Evaluate(DecodedNetworks network) { int cycles = 0; double teamDispersion = 0; double distfromcentre = 0; double[] dist = new double[players.Count]; double[] values = new double[4]; for (int i = 0; i < players.Count; i++) { players[i].net = network; } do { timer.RunCycle(); cycles++; //computes the team dispersion on the field of play for (int j = 0; j < players[0].teammates.Count; j++) { dist[j] += players[0].teammatesp[j].distance(players[0].Position); } distfromcentre += ballPos; } while (!kref.episodeEnded); kref.episodeEnded = false; for (int j = 0; j < players[0].teammates.Count; j++) { teamDispersion += (dist[j] / cycles); } distfromcentre /= cycles; teamDispersion /= (players[0].teammates.Count); cycles /= 10; values[0] = cycles; values[1] = teamDispersion; values[2] = no_passes; values[3] = distfromcentre; no_passes = 0; distfromcentre = 0; teamDispersion = 0; return(values); }
//based on phillip modified static bool HyperNEATKeepaway(List <NetworkGenome> genomes, bool test) { if (!test) { int evaluations = 0; Dictionary <int, int> evalCount = new Dictionary <int, int>(); List <int> keys = new List <int>(mapping.Keys); for (int i = 0; i < genomes.Count; i++) { possibleValues.Add(i); } for (int i = 0; i < keys.Count; i++) { if (!genomes.Any(gen => gen.Id == keys[i])) { mapping.Remove(keys[i]); } else { possibleValues.Remove(mapping[keys[i]]); } } for (int i = 0; i < genomes.Count; i++) { genomes[i].Fitness = 0; for (int l = 0; l < maxVec.Length; l++) { genomes[i].BehaviorType.bVector[l] = 0; } if (!mapping.ContainsKey(genomes[i].Id)) { DecodedNetworks net = sub.GenerateNetwork(MapWeights, DecodedNetworks.DecodeGenome(genomes[i])); if (createdNetworks.Count < genomes.Count) { createdNetworks.Add(net.Clone() as DecodedNetworks); mapping.Add(genomes[i].Id, i); } else { createdNetworks[possibleValues[0]].CopyLinks(net); mapping.Add(genomes[i].Id, possibleValues[0]); possibleValues.RemoveAt(0); } } } for (int i = 0; i < genomes.Count; i++) { //receives the fitness value and behavioral characterisation values from //Fitness Evaluation function. double[] fitness = new double[4]; for (int j = 0; j < config.Episodes; j++) { fitness = Evaluate(createdNetworks[mapping[genomes[i].Id]]); genomes[i].RealFitness += fitness[0]; for (int v = 0; v < fitness.Length; v++) { genomes[i].BehaviorType.bVector[v] += fitness[v]; } } genomes[i].RealFitness /= config.Episodes; for (int v = 0; v < fitness.Length; v++) { genomes[i].BehaviorType.bVector[v] /= config.Episodes; } evalCount[genomes[i].Id] = config.Episodes; evaluations += config.Episodes; } List <double> lists = new List <double>(); genomes.Sort((a, b) => b.Fitness.CompareTo(a.Fitness)); double Max0 = 0; double Max1 = 0; double Max2 = 0; double fit = 0; for (int i = 0; i < genomes.Count; i++) { maxVec[0] = genomes[i].RealFitness; for (int v = 1; v < maxVec.Length; v++) { if (genomes[i].BehaviorType.bVector[v] > maxVec[v]) { maxVec[v] = genomes[i].BehaviorType.bVector[v]; } } } //double fitnormaliser = 11; for (int j = 0; j < genomes.Count; j++) { genomes[j].RealFitness /= fitnormaliser; genomes[j].BehaviorType.bVector[0] /= fitnormaliser; for (int v = 1; v < maxVec.Length; v++) { genomes[j].BehaviorType.bVector[v] /= maxVec[v]; } } foreach (NetworkGenome genome in genomes) { genome.Novelty = genome.NoveltyMeasure.computeNovelty(genome, genomes); genome.Genotypic_Diversity = genome.GenomeDiversity.DiversityDistance(genomes, genome) / 110; switch (config.SearchMethod) { case 1: { genome.Fitness = genome.RealFitness; //Objective based Search break; } case 2: { genome.Fitness = genome.Novelty; //Novelty Search break; } case 3: { genome.Fitness = genome.RealFitness * config.scalariser + genome.Novelty * (1 - config.scalariser); break; //Hybrid (Novelty + Objective based Search) } case 4: { genome.Fitness = genome.Genotypic_Diversity; //Genotypic Diversity Search break; } case 5: { genome.Fitness = genome.RealFitness * config.scalariser + genome.Genotypic_Diversity * (1 - config.scalariser); break; //Hybrid (Objective + Genotypic Diversity) } default: { genome.Fitness = genome.RealFitness; break; } } } ComputeNovelty.addToArchive(genomes); //add the best N genomes to archive return(false); } else { for (int i = 0; i < genomes.Count; i++) { if (genomes.FindIndex(g => genomes[i].Id == g.Id) < i) { continue; } genomes[i].Fitness = 0; { DecodedNetworks net = sub.GenerateNetwork(MapWeights, DecodedNetworks.DecodeGenome(genomes[i])); double[] fitness = new double[4]; for (int j = 0; j < config.Episodes; j++) { fitness = Evaluate(createdNetworks[mapping[genomes[i].Id]]); genomes[i].RealFitness += fitness[0]; for (int v = 0; v < fitness.Length; v++) { genomes[i].BehaviorType.bVector[v] += fitness[v]; } } genomes[i].RealFitness /= config.Episodes; for (int v = 0; v < fitness.Length; v++) { genomes[i].BehaviorType.bVector[v] /= config.Episodes; } } } //double fitnormaliser = 11; for (int i = 0; i < genomes.Count; i++) { maxVec[0] = genomes[i].RealFitness; for (int v = 1; v < maxVec.Length; v++) { if (genomes[i].BehaviorType.bVector[v] > maxVec[v]) { maxVec[v] = genomes[i].BehaviorType.bVector[v]; } } } for (int j = 0; j < genomes.Count; j++) { genomes[j].RealFitness /= fitnormaliser; genomes[j].BehaviorType.bVector[0] /= fitnormaliser; for (int v = 1; v < maxVec.Length; v++) { genomes[j].BehaviorType.bVector[v] /= maxVec[v]; } } foreach (NetworkGenome genome in genomes) { genome.Novelty = genome.NoveltyMeasure.computeNovelty(genome, genomes); genome.Genotypic_Diversity = genome.GenomeDiversity.DiversityDistance(genomes, genome) / 110; switch (config.SearchMethod) { case 1: { genome.Fitness = genome.RealFitness; //Objective based Search break; } case 2: { genome.Fitness = genome.Novelty; //Novelty Search break; } case 3: { genome.Fitness = genome.RealFitness * config.scalariser + genome.Novelty * (1 - config.scalariser); break; //Hybrid (Novelty + Objective based Search) } case 4: { genome.Fitness = genome.Genotypic_Diversity; //Genotypic Diversity Search break; } case 5: { genome.Fitness = genome.RealFitness * config.scalariser + genome.Genotypic_Diversity * (1 - config.scalariser); break; //Hybrid (Objective + Genotypic Diversity) } default: { genome.Fitness = genome.RealFitness; break; } } } ComputeNovelty.addToArchive(genomes); //add the N genomes to archive } return(false); }
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); }
public DecodedNetworks GenerateNetwork(MapWeights create, DecodedNetworks cppn) { for (int index = 0; index < this.net.ActivatingNodes.Count; ++index) { this.net.ActivatingNodes[index].links.Clear(); } create(this.sub, cppn); double val1 = 0.0; foreach (LinkGene linkGene in this.sub.Links) { val1 = Math.Max(val1, Math.Abs(linkGene.Weight)); } List <LinkGene> list = new List <LinkGene>(); double staticThreshold1 = Substrates.param.StaticThreshold; DecodedNetworks.Link link1; using (List <LinkGene> .Enumerator enumerator = this.sub.Links.GetEnumerator()) { while (enumerator.MoveNext()) { LinkGene link = enumerator.Current; link.Weight *= Substrates.param.WeightRange / (2.0 * val1); double staticThreshold2 = Substrates.param.StaticThreshold; if (Substrates.param.ThresholdByDistance) { SubstrateNodes substrateNode1 = this.sub.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Source)) as SubstrateNodes; SubstrateNodes substrateNode2 = this.sub.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Target)) as SubstrateNodes; double d = 0.0; for (int index = 0; index < substrateNode1.Coordinate.Count; ++index) { double num = substrateNode1.Coordinate[index] - substrateNode2.Coordinate[index]; d += num * num; } double num1 = Math.Sqrt(d); staticThreshold2 += num1 * Substrates.param.StaticThreshold; } if (Math.Abs(link.Weight) >= staticThreshold2) { link1.sourceNode = link.Source; link1.weight = link.Weight; this.net.Neurons[link.Target].links.Add(link1); } } } for (int index = 0; index < this.net.ActivatingNodes.Count; ++index) { link1.sourceNode = -1; link1.weight = 0.0; this.net.ActivatingNodes[index].links.Add(link1); } if (Substrates.param.Normalize) { double num1 = 1.0; for (int index1 = 0; index1 < this.net.ActivatingNodes.Count; ++index1) { double d = 0.0; for (int index2 = 0; index2 < this.net.ActivatingNodes[index1].links.Count - 1; ++index2) { d += this.net.ActivatingNodes[index1].links[index2].weight * this.net.ActivatingNodes[index1].links[index2].weight; } num1 = Math.Max(num1, Math.Sqrt(d)); } double num2 = Math.Sqrt(num1); for (int index1 = 0; index1 < this.net.ActivatingNodes.Count; ++index1) { for (int index2 = 0; index2 < this.net.ActivatingNodes[index1].links.Count - 1; ++index2) { DecodedNetworks.Link link2; link2.sourceNode = this.net.ActivatingNodes[index1].links[index2].sourceNode; link2.weight = this.net.ActivatingNodes[index1].links[index2].weight / num2; this.net.ActivatingNodes[index1].links[index2] = link2; } } } return(this.net); }
public void GenerateLinks(LinkGen linkGen) { linkGen(this.sub); this.net = DecodedNetworks.DecodeGenome(this.sub); }