<commit_before>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * 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:
 *    <NAME> (<EMAIL>)
 */
package org.jenetics;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.jenetics.internal.util.Equality.eq;

import java.io.Serializable;
import java.util.function.Function;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;
import org.jenetics.internal.util.jaxb;

import org.jenetics.util.Verifiable;

/**
 * The {@code Phenotype} consists of a {@link Genotype} plus a fitness
 * {@link Function}, where the fitness {@link Function} represents the
 * environment where the {@link Genotype} lives.
 * This class implements the {@link Comparable} interface, to define a natural
 * order between two {@code Phenotype}s. The natural order of the
 * {@code Phenotypes} is defined by its fitness value (given by the
 * fitness {@link Function}. The {@code Phenotype} is immutable and therefore
 * can't be changed after creation.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 * @version !__version__! &mdash; <em>$Date: 2015-01-06 $</em>
 */
@XmlJavaTypeAdapter(Phenotype.Model.Adapter.class)
public final class Phenotype<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements
		Comparable<Phenotype<G, C>>,
		Verifiable,
		Serializable,
		Runnable
{
	private static final long serialVersionUID = 4L;

	private final Genotype<G> _genotype;

	private final Function<? super Genotype<G>, ? extends C> _function;
	private final Function<? super C, ? extends C> _scaler;

	private final long _generation;

	// Storing the fitness value for lazy evaluation.
	private transient volatile boolean _evaluated = false;
	private C _rawFitness = null;
	private C _fitness = null;

	/**
	 * Create a new phenotype from the given arguments.
	 *
	 * @param genotype the genotype of this phenotype.
	 * @param generation the current generation of the generated phenotype.
	 * @param function the fitness function of this phenotype.
	 * @param scaler the fitness scaler.
	 * @throws NullPointerException if one of the arguments is {@code null}.
	 * @throws IllegalArgumentException if the given {@code generation} is
	 *         {@code < 0}.
	 */
	private Phenotype(
		final Genotype<G> genotype,
		final long generation,
		final Function<? super Genotype<G>, ? extends C> function,
		final Function<? super C, ? extends C> scaler
	) {
		_genotype = requireNonNull(genotype, "Genotype");
		_function = requireNonNull(function, "Fitness function");
		_scaler = requireNonNull(scaler, "Fitness scaler");
		if (generation < 0) {
			throw new IllegalArgumentException(format(
				"Generation must not < 0 and was %s.", generation
			));
		}
		_generation = generation;
	}

	/**
	 * This method returns a copy of the {@code Genotype}, to guarantee a
	 * immutable class.
	 *
	 * @return the cloned {@code Genotype} of this {@code Phenotype}.
	 * @throws NullPointerException if one of the arguments is {@code null}.
	 */
	public Genotype<G> getGenotype() {
		return _genotype;
	}

	/**
	 * Evaluates the (raw) fitness values and caches it so the fitness calculation
	 * is performed only once.
	 *
	 * @return this phenotype, for method chaining.
	 */
	public Phenotype<G, C> evaluate() {
		if (!_evaluated) {
			eval();
		}
		return this;
	}

	private synchronized void eval() {
		if (!_evaluated) {
			_rawFitness = _rawFitness == null ?
				_function.apply(_genotype) : _rawFitness;
			_fitness = _fitness == null ?
				_scaler.apply(_rawFitness): _fitness;
			_evaluated = true;
		}
	}

	/**
	 * This method simply calls the {@link #evaluate()} method. The purpose of
	 * this method is to have a simple way for concurrent fitness calculation
	 * for expensive fitness values.
	 */
	@Override
	public void run() {
		evaluate();
	}

	/**
	 * Return the fitness function used by this phenotype to calculate the
	 * (raw) fitness value.<commit_msg>Fix lazy evaluation of fitness value in 'Phenotype' class.<commit_after>/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ <NAME>
 *
 * 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:
 *    <NAME> (<EMAIL>)
 */
package org.jenetics;

import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static org.jenetics.internal.util.Equality.eq;

import java.io.Serializable;
import java.util.function.Function;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;
import org.jenetics.internal.util.jaxb;

import org.jenetics.util.Verifiable;

/**
 * The {@code Phenotype} consists of a {@link Genotype} plus a fitness
 * {@link Function}, where the fitness {@link Function} represents the
 * environment where the {@link Genotype} lives.
 * This class implements the {@link Comparable} interface, to define a natural
 * order between two {@code Phenotype}s. The natural order of the
 * {@code Phenotypes} is defined by its fitness value (given by the
 * fitness {@link Function}. The {@code Phenotype} is immutable and therefore
 * can't be changed after creation.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since 1.0
 * @version !__version__! &mdash; <em>$Date: 2015-01-06 $</em>
 */
@XmlJavaTypeAdapter(Phenotype.Model.Adapter.class)
public final class Phenotype<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	implements
		Comparable<Phenotype<G, C>>,
		Verifiable,
		Serializable,
		Runnable
{
	private static final long serialVersionUID = 4L;

	private final Genotype<G> _genotype;

	private final Function<? super Genotype<G>, ? extends C> _function;
	private final Function<? super C, ? extends C> _scaler;

	private final long _generation;

	// Storing the fitness value for lazy evaluation.
	private transient volatile boolean _evaluated = false;
	private C _rawFitness = null;
	private C _fitness = null;

	/**
	 * Create a new phenotype from the given arguments.
	 *
	 * @param genotype the genotype of this phenotype.
	 * @param generation the current generation of the generated phenotype.
	 * @param function the fitness function of this phenotype.
	 * @param scaler the fitness scaler.
	 * @throws NullPointerException if one of the arguments is {@code null}.
	 * @throws IllegalArgumentException if the given {@code generation} is
	 *         {@code < 0}.
	 */
	private Phenotype(
		final Genotype<G> genotype,
		final long generation,
		final Function<? super Genotype<G>, ? extends C> function,
		final Function<? super C, ? extends C> scaler
	) {
		_genotype = requireNonNull(genotype, "Genotype");
		_function = requireNonNull(function, "Fitness function");
		_scaler = requireNonNull(scaler, "Fitness scaler");
		if (generation < 0) {
			throw new IllegalArgumentException(format(
				"Generation must not < 0 and was %s.", generation
			));
		}
		_generation = generation;
	}

	/**
	 * This method returns a copy of the {@code Genotype}, to guarantee a
	 * immutable class.
	 *
	 * @return the cloned {@code Genotype} of this {@code Phenotype}.
	 * @throws NullPointerException if one of the arguments is {@code null}.
	 */
	public Genotype<G> getGenotype() {
		return _genotype;
	}

	/**
	 * Evaluates the (raw) fitness values and caches it so the fitness calculation
	 * is performed only once.
	 *
	 * @return this phenotype, for method chaining.
	 */
	public Phenotype<G, C> evaluate() {
		if (!_evaluated) {
			eval();
		}
		return this;
	}

	private synchronized void eval() {
		if (!_evaluated) {
			if (_rawFitness == null) _function.apply(_genotype);
			if (_fitness == null) _scaler.apply(_rawFitness);
			_evaluated = true;
		}
	}

	/**
	 * This method simply calls the {@link #evaluate()} method. The purpose of
	 * this method is to have a simple way for concurrent fitness calculation
	 * for expensive fitness values.
	 */
	@Override
	public void run() {
		evaluate();
	}

	/**
	 * Return the fitness function used by this phenotype to calculate the
	 * (raw) fitness value.
{'fraction_non_alphanumeric': 0.10393939393939394, 'fraction_numerical': 0.0036363636363636364, 'mean_word_length': 4.095728255275348, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '368863', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2808, 'n_words': 1053}