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

📄 selectfitnessrange.java

📁 遗传算法源代码,实现了选择操作、交叉操作和变异操作
💻 JAVA
字号:
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * SelectLeastFitness Class * * location: net.openai.ai.ga.selection.common.SelectLeastFitness * */package net.openai.ai.ga.selection.common;import net.openai.ai.ga.selection.*;import net.openai.ai.ga.population.*;import net.openai.ai.ga.cell.*;import java.util.Iterator;/** * <code>SelectFitnessRange</code> is a <code>SelectionAlgorithm</code> * that will return a portion of the passed <code>Population</code> that * have a fitness in the given range. * * @author	Jared Grubb * @version	%I%, %G% * @since	JDK1.3 */public class SelectFitnessRange implements SelectionAlgorithm {    private double lowerBound, upperBound;    private boolean lowerExclusive, upperExclusive;    /**     * Creates a new <code>SelectFitnessRange</code> object for choosing     * the <code>Cell</code>s out of the specified <code>Population</code> who     * have a fitness in the specified range, inclusive. (Equilivant to     * <code>SelectFitnessRange(lowerBound, false, upperBound, false)</code>).     *     * @param lowerBound the lower bound of the fitness range to allow, inclusive     * @param upperBound the upper bound of the fitness range to allow, inclusive     * @throws <code>ArithmeticException</code> on lowerBound>upperBound     */    public SelectFitnessRange(double lowerBound, double upperBound) {        this(lowerBound, false, upperBound, false);    }    /**     * Creates a new <code>SelectFitnessRange</code> object for choosing     * the <code>Cell</code>s out of the specified <code>Population</code> who     * have a fitness in the specified range, specified with whether each     * bound is treated inclusive or exclusive.     *     * @param lowerBound the lower bound of the fitness range to allow     * @param lowerExclusive <code>false</code> lowerBound is inclusive     *                       <code>true</code> lowerBound is exclusive     * @param upperBound the upper bound of the fitness range to allow     * @param upperExclusive <code>false</code> upperBound is inclusive     *                       <code>true</code> upperrBound is exclusive     * @throws <code>ArithmeticException</code> on lowerBound>upperBound     */    public SelectFitnessRange(double lowerBound, boolean lowerExclusive,                              double upperBound, boolean upperExclusive) {        if (lowerBound>upperBound)            throw new ArithmeticException(                "SelectFitnessRange: lowerBound exceed upperBound, "+lowerBound+">"+upperBound);        this.lowerBound = lowerBound;        this.lowerExclusive = lowerExclusive;        this.upperBound = upperBound;        this.upperExclusive = upperExclusive;    }    /**     * Chooses the <code>Cell</code>s in the specified <code>Population</code>     * with fitness in a given range. There is no limit to the number of     * members returned; it is possible that no cells are returned (when none     * meet the range requirements) or that all the cells are returned (when     * they all meet the requirements).     *     * @param pop  the <code>Population</code> to choose from     * @return a selected <code>Population</code> in arbitrary order     */    public Population selectFromPopulation(Population pop) {        Population output = new Population();        Cell tempCell;        double tempFitness;        for(Iterator i=pop.getCellIterator(); i.hasNext(); ) {            tempCell = (Cell) i.next();            tempFitness = tempCell.getFitness();            if (tempFitness>this.upperBound || tempFitness<this.lowerBound)                continue;            if (lowerExclusive && tempFitness==this.lowerBound)                continue;            if (upperExclusive && tempFitness==this.upperBound)                continue;            output.addCell(tempCell);        }        return output;    }}/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/

⌨️ 快捷键说明

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