aima.core.search.local.GeneticAlgorithmForNumbers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
The newest version!
package aima.core.search.local;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Variant of the genetic algorithm which uses double numbers from a fixed
* interval instead of symbols from a finite alphabet in the representations of
* individuals. Reproduction uses values somewhere between the values of the
* parents. Mutation adds some random offset. Progress tracer implementations
* can be used to get informed about the running iterations.
* A typical use case for this genetic algorithm version is finding maximums in
* a given mathematical (fitness) function.
*
* @author Ruediger Lunde
*
*/
public class GeneticAlgorithmForNumbers extends GeneticAlgorithm {
private double minimum;
private double maximum;
/**
* Constructor.
*
* @param individualLength
* vector length used for the representations of individuals. Use
* 1 for analysis of functions f(x).
* @param min
* minimal value to be used in vector elements.
* @param max
* maximal value to be used in vector elements.
* @param mutationProbability
* probability of mutations.
*/
public GeneticAlgorithmForNumbers(int individualLength, double min, double max, double mutationProbability) {
super(individualLength, Collections. emptySet(), mutationProbability);
minimum = min;
maximum = max;
}
/** Convenience method. */
public Individual createRandomIndividual() {
List representation = new ArrayList(individualLength);
for (int i = 0; i < individualLength; i++)
representation.add(minimum + random.nextDouble() * (maximum - minimum));
return new Individual(representation);
}
/**
* Produces for each number in the descendant's representation a random
* value between the corresponding values of its parents.
*/
@Override
protected Individual reproduce(Individual x, Individual y) {
List newRep = new ArrayList(x.length());
double r = random.nextDouble();
for (int i = 0; i < x.length(); i++)
newRep.add(x.getRepresentation().get(i) * r + y.getRepresentation().get(i) * (1 - r));
return new Individual<>(newRep);
}
/**
* Changes each component in the representation by random. The maximum
* change is +- (maximum - minimum) / 4, but smaller changes have a higher
* likelihood.
*/
@Override
protected Individual mutate(Individual child) {
List rep = child.getRepresentation();
List newRep = new ArrayList();
for (double num : rep) {
double r = random.nextDouble() - 0.5;
num += r * r * r * (maximum - minimum) / 2;
if (num < minimum)
num = minimum;
else if (num > maximum)
num = maximum;
newRep.add(num);
}
return new Individual<>(newRep);
}
}