GeneticSharp is a fast, extensible, multi-platform and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).
MIT License
1.26k
stars
330
forks
source link
Uniform Mutation trying to mutate genes outside of the chromossome array #124
I am using Genetic Sharp to generate the Best team of Drones for a mission. Each gene in my chromossome stores the amount of a specific type of drone. The algorithm works perfectly fine except sometimes when performing a Uniform mutation it will try to change a gene outside of the chromossome. How could this happen? Am I doing something wrong?
Here is my code:
Chromossome
public class TeamChromosome : ChromosomeBase
{
private readonly int[] _maxAmounts;
public TeamChromosome(int[] maxAmounts) : base(maxAmounts.Length)
{
_maxAmounts = maxAmounts;
CreateGenes();
}
public override IChromosome CreateNew()
{
return new TeamChromosome(_maxAmounts);
}
public override Gene GenerateGene(int geneIndex)
{
// Generate random gene value within the maximum amount for the type
return new Gene(RandomizationProvider.Current.GetInt(0, _maxAmounts[geneIndex] + 1));
}
}
And my fitness Function
public class DroneTeamFitness : IFitness
{
private MissionPlanner missionPlanner;
private List<int> quantities;
private List<List<Drone>> capableDrones;
private bool basedOnTime;
public DroneTeamFitness(MissionPlanner missionPlanner, List<int> quantities, List<List<Drone>> capableDrones, bool basedOnTime)
{
this.missionPlanner = missionPlanner;
this.quantities = quantities;
this.capableDrones = capableDrones;
this.basedOnTime = basedOnTime;
}
public double Evaluate(IChromosome chromosome)
{
var genes = chromosome.GetGenes();
List<Drone> team = new List<Drone>();
int droneIndex = 0;
for(int i = 0; i < quantities.Count; i++)
{
int count = 0;
foreach(Drone drone in capableDrones[i])
{
count += (int)genes[droneIndex].Value;
for(int j = 0; j < (int)genes[droneIndex].Value; j++)
{
team.Add(drone);
}
droneIndex++;
}
if(count > quantities[i])
{
return 0;
}
}
// Check if the team is empty
if(team.Count == 0)
{
return 0;
}
(List <Drone> teamOverWater, List <Drone> teamUnderWater) = missionPlanner.assignDroneToWorkingArea(team);
// Check if the team can perform the mission
Mission mission = missionPlanner.mission;
if ((mission.HeigthLimit.upperLimit > 0 && teamOverWater.Count == 0) || mission.HeigthLimit.lowerLimit < 0 && teamUnderWater.Count == 0)
return 0;
List<List<DronePath>> missionPaths = missionPlanner.PlanMission(teamOverWater, teamUnderWater);
double missionCost = PathEvaluation.calculateMissionCost(missionPaths);
double missionTime = PathEvaluation.calculateMissionTime(missionPaths);
// You can add more criteria to evaluate the fitness
if (basedOnTime)
{
return 1 / missionTime;
}
return 1 / missionCost;
}
}
Managed to fix it by changing the UniformMutation constructer from UniforMutation(true) to UniformMutation(mutableGenesIndexes) with the indexes of the chromossome.
I am using Genetic Sharp to generate the Best team of Drones for a mission. Each gene in my chromossome stores the amount of a specific type of drone. The algorithm works perfectly fine except sometimes when performing a Uniform mutation it will try to change a gene outside of the chromossome. How could this happen? Am I doing something wrong?
Here is my code:
Chromossome
And my fitness Function