From 270efc55ffdbca3a65598cd720a706d007b61bb4 Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 5 Feb 2025 17:19:50 +0100 Subject: [PATCH 1/5] #127 FIX TournamentSelection AllowWinnerCompeteNextTournament=false not working --- src/GeneticSharp.Domain/Selections/TournamentSelection.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs index 58c531b5..479ae9ce 100644 --- a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs +++ b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs @@ -85,7 +85,7 @@ protected override IList PerformSelectChromosomes(int number, Gener var candidates = generation.Chromosomes.ToList(); var selected = new List(); - while (selected.Count < number) + while (selected.Count < number && Size <= candidates.Count) { var randomIndexes = RandomizationProvider.Current.GetUniqueInts(Size, 0, candidates.Count); var tournamentWinner = candidates.Where((c, i) => randomIndexes.Contains(i)).OrderByDescending(c => c.Fitness).First(); @@ -98,7 +98,10 @@ protected override IList PerformSelectChromosomes(int number, Gener } } - return selected; + if(selected.Count < number && candidates.Any()) + selected.Add(candidates.First().Clone()); + + return selected; } } } From 410c97a43201b2f699654bd87b3d5050f6e4f19d Mon Sep 17 00:00:00 2001 From: Gerard Date: Wed, 5 Feb 2025 17:26:32 +0100 Subject: [PATCH 2/5] fix --- .../Selections/TournamentSelection.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs index 479ae9ce..9367fdcd 100644 --- a/src/GeneticSharp.Domain/Selections/TournamentSelection.cs +++ b/src/GeneticSharp.Domain/Selections/TournamentSelection.cs @@ -98,9 +98,13 @@ protected override IList PerformSelectChromosomes(int number, Gener } } - if(selected.Count < number && candidates.Any()) - selected.Add(candidates.First().Clone()); - + while(selected.Count < number && candidates.Any()) + { + var canditate = candidates.First().Clone(); + selected.Add(canditate); + candidates.Remove(canditate); + } + return selected; } } From 4677b2827ebef4b38fe1a2c329f6e2cd07c49d02 Mon Sep 17 00:00:00 2001 From: "gerard.tous" Date: Fri, 7 Feb 2025 14:44:29 +0100 Subject: [PATCH 3/5] .net version --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index a05a252d..c8c0e3f2 100644 --- a/global.json +++ b/global.json @@ -2,4 +2,4 @@ "sdk": { "version": "6.0.400" } -} \ No newline at end of file +} \ No newline at end of file From 76b96eef85c00e8175eaeb0b475e4805a3114901 Mon Sep 17 00:00:00 2001 From: "gerard.tous" Date: Fri, 7 Feb 2025 15:10:41 +0100 Subject: [PATCH 4/5] .net version 8 --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index c8c0e3f2..cefcbf14 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "6.0.400" + "version": "8" } } \ No newline at end of file From ad5692993427dd1d824b7f444d4751666cc32975 Mon Sep 17 00:00:00 2001 From: "gerard.tous" Date: Fri, 14 Feb 2025 12:01:54 +0100 Subject: [PATCH 5/5] IslandModelStragegy logic --- .../OperatorsStrategy/IslandModelStrategy.cs | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/GeneticSharp.Domain/OperatorsStrategy/IslandModelStrategy.cs diff --git a/src/GeneticSharp.Domain/OperatorsStrategy/IslandModelStrategy.cs b/src/GeneticSharp.Domain/OperatorsStrategy/IslandModelStrategy.cs new file mode 100644 index 00000000..e5883e33 --- /dev/null +++ b/src/GeneticSharp.Domain/OperatorsStrategy/IslandModelStrategy.cs @@ -0,0 +1,85 @@ +using System.Collections.Generic; +using System.Linq; + +namespace GeneticSharp +{ + public class IslandModelStrategy : OperatorsStrategyBase + { + private double _migrationProbability; // Probability of a chromosome migrating to another island + private int _islandCount; // Number of islands (subpopulations) + private Dictionary _islandMapping = new Dictionary(); // Tracks which island each chromosome belongs to + + public IslandModelStrategy(int islandCount, double migrationProbability = 0.1) + { + _islandCount = islandCount; + _migrationProbability = migrationProbability; + } + + public override IList Cross(IPopulation population, ICrossover crossover, float crossoverProbability, IList parents) + { + // Asignar islas a los cromosomas si no tienen una + int i = 0; + foreach (var p in parents) + { + if (!_islandMapping.ContainsKey(p)) + { + _islandMapping[p] = (RandomizationProvider.Current.GetDouble() < _migrationProbability) + ? RandomizationProvider.Current.GetInt(0, _islandCount) // Migrar a una isla aleatoria + : i++ % _islandCount; // Asignar de manera cíclica + } + } + + // Migrar cromosomas a otras islas + foreach (var c in parents) + { + if(RandomizationProvider.Current.GetDouble() < _migrationProbability) + _islandMapping[c] = RandomizationProvider.Current.GetInt(0, _islandCount); + } + + var offspring = new List(); + + // Agrupar padres por isla y hacer crossover dentro de cada isla + foreach (var island in parents.GroupBy(r => _islandMapping[r])) + { + var islandParents = island.OrderBy(r => RandomizationProvider.Current.GetDouble()).ToList(); + + // Verificar si hay suficientes padres para hacer cruces + if (islandParents.Count < crossover.ParentsNumber) + { + continue; // O podrías duplicar padres para completar el grupo + } + + // Hacer cruces + for (int j = 0; j <= islandParents.Count - crossover.ParentsNumber; j += crossover.ParentsNumber) + { + var children = SelectParentsAndCross(population, crossover, crossoverProbability, islandParents, j); + if (children != null) + { + // Asignar hijos a la misma isla de sus padres + foreach (var c in children) + _islandMapping[c] = island.Key; + offspring.AddRange(children); + } + } + } + + // Borrar los elementos de islandMapping que ya no están en la población + var allChromosomes = new HashSet(parents.Concat(offspring).Concat(population.CurrentGeneration.Chromosomes)); + var toDelete = _islandMapping.Keys.Except(allChromosomes).ToList(); + foreach (var c in toDelete) + _islandMapping.Remove(c); + + return offspring; + } + + + public override void Mutate(IMutation mutation, float mutationProbability, IList chromosomes) + { + // Apply mutation to each chromosome individually + foreach (var c in chromosomes) + { + mutation.Mutate(c, mutationProbability); + } + } + } +}