本文整理汇总了C#中Population.GetBreedingPoolByID方法的典型用法代码示例。如果您正苦于以下问题:C# Population.GetBreedingPoolByID方法的具体用法?C# Population.GetBreedingPoolByID怎么用?C# Population.GetBreedingPoolByID使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Population
的用法示例。
在下文中一共展示了Population.GetBreedingPoolByID方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BreedPopulation
public Population BreedPopulation(ref Population sourcePopulation, int currentGeneration) {
#region Pre-Crossover, Figuring out how many agents to breed etc.
int LifetimeGeneration = currentGeneration + sourcePopulation.trainingGenerations;
int totalNumWeightMutations = 0;
//float totalWeightChangeValue = 0f;
// go through species list and adjust fitness
List<SpeciesBreedingPool> childSpeciesPoolsList = new List<SpeciesBreedingPool>(); // will hold agents in an internal list to facilitate crossover
for (int s = 0; s < sourcePopulation.speciesBreedingPoolList.Count; s++) {
SpeciesBreedingPool newChildSpeciesPool = new SpeciesBreedingPool(sourcePopulation.speciesBreedingPoolList[s].templateGenome, sourcePopulation.speciesBreedingPoolList[s].speciesID); // create Breeding Pools
// copies the existing breeding pools but leaves their agentLists empty for future children
childSpeciesPoolsList.Add(newChildSpeciesPool); // Add to list of pools
}
sourcePopulation.RankAgentArray(); // based on modified species fitness score, so compensated for species sizes
Agent[] newAgentArray = new Agent[sourcePopulation.masterAgentArray.Length];
// Calculate total fitness score:
float totalScore = 0f;
if (survivalByRaffle) {
for (int a = 0; a < sourcePopulation.populationMaxSize; a++) { // iterate through all agents
totalScore += sourcePopulation.masterAgentArray[a].fitnessScoreSpecies;
}
}
// Figure out How many Agents survive
int numSurvivors = Mathf.RoundToInt(survivalRate * (float)sourcePopulation.populationMaxSize);
//Depending on method, one at a time, select an Agent to survive until the max Number is reached
int newChildIndex = 0;
// For ( num Agents ) {
for (int i = 0; i < numSurvivors; i++) {
// If survival is by fitness score ranking:
if (survivalByRank) {
// Pop should already be ranked, so just traverse from top (best) to bottom (worst)
newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[newChildIndex];
SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID);
survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]);
//SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species'
newChildIndex++;
}
// if survival is completely random, as a control:
if (survivalStochastic) {
int randomAgent = UnityEngine.Random.Range(0, numSurvivors - 1);
// Set next newChild slot to a randomly-chosen agent within the survivor faction -- change to full random?
newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[randomAgent];
SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID);
survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]);
//SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species'
newChildIndex++;
}
// if survival is based on a fitness lottery:
if (survivalByRaffle) { // Try when Fitness is normalized from 0-1
float randomSlicePosition = UnityEngine.Random.Range(0f, totalScore);
float accumulatedFitness = 0f;
for (int a = 0; a < sourcePopulation.populationMaxSize; a++) { // iterate through all agents
accumulatedFitness += sourcePopulation.masterAgentArray[a].fitnessScoreSpecies;
// if accum fitness is on slicePosition, copy this Agent
//Debug.Log("NumSurvivors: " + numSurvivors.ToString() + ", Surviving Agent " + a.ToString() + ": AccumFitness: " + accumulatedFitness.ToString() + ", RafflePos: " + randomSlicePosition.ToString() + ", TotalScore: " + totalScore.ToString() + ", newChildIndex: " + newChildIndex.ToString());
if (accumulatedFitness >= randomSlicePosition) {
newAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[a]; // add to next gen's list of agents
SpeciesBreedingPool survivingAgentBreedingPool = sourcePopulation.GetBreedingPoolByID(childSpeciesPoolsList, newAgentArray[newChildIndex].speciesID);
survivingAgentBreedingPool.AddNewAgent(newAgentArray[newChildIndex]);
//SortNewAgentIntoSpecies(newAgentArray[newChildIndex], childSpeciesList); // sorts this surviving agent into next generation's species'
newChildIndex++;
break;
}
}
}
}
// Figure out how many new agents must be created to fill up the new population:
int numNewChildAgents = sourcePopulation.populationMaxSize - numSurvivors;
int numEligibleBreederAgents = Mathf.RoundToInt(breedingRate * (float)sourcePopulation.populationMaxSize);
int currentRankIndex = 0;
// Once the agents are ranked, trim the BreedingPools of agents that didn't make the cut for mating:
if(useSpeciation) {
for (int s = 0; s < sourcePopulation.speciesBreedingPoolList.Count; s++) {
int index = 0;
int failsafe = 0;
int numAgents = sourcePopulation.speciesBreedingPoolList[s].agentList.Count;
while (index < numAgents) {
if (index < sourcePopulation.speciesBreedingPoolList[s].agentList.Count) {
if (sourcePopulation.speciesBreedingPoolList[s].agentList[index].fitnessRank >= numEligibleBreederAgents) {
sourcePopulation.speciesBreedingPoolList[s].agentList.RemoveAt(index);
}
else {
index++;
}
}
else {
break;
}
failsafe++;
if (failsafe > 500) {
Debug.Log("INFINITE LOOP! hit failsafe 500 iters -- Trimming BreedingPools!");
break;
}
//.........这里部分代码省略.........