public class GeneticTrainingAlgorithm
extends java.lang.Object
Constructor and Description |
---|
GeneticTrainingAlgorithm(int[] topology,
int numGenes,
double learningRate,
double mutation,
boolean classify,
boolean keepBest)
Train the neural networks using a version of a genetic training
algorithm.
|
Modifier and Type | Method and Description |
---|---|
NeuralNetwork |
best(boolean max)
Get the best neural network based on its fitness.
|
double |
bestFitness(boolean max)
Get the best fitness in the generation.
|
void |
evolve(boolean max)
Evolve the neural networks to create the next generation.
|
double |
getFitness(NeuralNetwork net)
Get the fitness of a neural network.
|
java.util.HashMap<NeuralNetwork,java.lang.Double> |
getGenes()
Get a HashMap linking the neural networks to their fitness.
|
void |
load(java.lang.String filename)
Load a gene pool from a file.
|
void |
setFitness(NeuralNetwork net,
double score)
Set the fitness of a neural network.
|
public GeneticTrainingAlgorithm(int[] topology, int numGenes, double learningRate, double mutation, boolean classify, boolean keepBest)
topology
- The topology of the networks in this format int[]{input,
hidden1, hidden2, hiddenN, output}numGenes
- The number of different genes per generation.learningRate
- The amount a mutation affects the genes of the next
generation.mutation
- The percent chance of a mutation occurring: a double from 0..1
inclusiveclassify
- A boolean flag specifying whether this is a classification
problem.keepBest
- A boolean flag specifying whether to keep the gene with the
best fitness in the next generation without mutation.public java.util.HashMap<NeuralNetwork,java.lang.Double> getGenes()
public void load(java.lang.String filename)
filename
- The filename of the file containing the weight values for a
neural net.public void setFitness(NeuralNetwork net, double score)
net
- The neural network being evaluated.score
- The fitness of the neural network.public double getFitness(NeuralNetwork net)
net
- The neural network being evaluated.public void evolve(boolean max)
max
- A boolean flag of whether to maximize or minimize the fitness.
Max is true, min is false.public NeuralNetwork best(boolean max)
max
- A boolean flag of whether a higher fitness is better than a
lower fitness. true for max, false for min.public double bestFitness(boolean max)
max
- A boolean flag of whether a higher fitness is better than a
lower fitness. true for max, false for min.