-
-
Notifications
You must be signed in to change notification settings - Fork 344
Self adapter mutation #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
G3r4rd00
wants to merge
17
commits into
giacomelli:master
Choose a base branch
from
G3r4rd00:SelfAdapterMutation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
270efc5
#127 FIX TournamentSelection AllowWinnerCompeteNextTournament=false …
G3r4rd00 410c97a
fix
G3r4rd00 2ab5a4b
SelfAdaptive
e6e8290
add logic
d150d08
fix
cfd1ecb
.net verison
4a81562
fix problems
cfe8456
Gaussian Mutation
ae51944
fix errors
68e4bda
.net
a607be5
add display name
be2205d
unit test fix errors
946d966
english comments and .net version
eeb5e14
.net version "6.0.400"
9583970
.net version
ec26e43
crossover improvement
2009aa5
.net version 8
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "sdk": { | ||
| "version": "6.0.400" | ||
| "version": "8" | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
src/GeneticSharp.Domain.UnitTests/Chromosomes/SelfAdaptiveChromosomeTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace GeneticSharp.Domain.UnitTests.Chromosomes | ||
| { | ||
| [TestFixture] | ||
| public class SelfAdaptiveChromosomeTest | ||
| { | ||
| [Test] | ||
| public void Constructor_ValidParameters_InitializesCorrectly() | ||
| { | ||
| int length = 10; | ||
| double minValue = 0.0; | ||
| double maxValue = 1.0; | ||
| double initMutationProvVal = 0.05; | ||
|
|
||
| var chromosome = new SelfAdaptiveChromosome(length, minValue, maxValue, initMutationProvVal); | ||
|
|
||
| Assert.AreEqual(length, chromosome.Length); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Clone_ValidChromosome_ClonesCorrectly() | ||
| { | ||
| int length = 10; | ||
| var chromosome = new SelfAdaptiveChromosome(length); | ||
|
|
||
| var clone = chromosome.Clone() as SelfAdaptiveChromosome; | ||
|
|
||
| Assert.IsNotNull(clone); | ||
| Assert.AreEqual(chromosome.Length, clone.Length); | ||
| } | ||
|
|
||
| [Test] | ||
| public void CreateNew_ValidChromosome_CreatesNewInstance() | ||
| { | ||
| int length = 10; | ||
| var chromosome = new SelfAdaptiveChromosome(length); | ||
|
|
||
| var newChromosome = chromosome.CreateNew() as SelfAdaptiveChromosome; | ||
|
|
||
| Assert.IsNotNull(newChromosome); | ||
| Assert.AreEqual(length, newChromosome.Length); | ||
| } | ||
|
|
||
| [Test] | ||
| public void GenerateGene_ValidIndex_GeneratesGeneWithinRange() | ||
| { | ||
| int length = 10; | ||
| double minValue = 0.0; | ||
| double maxValue = 1.0; | ||
| var chromosome = new SelfAdaptiveChromosome(length, minValue, maxValue); | ||
|
|
||
| for (int i = 0; i < length; i++) | ||
| { | ||
| var gene = chromosome.GenerateGene(i); | ||
| Assert.IsTrue((double)gene.Value >= minValue && (double)gene.Value <= maxValue); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/GeneticSharp.Domain.UnitTests/Mutations/SelfAdaptiveMutationTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace GeneticSharp.Domain.UnitTests.Mutations | ||
| { | ||
| [TestFixture] | ||
| public class SelfAdaptiveMutationTest | ||
| { | ||
| [Test] | ||
| public void Constructor_ValidParameters_InitializesCorrectly() | ||
| { | ||
| double tau = 0.1; | ||
| double minMutationRate = 0.05; | ||
| double maxMutationRate = 0.9; | ||
|
|
||
| var mutation = new SelfAdaptiveMutation(tau, minMutationRate, maxMutationRate); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| [Test] | ||
| public void PerformMutate_ValidChromosome_MutatesCorrectly() | ||
| { | ||
| int length = 100; | ||
| var chromosome = new SelfAdaptiveChromosome(length, 0,20); | ||
| var mutation = new SelfAdaptiveMutation(); | ||
| var random = new Random(); | ||
|
|
||
|
|
||
|
|
||
| mutation.Mutate(chromosome, 1.0f); | ||
|
|
||
| // Check if mutation probabilities are within the expected range | ||
| for (int i = 0; i < length; i++) | ||
| { | ||
| Assert.That(chromosome.GetMutationProbability(i), Is.InRange(mutation.MinMutationRate, mutation.MaxMutationRate)); | ||
| } | ||
|
|
||
| // Check if genes have been mutated | ||
| var genes = chromosome.GetGenes(); | ||
| for (int i = 0; i < length; i++) | ||
| { | ||
| Assert.That((double)genes[i].Value, Is.InRange(chromosome._minValue, chromosome._maxValue)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/GeneticSharp.Domain/Chromosomes/SelfAdaptiveChromosome.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| namespace GeneticSharp | ||
| { | ||
| public class SelfAdaptiveChromosome : ChromosomeBase | ||
| { | ||
| Dictionary<int, double> _mutationProbabilities; | ||
| int _length; | ||
| double _initMutationProvVal; | ||
| public readonly double _minValue, _maxValue; | ||
|
|
||
| public SelfAdaptiveChromosome(int length, double minValue = double.MinValue, double maxValue = double.MaxValue, double initMutationProvVal = 0.05) | ||
| : base(length) | ||
| { | ||
| _length = length; | ||
| _minValue = minValue; | ||
| _maxValue = maxValue; | ||
| _initMutationProvVal = initMutationProvVal; | ||
| _mutationProbabilities = new Dictionary<int, double>(); | ||
|
|
||
| var random = RandomizationProvider.Current; | ||
| for (int i = 0; i < length; i++) | ||
| { | ||
| var g = new Gene(random.GetDouble(minValue, maxValue)); | ||
| base.ReplaceGene(i, g); | ||
| } | ||
| } | ||
|
|
||
| public double GetMutationProbability(int index) | ||
| { | ||
| double d; | ||
| if (!_mutationProbabilities.TryGetValue(index, out d)) | ||
| return _initMutationProvVal; | ||
| return d; | ||
| } | ||
|
|
||
| public void SetMutationProbability(int index, double prov) | ||
| { | ||
| if(prov != _initMutationProvVal) | ||
| _mutationProbabilities[index] = prov; | ||
| } | ||
|
|
||
| public override IChromosome Clone() | ||
| { | ||
| SelfAdaptiveChromosome c = (SelfAdaptiveChromosome)base.Clone(); | ||
| c._mutationProbabilities = _mutationProbabilities.ToDictionary(r => r.Key, r => r.Value); | ||
| return c; | ||
| } | ||
|
|
||
|
|
||
| public override IChromosome CreateNew() | ||
| { | ||
| var e = new SelfAdaptiveChromosome(_length, _minValue, _maxValue, _initMutationProvVal); | ||
| return e; | ||
| } | ||
|
|
||
| public override Gene GenerateGene(int geneIndex) | ||
| { | ||
| var random = RandomizationProvider.Current; | ||
| double value = random.GetDouble(_minValue, _maxValue); | ||
| var g = new Gene(value); | ||
| base.ReplaceGene(geneIndex, g); | ||
| return new Gene(value); | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
src/GeneticSharp.Domain/Crossovers/SelfAdaptiveCrossover.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Linq; | ||
| using GeneticSharp; | ||
|
|
||
| namespace GeneticSharp | ||
| { | ||
| [DisplayName("Self Adaptive Crossover")] | ||
| public class SelfAdaptiveCrossover : CrossoverBase | ||
| { | ||
| public SelfAdaptiveCrossover() : base(2, 2) { } | ||
|
|
||
| protected override IList<IChromosome> PerformCross(IList<IChromosome> parents) | ||
| { | ||
| var parent1 = parents[0] as SelfAdaptiveChromosome; | ||
| var parent2 = parents[1] as SelfAdaptiveChromosome; | ||
|
|
||
| if (parent1 == null || parent2 == null) | ||
| { | ||
| throw new ArgumentException("Both parents must be of type SelfAdaptiveChromosome."); | ||
| } | ||
|
|
||
| IList<IChromosome> ret = UniformCrossover(parent1, parent2); | ||
| return ret; | ||
| } | ||
|
|
||
| private IList<IChromosome> UniformCrossover(SelfAdaptiveChromosome parent1, SelfAdaptiveChromosome parent2) | ||
| { | ||
| int length = parent1.Length; | ||
|
|
||
|
|
||
| SelfAdaptiveChromosome offspring1 = (SelfAdaptiveChromosome)parent1.Clone(); | ||
| SelfAdaptiveChromosome offspring2 = (SelfAdaptiveChromosome)parent2.Clone(); | ||
|
|
||
| for (int i = 0; i < length; i++) | ||
| { | ||
| if (RandomizationProvider.Current.GetDouble() < 0.5) | ||
| { | ||
| var v = offspring1.GetMutationProbability(i); | ||
| offspring1.SetMutationProbability(i,offspring2.GetMutationProbability(i)); | ||
| offspring2.SetMutationProbability(i, v); | ||
| } | ||
|
|
||
|
|
||
| if (RandomizationProvider.Current.GetDouble() < 0.5) | ||
| { | ||
| var g = offspring1.GetGene(i); | ||
| offspring1.ReplaceGene(i, offspring2.GetGene(i)); | ||
| offspring2.ReplaceGene(i, g); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return new List<IChromosome>() { offspring1, offspring2 }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GeneticSharp is officially only supporting .NET 6, so it's not a good idea to change it here.
When I have some time available, I will make a PR to support .NET 9.