📄 stringgene.java
字号:
String s;
if (m_value == null) {
s = "null";
}
else {
if (m_value.equals("")) {
s = "\"\"";
}
else {
s = m_value;
}
}
return encode("" + s) +
PERSISTENT_FIELD_DELIMITER + m_minLength +
PERSISTENT_FIELD_DELIMITER + m_maxLength +
PERSISTENT_FIELD_DELIMITER +
encode("" + m_alphabet);
}
@Override
public String getBusinessKey() {
return m_value + PERSISTENT_FIELD_DELIMITER + m_minLength
+ PERSISTENT_FIELD_DELIMITER + m_maxLength;
}
/**
* Sets the value (allele) of this Gene to the new given value. This class
* expects the value to be a String instance. If the value is shorter or
* longer than the minimum or maximum length or any character is not within
* the valid alphabet an exception is thrown.
*
* @param a_newValue the new value of this Gene instance
*
* @author Klaus Meffert
* @since 1.1
*/
public void setAllele(final Object a_newValue) {
if (a_newValue != null) {
String temp = (String) a_newValue;
if (temp.length() < m_minLength ||
temp.length() > m_maxLength) {
throw new IllegalArgumentException(
"The given value is too short or too long!");
}
// Check for validity of alphabet.
// -------------------------------
if (!isValidAlphabet(temp, m_alphabet)) {
throw new IllegalArgumentException("The given value contains"
+ " at least one invalid character.");
}
if (getConstraintChecker() != null) {
if (!getConstraintChecker().verify(this, a_newValue, null, -1)) {
return;
}
}
m_value = temp;
}
else {
m_value = null;
}
}
/**
* Provides an implementation-independent means for creating new Gene
* instances.
*
* @return a new Gene instance of the same type and with the same setup as
* this concrete Gene
*
* @author Klaus Meffert
* @since 1.1
*/
protected Gene newGeneInternal() {
try {
StringGene result = new StringGene(getConfiguration(), m_minLength,
m_maxLength, m_alphabet);
result.setConstraintChecker(getConstraintChecker());
return result;
} catch (InvalidConfigurationException iex) {
throw new IllegalStateException(iex.getMessage());
}
}
/**
* Compares this StringGene with the specified object (which must also
* be a StringGene) for order, which is determined by the String
* value of this Gene compared to the one provided for comparison.
*
* @param a_other the StringGene to be compared to this StringGene
* @return a negative int, zero, or a positive int as this object
* is less than, equal to, or greater than the object provided for comparison
*
* @throws ClassCastException if the specified object's type prevents it
* from being compared to this StringGene
*
* @author Klaus Meffert
* @since 1.1
*/
public int compareTo(Object a_other) {
StringGene otherStringGene = (StringGene) a_other;
// First, if the other gene (or its value) is null, then this is
// the greater allele. Otherwise, just use the String's compareTo
// method to perform the comparison.
// ---------------------------------------------------------------
if (otherStringGene == null) {
return 1;
}
else if (otherStringGene.m_value == null) {
// If our value is also null, then we're the same. Otherwise,
// this is the greater gene.
// ----------------------------------------------------------
if (m_value == null) {
if (isCompareApplicationData()) {
return compareApplicationData(getApplicationData(),
otherStringGene.getApplicationData());
}
else {
return 0;
}
}
else {
return 1;
}
}
else {
int res = m_value.compareTo(otherStringGene.m_value);
if (res == 0) {
if (isCompareApplicationData()) {
return compareApplicationData(getApplicationData(),
otherStringGene.getApplicationData());
}
else {
return 0;
}
}
else {
return res;
}
}
}
public int size() {
return m_value.length();
}
public int getMaxLength() {
return m_maxLength;
}
public int getMinLength() {
return m_minLength;
}
public void setMinLength(int m_minLength) {
this.m_minLength = m_minLength;
}
public void setMaxLength(int m_maxLength) {
this.m_maxLength = m_maxLength;
}
public String getAlphabet() {
return m_alphabet;
}
/**
* Sets the valid alphabet of the StringGene. The caller needs to care that
* there are no doublettes in the alphabet. Otherwise there is no guarantee
* for correct functioning of the class!
* @param a_alphabet valid alphabet for allele
*
* @author Klaus Meffert
* @since 1.1
*/
public void setAlphabet(String a_alphabet) {
m_alphabet = a_alphabet;
}
/**
* Retrieves a string representation of this StringGene's value that
* may be useful for display purposes.
*
* @return a string representation of this StringGene's value
*
* @author Klaus Meffert
* @since 1.1
*/
public String toString() {
String s = "StringGene=";
if (m_value == null) {
s += "null";
}
else {
if (m_value.equals("")) {
s += "\"\"";
}
else {
s += m_value;
}
}
return s;
}
/**
* Retrieves the String value of this Gene, which may be more convenient in
* some cases than the more general getAllele() method.
*
* @return the String value of this Gene
*
* @since 1.1
*/
public String stringValue() {
return m_value;
}
/**
* Checks whether a string value is valid concerning a given alphabet.
* @param a_value the value to check
* @param a_alphabet the valid alphabet to check against
* @return true: given string value is valid
*
* @author Klaus Meffert
* @since 1.1
*/
private boolean isValidAlphabet(String a_value, String a_alphabet) {
if (a_value == null || a_value.length() < 1) {
return true;
}
if (a_alphabet == null) {
return true;
}
if (a_alphabet.length() < 1) {
return false;
}
// Loop over all characters of a_value.
// ------------------------------------
int length = a_value.length();
char c;
for (int i = 0; i < length; i++) {
c = a_value.charAt(i);
if (a_alphabet.indexOf(c) < 0) {
return false;
}
}
return true;
}
/**
* Applies a mutation of a given intensity (percentage) onto the atomic
* element at given index (NumberGenes only have one atomic element).
* @param index index of atomic element, between 0 and size()-1
* @param a_percentage percentage of mutation (greater than -1 and smaller
* than 1).
*
* @author Klaus Meffert
* @since 1.1
*/
public void applyMutation(int index, double a_percentage) {
String s = stringValue();
int index2 = -1;
boolean randomize;
int len = 0;
if (m_alphabet != null) {
len = m_alphabet.length();
if (len < 1) {
// Does mutation make sense here?
// ------------------------------
randomize = true;
}
else {
randomize = false;
}
}
else {
randomize = true;
}
char newValue;
RandomGenerator rn = getConfiguration().getRandomGenerator();
if (!randomize) {
int indexC = m_alphabet.indexOf(s.charAt(index));
index2 = indexC + (int) Math.round(len * a_percentage);
// If index of new character out of bounds then randomly choose a new
// character. This randomness is assumed to help in the process of
// evolution.
// ------------------------------------------------------------------
if (index2 < 0 || index2 >= len) {
index2 = rn.nextInt(len);
}
newValue = m_alphabet.charAt(index2);
}
else {
index2 = rn.nextInt(256);
newValue = (char) index2;
}
// Set mutated character by concatenating the String with it.
// ----------------------------------------------------------
if (s == null) {
s = "" + newValue;
}
else {
s = s.substring(0, index) + newValue + s.substring(index + 1);
}
setAllele(s);
}
protected Object getInternalValue() {
return m_value;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -