Skip to content
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

Uniform Mutation trying to mutate genes outside of the chromossome array #124

Open
JJ223 opened this issue May 22, 2024 · 1 comment
Open

Comments

@JJ223
Copy link

JJ223 commented May 22, 2024

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;
    }
}
@JJ223
Copy link
Author

JJ223 commented May 22, 2024

Managed to fix it by changing the UniformMutation constructer from UniforMutation(true) to UniformMutation(mutableGenesIndexes) with the indexes of the chromossome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant