📄 stringgene.java
字号:
}
}
/**
* 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 throwsn
*
* @param a_newValue the new value of this Gene instance.
*
* @author Klaus Meffert
* @since 1.1
*/
public void setAllele(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 (m_geneAlleleChecker != null) {
if (!m_geneAlleleChecker.verify(this, a_newValue)) {
return;
}
}
m_value = temp;
}
else {
m_value = null;
}
}
/**
* Sets the constraint checker to be used for this gene whenever method
* setAllele(Object a_newValue) is called
* @param a_constraintChecker the constraint checker to be set
*
* @author Klaus Meffert
* @since 2.0
*/
public void setConstraintChecker(IGeneConstraintChecker a_constraintChecker) {
m_geneAlleleChecker = a_constraintChecker;
}
/**
* @return IGeneConstraintChecker the constraint checker to be used whenever
* method setAllele(Object a_newValue) is called
*
* @author Klaus Meffert
* @since 2.0
*/
public IGeneConstraintChecker getConstraintChecker() {
return m_geneAlleleChecker;
}
/**
* Provides an implementation-independent means for creating new Gene
* instances. The new instance that is created and returned should be
* setup with any implementation-dependent configuration that this Gene
* instance is setup with (aside from the actual value, of course). For
* example, if this Gene were setup with bounds on its value, then the
* Gene instance returned from this method should also be setup with
* those same bounds. This is important, as the JGAP core will invoke this
* method on each Gene in the sample Chromosome in order to create each
* new Gene in the same respective gene position for a new Chromosome.
* <p>
* It should be noted that nothing is guaranteed about the actual value
* of the returned Gene and it should therefore be considered to be
* undefined.
*
* @return A new Gene instance of the same type and with the same
* setup as this concrete Gene.
*
* @author Klaus Meffert
* @since 1.1
*/
public Gene newGene() {
return new StringGene(m_minLength, m_maxLength, m_alphabet);
}
/**
* 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 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 other) {
StringGene otherStringGene = (StringGene) 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.
// ----------------------------------------------------------
return m_value == null ? 0 : 1;
}
else {
try {
return m_value.compareTo(otherStringGene.m_value);
}
catch (ClassCastException e) {
e.printStackTrace();
throw e;
}
}
}
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
* @param a_alphabet valid aplhabet for allele
*
* @author Klaus Meffert
* @since 1.1
*/
public void setAlphabet(String a_alphabet) {
/**@todo optionally check if alphabet contains doublettes*/
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() {
if (m_value == null) {
return "null";
}
else {
if (m_value.equals("")) {
return "\"\"";
}
else {
return m_value.toString();
}
}
}
/**
* 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 (String) 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) {
randomize = true;
}
else {
randomize = false;
}
}
else {
randomize = true;
}
char newValue;
RandomGenerator rn;
if (Genotype.getConfiguration() != null) {
rn = Genotype.getConfiguration().getRandomGenerator();
}
else {
rn = new StockRandomGenerator();
}
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 helps 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 + -