From 4eb6f8720d94b02d6a5efa86658f7158bfbd5160 Mon Sep 17 00:00:00 2001 From: "judahreeves@yahoo.com" Date: Sat, 12 Jul 2025 23:43:02 -0400 Subject: [PATCH] Support DeletionMutation Support RandomMutation --- .../Chromosomes/IChromosome.cs | 2 +- .../Mutations/DeletionMutation.cs | 47 +++++++++++++++++++ .../Mutations/RandomMutation.cs | 35 ++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/GeneticSharp.Domain/Mutations/DeletionMutation.cs create mode 100644 src/GeneticSharp.Domain/Mutations/RandomMutation.cs diff --git a/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs b/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs index a469c6ec..08ead454 100644 --- a/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs +++ b/src/GeneticSharp.Domain/Chromosomes/IChromosome.cs @@ -1,6 +1,6 @@ using System; -namespace GeneticSharp +namespace GeneticSharp { /// /// Defines an interface for a chromosome. diff --git a/src/GeneticSharp.Domain/Mutations/DeletionMutation.cs b/src/GeneticSharp.Domain/Mutations/DeletionMutation.cs new file mode 100644 index 00000000..f11e09f2 --- /dev/null +++ b/src/GeneticSharp.Domain/Mutations/DeletionMutation.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeneticSharp.Domain.Mutations +{ + /// + /// Replaces a gene with the supplied replacement gene, which should do nothing. Useful for search criteria allowing for a wider search space. + /// + public class DeletionMutation :MutationBase + { + private Gene _replacementGene; + + public DeletionMutation(Gene replacementGene) :base() + { + _replacementGene = replacementGene; + } + + protected override void PerformMutate(IChromosome chromosome, float probability) + { + ValidateLength(chromosome); + + if (RandomizationProvider.Current.GetDouble() <= probability) + { + int deletionIndex = RandomizationProvider.Current.GetInt(0, chromosome.Length-1); + + chromosome.ReplaceGene(deletionIndex, _replacementGene); + + + } + } + + /// + /// Validate length of the chromosome. + /// + /// The chromosome. + protected virtual void ValidateLength(IChromosome chromosome) + { + if (chromosome.Length < 3) + { + throw new MutationException(this, "A chromosome should have, at least, 3 genes. {0} has only {1} gene.".With(chromosome.GetType().Name, chromosome.Length)); + } + } + } +} diff --git a/src/GeneticSharp.Domain/Mutations/RandomMutation.cs b/src/GeneticSharp.Domain/Mutations/RandomMutation.cs new file mode 100644 index 00000000..66c59e3c --- /dev/null +++ b/src/GeneticSharp.Domain/Mutations/RandomMutation.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeneticSharp.Domain.Mutations +{ + /// + /// Performs a random mutation of one supplied. + /// + public class RandomMutation : IMutation + { + private readonly bool _isOrdered; + private readonly IMutation[] _mutations; + private RandomMutation() { } + + public RandomMutation(bool isOrdered, params IMutation[] mutations) + { + _isOrdered = isOrdered; + _mutations = mutations; + + if (_mutations.Length == 0) { throw new ArgumentException("Requires at least one mutation type."); } + } + public bool IsOrdered => _isOrdered; + + public void Mutate(IChromosome chromosome, float probability) + { + int i = Random.Shared.Next(0, _mutations.Length); + _mutations[i].Mutate(chromosome, probability); + + + } + } +}