/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 */
package io.jenetics.ext;

import static java.lang.String.format;
import static io.jenetics.internal.util.Hashes.hash;

import java.util.Random;

import io.jenetics.AltererResult;
import io.jenetics.Chromosome;
import io.jenetics.Gene;
import io.jenetics.Genotype;
import io.jenetics.Mutator;
import io.jenetics.MutatorResult;
import io.jenetics.Phenotype;
import io.jenetics.util.ISeq;
import io.jenetics.util.RandomRegistry;
import io.jenetics.util.Seq;

/**
 * Mutator implementation which is part of the
 * <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
 * algorithm. The <i>Weasel program</i> is an thought experiment by Richard
 * Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
 * combined with non-random cumulative <i>selection</i>.
 * <p>
 * The mutator mutates the genes of <i>every</i> chromosome of <i>every</i>
 * genotype in the population with the given mutation probability.
 * </p>
 * {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
 * <pre>{@code
 * final Engine<CharacterGene, Integer> engine = Engine
 *     .builder(fitness, gtf)
 *      // Set the 'WeaselSelector'.
 *     .selector(new WeaselSelector<>())
 *      // Disable survivors selector.
 *     .offspringFraction(1)
 *      // Set the 'WeaselMutator'.
 *     .alterers(new WeaselMutator<>(0.05))
 *     .build();
 * }</pre>
 *
 * @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
 * @see WeaselSelector
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
 * @since 3.5
 * @version 3.5
 */
public class WeaselMutator<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends Mutator<G, C>
{

	public WeaselMutator(final double probability) {
		super(probability);
	}

	public WeaselMutator() {
		this(0.05);
	}

	@Override
	public AltererResult<G, C>
	alter(final Seq<Phenotype<G, C>> population, final long generation) {
		final Random random = RandomRegistry.getRandom();
		final Seq<MutatorResult<Phenotype<G, C>>> result = population
			.map(pt -> mutate(pt, generation, _probability, random));

		return AltererResult.of(
			result.map(MutatorResult::getResult).asISeq(),
			result.stream().mapToInt(MutatorResult::getMutations).sum()
		);
	}

	@Override
	protected MutatorResult<Genotype<G>> mutate(
		final Genotype<G> genotype,
		final double p,
		final Random random
	) {
		final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq()
			.map(gt -> mutate(gt, p, random));

		return MutatorResult.of(
			Genotype.of(result.map(MutatorResult::getResult)),
			result.stream().mapToInt(MutatorResult::getMutations).sum()
		);
	}

	@Override
	public int hashCode() {
		return hash(_probability);
	}

	@Override
	public boolean equals(final Object obj) {
		return obj == this ||
			obj instanceof WeaselMutator &&
			Double.compare(((WeaselMutator) obj)._probability, _probability) == 0;
	}

	@Override
	public String toString() {
		return format("WeaselMutator[%f]", _probability);
	}

}

@@ -805,61 +805,8 @@
 mat;
-%0Aimport static io.jenetics.internal.util.Hashes.hash;
 %0A%0Aim
@@ -2292,19 +2292,29 @@
 version 
-3.5
+!__version__!
 %0A */%0Apub
@@ -3376,268 +3376,8 @@
 %09%7D%0A%0A
-%09@Override%0A%09public int hashCode() %7B%0A%09%09return hash(_probability);%0A%09%7D%0A%0A%09@Override%0A%09public boolean equals(final Object obj) %7B%0A%09%09return obj == this %7C%7C%0A%09%09%09obj instanceof WeaselMutator &&%0A%09%09%09Double.compare(((WeaselMutator) obj)._probability, _probability) == 0;%0A%09%7D%0A%0A
 %09@Ov

Remove 'equals' and 'hashCode' from 'WeaselMutator'.