⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrayselecttournament.java

📁 遗传算法源代码,实现了选择操作、交叉操作和变异操作
💻 JAVA
字号:
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * ArraySelectTournament Class * * location: net.openai.ai.ga.selection.common.ArraySelectTournament * */package net.openai.ai.ga.selection.common;import net.openai.ai.ga.population.*;import net.openai.ai.ga.selection.*;/** * <code>ArraySelectTournament</code> is a method by which <code> * Population</code> members are created and chosen using a tournament on a * <code>Population</code>. This algorithm is used for the selection of * parents for recombination (reproduction). * * @author	Jared Grubb * @version	%I%, %G% * @since	JDK1.3 */public class ArraySelectTournament implements ArraySelectionAlgorithm {    private SelectionAlgorithm[] groups;    private Population basePopulation;    private PopulationArray basePopulationArray;    private int goal;    /**     * Creates a new <code>ArraySelectTournament</code> that creates the     * specified number of parent sets by choosing a random parent for each     * parent slot according to each specified selection algorithm. Equilivant to     * <code>ArraySelectTournament(groupPerParent,goal,new Population())</code>.     * This creates populations based on the default <code>Population</code>     * type and collection.     *     * @param groupPerParent the <code>SelectionAlgorithm[]</code> that     *     represents a selection algorithm for each parental slot     * @param goal the number of parent sets to create     */    public ArraySelectTournament(SelectionAlgorithm[] groupPerParent, int goal)    {        this(groupPerParent,goal,new Population(),new PopulationArray());    }    /**     * Creates a new <code>ArraySelectTournament</code> that creates the     * specified number of parent sets by choosing a random parent for each     * parent slot according to each specified selection algorithm and adds     * these into the specified base population. Normally, the base population     * will be an empty population using a certain collection type.     * Equilivant to <code>ArraySelectTournament(groupPerParent, goal,     * basePopulation, new PopulationArray())</code>.     *     * @param groupPerParent the <code>SelectionAlgorithm[]</code> that     *     represents a selection algorithm for each parental slot     * @param goal the number of parent sets to create     * @param basePopulation the <code>Population</code> that each parent set     *     will be added into     */    public ArraySelectTournament(SelectionAlgorithm[] groupPerParent, int goal,                                 Population basePopulation) {        this(groupPerParent,goal,basePopulation,new PopulationArray());    }    /**     * Creates a new <code>ArraySelectTournament</code> that creates the     * specified number of parent sets by choosing a random parent for each     * parent slot according to each specified selection algorithm and adds     * these into the specified base population, which are in turn added into     * a clone of the base population array. Normally, the base population     * and base population array will be empty and implement certain collection     * types.     *     * @param groupPerParent the <code>SelectionAlgorithm[]</code> that     *     represents a selection algorithm for each parental slot     * @param goal the number of parent sets to create     * @param basePopulation the <code>Population</code> that each parent set     *     will be added into     * @param basePopulationArray the <code>PopulationArray</code> that will     *     will be used to hold the parent sets     */    public ArraySelectTournament(SelectionAlgorithm[] groupPerParent, int goal,             Population basePopulation, PopulationArray basePopulationArray) {        this.groups = groupPerParent;        this.goal = goal;        this.basePopulation = basePopulation;        this.basePopulationArray = basePopulationArray;    }    /**     * Creates a <code>PopulationArray</code> according to a tournament. The     * base population array is cloned. A parent set (a <code>Population</code>)     * is created first by cloning the base population specified in the     * constructor. Then, a member is chosen at random from each of the     * selection algorithms specified in the constructor. Each selection     * algorithm is queried only once and the results are used for every parent     * set.     *     * <p>For example, suppose the selection algorithms returned the following     * populations: {1,2,3},{3,5,7}, and {5,6,7,8,9,10}. The base population     * is the default, an empty <code>new Population()</code>. Then the     * following is a possible output for a goal of 5 parent sets:     * <ul>     *   <li>{1,5,10}     *   <li>{2,7,9}     *   <li>{3,3,5}     *   <li>{3,7,5}     *   <li>{3,7,5}     * </ul>     *     * @param pop  the <code>Population</code> to choose from     * @return a <code>PopulationArray</code> derived from the passed     * 		<code>Population</code>     *     */    public PopulationArray selectFromPopulation(Population pop) {        PopulationArray toReturn = new PopulationArray(this.basePopulationArray);        Population[] pops = new Population[groups.length];        Population working;        for(int i=0; i<this.groups.length;i++) {            pops[i] = groups[i].selectFromPopulation(pop);        }        for(int j=0; j<this.goal; j++) {            working = new Population(this.basePopulation);            for(int i=0; i<this.groups.length; i++) {                try {                   working.addCell(pops[i].getCell(                      (int)(Math.random() * pops[i].getSize())));                } catch (IndexOutOfBoundsException e) {                   // Ignore empty populations                }            }            toReturn.addPopulation(working);        }        return toReturn;    }}/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/

⌨️ 快捷键说明

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