当前位置: 首页>>代码示例>>C#>>正文


C# Population.CopyPopulationSettings方法代码示例

本文整理汇总了C#中Population.CopyPopulationSettings方法的典型用法代码示例。如果您正苦于以下问题:C# Population.CopyPopulationSettings方法的具体用法?C# Population.CopyPopulationSettings怎么用?C# Population.CopyPopulationSettings使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Population的用法示例。


在下文中一共展示了Population.CopyPopulationSettings方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BreedPopulation

    public Population BreedPopulation(ref Population sourcePopulation)
    {
        for(int m = 0; m < sourcePopulation.masterAgentArray.Length; m++) {
            //sourcePopulation.masterAgentArray[m].brain.genome.PrintBiases("sourcePop " + sourcePopulation.masterAgentArray[m].fitnessScore.ToString() + ", " + m.ToString() + ", ");
            //newPop.masterAgentArray[m].brain.genome.PrintBiases("newPop " + m.ToString() + ", ");
        }
        // rank sourcePop by fitness score // maybe do this as a method of Population class?
        sourcePopulation.RankAgentArray();

        Population newPopulation = new Population();
        newPopulation = sourcePopulation.CopyPopulationSettings();

        // 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].fitnessScore;
            }
        }

        // Create the Population that will hold the next Generation agentArray:
        Population newPop = sourcePopulation.CopyPopulationSettings();

        // Figure out How many Agents survive
        int numSurvivors = Mathf.RoundToInt(survivalRate * (float)newPop.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)
                newPopulation.masterAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[newChildIndex];
                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 completely randomly-chosen agent
                newPopulation.masterAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[randomAgent];
                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].fitnessScore;
                    // 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) {
                        newPopulation.masterAgentArray[newChildIndex] = sourcePopulation.masterAgentArray[a];
                        newChildIndex++;
                    }

                }
            }
        //		set newPop Agent to lucky sourcePop index
        //////////	Agent survivingAgent = sourcePopulation.Select
        // Fill up newPop agentArray with the surviving Agents
        // Keep track of Index, as that will be needed for new agents
        }

        // Figure out how many new agents must be created to fill up the new population:
        int numNewChildAgents = newPopulation.populationMaxSize - numSurvivors;
        int numEligibleBreederAgents = Mathf.RoundToInt(breedingRate * (float)newPop.populationMaxSize);
        int currentRankIndex = 0;

        float totalScoreBreeders = 0f;
        if(breedingByRaffle) {
            for(int a = 0; a < numEligibleBreederAgents; a++) { // iterate through all agents
                totalScoreBreeders += sourcePopulation.masterAgentArray[a].fitnessScore;
            }
        }
        //float[][] parentAgentChromosomes = new float[][];
        // Iterate over numAgentsToCreate :
        // Change to While loop?
        int newChildrenCreated = 0;
        while(newChildrenCreated < numNewChildAgents) {
        //		Find how many parents random number btw min/max
            int numParentAgents = UnityEngine.Random.Range (minNumParents, maxNumParents);
            int numChildAgents = 1;
            if(numNewChildAgents - newChildrenCreated >= 2) {  // room for two more!
                numChildAgents = 2;
                //Debug.Log ("numNewChildAgents: " + numNewChildAgents.ToString() + " - newChildrenCreated: " + newChildrenCreated.ToString() + " = numChildAgents: " + numChildAgents.ToString());
            }
            float[][] parentAgentBiases = new float[numParentAgents][];
            float[][] parentAgentWeights = new float[numParentAgents][];
            for(int p = 0; p < numParentAgents; p++) {
        //		Iterate over numberOfParents :
        //			Depending on method, select suitable agents' genome.Arrays until the numberOfPArents is reached, collect them in an array of arrays
                // If breeding is by fitness score ranking:
                if(breedingByRank) {
                    // Pop should already be ranked, so just traverse from top (best) to bottom (worst) to select parentAgents
                    if(currentRankIndex >= numEligibleBreederAgents) { // if current rank index is greater than the num of eligible breeders, then restart the index to 0;
                        currentRankIndex = 0;
                    }
                    //parentAgentChromosomes[p] = new float[sourcePopulation.masterAgentArray[currentRankIndex].genome.genomeBiases.Length];
//.........这里部分代码省略.........
开发者ID:eaclou,项目名称:ANNTrainerProject,代码行数:101,代码来源:CrossoverManager.cs

示例2: PerformCrossover

    public void PerformCrossover(ref Population sourcePopulation)
    {
        Population newPop = sourcePopulation.CopyPopulationSettings();

        if(numFactions > 1) {

            Population[] sourceFactions = sourcePopulation.SplitPopulation(numFactions);
            Population[] newFactions = new Population[numFactions];
            for(int i = 0; i < numFactions; i++) {
                // Make a Genome array of each faction
                // Then BreedAgentPool on each Array?
                // Then Add those genomes to new Population masterAgentArray?
                //newFactions[i] = sourceFactions[i].CopyPopulationSettings();
                Debug.Log ("FactionSize: " + sourceFactions[i].populationMaxSize.ToString());
                newFactions[i] = BreedPopulation(ref sourceFactions[i]);
            }
            // Add them back together!
            newPop.SetToCombinedPopulations(newFactions);

        }
        else {
            newPop = BreedPopulation(ref sourcePopulation);
        }
        sourcePopulation = newPop;
    }
开发者ID:eaclou,项目名称:ANNTrainerProject,代码行数:25,代码来源:CrossoverManager.cs


注:本文中的Population.CopyPopulationSettings方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。