package aima.core.search.local;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import aima.core.search.framework.GoalTest;
import aima.core.search.framework.Metrics;
import aima.core.util.Util;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 4.8, page
* 129.
*
*
*
* function GENETIC-ALGORITHM(population, FITNESS-FN) returns an individual * inputs: population, a set of individuals * FITNESS-FN, a function that measures the fitness of an individual * * repeat * new_population <- empty set * for i = 1 to SIZE(population) do * x <- RANDOM-SELECTION(population, FITNESS-FN) * y <- RANDOM-SELECTION(population, FITNESS-FN) * child <- REPRODUCE(x, y) * if (small random probability) then child <- MUTATE(child) * add child to new_population * population <- new_population * until some individual is fit enough, or enough time has elapsed * return the best individual in population, according to FITNESS-FN * -------------------------------------------------------------------------------- * function REPRODUCE(x, y) returns an individual * inputs: x, y, parent individuals * * n <- LENGTH(x); c <- random number from 1 to n * return APPEND(SUBSTRING(x, 1, c), SUBSTRING(y, c+1, n)) ** * Figure 4.8 A genetic algorithm. The algorithm is the same as the one * diagrammed in Figure 4.6, with one variation: in this more popular version, * each mating of two parents produces only one offspring, not two. * * @author Ciaran O'Reilly * @author Mike Stampone * * @param * the type of the alphabet used in the representation of the * individuals in the population (this is to provide flexibility in * terms of how a problem can be encoded). */ public class GeneticAlgorithm { protected static final String POPULATION_SIZE = "populationSize"; protected static final String ITERATIONS = "iterations"; protected static final String TIME_IN_MILLISECONDS = "timeInMilliseconds"; // protected Metrics metrics = new Metrics(); // protected int individualLength; protected List finiteAlphabet; protected double mutationProbability; protected Random random; public GeneticAlgorithm(int individualLength, Set finiteAlphabet, double mutationProbability) { this(individualLength, finiteAlphabet, mutationProbability, new Random()); } public GeneticAlgorithm(int individualLength, Set finiteAlphabet, double mutationProbability, Random random) { this.individualLength = individualLength; this.finiteAlphabet = new ArrayList(finiteAlphabet); this.mutationProbability = mutationProbability; this.random = random; assert (this.mutationProbability >= 0.0 && this.mutationProbability <= 1.0); } /** * Returns the best individual in the specified population, according to the * specified FITNESS-FN and goal test. * * @param population * a set of individuals * @param fitnessFn * a function that measures the fitness of an individual * @param goalTest * test determines whether a given individual is fit enough to * return. * @param maxTimeMilliseconds * the maximum time in milliseconds that the algorithm is to run * for (approximate). Only used if > 0L. * @return the best individual in the specified population, according to the * specified FITNESS-FN and goal test. */ // function GENETIC-ALGORITHM(population, FITNESS-FN) returns an individual // inputs: population, a set of individuals // FITNESS-FN, a function that measures the fitness of an individual public Individual geneticAlgorithm(Set