public IList <StringUnit> Breed() { //Get random index and split there. var len = ParentA.Genome.Length; var splitIndex = ThreadRandom.Next(len); var parentAFirst = ParentA.Genome.ToString(0, splitIndex); var parentASecond = ParentA.Genome.ToString(splitIndex, len - (splitIndex)); var parentBFirst = ParentB.Genome.ToString(0, splitIndex); var parentBSecond = ParentB.Genome.ToString(splitIndex, len - (splitIndex)); //AF -> BS and BF -> AS var firstChild = new StringUnit(new StringBuilder(string.Concat(parentAFirst, parentBSecond)), ParentA, ParentB); var secondChild = new StringUnit(new StringBuilder(string.Concat(parentBFirst, parentASecond)), ParentA, ParentB); var children = new List <StringUnit>() { firstChild, secondChild }; //Attempt mutation //todo: make Mutate method part of IPopulationUnit to avoid type casting here? foreach (StringUnit child in children) { child.Mutate(); child.CalculateFitness(TargetString); } return(children); }
public StringUnit(StringBuilder genome, StringUnit parentA, StringUnit parentB) { ParentA = parentA; ParentB = parentB; _isRoot = false; DefaultFitness = Globals.DefaultFitness; SetupBuilder(genome); }
public void InsertRandomUnits(int count) { for (int i = 0; i < count; i++) { var newUnit = new StringUnit(RandomStringGenome(TargetLength), true); newUnit.SetFitness(TargetLength); AddUnitToPopulation(newUnit); } }
public void SeedPopulation() { for (int i = 0; i < PopulationSize; i++) { var newUnit = new StringUnit(RandomStringGenome(TargetLength), true); newUnit.SetFitness(TargetLength); AddUnitToPopulation(newUnit); } }
public void ReplaceUnit(StringUnit otherUnit) { this.Genome.Clear().Append(otherUnit.Genome); this.ParentA = otherUnit.ParentA; this.ParentB = otherUnit.ParentB; this.IsRoot = otherUnit.IsRoot; this.Fitness = otherUnit.Fitness; this.DefaultFitness = otherUnit.DefaultFitness; }
//Reusal public void ReplaceUnit(string newGenome, StringUnit parentA, StringUnit parentB, bool isRoot, int fitness, int defaultFitness) { this.Genome.Clear().Append(newGenome); this.ParentA = parentA; this.ParentB = parentB; this.IsRoot = isRoot; this.Fitness = fitness; this.DefaultFitness = defaultFitness; }
public StringUnit GenerateRandomPopulationUnit(bool isRoot, StringUnit parentA, StringUnit parentB) { var newUnit = new StringUnit(RandomStringGenome(TargetLength), isRoot); if (parentA == null) { return(newUnit); } newUnit.ParentA = parentA; newUnit.ParentB = parentB; return(newUnit); }
public StringFamilialUnit(StringUnit parentA, StringUnit parentB, string targetString) { this.ParentA = parentA; this.ParentB = parentB; this.TargetString = targetString; }
public void AddUnitToPopulation(StringUnit unit) { this.CurrentPopulation.Add(unit); }