Пример #1
0
        public static EGene[] ParseGenes(string genes)
        {
            genes = genes.ToUpper();
            EGene[] output = new EGene[genes.Length];

            for (int i = 0; i < genes.Length; i++)
            {
                switch (genes[i])
                {
                case 'X':
                case 'W':
                case 'G':
                case 'Y':
                case 'H':
                case '?':
                    output[i] = (EGene)genes[i];
                    break;

                default:
                    throw new ArgumentException(nameof(genes));
                }
            }

            return(output);
        }
Пример #2
0
        private void InitializePopulation(EGene gene, int geneLength)
        {
            this.Indivisual = new List <Indivisual>();

            for (int i = 0; i < PopulationSize; i++)
            {
                this.Indivisual.Add(new Indivisual(PopulationSize, geneLength, gene));
            }
        }
Пример #3
0
 public Indivisual(int populationSize, int geneLength, EGene gene)
 {
     switch (gene)
     {
     case GACS.Gene.EGene.FixedBinaryGene:
         Gene = new FixedBinaryGene(geneLength);
         break;
     }
 }
Пример #4
0
        /// <summary>
        /// Crossbreeds genes.
        /// </summary>
        /// <param name="breeds">
        /// Breeds is a colection of breeds which are a collection of genes.
        /// [x][y] or [breed][gene]
        /// y ->
        /// 123456  x|
        /// 123456   V
        /// </param>
        /// <returns>Returns a colection of breeds that are possible.</returns>
        public List <List <EGene> > CrossbreedSimple(List <List <EGene> > breeds)
        {
            if (breeds == null)
            {
                throw new ArgumentNullException(nameof(breeds));
            }
            if (!IsValidCountForCrossbreed(breeds.Count))
            {
                throw new ArgumentException(nameof(breeds));
            }

            //check if breeds and genes exist and are valid for crossbreed
            if (breeds.Any(breed => breed == null))
            {
                throw new ArgumentNullException(nameof(breeds));
            }
            if (!BreedsAreSameLength(breeds))
            {
                throw new ArgumentException(nameof(breeds));
            }
            if (breeds[0].Count < 1)
            {
                throw new ArgumentException(nameof(breeds));
            }

            /// possibleGenes[x][y] or [geneSlot][genePossibility]
            ///  x ->
            /// 123456  y|
            ///  2 4     V
            var possibleGenes = new List <List <EGene> >();

            //crossbreed for each geneSlot
            for (int geneIndex = 0; geneIndex < breeds[0].Count; geneIndex++)
            {
                var genes = new EGene[breeds.Count];

                for (int breedIndex = 0; breedIndex < breeds.Count; breedIndex++)
                {
                    genes[breedIndex] = breeds[breedIndex][geneIndex];
                }

                possibleGenes.Add(CrossbreedGenes(genes));
            }

            //then purmuatate all combinations
            /// outputGenes[x][y] or [breed][gene]
            /// [x][y] or [breed][gene]
            /// y ->
            /// 123456  x|
            /// 123456   V
            var outputGenes = PurmutatePossibleGenes(possibleGenes);

            return(outputGenes);
        }
Пример #5
0
        /// <summary>
        /// Gets the weight of a <paramref name="gene"/>.
        /// </summary>
        /// <param name="gene">Gene that you want to the weight for.</param>
        /// <returns>Returns a float that is the wieght of <paramref name="gene"/>.</returns>
        public float GetWeightOfGene(EGene gene)
        {
            switch (gene)
            {
            case EGene.X:
            case EGene.W:
                return(1);

            case EGene.G:
            case EGene.Y:
            case EGene.H:
                return(.6f);

            case EGene.None:
                return(0);

            default:
                throw new ArgumentException(nameof(gene));
            }
        }
Пример #6
0
        /// <summary>
        /// Crossbreeds genes.
        /// </summary>
        /// <param name="breeds">
        /// Breeds is a colection of breeds which are a collection of genes.
        /// [x][y] or [breed][gene]
        /// y ->
        /// 123456  x|
        /// 123456   V
        /// </param>
        /// <returns>Returns a colection of breeds that are possible.</returns>
        public List <Breed> Crossbreed(IList <Breed> breeds)
        {
            if (breeds == null)
            {
                throw new ArgumentNullException(nameof(breeds));
            }
            if (!IsValidCountForCrossbreed(breeds.Count))
            {
                throw new ArgumentException(nameof(breeds));
            }

            //check if breeds and genes exist and are valid for crossbreed
            if (breeds.Any(breed => breed == null))
            {
                throw new ArgumentNullException(nameof(breeds));
            }
            if (!BreedsAreSameLength(breeds))
            {
                throw new ArgumentException(nameof(breeds));
            }
            if (breeds[0].Genes.Length < 1)
            {
                throw new ArgumentException(nameof(breeds));                             //check if breeds have genes
            }
            /// possibleGenes[x][y] or [geneSlot][genePossibility]
            ///  x ->
            /// 123456  y|
            ///  2 4     V
            var possibleGenes = new List <List <EGene> >();

            //crossbreed for each geneSlot
            for (int geneIndex = 0; geneIndex < breeds[0].Genes.Length; geneIndex++)
            {
                var genes = new EGene[breeds.Count];

                for (int breedIndex = 0; breedIndex < breeds.Count; breedIndex++)
                {
                    genes[breedIndex] = breeds[breedIndex].Genes[geneIndex];
                }

                possibleGenes.Add(CrossbreedGenes(genes));
            }

            //then purmuatate all combinations
            /// outputGenes[x][y] or [breed][gene]
            /// [x][y] or [breed][gene]
            /// y ->
            /// 123456  x|
            /// 123456   V
            var outputGenes = PurmutatePossibleGenes(possibleGenes);

            var outputBreeds = new List <Breed>(outputGenes.Count);
            var parentGenes  = new string[breeds.Count];
            int generation   = GetGeneration(breeds);

            for (int i = 0; i < parentGenes.Length; i++)
            {
                parentGenes[i] = breeds[i].ToString();
            }

            foreach (List <EGene> genes in outputGenes)
            {
                outputBreeds.Add(new Breed(genes.ToArray(), parentGenes, generation));
            }

            return(outputBreeds);
        }
Пример #7
0
 public Population(int populationSize, EGene gene, int geneLength)
 {
     PopulationSize = populationSize;
     InitializePopulation(gene, geneLength);
 }