Interface AlgorithmDefinition<T>

  • Type Parameters:
    T - the hypothesis class to use, can be any Java class. The class should be immutable.

    public interface AlgorithmDefinition<T>
    Hypothesis creation, manipulation and evaluation callbacks for the genetic algorithm. An instance of this interface is needed to initialize a GeneticAlgorithm instance using the GeneticAlgorithmBuilder(AlgorithmDefinition) constructor. The hypothesis should be implemented as an immutable classes since the package caches the fitness of the hypothesis.
    Since:
    3.0.0
    • Method Detail

      • initialize

        void initialize​(Random r)
        Initializes an instance of the algorithm definition. This method will be called only once. The source of randomness is passed in to use resources thoughtfully. Besides that, using the same random number generator will create more predictable results for unit tests.
        Parameters:
        r - the non-null source of randomness that can be used.
      • newRandomHypothesis

        T newRandomHypothesis()
        Randomly create a hypothesis. The created hypothesis will be required to be able to be passed in to each of the other methods in this implementation afterwards.
        Returns:
        a new random hypothesis after initialization.
      • mutateHypothesis

        T mutateHypothesis​(T instance)
        Implements the mutation operator on a hypothesis. The mutation operator flips one bit of the genome of the hypothesis. Example: If a hypothesis consists of the vector (a,b,c), then a possible mutation result could be (a,d,c) or (d,b,c). Implementations will need to create a copy of the input and flip one bit.
        Parameters:
        instance - the hypothesis to mutate. The instance needs to stay unmodified.
        Returns:
        the mutated version of the hypothesis.
        See Also:
        the other genetic operator on this instance.
      • crossOverHypothesis

        Collection<T> crossOverHypothesis​(T first,
                                          T second)
        Calculates two-element crossover offspring of two parent hypothesis. The two offsprings are a combination of first and the second instances. Example: if the parents are the vectors (a,b) and (c,d), possible cross over children could be (a,d) and (c,b). Implementations will need to find a random cross-over point, and then create one or more offsprings by recombining the parents.
        Parameters:
        first - the first hypothesis to cross-over with. Needs to stay unmodified.
        second - the second hypothesis to cross-over with. Needs to stay unmodified.
        Returns:
        the collection of offsprings.
        See Also:
        the other genetic operator on this instance.
      • calculateFitness

        double calculateFitness​(T hypothesis)
        Calculates the fitness of a hypothesis. A bigger fitness means a better performance of this hypothesis. The winner hypothesis will be the one with the highest fitness function result. If calculating the fitness function is expensive, consider using the GeneticAlgorithmBuilder.withExecutorService(ExecutorService) to have a higher throughput.
        Parameters:
        hypothesis - the hypothesis to calculate the fitness for.
        Returns:
        a fitness where a bigger number means more fitness. Please be advised that negative numbers will be problematic for the algorithm.
      • loop

        boolean loop​(T hypothesis)
        Checks whether we still need to continue the search. Helps in the decision whether GeneticAlgorithm.findMaximum() should continue searching or not. Good implementations for the loopCondition are:
        • a maximum number of generations
        • a maximum amount of time, for example 10 seconds of optimization
        • a check whether the supplied hypothesis solves the problem
        Parameters:
        hypothesis - the current best hypothesis to consider when choosing to continue searching or not. Needs to stay unmofified.
        Returns:
        true if the search should go on, false to stop searching.
        See Also:
        GeneticAlgorithm.findMaximum()