Esempio n. 1
0
        /// <summary>
        /// Create a new genome by combining two genomes.
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static TankGenome CrossOver(TankGenome a, TankGenome b)
        {
            if (a == null || b == null)
            {
                throw new ArgumentNullException(a == null ? "a" : "b");
            }

            var result = new TankGenome();
            var points = TotalAttributePoints;

            // populate the attributes with the minimum value from each parent
            foreach (var attribute in Attributes)
            {
                var value = Math.Min(a.GetAttribute(attribute),
                                     b.GetAttribute(attribute));
                result.m_attributes[attribute] = value;
                points -= value;
            }

            // distribute the remaining points
            while (points > 0)
            {
                result.m_attributes[result.GetRandomAttribute(false, true, null)]++;
                points--;
            }

            result.GenomeType = GenomeType.CrossOver;
            Debug.Assert(result.Validate());
            return(result);
        }