Exemplo n.º 1
0
        public static void GenerateGenes(ref Specimen spec)
        {
            if (spec.Length <= 0)
            {
                throw new ArgumentException("Specimen Genes Length must be greater that zero!");
            }

            double min = Specimen.MinGeneValue;
            double max = Specimen.MaxGeneValue;

            var genes = new double[spec.Length];

            for (int i = 0; i < spec.Length; i++)
            {
                genes[i] = GeneRandom.Sample();
            }

            spec.Genes = genes;
        }
Exemplo n.º 2
0
        public static Specimen Mutate(this Specimen spec)
        {
            if (spec.Genes == null || spec.Genes.Length == 0)
            {
                throw new ArgumentException("Genes array cannot be empty!");
            }

            var mutated = new Specimen();

            SetGenes(ref mutated, spec.Genes);

            for (int i = 0; i < spec.Genes.Length; i++)
            {
                if (UniteRandom.Sample() <= Specimen.MutationRate)
                {
                    mutated.Genes[i] = GeneRandom.Sample();
                }
            }

            return(mutated);
        }