Beispiel #1
0
    public ga(int _populationSize, int chromosomeLength, Dictionary<byte, float> _codex)
    {
        populationSize = _populationSize;
        codex = _codex;

        thisProb.Add(new KeyValuePair<object, float>(true, .35f));
        thisProb.Add(new KeyValuePair<object, float>(false, .65f));

        probGen = new ProbGen(thisProb);

        for (int i = 0; i < populationSize; i++) {
            population.Add(new chromosome(chromosomeLength, this));
        }
    }
Beispiel #2
0
    private void CyclePopulation()
    {
        List<KeyValuePair<object, float>> fitnessProbList = new List<KeyValuePair<object, float>>();
        float magnitude = 0;
        foreach(chromosome chr in population) {
            magnitude += chr.fitness;
        }
        for(int k = 0; k < population.Count-1; k++) {
            fitnessProbList.Add(new KeyValuePair<object, float>(k, population[k].fitness-1 / magnitude));
        }
        ProbGen fitnessProb = new ProbGen(fitnessProbList);

        for (int i = 0; i < population.Count-1; i++) {
            chromosome thisChr = population[i];
            chromosome partner = population[(int)fitnessProb.next()];

            for(int j = 0; j < thisChr.genes.Count; j++) {
                if(Random.value > .5f) {
                    thisChr.genes[j] = partner.genes[j];
                }
            }
            population[i] = thisChr;
        }
    }