示例#1
0
        /// <summary>
        /// Converts genome (array of bits) to the path through the all cities (where index in array = sequential number of city; value = city "name")
        /// </summary>
        /// <param name="genome"></param>
        /// <example>
        /// Input:              0010.0110.1000.1001.0001
        /// Covert bits to int  ↓
        ///                     2.6.8.9.1
        /// Convert to path     ↓
        ///                     5.1.2.3.4
        ///               Path (5=>1=>2=>3=>4)
        /// </example>
        /// <returns></returns>
        private int[] genomeToPath(IIndividual genome)
        {
            const int bitsPerPoint = 4;
            int       pointsCount  = PathMatrix.GetLength(0);

            if (genome.Count() != pointsCount * bitsPerPoint)
            {
                throw new ArgumentException();
            }

            return(Enumerable.Range(0, pointsCount)
                   .Select(i =>
                           new {
                city = i,
                number = BitsToInt(genome
                                   .Skip(i * bitsPerPoint)
                                   .Take(bitsPerPoint)
                                   .ToArray())
            })
                   .OrderBy(x => x.number)
                   .Select(x => x.city)
                   .ToArray());
        }