📄 world.java
字号:
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * World * * location: net.openai.ai.ga.world.World * */package net.openai.ai.ga.world;import net.openai.ai.ga.environment.*;import net.openai.ai.ga.population.*;import net.openai.ai.ga.selection.*;/** * World contains all the information necessary to simulate a complete * genetic algorithm implementation. This includes: * <ul> * <li>An environment (the problem to be solved) * <li>A population (possible solutions to solve the problem) * <li>Parent selection algorithm (ways to choose parents to create new * solutions to solve the problem) * <li>Mutation selection algorithm (ways to choose members of the * population to mutate * <li>Survival selection algorithm (ways to choose who will survive into * the next generation * </ul> * <p>One iteration performs the following operations: * <ul> * <li>Mature the population (increase its age) * <li>Adjust the environment to fit its population * <li>Evaluate the population * <li>Create new offspring * <li>Mutate the population * <li>Trim the population * </ul> * @author Jared Grubb * @version %I%, %G% * @since JDK1.3 */public class World implements java.io.Serializable { /** * The environment that the population must evaluate against */ private Environment environment; /** * The name of this World object. Used in GeneticAlgorithm operations */ private String name; /** * The population of this world */ private Population population; /** * The parent selection algorithm by which new cells are created. * This occurs before mutation and before survival selection. */ private ArraySelectionAlgorithm parentSelection; /** * The mutation selection algorithm by which cells in the population * are selected to mutate. This occurs after parent selection (and may * therefore include new offspring) but before survival selection. */ private SelectionAlgorithm mutationSelection; /** * The survival selection algorithm by which the population to survive is * chosen at the end of an iteration. It occurs after parent selection and * mutation. */ private SelectionAlgorithm survivalSelection; /** * The number of generations this world has been iterated through. */ private int generation; /** * Reset generation count and create an empty <code>Population</code> and no * selection algorithms. Gives the world a name. * * @param name a <code>String</code> for the name of this World */ public World(String name) { this(name, new Population()); } /** * Reset generation count, use the specified <code>Population</code>, and * initialize no selection algorithms. * * @param name a <code>String</code> for the name of this World * @param initialPopulation the inital <code>Population</code> for this World */ public World(String name, Population initialPopulation) { this(name, initialPopulation, null, null, null); } /** * Reset generation count and create an empty population and specified * selection algorithms. Gives the world a name. Assign * * @param name a <code>String</code> for the name of this World * @param parentSelection an <code>ArraySelectionAlgorithm</code> * to be used for parent selection. * @param mutationSelection a <code>SelectionAlgorithm</code> * to be used for mutation selection. * @param survivalSelection a <code>SelectionAlgorithm</code> * to be used for survival selection. */ public World(String name, ArraySelectionAlgorithm parentSelection, SelectionAlgorithm mutationSelection, SelectionAlgorithm survivalSelection) { this(name, new Population(), parentSelection, mutationSelection, survivalSelection); } /** * Reset generation count and create an empty population and specified * selection algorithms. Gives the world a name. Assign * * @param name a <code>String</code> for the name of this World * @param initialPopulation the inital <code>Population</code> for this World * @param parentSelection an <code>ArraySelectionAlgorithm</code> * to be used for parent selection. * @param mutationSelection a <code>SelectionAlgorithm</code> * to be used for mutation selection. * @param survivalSelection a <code>SelectionAlgorithm</code> * to be used for survival selection. */ public World(String name, Population initialPopulation, ArraySelectionAlgorithm parentSelection, SelectionAlgorithm mutationSelection, SelectionAlgorithm survivalSelection) { this.generation = 0; this.population = initialPopulation; this.parentSelection = parentSelection; this.mutationSelection= mutationSelection; this.survivalSelection= survivalSelection; } /** * Returns the name of this World. * * @return a <code>String</code> containing the name of this World */ public String getName() { return this.name; } /** * Returns a string showing the current status of this World. Returns * the name of the World followed by the String returned by the * Population's toString(). * * @return a <code>String</code> showing the status of this World */ public String toString() { return this.getName() + ":" + this.population; } /** * Returns the Environment assigned to this World. * * @return the <code>Environment</code> assigned to this World */ public Environment getEnvironment() { return this.environment; } /** * Sets the Environment used in this World. * * @param newEnvironment the <code>Environment</code> to use in this World */ public void setEnvironment(Environment newEnvironment) { this.environment = newEnvironment; } /** * Returns the Population assigned to this World. * * @return the <code>Population</code> assigned to this World */ public Population getPopulation() { return this.population; } /** * Sets the Population used in this World. * * @param newPopulation the <code>Population</code> to use in this World */ public void setPopulation(Population newPopulation) { this.population = newPopulation; } /** * Iterates this World through one generation. Does the following items: * <ul> * <li>Calls the environment's reactToPopulation method * <li>Matures the population * <li>Evaluates the population * <li>Creates new offspring * <li>Mutates the population * <li>Trims the population * </ul> */ public void iterate() { this.generation++; this.environment.reactToPopulation(this.population); this.maturePopulation(); this.evaluatePopulation(); this.createOffspring(); this.mutatePopulation(); this.trimPopulation(); } /** * Calls the Population's mature method. */ private void maturePopulation() { this.population.mature(); } /** * Calls the Population's evaluate method. */ private void evaluatePopulation() { this.population.evaluate(environment); } /** * Subjects the population to the parent selection algorithm and passes the * returned PopulationArray to the Population's <code>combine()</code> * method. */ private void createOffspring() { PopulationArray parents = this.parentSelection.selectFromPopulation(this.population); this.population.combine(parents); } /** * Subjects the population to the mutation selection algorithm and passes * the returned Population to the Population's <code>mutate()</code> * method. */ private void mutatePopulation() { Population mutants = this.mutationSelection.selectFromPopulation(this.population); this.population.mutate(mutants); } /** * Subjects the population to the survival selection algorithm and sets the * population to equal the Population returned. */ private void trimPopulation() { Population oldPop = new Population(this.population); this.population.removeAllCellsBut( this.survivalSelection.selectFromPopulation(this.population)); oldPop.removeCells(this.population); Population.condemnPopulation(oldPop); } /** * Sets the parent selection algorithm to the given algorithm. This * algorithm is used to determine the groups of cells that will be used * to create offspring. * * @param parentSelection a <code>ArraySelectionAlgorithm</code> to set to */ public void setParentSelectionAlgorithm( ArraySelectionAlgorithm parentSelection) { this.parentSelection = parentSelection; } /** * Returns the current parent selection algorithm. * * @return the parent-selection <code>ArraySelectionAlgorithm</code> */ public ArraySelectionAlgorithm getParentSelectionAlgorithm() { return this.parentSelection; } /** * Sets the mutation selection algorithm to the given algorithm. This * algorithm is used to determine the cells that will subjected to * mutation. * * @param mutationSelection a <code>SelectionAlgorithm</code> to set to */ public void setMutationSelectionAlgorithm( SelectionAlgorithm mutationSelection) { this.mutationSelection = mutationSelection; } /** * Returns the current mutation selection algorithm. This algorithm is * used to determine the cells that will subjected to mutation. * * @return the mutation-selection <code>SelectionAlgorithm</code> */ public SelectionAlgorithm getMutationSelectionAlgorithm() { return this.mutationSelection; } /** * Sets the survival selection algorithm to the given algorithm. This * algorithm is used to determine the cells that will survive into * the next generation. * * @param survivalSelection a <code>SelectionAlgorithm</code> to set to */ public void setSurvivalSelectionAlgorithm( SelectionAlgorithm survivalSelection) { this.survivalSelection = survivalSelection; } /** * Returns the current survival selection algorithm. This algorithm is * used to determine the cells that will survive into the next generation. * * @return the mutation-selection <code>SelectionAlgorithm</code> */ public SelectionAlgorithm getSurvivalSelectionAlgorithm() { return this.survivalSelection; }}/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -