public CPPNNEATGenome(ICPPNNEATGA parentGA, CPPNNEATGenome <PType> parent, CPPNNEATGenome <PType> partner) : this(parentGA) { var differences = new DifferenceAnalysis(parent.GeneCollection, partner.GeneCollection); var disjointAndExcessSource = differences.FirstCollection; if (partner.Score > parent.Score) { disjointAndExcessSource = differences.SecondCollection; } else if (partner.Score == parent.Score) { disjointAndExcessSource = (MathExtensions.RandomNumber() <= 0.5) ? differences.FirstCollection : differences.SecondCollection; } Func <CPPNNEATLinkGene, CPPNNEATLinkGene, Complex> weightSelector = null; if (MathExtensions.RandomNumber() <= parentGA.MateByAveragingRate) { weightSelector = (first, second) => (first.Weight + second.Weight) / 2; } else { weightSelector = (first, second) => (MathExtensions.RandomNumber() <= 0.5) ? first.Weight : second.Weight; } foreach (var match in differences.Matches) { var geneToCopy = match.FirstCollection; var newGene = geneToCopy.Copy(); newGene.Enabled = true; newGene.Weight = weightSelector(match.FirstCollection, match.SecondCollection); GeneCollection.TryAddLinkGene(newGene); if ((!match.FirstCollection.Enabled || !match.SecondCollection.Enabled) && MathExtensions.RandomNumber() <= parentGA.DisableGeneRate) { GeneCollection.DisableLinkGene(newGene.InnovationNumber); } } foreach (var linkGene in disjointAndExcessSource .Disjoint.Union(disjointAndExcessSource.Excess)) { GeneCollection.TryAddLinkGene(linkGene.Copy()); } }
protected override void InnerMutate() { var parent = Parent as ICPPNNEATGA; var mutationProbabilities = new double[TOTAL_MUTATIONS]; mutationProbabilities[LINK_MUTATION_INDEX] = parent.NewLinkRate; mutationProbabilities[NEURON_MUTATION_INDEX] = parent.NewNeuronRate; mutationProbabilities[WEIGHT_MUTATION_INDEX] = parent.WeightMutationRate; var selected = Enumerable.Range(0, TOTAL_MUTATIONS).RouletteWheelSingle(i => mutationProbabilities[i]); switch (selected) { case LINK_MUTATION_INDEX: GeneCollection.TryCreateLinkGene(); break; case NEURON_MUTATION_INDEX: GeneCollection.TryCreateNeuronGene(); break; case WEIGHT_MUTATION_INDEX: foreach (var link in GeneCollection.LinkGenes) { if (MathExtensions.RandomNumber() <= parent.WeightMutationRate) { if (MathExtensions.RandomNumber() <= parent.WeightPertubationRate) { link.Weight += MathExtensions.ComplexRandom(-parent.MaxPerturbation, parent.MaxPerturbation); } else { link.Weight = parent.GetRandomWeight(); } } } break; } }
public override void Initialise() { GeneCollection.Initialise(); }
protected CPPNNetwork GetNetwork() { GeneCollection.Update(); return(GeneCollection.Phenome); }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object can be used to retrieve data from input parameters and /// to store data in output parameters.</param> protected override void SolveInstance(IGH_DataAccess DA) { Curve boundary = new PolylineCurve(); bool shouldInstantiateRooms = false; adjStrList = new List <string>(); List <Line> adjLines = new List <Line>(); bool shouldClearGenes = false; bool springCollAllGenes = false; bool adjustArea = false; DA.GetData("Reset", ref shouldInstantiateRooms); DA.GetData("Boundary", ref boundary); currentInputRooms.Clear(); DA.GetDataList(1, currentInputRooms); if (shouldInstantiateRooms || rooms.Count == 0 || boundaryArea != AreaMassProperties.Compute(boundary).Area || originalInputRooms == null || !RoomListsAreEqual(originalInputRooms, currentInputRooms)) { shouldClearGenes = true; boundaryArea = AreaMassProperties.Compute(boundary).Area; rooms.Clear(); originalInputRooms.Clear(); DA.GetDataList(1, rooms); DA.GetDataList(1, originalInputRooms); } DA.GetData("ProportionThreshold", ref proportionThreshold); DA.GetData("FF Balance", ref Gene.fitnessFunctionBalance); DA.GetDataList("Adjacencies", adjStrList); DA.GetData("SpringCollAllGenes", ref springCollAllGenes); DA.GetData("AdjustArea", ref adjustArea); int[,] adjArray = new int[adjStrList.Count, 2]; for (int i = 0; i < adjStrList.Count; i++) { adjArray[i, 0] = Int32.Parse((adjStrList[i].Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries)[0])); adjArray[i, 1] = Int32.Parse((adjStrList[i].Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries)[1])); } Gene.proportionThreshold = proportionThreshold; Gene.adjacencyList = adjArray; if (shouldClearGenes) { // Adapt the summarized rooms area to the boundary area if it is needed if (adjustArea) { double roomSumArea = 0; foreach (Curve room in rooms) { roomSumArea += AreaMassProperties.Compute(room).Area; } foreach (Curve room in rooms) { room.Transform(Transform.Scale(room.GetBoundingBox(false).Center, Math.Sqrt(boundaryArea / roomSumArea)));// AreaMassProperties.Compute(room).Area; } } shouldClearGenes = false; geneCollection = new GeneCollection(15, boundary); // Let's create some starting genes foreach (Gene gene in geneCollection.genes) { if (Menu_ShuffleRoomsAtFirst) { gene.InstantiateRandomly(rooms); } else { gene.Instantiate(rooms); } } } geneCollection.Iterate(); if (springCollAllGenes) { for (int l = 0; l < geneCollection.genes.Count; l++) { Gene gene = geneCollection.genes[l]; rooms = gene.GetCurves(); rooms = CollisionDetectionMain(rooms, boundary); gene.collection.Clear(); gene.Instantiate(rooms); } } rooms = geneCollection.GetBest(); AdjacentContraction(rooms, adjStrList, boundary, out adjLines); List <GH_Curve> GH_rooms = new List <GH_Curve>(); foreach (Curve c in rooms) { GH_rooms.Add(new GH_Curve(c)); } DA.SetDataList(0, GH_rooms); DA.SetDataList(1, adjLines); }