private static int[] RandomDeath(SimParams par, Population pop) { float[] PickChance = LearningPenalty(par, pop.LearningThreshold); int numLostBirds = (int)Math.Floor(par.NumBirds * par.PercentDeath); int[] LostBirds = par.RandomSampleUnequal(PickChance, numLostBirds, false); return(LostBirds); }
//Birth private static int[] ChooseMaleFathers(SimParams par, Population pop, int[] vacant, List <int> notVacant) { /*Picks fathers to sire chicks into vacancies, * Uses only living males. * It is likely for high quality males to sire multiple * offspring in one sim step for both local and global scopes*/ //Remove songless birds from notVacant, and mark all not in that list as unavailable HashSet <int> PotentialFathersTemp = new HashSet <int>(notVacant.Where(x => pop.SyllableRepertoire[x] > 0)); HashSet <int> Unavailable = new HashSet <int>(Enumerable.Range(0, par.NumBirds)); Unavailable.ExceptWith(PotentialFathersTemp); //Get the probabilities List <int> PotentialFathers = PotentialFathersTemp.ToList(); float[] Probs = ReproductiveProbability(par, pop, PotentialFathers); //pick fathers int[] Fathers = new int[vacant.Length]; if (par.LocalBreeding) { float[] Probability = Enumerable.Repeat(0f, par.NumBirds).ToArray(); for (int i = 0; i < PotentialFathers.Count; i++) { Probability[PotentialFathers[i]] = Probs[i]; } for (int i = 0; i < Fathers.Length; i++) { Fathers[i] = Locations.GetLocalBirds(par, pop, vacant[i], Unavailable, probs: Probability)[0]; } } else { //float[] PotenProbs = new float[PotentialFathers.Count]; int[] FatherIndex; //for(int i=0;i<PotenProbs.Length;i++){ // PotenProbs[i] = Probability[PotentialFathers[i]]; //} FatherIndex = par.RandomSampleUnequal(Probs, vacant.Length, true); for (int i = 0; i < Fathers.Length; i++) { Fathers[i] = PotentialFathers[FatherIndex[i]]; } } int[] lol = new int[Fathers.Length]; for (int i = 0; i < Fathers.Length; i++) { lol[i] = pop.SyllableRepertoire[Fathers[i]]; } return(Fathers); }
//Getting tutors, the original runs faster, but cannot get more than one tutor private static int[] ChooseTutors(SimParams par, Population pop, List <int> learners, List <int> notVacant) { //Picks tutos that are alive, not chicks, and not songless, //Males can tutor multiple learners /*remove chicks and songless birds + any Misc, mark all excluded * birds as unavailable*/ HashSet <int> PotentialTutorsTemp = new HashSet <int>(notVacant.Where(x => pop.Age[x] > 0)); PotentialTutorsTemp.ExceptWith(PotentialTutorsTemp.Where(x => pop.SyllableRepertoire[x] == 0).ToArray()); //pick Tutors int[] Tutors = new int[learners.Count]; if (par.LocalTutor) { //Set up unavailable for local testing HashSet <int> Unavailable = new HashSet <int>(Enumerable.Range(0, par.NumBirds)); Unavailable.ExceptWith(PotentialTutorsTemp); for (int i = 0; i < learners.Count; i++) { if (par.SocialCues) { Tutors[i] = Locations.GetLocalBirds(par, pop, learners[i], Unavailable, 1, pop.Bred)[0]; } else { Tutors[i] = Locations.GetLocalBirds(par, pop, learners[i], Unavailable)[0]; } } } else //drawn individually so no learner is his own tutor { List <int> PotentialTutors; for (int i = 0; i < learners.Count; i++) { PotentialTutors = PotentialTutorsTemp.ToList(); PotentialTutors.Remove(learners[i]); if (par.SocialCues) { float[] TutorProbs = new float[PotentialTutors.Count]; for (int j = 0; j < TutorProbs.Length; j++) { TutorProbs[j] = pop.Bred[PotentialTutors[j]]; } Tutors[i] = PotentialTutors[par.RandomSampleUnequal(TutorProbs, 1)[0]]; } else { Tutors[i] = PotentialTutors[par.RandomSampleEqualReplace(PotentialTutors, 1)[0]]; } } } return(Tutors); }
private static List <int>[] ChooseMultipleTutors(SimParams par, Population pop, List <int> learners, List <int> notVacant, int numTutors) { /*remove chicks and songless birds + any Misc, mark all excluded * birds as unavailable*/ HashSet <int> PotentialTutorsTemp = new HashSet <int>(notVacant.Where(x => pop.Age[x] > 0)); PotentialTutorsTemp.ExceptWith(PotentialTutorsTemp.Where(x => pop.SyllableRepertoire[x] == 0).ToArray()); List <int>[] Tutors = new List <int> [learners.Count]; if (par.LocalTutor) { HashSet <int> Unavailable = new HashSet <int>(Enumerable.Range(0, par.NumBirds)); Unavailable.ExceptWith(PotentialTutorsTemp); for (int i = 0; i < learners.Count; i++) { if (par.SocialCues) { Tutors[i] = Locations.GetLocalBirds(par, pop, learners[i], Unavailable, numTutors, pop.Bred).ToList(); } else { Tutors[i] = Locations.GetLocalBirds(par, pop, learners[i], Unavailable, numTutors).ToList(); } } } else { List <int> PotentialTutors; for (int i = 0; i < learners.Count; i++) { PotentialTutors = PotentialTutorsTemp.ToList(); PotentialTutors.Remove(learners[i]); if (par.SocialCues) { float[] TutorProbs = new float[PotentialTutors.Count]; for (int j = 0; j < TutorProbs.Length; j++) { TutorProbs[j] = pop.Bred[PotentialTutors[j]]; } int[] ChosenIndex = par.RandomSampleUnequal(TutorProbs, numTutors); for (int j = 0; j < numTutors; j++) { ChosenIndex[j] = PotentialTutors[ChosenIndex[j]]; } Tutors[i] = ChosenIndex.ToList(); } else { Tutors[i] = par.RandomSampleEqualNoReplace(PotentialTutors, numTutors).ToList(); } } } return(Tutors); }
private static AssignResults AssignFemale(SimParams par, List <int>[] maleSong, List <int>[] femaleSong) { int[] Order = new int[femaleSong.Count()]; float[] Match = new float[femaleSong.Count()]; List <int> Available = Enumerable.Range(0, par.NumBirds).ToList(); float[] probability = Enumerable.Repeat(0f, femaleSong.Count()).ToArray(); int[] ScrambleFemales = par.RandomSampleUnequal(probability, femaleSong.Count(), false); for (int i = 0; i < femaleSong.Count() - 1; i++) { List <float> TempMatches = new List <float> { }; for (int j = 0; j < Available.Count(); j++) { TempMatches.Add(GetMatch(par, maleSong[Available[j]], femaleSong[ScrambleFemales[i]])); } List <float> ModifyMatches = TempMatches; List <int> WrongSong = Enumerable.Range(0, TempMatches.Count()).Where(x => TempMatches[x] == 0f).ToList(); for (int j = 0; j < WrongSong.Count(); j++) { ModifyMatches[WrongSong[j]] = 0.001f; } float[] TempProbability = ModifyMatches.ToArray(); int[] Chosen = par.RandomSampleUnequal(TempProbability, 1, true); Order[i] = Available[Chosen[0]]; Match[i] = TempMatches[Chosen[0]]; Available.RemoveAt(Chosen[0]); } //Assign remaining info Match[femaleSong.Length - 1] = GetMatch(par, maleSong[Available[0]], femaleSong[ScrambleFemales[femaleSong.Length - 1]]); Order[femaleSong.Length - 1] = Available[0]; AssignResults Results = new AssignResults(Match, Order, ScrambleFemales); return(Results); }
//Getting local and global birds public static int[] GetLocalBirds(SimParams par, Population pop, int target, HashSet <int> unavailable, int numBirds = 1, float[] probs = null) { //pick Tutors locally int[] Birds = new int[numBirds]; int[] UsableInd; UsableInd = LocalSearch(par, pop, target, unavailable, numBirds).ToArray(); //return the right number of birds if (UsableInd.Length == numBirds)//only numTutors choice(s) { return(UsableInd); } else //Multiple choices { if (probs != null)//unequal probabilities { int[] Index; float[] Prob = new float[UsableInd.Length]; for (int j = 0; j < UsableInd.Length; j++) { Prob[j] = probs[UsableInd[j]]; } Index = par.RandomSampleUnequal(Prob, numBirds, false); for (int j = 0; j < numBirds; j++) { Birds[j] = UsableInd[Index[j]]; } return(Birds); } if (numBirds == 1)//can use the faster with replace { Birds[0] = UsableInd[par.RandomSampleEqualReplace(UsableInd.ToList(), 1)[0]]; return(Birds); }//need the slower without replace Birds = par.RandomSampleEqualNoReplace(UsableInd.ToList(), numBirds); return(Birds); } }
//Death private static int[] AgeDeath(SimParams par, Population pop) { //Send birds to the circus based on retirement age List <int> DeadIndex = new List <int> { }; float[] PickChance = LearningPenalty(par, pop.LearningThreshold); int[] AgePool; float Dead; int MustDie; float ChanceDie; int[] Chosen; int[] ChosenInd; float[] PickChanceSubSet; for (int i = 0; i < par.MaxAge; i++) { //which birds are age i AgePool = Enumerable.Range(0, pop.Age.Length) .Where(x => pop.Age[x] == i).ToArray(); if (AgePool.Length == 0)//No birds of a given age { continue; } Dead = AgePool.Length * (1 - pop.SurvivalChance[i]); MustDie = (int)Math.Floor(Dead); ChanceDie = Dead % 1; if (par.NextFloat() < ChanceDie) { MustDie += 1; } if (MustDie == 0) { continue; } Chosen = new int[MustDie]; if (MustDie >= AgePool.Length) { Chosen = AgePool; } else { PickChanceSubSet = new float[AgePool.Length]; for (int j = 0; j < AgePool.Length; j++) { PickChanceSubSet[j] = PickChance[AgePool[j]]; } ChosenInd = par.RandomSampleUnequal(PickChanceSubSet, MustDie, false); for (int j = 0; j < ChosenInd.Length; j++) { Chosen[j] = AgePool[ChosenInd[j]]; } } DeadIndex.AddRange(Chosen); } DeadIndex.AddRange(Enumerable.Range(0, pop.Age.Length).Where(x => pop.Age[x] == par.MaxAge).ToArray());//birds of max age must die /*double[] GetIndexer = new double[DeadIndex.Count]; * for(int i=0;i<DeadIndex.Count;i++){ * GetIndexer[i] = PickChance[DeadIndex[i]]; * } * Console.WriteLine(PickChance.Average()); * //Console.WriteLine(GetIndexer.Average());*/ return(DeadIndex.ToArray()); }