Exemplo n.º 1
0
        /// <summary>
        /// Sets the value for the specified gene on the given child,
        /// respecting its parents and the latest parameter origin.
        /// </summary>
        /// <param name="node">
        /// Specifies the gene to set the value for.
        /// Is allowed to be an <see cref="AndNode" />. In this case, nothing happens.
        /// </param>
        /// <param name="parent1">The first parent.</param>
        /// <param name="parent2">The second parent.</param>
        /// <param name="child">The child. Genome will be modified.</param>
        /// <param name="lastParameterOrigin">
        /// The latest parameter origin.
        /// Influences the probability which parent's gene the child inherits.
        /// </param>
        /// <returns>
        /// The origin of the inherited gene value.
        /// For an <see cref="AndNode" />, returns the given parameter origin.
        /// </returns>
        private ParameterOrigin SetGeneValue(
            IParameterTreeNode node,
            Genome parent1,
            Genome parent2,
            Genome child,
            ParameterOrigin lastParameterOrigin = ParameterOrigin.Open)
        {
            // If the node is not a parameter node, we do not have to set a value.
            // The parameter origin does not change.
            var nodeAsParameter = node as IParameterNode;

            if (nodeAsParameter == null)
            {
                return(lastParameterOrigin);
            }

            // If it is a parameter node, find the parents' gene values.
            var parameterName    = nodeAsParameter.Identifier;
            var parent1GeneValue = parent1.GetGeneValue(parameterName);
            var parent2GeneValue = parent2.GetGeneValue(parameterName);

            // If they are equal and the last parameter's origin is open, set child's parameter value to it
            // and keep the origin.
            if (lastParameterOrigin == ParameterOrigin.Open && object.Equals(parent1GeneValue, parent2GeneValue))
            {
                child.SetGene(parameterName, parent1GeneValue);
                return(lastParameterOrigin);
            }

            // Otherwise, randomly decide on one of the two parents.
            // The probability for the parents is dependent on the last parameter's origin.
            double probabilityForFirstParent;

            switch (lastParameterOrigin)
            {
            case ParameterOrigin.FirstParent:
                probabilityForFirstParent = 1 - this._crossoverSwitchProbability;
                break;

            case ParameterOrigin.SecondParent:
                probabilityForFirstParent = this._crossoverSwitchProbability;
                break;

            case ParameterOrigin.Open:
                probabilityForFirstParent = 0.5;
                break;

            default:
                throw new ArgumentException($"Parameter origin {lastParameterOrigin} is unknown.");
            }

            // Throw a (biased) coin and decide on one parent to inherit the gene value from.
            var useFirstParent = Randomizer.Instance.Decide(probabilityForFirstParent);

            if (useFirstParent)
            {
                child.SetGene(parameterName, parent1GeneValue);
                return(ParameterOrigin.FirstParent);
            }

            child.SetGene(parameterName, parent2GeneValue);
            return(ParameterOrigin.SecondParent);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Genome"/> class.
 /// </summary>
 /// <param name="genome">The genome to copy.</param>
 /// <param name="age">The age of the created genome.</param>
 /// <exception cref="ArgumentOutOfRangeException">Thrown if the specified age is negative.</exception>
 public Genome(Genome genome, int age)
     : this(genome)
 {
     this.Age = age;
 }
Exemplo n.º 3
0
 /// <summary>
 /// Decides whether the given genome is valid by calling <see cref="CheckAgainstParameterTree(Genome)" />.
 /// </summary>
 /// <param name="genome">The genome to test.</param>
 /// <returns>False if the genome is invalid.</returns>
 public virtual bool IsGenomeValid(Genome genome)
 {
     return(this.CheckAgainstParameterTree(genome));
 }