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

📄 fixedbinarystring.java

📁 遗传算法源代码,实现了选择操作、交叉操作和变异操作
💻 JAVA
字号:
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*//** * FixedBinaryString *  * location: net.openai.ai.ga.cell.encoding.FixedBinaryString *  */package net.openai.ai.ga.cell.encoding;/** * The <code>FixedBinaryString</code> is an encoding method using a string * of binary digits. The value is represented as a <code>long</code> and has * a fixed length in bits. It supports single cross-over and mutation. *  * @author	Jared Grubb * @version	%I%, %G% * @since	JDK1.3 */public class FixedBinaryString {    /**     * Every bit is a bit in a long. Therefore this string is limited to 64 bits     */    private long   bits;    /**     * The number of bits in the binary string     */    private int    size;    /**     * The mask to and bits with every time it is set     */    private long   mask;    /**     * The name of this binary string     */    private String name;    /**     * Creates a new <code>FixedBinaryString</code> with the specified bit-     * length and given name.     *      * @param name	a <code>String</code> representing the string's name     * @param size	the length of the binary string in bits     */    public FixedBinaryString(String name, int size) {	setName(name);	setSize(size);    }    /**     * Creates a new <code>FixedBinaryString</code> with the specified bit-length     * and initial value and given name.     *      * @param name	a <code>String</code> representing the string's name     * @param size	the length of the binary string in bits     * @param value	the initial value for the binary string     */    public FixedBinaryString(String name, int size, long value) {	setName(name);	setSize(size);	setLong(value);    }    /**     * Creates and returns a clone of the given <code>FixedBinaryString</code>.     * Throws <code>NullPointerException</code> when passed <code>null</code>.     *      * @param toClone	the binary string to clone     */    public FixedBinaryString(FixedBinaryString toClone) {	this(toClone.getName(), toClone.getSize(), toClone.getLong());    }    /**     * Returns the length of this binary string in bits     *      * @returns the length of the string in bits     */    public int getSize() {	return size;    }     /**     * Sets the length of this binary string in bits     *      * @param size	the length of the binary string in bits     */    public void setSize(int size) {	if (size < 0 || size > 64) {	    // Throw exception	} 	this.size = size;	// For example: size = 6:	// -1<<size = ...1111_1100_0000	// ~(-1<<size)= ...0000_0011_1111	this.mask = ~(-1L << size);    }     /**     * Returns the value of this binary string as a <code>long</code>.     *      * @returns a <code>long</code> value of this binary string     */    public long getLong() {	return bits;    }     /**     * Sets the binary string to the value as a <code>long</code>. Performs a     * bit-wise AND (&) to clear all bits above the length of this binary string.     *      * @param newVal	a <code>long</code> value to set this to     */    public void setLong(long newVal) {	bits = (newVal & this.mask);    }     /**     * Returns the bit-status of a certain bit in this binary string.     *      * @returns	<code>false</code> if the bit is 0;     * <code>true</code> if the bit is 1;     */    public boolean getBit(int bitNum) {	if (bitNum < 0 || bitNum > this.size) {	    // Throw exception	    return false;	} 	return (bits & (1L << bitNum)) != 0;    }     /**     * Sets a given bit in the binary string     *      * @param bitNum	sets the given bit to <code>true</code> ("1")     */    public void setBit(int bitNum) {	if (bitNum < 0 || bitNum > this.size) {	    // Throw exception	    return;	} 	this.bits &= ~(1L << bitNum);    }     /**     * Clears a given bit in the binary string     *      * @param bitNum	sets the given bit to <code>false</code> ("0")     */    public void clearBit(int bitNum) {	if (bitNum < 0 || bitNum > this.size) {	    // Throw exception	    return;	} 	this.bits |= (1L << bitNum);    }     /**     * Sets the name of this binary string to the given name     *      * @param name	a <code>String</code> for the new name     */    public void setName(String name) {	this.name = name;    }     /**     * Gets the name of this binary string     *      * @returns	a <code>String</code> of the name of this binary string     */    public String getName() {	return this.name;    }     /**     * Returns a <code>String</code> representation of this binary string as a     * string of binary digits. Leading zeroes are trimmed.     *      * @returns a <code>String</code> representation in binary     */    public String toBinaryString() {	return Long.toBinaryString(bits);    }     /**     * Returns a <code>String</code> representation of this binary string as a     * string of binary digits, including leading zeroes     *      * @returns a <code>String</code> representation in binary     */    public String toFullBinaryString() {	String full = this.toBinaryString();	for (int i = full.length(); i < this.size; i++) {	    full = "0" + full;	}	return full;    }     /**     * Returns a <code>String</code> representation of this binary string as a     * string of hex digits. Leading zeroes are trimmed.     *      * @returns a <code>String</code> representation in hexadecimal     */    public String toHexString() {	return Long.toHexString(bits);    }     /**     * Returns a <code>String</code> representation of this binary string as a     * string of hexadecimal digits, including leading zeroes     *      * @returns a <code>String</code> representation in hexadecimal     */    public String toFullHexString() {	String full = this.toHexString();	int    expSize = this.size / 4;	if (this.size % 4 != 0) {	    expSize++;	} 	for (int i = full.length(); i < expSize; i++) {	    full = "0" + full;	}	return full;    }     /**     * Returns a new <code>FixedBinaryString</code> based on two other     * binary strings of the same length. A cross-over point is picked at     * random and all bits before this point are the same as the first parent's     * and all bits after this point are the same as the second parent's.     * Neither parent is altered during this call.     *      * @returns	a new <code>FixedBinaryString</code>     */    public static FixedBinaryString cross(FixedBinaryString first, 					  FixedBinaryString second) {	if (first.getSize() != second.getSize()) {	    // Throw exception	    return null;	} 	FixedBinaryString n = new FixedBinaryString(first);	long		  crossover_pt = -1L 					 << ((long) (Math.random() 					 * (double) (first.getSize())));	n.setLong((first.getLong() & crossover_pt) 		  + (second.getLong() & ~crossover_pt));	return n;    }     /**     * Mutates a given number of bits in the binary string. The mutation is the     * inversion of the bit. A non-positive argument results in no change in the     * string.     *      * @param amount the number of bits to invert ("mutate")     */    public void mutate(int amount) {	for (int i = 0; i < amount; i++) {	    bits ^= 1L << ((long) (Math.random() * (double) (this.size)));	}     } }/*--- formatting done in "Sun Java Convention" style on 12-28-2000 ---*/

⌨️ 快捷键说明

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