populationarray.java

来自「遗传算法源代码,实现了选择操作、交叉操作和变异操作」· Java 代码 · 共 211 行

JAVA
211
字号
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * PopulationArray Class * * location: net.openai.ai.ga.population.PopulationArray * */package net.openai.ai.ga.population;import net.openai.ai.ga.*;import java.util.*;/** * The PopulationArray is the equilivant of an array of <code>Population</code> * elements. This offers the advantage of dynamicism in size and in adding * and removing elements from the collection. * * @author	Jared Grubb * @version	%I%, %G% * @since	JDK1.3 */public class PopulationArray implements java.io.Serializable {    /**     * The <code>Collection</code> that holds all the <code>Population</code>s     */    protected Collection pops;    /**     * Creates a <code>PopulationArray</code> using an <code>ArrayList</code>     */    public PopulationArray() {	this(new ArrayList());    }    /**     * Creates a <code>PopulationArray</code> with a given initial     * <code>Collection</code>     *     * @param initialCollection the <code>Collection</code> to initialize to     */    public PopulationArray(Collection initialCollection) {	this.pops = initialCollection;    }    /**     * Creates a shallow clone of the passed <code>PopulationArray</code>.     * This is not a complete clone, in which each of the <code>Population     * </code>s would also be cloned; this instead creates a copy of the     * list of <code>Population</code>s to which additions and removals     * do not affect the original population.     */    public PopulationArray(PopulationArray toClone) {	try {	    this.pops = (Collection) toClone.pops.getClass().newInstance();	} catch (InstantiationException e) {	    db("Cannot instantiate new clone!");	    throw new RuntimeException(	        "Cannot clone population array's collection: InstantiationException");	} catch (IllegalAccessException e) {	    db("Cannot access ojbect!");	    throw new RuntimeException(	        "Cannot clone population array's collection: IllegalAccessException");	}	this.addPopulations(toClone);    }    /**     * Adds a <code>Population</code> to this <code>PopulationArray</code>.     * Does nothing when given <code>null</code>.     *     * @param toAdd	the <code>Population</code> to add     */    public void addPopulation(Population toAdd) {	if (toAdd != null) {	    pops.add(toAdd);	}    }    /**     * Adds all the <code>Population</code>s in the given <code>     * PopulationArray</code>. Does nothing on <code>null</code> or an     * empty <code>PopulationArray</code>.     *     * @param toAdd	the <code>PopulationArray</code> to add     */    public void addPopulations(PopulationArray toAdd) {	if (toAdd != null) {	    this.pops.addAll(toAdd.pops);	}    }    /**     * Removes a <code>Population</code> from this <code>PopulationArray     * </code>. Returns whether the removal was successful or not. Returns     * <code>true</code> when given a <code>null</code>.     *     * @param toRemove	the <code>Cell</code> to remove     * @return <code>true</code> on success;<br>     *         <code>false</code> otherwise     */    public boolean removePopulation(Population toRemove) {	return (toRemove==null) ? true : pops.remove(toRemove);    }    /**     * Removes a group of <code>Population</code>s from this <code>     * PopulationArray</code>. Does not indicate whether the removal was     * successful or not. Does nothing when given a <code>null</code> or an     * empty <code>Population</code>. Does not throw     * ArrayIndexOutOfBoundsException.     *     * @param toRemove	the <code>PopulationArray</code> to remove     */    public void removePopulations(PopulationArray toRemove) {	if (toRemove!=null) {	    this.pops.removeAll(toRemove.pops);	}    }    /**     * Removes all instances of <code>Population</code> from this <code>     * PopulationArray</code>. Does not indicate whether any removal took     * place. Does nothing when given a <code>null</code> or a non-existant     * <code>Population</code>.     *     * @param toRemove	the <code>PopulationArray</code> to remove     */    public void removeAllSameAs(Population toRemove) {	while (removePopulation(toRemove));    }    /**     * Returns a <code>Population[]</code> array representing all the     * <code>Population</code>s in this population array. Returns an empty     * array when empty.     *     * @return <code>Population[]</code> of all <code>Population</code>s     */    public Population[] getPopulationArray() {	return (Population[]) (pops.toArray(new Population[0]));    }    /**     * Returns a <code>Collection</code> representing the internal     * representation of the <code>PopulationArray</code>     *     * @return <code>Population[]</code> of all <code>Population</code>s     */    public Collection getPopulations() {	return pops;    }    /**     * Returns the number of <code>Population</code>s in this <code>     * PopulationArrays</code>.     *     * @return the number of <code>Population</code>s     */    public int getSize() {	return pops.size();    }    /**     * Returns an Iterator to be used to iterate through all the <Code>     * Population</code>s in this <code>PopulationArray</code>.     *     * @return	an <code>Iterator</code> on the <code>Population</code>     * collection     */    public Iterator getPopulationIterator() {	return pops.iterator();    }    /**     * Removes non-<code>Population</code> objects from the <code>     * Collection</code>     */    public void cleanse() {	Population c;	for (Iterator i = this.getPopulationIterator(); i.hasNext(); ) {	    try {	    	c = (Population) i.next();	    } catch (ClassCastException e) {	    	i.remove();	    }	}    }    private static void db(String message) {    	if (GeneticAlgorithm.getDebug()) {    	    System.err.println("PopulationArray: " + message);    	}    }    public String toString() {        StringBuffer sb = new StringBuffer();        for(Iterator i=this.getPopulationIterator(); i.hasNext(); ) {            sb.append((Population)(i.next()));        }        return sb.toString();    }}/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?