/// <summary> /// Creates the collection of <see cref="GeneticEntity"/> objects contained by this population. /// </summary> /// <remarks> /// <para>The default implementation of this method creates X <see cref="GeneticEntity"/> objects /// where X is equal to <see cref="MinimumPopulationSize"/>.</para> /// <para><b>Notes to implementers:</b> This method can be overriden in a derived class /// to customize how a population is filled with <see cref="GeneticEntity"/> objects /// or how those <see cref="GeneticEntity"/> objects are created.</para> /// </remarks> protected virtual Task InitializeCoreAsync() { this.AssertIsInitialized(); for (int i = 0; i < this.Algorithm !.PopulationSeed !.MinimumPopulationSize; i++) { GeneticEntity entity = (GeneticEntity)this.Algorithm.GeneticEntitySeed !.CreateNewAndInitialize(); this.geneticEntities.Add(entity); } return(Task.FromResult(true)); }
/// <summary> /// Attempts to mutate the <paramref name="entity"/>. /// </summary> /// <param name="entity"><see cref="GeneticEntity"/> to be mutated.</param> /// <returns> /// A potentially mutated clone of the <paramref name="entity"/>. /// </returns> /// <remarks> /// If the <see cref="GeneticEntity"/> was mutated, its <see cref="GeneticEntity.Age"/> property will be set to zero. /// </remarks> /// <exception cref="ArgumentNullException"><paramref name="entity"/> is null.</exception> public GeneticEntity Mutate(GeneticEntity entity) { if (entity is null) { throw new ArgumentNullException(nameof(entity)); } GeneticEntity clonedEntity = entity.Clone(); bool isMutated = this.GenerateMutation(clonedEntity); if (isMutated) { clonedEntity.Age = 0; } return(clonedEntity); }
/// <summary> /// Applies the <see cref="GenFx.MutationOperator"/>, if one is set, to the <paramref name="entities"/>. /// </summary> /// <param name="entities">List of entities to be potentially mutated.</param> /// <returns>List of entities after the mutation was applied.</returns> /// <exception cref="ArgumentNullException"><paramref name="entities"/> is null.</exception> protected IList <GeneticEntity> ApplyMutation(IList <GeneticEntity> entities) { if (entities == null) { throw new ArgumentNullException(nameof(entities)); } if (this.MutationOperator == null) { return(entities); } List <GeneticEntity> mutants = new List <GeneticEntity>(); foreach (GeneticEntity entity in entities) { GeneticEntity newEntity = this.MutationOperator.Mutate(entity); mutants.Add(newEntity); } return(mutants); }
/// <summary> /// When overriden in a derived class, attempts to mutate the <paramref name="entity"/>. /// </summary> /// <param name="entity"><see cref="GeneticEntity"/> to be mutated.</param> /// <returns>true if a mutation occurred; otherwise, false.</returns> /// <remarks> /// <b>Notes to implementers:</b> When this method is overriden, each segment of data making up the /// representation of the <see cref="GeneticEntity"/> should be attempted to be mutated. Use the /// <see cref="MutationRate"/> property to determine whether a component /// of the <see cref="GeneticEntity"/> should be mutated or not. /// </remarks> protected abstract bool GenerateMutation(GeneticEntity entity);
/// <summary> /// When overriden in a derived class, returns the calculated fitness value of the <paramref name="entity"/>. /// </summary> /// <param name="entity"><see cref="GeneticEntity"/> whose calculated fitness value is to be returned.</param> /// <returns> /// The calculated fitness value of the <paramref name="entity"/>. /// </returns> public abstract Task <double> EvaluateFitnessAsync(GeneticEntity entity);