Interface | Description |
---|---|
Alterer<G extends Gene<?,G>> |
The Alterer is responsible for the changing/recombining the Population.
|
BoundedChromosome<A extends Comparable<? super A>,G extends BoundedGene<A,G>> |
Chromosome interface for
BoundedGene s. |
BoundedGene<A extends Comparable<? super A>,G extends BoundedGene<A,G>> |
Base interface for genes where the alleles are bound by a minimum and a
maximum value.
|
Chromosome<G extends Gene<?,G>> |
A chromosome consists of one or more genes.
|
Gene<A,G extends Gene<A,G>> |
Genes are the atoms of the Jenetics library.
|
NumericChromosome<N extends Number & Comparable<? super N>,G extends NumericGene<N,G>> |
Numeric chromosome interface.
|
NumericGene<N extends Number & Comparable<? super N>,G extends NumericGene<N,G>> |
Base interface for numeric genes.
|
Selector<G extends Gene<?,G>,C extends Comparable<? super C>> |
Selectors are responsible for selecting a given number of individuals from
the population.
|
Class | Description |
---|---|
AbstractAlterer<G extends Gene<?,G>> |
Abstract implementation of the alterer interface.
|
AbstractChromosome<G extends Gene<?,G>> |
The abstract base implementation of the Chromosome interface.
|
BitChromosome |
Implementation of the classical BitChromosome.
|
BoltzmannSelector<G extends Gene<?,G>,N extends Number & Comparable<? super N>> |
In this
Selector , the probability for selection is defined as. |
CharacterChromosome |
CharacterChromosome which represents character sequences.
|
CharacterGene |
Character gene implementation.
|
CompositeAlterer<G extends Gene<?,G>> |
Combines several alterers to one.
|
Crossover<G extends Gene<?,G>> |
Performs a
Crossover of two
Chromosome . |
DoubleChromosome |
Numeric chromosome implementation which holds 64 bit floating point numbers.
|
DoubleGene |
Implementation of the NumericGene which holds a 64 bit floating point number.
|
EnumGene<A> |
Gene which holds enumerable (countable) genes.
|
ExponentialRankSelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
An alternative to the "weak"
LinearRankSelector is to assign
survival probabilities to the sorted individuals using an exponential
function. |
ExponentialScaler | Deprecated
Will be removed in next major version, respectively replaced with
a variant which will be parametrized with
Double s. |
Float64Chromosome | Deprecated
Use
DoubleChromosome instead. |
Float64Gene | Deprecated
Use
DoubleGene instead. |
GaussianMutator<G extends NumericGene<?,G>> |
The GaussianMutator class performs the mutation of a
NumericGene . |
GeneticAlgorithm<G extends Gene<?,G>,C extends Comparable<? super C>> |
Getting started
|
Genotype<G extends Gene<?,G>> |
The central class the GA is working with, is the
Genotype . |
Integer64Chromosome | Deprecated
Use
LongChromosome instead. |
Integer64Gene | Deprecated
Use
LongGene instead. |
LinearRankSelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
In linear-ranking selection the individuals are sorted according to their
fitness values.
|
LongChromosome |
Numeric chromosome implementation which holds 64 bit integer numbers.
|
LongGene |
NumericGene implementation which holds a 64 bit integer number.
|
MeanAlterer<G extends Gene<?,G> & Mean<G>> |
The order (
Recombinator.getOrder() ) of this Recombination implementation is two. |
MonteCarloSelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
The Monte Carlo selector selects the individuals from a given population
randomly.
|
MultiPointCrossover<G extends Gene<?,G>> |
|
Mutator<G extends Gene<?,G>> |
This class is for mutating a chromosomes of an given population.
|
NumberChromosome<N extends Number<N>,G extends NumberGene<N,G>> | Deprecated
Use
AbstractNumericChromosome instead. |
NumberGene<N extends Number<N>,G extends NumberGene<N,G>> | Deprecated
Use
AbstractNumericGene instead. |
NumberStatistics<G extends Gene<?,G>,R extends Number & Comparable<? super R>> | |
NumberStatistics.Builder<G extends Gene<?,G>,R extends Number & Comparable<? super R>> |
Builder for the NumberStatistics class.
|
NumberStatistics.Calculator<G extends Gene<?,G>,R extends Number & Comparable<? super R>> | |
PartiallyMatchedCrossover<T> |
The
PartiallyMatchedCrossover (PMX) guarantees that all Gene s
are found exactly once in each chromosome. |
PermutationChromosome<T> |
The mutable methods of the
AbstractChromosome has been overridden so
that no invalid permutation will be created. |
Phenotype<G extends Gene<?,G>,C extends Comparable<? super C>> | |
Population<G extends Gene<?,G>,C extends Comparable<? super C>> |
A population is a collection of Phenotypes.
|
ProbabilitySelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
Probability selectors are a variation of fitness proportional selectors and
selects individuals from a given population based on it's selection
probability P(i).
|
Recombinator<G extends Gene<?,G>> |
An enhanced genetic algorithm (EGA) combine elements of existing solutions in
order to create a new solution, with some of the properties of each parent.
|
RouletteWheelSelector<G extends Gene<?,G>,N extends Number & Comparable<? super N>> |
The roulette-wheel selector is also known as fitness proportional selector,
but in the Jenetics library it is implemented as probability selector.
|
SinglePointCrossover<G extends Gene<?,G>> |
|
Statistics<G extends Gene<?,G>,C extends Comparable<? super C>> |
Data object which holds performance indicators of a given
Population . |
Statistics.Builder<G extends Gene<?,G>,C extends Comparable<? super C>> |
Builder for the Statistics class.
|
Statistics.Calculator<G extends Gene<?,G>,C extends Comparable<? super C>> |
Class for calculating the statistics.
|
Statistics.Time |
Class which holds time statistic values.
|
StochasticUniversalSelector<G extends Gene<?,G>,N extends Number & Comparable<? super N>> |
StochasticUniversalSelector is a method for selecting a
population according to some given probability in a way that minimize chance
fluctuations. |
SwapMutator<G extends Gene<?,G>> |
The
SwapMutation changes the order of genes in a chromosome, with the
hope of bringing related genes closer together, thereby facilitating the
production of building blocks. |
termination |
Some default GA termination strategies.
|
TournamentSelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
In tournament selection the best individual from a random sample of s
individuals is chosen from the population Pg.
|
TruncationSelector<G extends Gene<?,G>,C extends Comparable<? super C>> |
In truncation selection individuals are sorted according to their fitness.
|
Enum | Description |
---|---|
BitGene |
Implementation of a BitGene.
|
Optimize |
This
enum determines whether the GA should maximize or minimize the
fitness function. |
The order of the single execution steps of genetic algorithms may slightly
differ from implementation to implementation. The following pseudo-code shows
the Jenetics genetic algorithm steps.
Line (1) creates the initial population and the line (2) calculates the
fitness value of the individuals. (This is done by the
GeneticAlgorithm.setup()
method.) Line (4) increases the generation
number and line (5) and (6) selects the survivor and the offspring population.
The offspring/survivor fraction is determined by the offspringFraction
property of the GA. The selected offspring are altered in line (7). The next
line combines the survivor population and the altered offspring population--
after removing the died individuals--to the new population. The steps from
line (4) to (9) are repeated until a given termination criterion is fulfilled.
Gene
is the base of the building block. Genes are
aggregated in Chromosome
s. One to n Chromosomes are
aggregated in Genotype
s. A Genotype and a fitness
Function
form the Phenotype
.
Phenotypes are collected into a Population
.
Factory<Genotype<?>>
,
and a fitness Function
. The Genotype
implements the
Factory
interface and can therefore be used as prototype for creating
the initial Population and for creating new random Genotypes.
public static void main(final String[] args) {
final Factory〈Genotype〈BitGene〉〉 gtf = Genotype.of(
BitChromosome.of(10, 0.5)
);
final Function〈Genotype〈BitGene〉 Double〉 ff = ...
final GeneticAlgorithm〈BitGene, Double〉
ga = new GeneticAlgorithm〈〉(gtf, ff, Optimize.MAXIMUM)
ga.setup();
ga.evolve(100);
System.out.println(ga.getBestPhenotype());
}
The genotype factory, gtf
, in the example above will create genotypes
which consists of one BitChromosome
with length 10. The one to zero
probability of the newly created genotypes is set to 0.5. The fitness function
is parameterized with a BitGene
and a Double
. That means
that the fitness function is calculating the fitness value as Double
.
The return type of the fitness function must be at least a Comparable
.
The GeneticAlgorithm
object is then created with the genotype factory
and the fitness function. In this example the GA tries to maximize the fitness
function. If you want to find the minimal value you have to change the optimize
parameter from Optimize.MAXIMUM
to Optimize.MINIMUM
. The
ga.setup()
call creates the initial population and calculates its
fitness value. Then the GA evolves 100 generations (ga.evolve(100)
)
an prints the best phenotype found so far onto the console.
public static void main(final String[] args) {
...
ga.setSelectors(new RouletteWheelSelector〈BitGene〉());
ga.setAlterers(
new SinglePointCrossover〈BitGene〉(0.1),
new Mutator〈BitGene〉(0.01)
);
ga.setup();
ga.evolve(100);
System.out.println(ga.getBestPhenotype());
}
The selection strategy for offspring and survivors are set to the
roulette-wheel selector. It is also possible to set the selector for
offspring and survivors independently with the setOffspringSelector
and setSurvivorSelector
methods. The alterers are concatenated, at
first the crossover (with probability 0.1) is performed and then the
chromosomes are mutated (with probability 0.01).
org.jenetics.util
package, supports native Java serialization
and XML serialization. For XML marshaling Jenetics internally uses
the XML support from the Javolution project.
// Writing the population to disk.
final File file = new File("population.xml");
IO.jaxb.write(ga.getPopulation(), file);
// Reading the population from disk.
final Population〈DoubleGene,Double〉 population =
(Population〈DoubleGene, Double〉)IO.jaxb.read(file);
ga.setPopulation(population);
import org.jenetics.BitChromosome;
import org.jenetics.BitGene;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.Mutator;
import org.jenetics.NumberStatistics;
import org.jenetics.Optimize;
import org.jenetics.RouletteWheelSelector;
import org.jenetics.SinglePointCrossover;
import org.jenetics.util.Factory;
import org.jenetics.util.Function;
final class OneCounter
implements Function〈Genotype〈BitGene〉, Integer〉
{
@Override
public Integer apply(final Genotype〈BitGene〉 genotype) {
return ((BitChromosome)genotype.getChromosome()).bitCount();
}
}
public class OnesCounting {
public static void main(String[] args) {
final Factory〈Genotype〈BitGene〉〉 gtf = Genotype.of(
BitChromosome.of(20, 0.15)
);
final Function〈Genotype〈BitGene〉, Integer〉 ff = new OneCounter();
final GeneticAlgorithm〈BitGene, Integer〉 ga =
new GeneticAlgorithm〈〉(gtf, ff, Optimize.MAXIMUM);
ga.setStatisticsCalculator(
new NumberStatistics.Calculator〈BitGene, Integer〉()
);
ga.setPopulationSize(50);
ga.setSelectors(
new RouletteWheelSelector〈BitGene, Integer〉()
);
ga.setAlterers(
new Mutator〈BitGene〉(0.55),
new SinglePointCrossover〈BitGene〉(0.06)
);
ga.setup();
ga.evolve(100);
System.out.println(ga.getBestStatistics());
}
}
import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.sin;
import org.jenetics.Chromosome;
import org.jenetics.EnumGene;
import org.jenetics.GeneticAlgorithm;
import org.jenetics.Genotype;
import org.jenetics.NumberStatistics.Calculator;
import org.jenetics.Optimize;
import org.jenetics.PartiallyMatchedCrossover;
import org.jenetics.PermutationChromosome;
import org.jenetics.SwapMutator;
import org.jenetics.util.Factory;
import org.jenetics.util.Function;
class FF
implements Function〈Genotype〈EnumGene<Integer〉〉, Double〉
{
private final double[][] _adjacence;
public FF(final double[][] adjacence) {
_adjacence = adjacence;
}
@Override
public Double apply(final Genotype〈EnumGene〈Integer〉〉 genotype) {
final Chromosome〈EnumGene〈Integer〉〉 path =
genotype.getChromosome();
double length = 0.0;
for (int i = 0, n = path.length(); i 〈 n; ++i) {
final int from = path.getGene(i).getAllele();
final int to = path.getGene((i + 1)%n).getAllele();
length += _adjacence[from][to];
}
return length;
}
}
public class TravelingSalesman {
public static void main(String[] args) {
final int stops = 20;
final Function〈Genotype〈EnumGene〈Integer〉〉, Double〉 ff =
new FF(adjacencyMatrix(stops));
final Factory〈Genotype〈EnumGene〈Integer〉〉〉 gt = Genotype.of(
PermutationChromosome.ofInteger(stops)
);
final GeneticAlgorithm〈EnumGene〈Integer〉, Double〉
ga = new GeneticAlgorithm〈〉(gt, ff, Optimize.MINIMUM);
ga.setStatisticsCalculator(
new Calculator〈EnumGene〈Integer〉, Double〉()
);
ga.setPopulationSize(300);
ga.setAlterers(
new SwapMutator〈EnumGene〈Integer〉〉(0.2),
new PartiallyMatchedCrossover〈Integer〉(0.3)
);
ga.setup();
ga.evolve(700);
System.out.println(ga.getBestStatistics());
System.out.println(ga.getBestPhenotype());
}
private static double[][] adjacencyMatrix(int stops) {
double[][] matrix = new double[stops][stops];
for (int i = 0; i 〈 stops; ++i) {
for (int j = 0; j 〈 stops; ++j) {
matrix[i][j] = chord(stops, abs(i - j), RADIUS);
}
}
return matrix;
}
private static double chord(int stops, int i, double r) {
return 2.0*r*abs(sin((PI*i)/stops));
}
private static double RADIUS = 10.0;
}
© 2007-2014 Franz Wilhelmstötter (2014-03-07 19:35)