Update of three integer operators
This commit is contained in:
parent
ed48ddc07c
commit
3cd5bcf73d
@ -7,8 +7,9 @@ import eva2.server.go.problems.InterfaceOptimizationProblem;
|
||||
import eva2.tools.math.RNG;
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: streiche
|
||||
* One-point crossover on integer individuals.
|
||||
*
|
||||
* User: mkron, streiche
|
||||
* Date: 18.05.2005
|
||||
* Time: 17:10:28
|
||||
* To change this template use File | Settings | File Templates.
|
||||
|
@ -2,7 +2,9 @@ package eva2.server.go.operators.initialization;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import eva2.gui.BeanInspector;
|
||||
import eva2.server.go.individuals.AbstractEAIndividual;
|
||||
import eva2.server.go.individuals.GIIndividualIntegerData;
|
||||
import eva2.server.go.individuals.InterfaceGAIndividual;
|
||||
import eva2.server.go.individuals.InterfaceGIIndividual;
|
||||
import eva2.server.go.problems.InterfaceOptimizationProblem;
|
||||
@ -36,6 +38,7 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
|
||||
private int segmentLength=4;
|
||||
private int targetElement=1;
|
||||
private int[] otherElements=new int[]{};
|
||||
private double disturbanceDegree=0.0;
|
||||
|
||||
public GAGIInitializeSegmentwise() {}
|
||||
|
||||
@ -51,11 +54,24 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
|
||||
bitsPerSegmentArray = new int[o.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) {
|
||||
segmentLength = segLen;
|
||||
bitsPerSegmentArray = bitsPerSeg;
|
||||
this(segLen, bitsPerSeg, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,16 +82,25 @@ public class GAGIInitializeSegmentwise implements InterfaceInitialization, java.
|
||||
* @param targetElement
|
||||
* @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;
|
||||
bitsPerSegmentArray = bitsPerSeg;
|
||||
this.targetElement = targetElement;
|
||||
this.otherElements = otherElements;
|
||||
this.disturbanceDegree = disturbRatio;
|
||||
}
|
||||
|
||||
public InterfaceInitialization clone() {
|
||||
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,
|
||||
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...)
|
||||
if (indy instanceof InterfaceGAIndividual) {
|
||||
((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!");
|
||||
}
|
||||
|
||||
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.
|
||||
*
|
||||
|
@ -8,19 +8,30 @@ import eva2.tools.math.RNG;
|
||||
|
||||
|
||||
/**
|
||||
* Created by IntelliJ IDEA.
|
||||
* User: streiche
|
||||
* Date: 19.05.2005
|
||||
* Time: 16:15:37
|
||||
* To change this template use File | Settings | File Templates.
|
||||
* Mutate an integer individual by shifting a connected subsequence within the genotype. The sequence
|
||||
* length is chosen uniformly randomly up to an upper limit. The destination position may either be
|
||||
* fully randomly or also limited to a maximal distance.
|
||||
*
|
||||
* User: mkron, streiche
|
||||
*/
|
||||
public class MutateGITranslocate implements InterfaceMutation, java.io.Serializable {
|
||||
|
||||
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) {
|
||||
this.m_MaxLengthOfTranslocate = mutator.m_MaxLengthOfTranslocate;
|
||||
}
|
||||
@ -54,39 +65,47 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
|
||||
}
|
||||
|
||||
/** 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
|
||||
*/
|
||||
public void mutate(AbstractEAIndividual individual) {
|
||||
if (individual instanceof InterfaceGIIndividual) {
|
||||
int[] x = ((InterfaceGIIndividual)individual).getIGenotype();
|
||||
int from, to, length;
|
||||
length = RNG.randomInt(1, this.m_MaxLengthOfTranslocate);
|
||||
if (x.length < length+2) return;
|
||||
from = RNG.randomInt(0, x.length - 1 - length);
|
||||
to = RNG.randomInt(0, x.length - 1 - length);
|
||||
//this.pintInt("Before ", x);
|
||||
int[] tmp = new int[x.length];
|
||||
int[] without = new int[x.length - length];
|
||||
int[] insert = new int[length];
|
||||
for (int i = 0; i < length; i++) insert[i] = x[i+from];
|
||||
for (int i = 0; i < without.length; i++) {
|
||||
if (i < from) without[i] = x[i];
|
||||
else without[i] = x[i+length];
|
||||
}
|
||||
for (int i = 0; i < to; i++) {
|
||||
tmp[i] = without[i];
|
||||
}
|
||||
for (int i = to; i < to+length; i++) {
|
||||
tmp[i] = insert[i-to];
|
||||
}
|
||||
for (int i = to+length; i < x.length; i++) {
|
||||
tmp[i] = without[i-length];
|
||||
}
|
||||
//System.out.println(""+from+"/"+to+"/"+length);
|
||||
//this.pintInt("After ", tmp);
|
||||
((InterfaceGIIndividual)individual).SetIGenotype(tmp);
|
||||
}
|
||||
if (individual instanceof InterfaceGIIndividual) {
|
||||
int[] x = ((InterfaceGIIndividual)individual).getIGenotype();
|
||||
int from, to, length;
|
||||
length = RNG.randomInt(1, this.m_MaxLengthOfTranslocate);
|
||||
if (x.length < length+2) return;
|
||||
from = RNG.randomInt(0, x.length - 1 - length);
|
||||
if (m_maxTransLocDistance<=0) {
|
||||
to = RNG.randomInt(0, x.length - 1 - length);
|
||||
} 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[] without = new int[x.length - length];
|
||||
int[] insert = new int[length];
|
||||
for (int i = 0; i < length; i++) insert[i] = x[i+from];
|
||||
for (int i = 0; i < without.length; i++) {
|
||||
if (i < from) without[i] = x[i];
|
||||
else without[i] = x[i+length];
|
||||
}
|
||||
for (int i = 0; i < to; i++) {
|
||||
tmp[i] = without[i];
|
||||
}
|
||||
for (int i = to; i < to+length; i++) {
|
||||
tmp[i] = insert[i-to];
|
||||
}
|
||||
for (int i = to+length; i < x.length; i++) {
|
||||
tmp[i] = without[i-length];
|
||||
}
|
||||
// System.out.println(""+from+"/"+to+"/"+length);
|
||||
// this.printInt("After ", tmp);
|
||||
((InterfaceGIIndividual)individual).SetIGenotype(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/** This method allows you to perform either crossover on the strategy parameters
|
||||
@ -98,12 +117,22 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
|
||||
// nothing to do here
|
||||
}
|
||||
|
||||
private void pintInt(String s, int[] x) {
|
||||
String tmp = "{"+x[0];
|
||||
for (int i = 1; i < x.length; i++) tmp += ", "+x[i];
|
||||
System.out.println(s+tmp+"}");
|
||||
}
|
||||
|
||||
// private void printInt(String s, int[] x) {
|
||||
// String tmp = "{"+x[0];
|
||||
// for (int i = 1; i < x.length; i++) tmp += ", "+x[i];
|
||||
// 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
|
||||
* operator
|
||||
* @return A descriptive string.
|
||||
@ -141,4 +170,17 @@ public class MutateGITranslocate implements InterfaceMutation, java.io.Serializa
|
||||
public String maxLengthOfTranslocateTipText() {
|
||||
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.";
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user