jenes.population
Class Population<T extends Chromosome>

java.lang.Object
  extended by jenes.population.Population<T>
Type Parameters:
T - The class of chromosomes individuals are made of.
All Implemented Interfaces:
java.lang.Iterable<Individual<T>>

public final class Population<T extends Chromosome>
extends java.lang.Object
implements java.lang.Iterable<Individual<T>>

The Population class represents a population of Individuals.

An important property of a population is its size, that is the number of individuals it contains. The DEFAULT_SIZE constant is used as default if the size is not specified.

Population.Statistics collects statistics regarding the population. Statistics can be accessed by the getStatistics() method and updated by the updateStatistics(jenes.population.Population.Statistics).

Since:
1.0
Version:
1.2
Author:
Luigi Troiano, Pierpaolo Lombardi, Giuseppe Pascale, Thierry Bodhuin
See Also:
Individual, Population.Statistics

Nested Class Summary
 class Population.Statistics
          The Population.Statistics class is only responsible for storing statistics about a population.
 
Field Summary
static int DEFAULT_SIZE
          The default population size
 
Constructor Summary
Population()
          Constructs a new empty population
Population(Individual<T> sample)
          Constructs a new population by cloning the individual sample.
Population(Individual<T> sample, int size)
          Constructs a new population by cloning the individual sample and specifying the initial size.
Population(java.util.List<Individual<T>> individuals)
          Constructs a new population with the specified list of individual and size equal to size of this list.
Population(Population<T> population)
          Constructs a new population from the specified one with the same size
 
Method Summary
 void add(Individual<T> individual)
          Adds an individual at the end of this population.
 void add(Individual<T>[] individuals)
          Adds multiple individuals at the end of this population.
 void add(java.util.List<Individual<T>> individuals)
          Adds the list of individuals at the end of this population.
 void add(Population<T> pop)
          Adds all the individuals contained by the specified population at this population.
 void clear()
          Removes all the individuals from this population.
 int getAge()
          Returns the age of this population
 java.util.ArrayList<Individual<T>> getAllLegalIndividuals()
          Returns the list of legal individals held by the population.
 Individual<T> getIndividual(int index)
          Returns the individual at the specified position.
 java.util.ArrayList<Individual<T>> getIndividuals()
          Returns the list of individuals
 double[] getScores()
          Returns an array with the scores of all the individuals contained by this population.
 Population.Statistics getStatistics()
          Returns a Statistics of this population
 void invalidate()
          Invalidates the population by setting as not evaluated its individuals.
 boolean isEmpty()
          Tests if this population is empty.
 java.util.Iterator<Individual<T>> iterator()
          Returns an interator related the individuals of this population.
 Individual<T> remove(int index)
          Removes the specified individual from this population.
 void replace(int index, Individual<T> individual)
          Replaces the individual at the specified position with the specified individual.
 void resizeAs(Population<T> population)
          Resizes the current population.
 void setAs(Population<T> pop)
          Sets this population as the specified one.
 Individual<T> setIndividual(Individual<T> individual, int index)
          Replaces the individual at the specified position in this population with the specified one.
 void setIndividualAs(int k, Individual<T> individual)
          Sets the Individual at the specified position equal to the specified one
 int size()
          Returns the number of chromosomes contained by this population.
 void sort()
          Sorts the individuals of this population
 void swap(Population<T> pop)
          Swaps this population with the speficied one; the age and the individuals will be swapped by this operation.
 java.lang.String toCompleteString()
          Returns a string complete representation of this population.
 java.lang.String toString()
          Returns a string representation of this population.
 void trimToSize()
          Trims the capacity of this Population instance to be the list's current size.
 void updateStatistics(Population.Statistics statistics)
          Sets the specified statistics according to the statistics of this population
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_SIZE

public static final int DEFAULT_SIZE
The default population size

See Also:
Constant Field Values
Constructor Detail

Population

public Population()
Constructs a new empty population


Population

public Population(Population<T> population)
Constructs a new population from the specified one with the same size

Parameters:
population - the population used as sample

Population

public Population(java.util.List<Individual<T>> individuals)
Constructs a new population with the specified list of individual and size equal to size of this list.

Parameters:
individuals - the list to be used in the population creation

Population

public Population(Individual<T> sample)
Constructs a new population by cloning the individual sample. The population assumes the default size.

Parameters:
sample - the individual sample

Population

public Population(Individual<T> sample,
                  int size)
Constructs a new population by cloning the individual sample and specifying the initial size.

Parameters:
sample - the individual sample
size - the length population
Method Detail

iterator

public final java.util.Iterator<Individual<T>> iterator()
Returns an interator related the individuals of this population.

Specified by:
iterator in interface java.lang.Iterable<Individual<T extends Chromosome>>

getAge

public final int getAge()
Returns the age of this population

Returns:
the age of this population

setAs

public final void setAs(Population<T> pop)
Sets this population as the specified one.

Parameters:
pop - the model population

swap

public final void swap(Population<T> pop)
Swaps this population with the speficied one; the age and the individuals will be swapped by this operation.

Parameters:
pop - the other population with witch make the individuals swap

add

public final void add(Individual<T> individual)
Adds an individual at the end of this population.

Parameters:
individual - the individual to add.

add

public final void add(Individual<T>[] individuals)
Adds multiple individuals at the end of this population.

Parameters:
individuals - the individuals to be added

add

public final void add(java.util.List<Individual<T>> individuals)
Adds the list of individuals at the end of this population.

Parameters:
individuals - the list of individuals to add

add

public final void add(Population<T> pop)
Adds all the individuals contained by the specified population at this population.

Parameters:
pop - the population to add.

resizeAs

public final void resizeAs(Population<T> population)
Resizes the current population. If the new size is bigger than the old size, new individuals will be added cloning the corresponding ones in the population argument If the new size is smaller than the old size, individuals will be removed

Parameters:
population - the Population to get the size from

remove

public final Individual<T> remove(int index)
Removes the specified individual from this population.

Parameters:
index - the individual to remove.
Returns:
the removed individual.

clear

public final void clear()
Removes all the individuals from this population.


trimToSize

public final void trimToSize()
Trims the capacity of this Population instance to be the list's current size. An application can use this operation to minimize the storage of a Population instance.


setIndividualAs

public final void setIndividualAs(int k,
                                  Individual<T> individual)
Sets the Individual at the specified position equal to the specified one

Parameters:
k - the position of the individual to be modify
individual - the Individual model

getIndividual

public final Individual<T> getIndividual(int index)
Returns the individual at the specified position.

Parameters:
index - the index of the desired individual.
Returns:
the desired individual.

setIndividual

public final Individual<T> setIndividual(Individual<T> individual,
                                         int index)
Replaces the individual at the specified position in this population with the specified one.

Parameters:
individual -
index - index of individual to replace.
Returns:
the individual previously at the specified position.

getScores

public final double[] getScores()
Returns an array with the scores of all the individuals contained by this population.

Returns:
an array with all the scores of this population.

getAllLegalIndividuals

public final java.util.ArrayList<Individual<T>> getAllLegalIndividuals()
Returns the list of legal individals held by the population. The list returned is recycled, thus the reference to previous legal individuals could become invalid.

Returns:
the list of legal individuals.

isEmpty

public final boolean isEmpty()
Tests if this population is empty.

Returns:
true if this population is empty; false otherwise.

size

public final int size()
Returns the number of chromosomes contained by this population.

Returns:
the size of this population.

replace

public final void replace(int index,
                          Individual<T> individual)
Replaces the individual at the specified position with the specified individual. Note:if a null Individual is passed as argument the individual at index position is removed

Parameters:
index - the position of the individual to replace.
individual - the replacing individual.

toCompleteString

public final java.lang.String toCompleteString()
Returns a string complete representation of this population.

Returns:
a new String complete string representing this population.

toString

public final java.lang.String toString()
Returns a string representation of this population.

Overrides:
toString in class java.lang.Object
Returns:
a new String object representing this population.

sort

public final void sort()
Sorts the individuals of this population


getStatistics

public final Population.Statistics getStatistics()
Returns a Statistics of this population

Returns:
the statistics of this population

updateStatistics

public final void updateStatistics(Population.Statistics statistics)
Sets the specified statistics according to the statistics of this population

Parameters:
statistics - the statistics to be set

getIndividuals

public java.util.ArrayList<Individual<T>> getIndividuals()
Returns the list of individuals

Returns:
individuals

invalidate

public void invalidate()
Invalidates the population by setting as not evaluated its individuals.