Beispiel #1
0
        public void Initialize()
        {
            Genes.Clear();

            Fitness = 0;

            int genes = _settings.GenomeLength;

            // create a random bit string (random genes)
            for (int i = 0; i < genes; ++i)
            {
                var gene       = new Gene <TCombinationModel>(_settings);
                int initPasses = 0;
                // Make sure all genes are unique
                do
                {
                    gene.Initialize();
                    initPasses++;
                } while (Genes.Any(g => g.GetActivePartsString().Equals(gene.GetActivePartsString())));

                Genes.Add(gene);
                Debug.WriteLineIf(initPasses > 1, $"# Re-initialized because of duplicate gene ({initPasses})");
            }
            RebuildModelFollowingGenes();
            UpdateFitnessScore();
        }
Beispiel #2
0
        public void FromParents(Neat parent, Neat other)
        {
            lock (ThinkLock) {
                OnRemove?.Invoke();
                Neurons.Clear();
                Genes.Clear();
            }

            if (other == null || R.NextDouble() > mutate_crossover)
            {
                CopyFrom(parent);
                VaryColor(parent, null);
            }
            else
            {
                Dictionary <int, Gene> genes = new Dictionary <int, Gene>();

                foreach (Gene gene in parent.Genes.Concat(other.Genes))
                {
                    if (!genes.ContainsKey(gene.innovation))
                    {
                        Gene mutate = gene;
                        if (!mutate.enabled && R.NextDouble() < mutate_enable)
                        {
                            mutate.enabled = true;
                        }
                        genes[gene.innovation] = mutate;
                    }
                }

                int max = 0;

                foreach (Gene gene in genes.Values)
                {
                    max = Max(max, Max(gene.from, gene.axon.destination));
                    Genes.Add(gene);
                }

                addNeurons(max);
                innovation = Max(parent.innovation, other.innovation);

                VaryColor(parent, other);
            }

            MutateEnable();
            Mutate();
        }