Update of three integer operators

This commit is contained in:
Marcel Kronfeld 2011-03-14 15:03:19 +00:00
parent ed48ddc07c
commit 3cd5bcf73d
3 changed files with 145 additions and 48 deletions

View File

@ -7,8 +7,9 @@ import eva2.server.go.problems.InterfaceOptimizationProblem;
import eva2.tools.math.RNG; import eva2.tools.math.RNG;
/** /**
* Created by IntelliJ IDEA. * One-point crossover on integer individuals.
* User: streiche *
* User: mkron, streiche
* Date: 18.05.2005 * Date: 18.05.2005
* Time: 17:10:28 * Time: 17:10:28
* To change this template use File | Settings | File Templates. * To change this template use File | Settings | File Templates.

View File

@ -2,7 +2,9 @@ package eva2.server.go.operators.initialization;
import java.util.BitSet; import java.util.BitSet;
import eva2.gui.BeanInspector;
import eva2.server.go.individuals.AbstractEAIndividual; import eva2.server.go.individuals.AbstractEAIndividual;
import eva2.server.go.individuals.GIIndividualIntegerData;
import eva2.server.go.individuals.InterfaceGAIndividual; import eva2.server.go.individuals.InterfaceGAIndividual;
import eva2.server.go.individuals.InterfaceGIIndividual; import eva2.server.go.individuals.InterfaceGIIndividual;
import eva2.server.go.problems.InterfaceOptimizationProblem; import eva2.server.go.problems.InterfaceOptimizationProblem;
@ -36,6 +38,7 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
private int segmentLength=4; private int segmentLength=4;
private int targetElement=1; private int targetElement=1;
private int[] otherElements=new int[]{}; private int[] otherElements=new int[]{};
private double disturbanceDegree=0.0;
public GAGIInitializeSegmentwise() {} public GAGIInitializeSegmentwise() {}
@ -51,11 +54,24 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
bitsPerSegmentArray = new int[o.bitsPerSegmentArray.length]; bitsPerSegmentArray = new int[o.bitsPerSegmentArray.length];
System.arraycopy(o.bitsPerSegmentArray, 0, bitsPerSegmentArray, 0, bitsPerSegmentArray.length); System.arraycopy(o.bitsPerSegmentArray, 0, bitsPerSegmentArray, 0, bitsPerSegmentArray.length);
} }
disturbanceDegree=o.disturbanceDegree;
}
public GAGIInitializeSegmentwise(int segLen, int bitsPerSeg, double disturbanceRatio) {
segmentLength = segLen;
bitsPerSegmentArray = new int[0];
bitsPerSegment=bitsPerSeg;
disturbanceDegree=disturbanceRatio;
}
public GAGIInitializeSegmentwise(int segLen, int[] bitsPerSegArr, double disturbanceRatio) {
segmentLength = segLen;
bitsPerSegmentArray = bitsPerSegArr;
disturbanceDegree=disturbanceRatio;
} }
public GAGIInitializeSegmentwise(int segLen, int[] bitsPerSeg) { public GAGIInitializeSegmentwise(int segLen, int[] bitsPerSeg) {
segmentLength = segLen; this(segLen, bitsPerSeg, 0);
bitsPerSegmentArray = bitsPerSeg;
} }
/** /**
@ -66,16 +82,25 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
* @param targetElement * @param targetElement
* @param otherElements * @param otherElements
*/ */
public GAGIInitializeSegmentwise(int segLen, int[] bitsPerSeg, int targetElement, int[] otherElements) { public GAGIInitializeSegmentwise(int segLen, int[] bitsPerSeg, int targetElement, int[] otherElements, double disturbRatio) {
segmentLength = segLen; segmentLength = segLen;
bitsPerSegmentArray = bitsPerSeg; bitsPerSegmentArray = bitsPerSeg;
this.targetElement = targetElement; this.targetElement = targetElement;
this.otherElements = otherElements; this.otherElements = otherElements;
this.disturbanceDegree = disturbRatio;
} }
public InterfaceInitialization clone() { public InterfaceInitialization clone() {
return new GAGIInitializeSegmentwise(this); return new GAGIInitializeSegmentwise(this);
} }
//
// public static void main(String[] args) {
// GAGIInitializeSegmentwise init = new GAGIInitializeSegmentwise(5, new int[]{5,0}, 0.5);
// GIIndividualIntegerData indy = new GIIndividualIntegerData();
// indy.setInitOperator(init);
// indy.init(null);
// System.out.println(indy.getStringRepresentation());
// }
public void initialize(AbstractEAIndividual indy, public void initialize(AbstractEAIndividual indy,
InterfaceOptimizationProblem problem) { InterfaceOptimizationProblem problem) {
@ -111,6 +136,10 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
// } // }
} }
} }
if (disturbanceDegree>0) {
disturb(indy, genotype, genotypeLen, intRange);
}
// write back the genotype (it may have been cloned, who knows...) // write back the genotype (it may have been cloned, who knows...)
if (indy instanceof InterfaceGAIndividual) { if (indy instanceof InterfaceGAIndividual) {
((InterfaceGAIndividual)indy).SetBGenotype((BitSet)genotype); ((InterfaceGAIndividual)indy).SetBGenotype((BitSet)genotype);
@ -121,6 +150,31 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
} else throw new RuntimeException("Error: "+ this.getClass() + " must be used with binary or integer individuals!"); } else throw new RuntimeException("Error: "+ this.getClass() + " must be used with binary or integer individuals!");
} }
private void disturb(AbstractEAIndividual indy, Object genotype, int genotypeLen, int[][] intRange) {
for (int i=0; i<genotypeLen; i++) {
if (RNG.flipCoin(disturbanceDegree)) {
setRandomValue(i, genotype, intRange);
}
}
}
private void setRandomValue(int i, Object genotype, int[][] range) {
if (genotype instanceof BitSet) {
BitSet geno=(BitSet)genotype;
geno.set(i, RNG.randomBoolean());
} else if (genotype instanceof int[]) {
int[] geno=(int[]) genotype;
if (otherElements.length>0) { // may choose between target and all other elements
int rnd=RNG.randomInt(0, otherElements.length); // between 0 and length (inclusively)
if (rnd==otherElements.length) geno[i]=targetElement;
else geno[i]=otherElements[rnd];
} else { // or choose a random int within the range
if (range==null) System.err.println("Error, missing int range to perform random disturbance in " + this.getClass());
geno[i]=RNG.randomInt(range[i][0], range[i][1]);
}
}
}
/** /**
* Treat both the binary and the integer case. * Treat both the binary and the integer case.
* *

View File

@ -8,19 +8,30 @@ import eva2.tools.math.RNG;
/** /**
* Created by IntelliJ IDEA. * Mutate an integer individual by shifting a connected subsequence within the genotype. The sequence
* User: streiche * length is chosen uniformly randomly up to an upper limit. The destination position may either be
* Date: 19.05.2005 * fully randomly or also limited to a maximal distance.
* Time: 16:15:37 *
* To change this template use File | Settings | File Templates. * User: mkron, streiche
*/ */
public class MutateGITranslocate implements InterfaceMutation, java.io.Serializable { public class MutateGITranslocate implements InterfaceMutation, java.io.Serializable {
int m_MaxLengthOfTranslocate = 4; int m_MaxLengthOfTranslocate = 4;
int m_maxTransLocDistance = -1;
public MutateGITranslocate() { public MutateGITranslocate() {}
public MutateGITranslocate(int maxTranslLen) {
this();
setMaxLengthOfTranslocate(maxTranslLen);
} }
public MutateGITranslocate(int maxTranslLen, int maxTransDist) {
this();
setMaxLengthOfTranslocate(maxTranslLen);
setMaxTranslocationDist(maxTransDist);
}
public MutateGITranslocate(MutateGITranslocate mutator) { public MutateGITranslocate(MutateGITranslocate mutator) {
this.m_MaxLengthOfTranslocate = mutator.m_MaxLengthOfTranslocate; this.m_MaxLengthOfTranslocate = mutator.m_MaxLengthOfTranslocate;
} }
@ -54,7 +65,7 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
} }
/** This method will mutate a given AbstractEAIndividual. If the individual /** This method will mutate a given AbstractEAIndividual. If the individual
* doesn't implement InterfaceGAIndividual nothing happens. * doesn't implement InterfaceGIIndividual nothing happens.
* @param individual The individual that is to be mutated * @param individual The individual that is to be mutated
*/ */
public void mutate(AbstractEAIndividual individual) { public void mutate(AbstractEAIndividual individual) {
@ -64,8 +75,16 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
length = RNG.randomInt(1, this.m_MaxLengthOfTranslocate); length = RNG.randomInt(1, this.m_MaxLengthOfTranslocate);
if (x.length < length+2) return; if (x.length < length+2) return;
from = RNG.randomInt(0, x.length - 1 - length); from = RNG.randomInt(0, x.length - 1 - length);
if (m_maxTransLocDistance<=0) {
to = RNG.randomInt(0, x.length - 1 - length); to = RNG.randomInt(0, x.length - 1 - length);
//this.pintInt("Before ", x); } else {
int minTo = Math.max(0, from-m_maxTransLocDistance);
int maxTo = Math.min(x.length - 1 - length, from + m_maxTransLocDistance);
// System.out.println("min/max-to: " + minTo + ", " + maxTo);
to = RNG.randomInt(minTo, maxTo);
// System.out.println("to is " + to);
}
// this.printInt("####\nBefore ", x);
int[] tmp = new int[x.length]; int[] tmp = new int[x.length];
int[] without = new int[x.length - length]; int[] without = new int[x.length - length];
int[] insert = new int[length]; int[] insert = new int[length];
@ -83,8 +102,8 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
for (int i = to+length; i < x.length; i++) { for (int i = to+length; i < x.length; i++) {
tmp[i] = without[i-length]; tmp[i] = without[i-length];
} }
//System.out.println(""+from+"/"+to+"/"+length); // System.out.println(""+from+"/"+to+"/"+length);
//this.pintInt("After ", tmp); // this.printInt("After ", tmp);
((InterfaceGIIndividual)individual).SetIGenotype(tmp); ((InterfaceGIIndividual)individual).SetIGenotype(tmp);
} }
} }
@ -98,11 +117,21 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
// nothing to do here // nothing to do here
} }
private void pintInt(String s, int[] x) { // private void printInt(String s, int[] x) {
String tmp = "{"+x[0]; // String tmp = "{"+x[0];
for (int i = 1; i < x.length; i++) tmp += ", "+x[i]; // for (int i = 1; i < x.length; i++) tmp += ", "+x[i];
System.out.println(s+tmp+"}"); // System.out.println(s+tmp+"}");
} // }
//
// public static void main(String args[]) {
// GIIndividualIntegerData indy = new GIIndividualIntegerData();
// indy.defaultInit(new I1Problem());
// indy.setMutationProbability(1);
// indy.setMutationOperator(new MutateGITranslocate(5,2));
// System.out.println(indy.getStringRepresentation());
// for (int i=1; i<100; i++) indy.mutate();
// System.out.println(indy.getStringRepresentation());
// }
/** 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
@ -141,4 +170,17 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
public String maxLengthOfTranslocateTipText() { public String maxLengthOfTranslocateTipText() {
return "Gives the maximum length of the translocated segment."; return "Gives the maximum length of the translocated segment.";
} }
/** This method allows you to set the max length of invert.
* @param n The max length of invert
*/
public void setMaxTranslocationDist(int n) {
this.m_maxTransLocDistance = n;
}
public int getMaxTranslocationDist() {
return this.m_maxTransLocDistance;
}
public String maxTranslocationDistTipText() {
return "Gives the maximum distance by which a segment may be translocated.";
}
} }