コード例 #1
0
        public Chromosome(Chromosome chromosomeToCopy)
        {
            _chromosomeData = new bool[chromosomeToCopy._chromosomeData.Length];
            _isSynchronized = chromosomeToCopy._isSynchronized;
            _genes = new Gene[chromosomeToCopy.Genes.Length];

            for(int i = 0; i < _genes.Length; ++i)
                _genes[i] = new Gene(chromosomeToCopy._genes[i]);
            Cook();
        }
コード例 #2
0
 public Dictionary<string, float> GetDifferences(Chromosome chromosome)
 {
     Dictionary<string, float> results = new Dictionary<string,float>();
     for(int i = 0; i < _genes.Length; ++i)
     {
     #if DEBUG
         if (Object.ReferenceEquals(_genes[i], chromosome._genes[i]))
             Debug.WriteLine("The two genes are the same reference!");
     #endif
         float sub = chromosome._genes[i].Ratio - _genes[i].Ratio;
         if (sub != 0)
             results.Add("d - " + i + ": ", sub);
     }
     return results;
 }
コード例 #3
0
        public SpawnerGroup(Spawner[] spawners, SpawnType[] spawnTypes)
        {
            m_spawners = spawners;
            m_spawnTypes = spawnTypes;

            Gene[] genes = new Gene[spawners.Length];
            for(int i = 0; i < m_spawners.Length; ++i)
                genes[i] = m_spawners[i].Gene;
            m_chromosome = new Chromosome(genes);

            float totalAppearanceRates = 0;
            for (int i = 0; i < spawnTypes.Length; ++i)
                totalAppearanceRates += m_spawnTypes[i].appearanceRate;
            m_spawnTypes[0].appearanceRate = m_spawnTypes[0].appearanceRate / totalAppearanceRates;
            for (int i = 1; i < spawnTypes.Length; ++i)
                m_spawnTypes[i].appearanceRate = m_spawnTypes[i].appearanceRate / totalAppearanceRates + m_spawnTypes[i - 1].appearanceRate;

            #if DEBUG
            string debugstring = "Ratios : ";

            for (int i = 0; i < spawnTypes.Length; ++i)
                debugstring += " " + m_spawnTypes[i].appearanceRate / totalAppearanceRates;

            #endif
        }
コード例 #4
0
        public void BuildFromParents(SpawnerGroup mum, SpawnerGroup dad)
        {
            if (mum == null || dad == null)
                return;

            m_chromosome = mum.m_chromosome.MakeChild(dad.m_chromosome, m_mutationsPerReproduction);
            m_chromosome.Mutate(m_mutationsPerReproduction);
            m_chromosome.Reheat();
            for (int i = 0; i < m_spawners.Length; ++i)
                m_spawners[i].Gene = m_chromosome.Genes[i];

            m_chromosome.PrintData();
        }
コード例 #5
0
        protected Chromosome CrossOver(Chromosome mate)
        {
            if (mate.ChromosomeData.Length != _chromosomeData.Length)
                throw new InvalidOperationException("The two chromosomes must have the same length!");
            Chromosome newChromosome = new Chromosome(this);

            int splitIndex = Globals.Random.Next(0, _chromosomeData.Length);
            bool[] newChromosomeData = new bool[_chromosomeData.Length];

            for (int i = 0; i < newChromosomeData.Length; ++i)
                if (i < splitIndex)
                    newChromosomeData[i] = _chromosomeData[i];
                else
                    newChromosomeData[i] = mate.ChromosomeData[i];

            newChromosome.ChromosomeData = newChromosomeData;
            newChromosome.Reheat();
            return newChromosome;
        }
コード例 #6
0
 //Used to generate children for child classes of the Chromosome class
 public Chromosome MakeChild(Chromosome mate, int mutationAmount)
 {
     Chromosome newChromosome = CrossOver(mate);
     for (int i = 0; i < mutationAmount; ++i)
         newChromosome.Mutate();
     newChromosome.Reheat();
     return newChromosome;
 }