Exemple #1
0
    public IGeneticIndividual[] Reproduce(IGeneticIndividual[] IParents, int numCrossoverPoints, int numChildren)
    {
        int numParents = IParents.Length;//convenient variable to have

        //convert array of type IGeneticIndividual to type TestGeneticIndividual
        TestGeneticIndividual[] parents = new TestGeneticIndividual[numParents];
        for (int i = 0; i < numParents; i++)
        {
            parents[i] = IParents[i] as TestGeneticIndividual;
        }
        TestGeneticIndividual[] children = new TestGeneticIndividual[numChildren]; //variable to hold generated children. Will be output by method
        for (int childIter = 0; childIter < numChildren; childIter++)              //iterate once for each child to be generated
        {
            int[] crossoverPoints = new int[numCrossoverPoints];
            //fill crossoverPoints array with random ints which are indexes of genes array
            for (int i = 0; i < crossoverPoints.Length; i++)
            {
                crossoverPoints[i] = (int)(RandHolder.NextDouble() * parents[0].genes.Length);
            }
            int activeParentIndex = 0;
            //generate child
            children[childIter] = new TestGeneticIndividual(parents[0].genes.Length);
            for (int i = 0; i < parents[0].genes.Length; i++)
            {
                children[childIter].genes[i] = parents[activeParentIndex].genes[i];
                for (int iter = 0; iter < numCrossoverPoints; iter++)
                {
                    if (i == crossoverPoints[iter])
                    {
                        int temp = (int)(RandHolder.NextDouble() * (parents.Length - 1));//minus one for value exclusion, so between first and second to last index
                        if (temp != activeParentIndex)
                        {
                            activeParentIndex = temp;
                        }
                        else
                        {
                            activeParentIndex = parents.Length - 1;//minus one because it is max index of array
                        }
                    }
                }
            }
        }
        //convert array of type TestGeneticIndividual to type IGeneticIndividual
        IGeneticIndividual[] IChildren = new IGeneticIndividual[numChildren];
        for (int i = 0; i < numChildren; i++)
        {
            IChildren[i] = children[i] as IGeneticIndividual;
        }
        return(IChildren);
    }
Exemple #2
0
    // Update is called once per frame
    void Update()
    {
        float secondsPerGeneration = 1 / numGenerationsPerSecond;

        if (Time.time > nextGenerationTime)
        {
            if (ga != null)
            {
                ga.TrainGeneration(1);
                TestGeneticIndividual bestIndividual  = (TestGeneticIndividual)ga.individuals[0];
                TestGeneticIndividual worstIndividual = (TestGeneticIndividual)ga.individuals[populationSize - 1];
                //debug
                plotBest.AddKey(Time.realtimeSinceStartup, (float)bestIndividual.Fitness());
                plotWorst.AddKey(Time.realtimeSinceStartup, (float)worstIndividual.Fitness());

                nextGenerationTime += secondsPerGeneration;
            }
        }
    }
Exemple #3
0
    // Use this for initialization
    void Start()
    {
        TestGeneticIndividual progenitor = new TestGeneticIndividual(testIndividualGeneSize);

        ga = new GeneticAlgorithm(progenitor, populationSize, numParents, environmentalPressure, eliteFraction, numCrossoverPoints, mutationChance, tournamentSize);
    }