Update to the swap-bits mutator. True changes are now preferred.
This commit is contained in:
parent
2873df5058
commit
68c25a6c53
@ -10,20 +10,25 @@ import eva2.server.go.problems.InterfaceOptimizationProblem;
|
|||||||
import eva2.tools.math.RNG;
|
import eva2.tools.math.RNG;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by IntelliJ IDEA.
|
* Swap two random bits of a GA individual. If preferPairs is true, unequal pairs
|
||||||
* User: streiche
|
* are picked with some preference (by trying for l/2 times, where l is the binary
|
||||||
|
* individual length).
|
||||||
|
*
|
||||||
|
* User: streiche, mkron
|
||||||
* Date: 05.08.2004
|
* Date: 05.08.2004
|
||||||
* Time: 17:45:36
|
* Time: 17:45:36
|
||||||
* To change this template use File | Settings | File Templates.
|
* To change this template use File | Settings | File Templates.
|
||||||
*/
|
*/
|
||||||
public class MutateGASwapBits implements InterfaceMutation, java.io.Serializable {
|
public class MutateGASwapBits implements InterfaceMutation, java.io.Serializable {
|
||||||
private int m_NumberOfMutations = 1;
|
private int m_NumberOfMutations = 1;
|
||||||
|
private boolean preferPairs = true; // if true, pairs of (1,0) are swapped with higher probability
|
||||||
|
|
||||||
public MutateGASwapBits() {
|
public MutateGASwapBits() {
|
||||||
|
|
||||||
}
|
}
|
||||||
public MutateGASwapBits(MutateGASwapBits mutator) {
|
public MutateGASwapBits(MutateGASwapBits mutator) {
|
||||||
this.m_NumberOfMutations = mutator.m_NumberOfMutations;
|
this.m_NumberOfMutations = mutator.m_NumberOfMutations;
|
||||||
|
this.setPreferTrueChange(mutator.isPreferTrueChange());
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This method will enable you to clone a given mutation operator
|
/** This method will enable you to clone a given mutation operator
|
||||||
@ -73,8 +78,8 @@ public class MutateGASwapBits implements InterfaceMutation, java.io.Serializable
|
|||||||
int[][] mutationIndices = new int[this.m_NumberOfMutations][2];
|
int[][] mutationIndices = new int[this.m_NumberOfMutations][2];
|
||||||
boolean tmpBit;
|
boolean tmpBit;
|
||||||
for (int i = 0; i < mutationIndices.length; i++) {
|
for (int i = 0; i < mutationIndices.length; i++) {
|
||||||
mutationIndices[i][0] = RNG.randomInt(0, ((InterfaceGAIndividual)individual).getGenotypeLength());;
|
mutationIndices[i][0] = getRandomIndex(individual, true); // may prefer true bits
|
||||||
mutationIndices[i][1] = RNG.randomInt(0, ((InterfaceGAIndividual)individual).getGenotypeLength());;
|
mutationIndices[i][1] = getRandomIndex(individual, false); // may prefer false bits
|
||||||
}
|
}
|
||||||
// double instances of mutationIndices could be checked here... *sigh*
|
// double instances of mutationIndices could be checked here... *sigh*
|
||||||
for (int i = 0; i < mutationIndices.length; i++) {
|
for (int i = 0; i < mutationIndices.length; i++) {
|
||||||
@ -87,6 +92,24 @@ public class MutateGASwapBits implements InterfaceMutation, java.io.Serializable
|
|||||||
//System.out.println("After Mutate: " +((GAIndividual)individual).getSolutionRepresentationFor());
|
//System.out.println("After Mutate: " +((GAIndividual)individual).getSolutionRepresentationFor());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getRandomIndex(AbstractEAIndividual individual, boolean maybePrefered) {
|
||||||
|
int genoLen = ((InterfaceGAIndividual)individual).getGenotypeLength();
|
||||||
|
int k = RNG.randomInt(0, genoLen);
|
||||||
|
if (isPreferTrueChange()) {
|
||||||
|
int maxTries=genoLen/2;
|
||||||
|
while (!(maybePrefered==((InterfaceGAIndividual)individual).getBGenotype().get(k)) && (maxTries>=0)) {
|
||||||
|
k=(k+RNG.randomInt(1,genoLen))%genoLen; // try next random position
|
||||||
|
maxTries--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return k;
|
||||||
|
}
|
||||||
|
|
||||||
|
// private int getRandomSecondIndex(int firstIndex, AbstractEAIndividual individual) {
|
||||||
|
// int genoLen = ((InterfaceGAIndividual)individual).getGenotypeLength();
|
||||||
|
// return RNG.randomInt(0, genoLen);
|
||||||
|
// }
|
||||||
|
|
||||||
/** This method allows you to get a string representation of the mutation
|
/** This method allows you to get a string representation of the mutation
|
||||||
* operator
|
* operator
|
||||||
* @return A descriptive string.
|
* @return A descriptive string.
|
||||||
@ -126,4 +149,13 @@ public class MutateGASwapBits implements InterfaceMutation, java.io.Serializable
|
|||||||
public String numberOfMutationsTipText() {
|
public String numberOfMutationsTipText() {
|
||||||
return "The number of bits to be swapped.";
|
return "The number of bits to be swapped.";
|
||||||
}
|
}
|
||||||
|
public void setPreferTrueChange(boolean preferPairs) {
|
||||||
|
this.preferPairs = preferPairs;
|
||||||
|
}
|
||||||
|
public boolean isPreferTrueChange() {
|
||||||
|
return preferPairs;
|
||||||
|
}
|
||||||
|
public String preferTrueChangeTipText() {
|
||||||
|
return "If set to true, mutation events will prefer swapping 1 and 0";
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user