public static League sortTeamMembers(List<TeamMember> members, int numTeams) { League league = new League(members, numTeams); Population population = new Population(members.Count, league, new FitnessCalculator(), new EliteSelection()); population.MutationRate = .5; population.CrossoverRate = .2; population.RunEpoch(); //run epoch until you get the same best chromosome 10 times double bestFitness = -Double.MaxValue; int count = 0; while(count < 5000) { population.RunEpoch(); double fitness = ((League)population.BestChromosome).getFitness(); Console.WriteLine("Fitness: " + fitness); if (fitness > bestFitness) { bestFitness = fitness; count = 0; } else { count++; } } //while (((League)population.BestChromosome).getFitness() != 80.0) // population.RunEpoch(); League best = ((League)population.BestChromosome); return best; }
public IChromosome CreateNew() { League league = new League(getPopulationClone(), numTeams); return league; }
public void Crossover(IChromosome pair) { //strategy: //1. A new league is to be formed. //2. Team 1 is added from this league to the new league //3. From the input league, if a team is mutually exclusive to all team members in the new team. //4. From this league, do the same thing. //5. Continue until teams run out. //6. Fill in the remaining teams randomly. League L2 = (League)pair; League hybrid = new League(getPopulationClone(), numTeams); hybrid.teams = new List<Team>(); //hybrid.teams.Add(teams.ElementAt(0)); for(int i = 0; i < teams.Count; i++) { Team L1Team = teams.ElementAt(i); Team L2Team = L2.teams.ElementAt(i); //they should have the same number of teams. if (!hybrid.teamsHaveCommonMemberWith(L1Team)) hybrid.teams.Add(L1Team); if (!hybrid.teamsHaveCommonMemberWith(L2Team)) hybrid.teams.Add(L2Team); } //fill existing teams into hybrid. List<TeamMember> remainingMembers = hybrid.membersNotOnATeam(); int previousTeams = hybrid.teams.Count; int idx = 0; while (remainingMembers.Count > 0) { TeamMember remove = remainingMembers.ElementAt(0); remainingMembers.RemoveAt(0); if (numTeams > hybrid.teams.Count) { Team addTeam = new Team(); addTeam.AddMember(remove); hybrid.teams.Add(addTeam); } else { hybrid.teams.ElementAt(previousTeams - 1 + idx % (numTeams - previousTeams)).AddMember(remove); } idx++; } this.teams = hybrid.teams; //L.teams //crossover does nothing right now }
public IChromosome Clone() { League l = new League(getPopulationClone(), numTeams); return l; }