Exemple #1
0
        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);
        }
Exemple #2
0
 public StringUnit(StringBuilder genome, StringUnit parentA, StringUnit parentB)
 {
     ParentA        = parentA;
     ParentB        = parentB;
     _isRoot        = false;
     DefaultFitness = Globals.DefaultFitness;
     SetupBuilder(genome);
 }
Exemple #3
0
 public void InsertRandomUnits(int count)
 {
     for (int i = 0; i < count; i++)
     {
         var newUnit = new StringUnit(RandomStringGenome(TargetLength), true);
         newUnit.SetFitness(TargetLength);
         AddUnitToPopulation(newUnit);
     }
 }
Exemple #4
0
 public void SeedPopulation()
 {
     for (int i = 0; i < PopulationSize; i++)
     {
         var newUnit = new StringUnit(RandomStringGenome(TargetLength), true);
         newUnit.SetFitness(TargetLength);
         AddUnitToPopulation(newUnit);
     }
 }
Exemple #5
0
 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;
 }
Exemple #6
0
 //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;
 }
Exemple #7
0
        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);
        }
Exemple #8
0
 public StringFamilialUnit(StringUnit parentA, StringUnit parentB, string targetString)
 {
     this.ParentA      = parentA;
     this.ParentB      = parentB;
     this.TargetString = targetString;
 }
Exemple #9
0
 public void AddUnitToPopulation(StringUnit unit)
 {
     this.CurrentPopulation.Add(unit);
 }