AddNodePolicyHandler.java
package net.bmahe.genetics4j.neat.mutation;
import java.util.random.RandomGenerator;
import org.apache.commons.lang3.Validate;
import net.bmahe.genetics4j.core.chromosomes.Chromosome;
import net.bmahe.genetics4j.core.mutation.GenericMutatorImpl;
import net.bmahe.genetics4j.core.mutation.MutationPolicyHandler;
import net.bmahe.genetics4j.core.mutation.MutationPolicyHandlerResolver;
import net.bmahe.genetics4j.core.mutation.Mutator;
import net.bmahe.genetics4j.core.mutation.chromosome.ChromosomeMutationHandler;
import net.bmahe.genetics4j.core.spec.AbstractEAConfiguration;
import net.bmahe.genetics4j.core.spec.AbstractEAExecutionContext;
import net.bmahe.genetics4j.core.spec.mutation.MutationPolicy;
import net.bmahe.genetics4j.core.util.ChromosomeResolverUtils;
import net.bmahe.genetics4j.neat.spec.mutation.AddNode;
public class AddNodePolicyHandler<T extends Comparable<T>> implements MutationPolicyHandler<T> {
private final RandomGenerator randomGenerator;
public AddNodePolicyHandler(final RandomGenerator _randomGenerator) {
Validate.notNull(_randomGenerator);
this.randomGenerator = _randomGenerator;
}
@Override
public boolean canHandle(final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver,
final MutationPolicy mutationPolicy) {
Validate.notNull(mutationPolicyHandlerResolver);
Validate.notNull(mutationPolicy);
return mutationPolicy instanceof AddNode;
}
@Override
public Mutator createMutator(final AbstractEAExecutionContext<T> eaExecutionContext,
final AbstractEAConfiguration<T> eaConfiguration,
final MutationPolicyHandlerResolver<T> mutationPolicyHandlerResolver, MutationPolicy mutationPolicy) {
Validate.notNull(eaExecutionContext);
Validate.notNull(eaConfiguration);
Validate.notNull(mutationPolicy);
Validate.notNull(mutationPolicyHandlerResolver);
final AddNode addNodeMutationPolicy = (AddNode) mutationPolicy;
final double populationMutationProbability = addNodeMutationPolicy.populationMutationProbability();
final ChromosomeMutationHandler<? extends Chromosome>[] chromosomeMutationHandlers = ChromosomeResolverUtils
.resolveChromosomeMutationHandlers(eaExecutionContext, eaConfiguration, mutationPolicy);
return new GenericMutatorImpl(randomGenerator,
chromosomeMutationHandlers,
mutationPolicy,
populationMutationProbability);
}
}