示例#1
0
    //N_Point Crossover
    //Basic theory:
    //1: random probability of happening the crossover between 0f and 1f
    //Loop through all chromossome pairs
    //Pick a random position to cut (n_cuts) ---------> should be dynamically changed in Unity, not in the code
    //Create 2 new chromossomes with the two parts cut (Clone?)
    private void n_Crossover(Individual partner, float probability)
    {
        BitFlipIndividual bitFlipPartner = (BitFlipIndividual)partner;

        //Debug.Log (n_cuts + " cuts");

        if (UnityEngine.Random.Range(0f, 1f) > probability)
        {
            return;
        }
        int crossoverPoint = Mathf.FloorToInt(chromosomeSize / (n_cuts + 1));

        for (int i = crossoverPoint; i < chromosomeSize; i += 2 * crossoverPoint)
        {
            for (int j = i; j < chromosomeSize && j < i + crossoverPoint; j++)
            {
                int  temp1 = chromosome1 [j];
                bool temp2 = chromosome2 [j];
                chromosome1 [j] = bitFlipPartner.chromosome1 [j];
                chromosome2 [j] = bitFlipPartner.chromosome2 [j];

                bitFlipPartner.chromosome1 [j] = temp1;
                bitFlipPartner.chromosome2 [j] = temp2;
            }
        }
    }
示例#2
0
    public virtual void InitPopulation()
    {
        switch (typeOfIndividual)
        {
        case IndividualType.Example:
            population = new List <Individual> ();

            while (population.Count < populationSize)
            {
                ExampleIndividual new_ind = new ExampleIndividual(individualSize, individualMultiplier);
                new_ind.Initialize();
                new_ind.Translate();
                population.Add(new_ind);
            }
            break;

        case IndividualType.BitFlip:
            population = new List <Individual> ();

            while (population.Count < populationSize)
            {
                BitFlipIndividual new_ind = new BitFlipIndividual(individualSize, individualMultiplier);
                new_ind.Initialize();
                new_ind.Translate();
                population.Add(new_ind);
            }
            break;
        }
    }
示例#3
0
    public override Individual Clone()
    {
        BitFlipIndividual new_ind = new BitFlipIndividual(totalSize, multiplier);

        chromosome1.CopyTo(new_ind.chromosome1, 0);
        chromosome2.CopyTo(new_ind.chromosome2, 0);

        //new_ind.Translate ();

        new_ind.fitness   = 0.0f;
        new_ind.evaluated = false;

        return(new_ind);
    }
示例#4
0
    private void onePointCrossover(Individual partner, float probability)
    {
        BitFlipIndividual bitFlipPartner = (BitFlipIndividual)partner;

        //go through chromosomes and alternate the according chromosome from parent1 and parent2
        int[]  newChromosome1        = new int[chromosomeSize];
        int[]  newPartnerChromosome1 = new int[chromosomeSize];
        bool[] newChromosome2        = new bool[chromosomeSize];
        bool[] newPartnerChromosome2 = new bool[chromosomeSize];


        if (Random.Range(0f, 1f) <= probability)
        {
            int cutoffPoint = Random.Range(0, chromosomeSize);
            for (int i = 0; i < cutoffPoint; i++)
            {
                newChromosome1 [i]        = chromosome1 [i];
                newChromosome2 [i]        = chromosome2 [i];
                newPartnerChromosome1 [i] = bitFlipPartner.chromosome1 [i];
                newPartnerChromosome2 [i] = bitFlipPartner.chromosome2 [i];
            }
            for (int i = cutoffPoint; i < chromosomeSize; i++)
            {
                newChromosome1 [i]        = bitFlipPartner.chromosome1 [i];
                newChromosome2 [i]        = bitFlipPartner.chromosome2 [i];
                newPartnerChromosome1 [i] = chromosome1 [i];
                newPartnerChromosome2 [i] = chromosome2 [i];
            }
        }

        chromosome1 = newChromosome1;
        chromosome2 = newChromosome2;

        bitFlipPartner.chromosome1 = newPartnerChromosome1;
        bitFlipPartner.chromosome2 = newPartnerChromosome2;
    }