0

Part of the code that I'm trying to understand as a beginner java student is below:

public class StandardGA<T extends Chromosome> extends GeneticAlgorithm<T> {

private static final long serialVersionUID = 5043503777821916152L;

private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(StandardGA.class);

/**
 * Constructor
 *
 * @param factory a {@link org.evosuite.ga.ChromosomeFactory} object.
 */
public StandardGA(ChromosomeFactory<T> factory) {
    super(factory);
}

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
protected void evolve() {

    List<T> newGeneration = new ArrayList<T>();

    // Elitism
    newGeneration.addAll(elitism());

    // new_generation.size() < population_size
    while (!isNextPopulationFull(newGeneration)) {

        T parent1 = selectionFunction.select(population);
        T parent2 = selectionFunction.select(population);

        T offspring1 = (T)parent1.clone();
        T offspring2 = (T)parent2.clone();

I'm unsure how to search for the answer for what I don't understand as I'm unsure what to call it when I search on the web for answers. If you look at this part of the code

public class StandardGA<T extends Chromosome> extends GeneticAlgorithm<T> {

There is <T extends Chromosome> which has triangular brackets with "T" in front of it. I'm unsure how you can use extends this way, and I think T is a type but also don't understand what it is or where to look for it.

Also what is the meaning of putting next to the class being extended to "Genetic Algorithms". Whats the difference between simply extending Genetic Algorithms as opposed to "Genetic Algorithms<T>".

With my limited understanding I assumed this "T" is a type because lower down in the pasted code you can see for example:

T parent1 = selectionFunction.select(population);

Which sets a variable parent1 as the type "T"? Or am I wrong and this part of code is trying to do something else? Thanks in advance for any answers.

darkphoton
  • 85
  • 7

1 Answers1

1

It's a bound. All it means is that T is required to be some type of Chromosome.

When you create or declare a StandardGA, you have to specify a type, which must be a Chromsome.

Example:

StandardGA<XXY> myGA = new StandardGA<XXY>();

Here's the link again on bounded types. https://docs.oracle.com/javase/tutorial/java/generics/bounded.html

GeneticAlgorithm<T> means that GeneticAlgorithm is itself declared with a type parameter.

 public class GeneticAlgorithm<T> {  ...

Otherwise the T would be a syntax error. You should read up on generics, it'll will help you a lot.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • ok thank you for the answer, this is a bit confusing for a beginner like me I think I'm trying to understand what's in the link at the moment. – darkphoton Apr 21 '16 at 04:16