我目前正在尝试遗传算法程序,因此在尝试寻找解决方案时,我正在利用遗传算子在一组树对象上进行复制,变异和交叉。我根据“FULL”树方法创建了一个初始种群,之后我试图为N代运行一个while循环。我们的想法是采用初始种群的数组,根据遗传算子操纵它,然后用新的种群替换原始种群。
我遇到的问题是我的树对象被复制/变异/越过未正确映射到树阵列中的正确行。下面是我的main方法和我的对象类的示例,以及我用于创建新群体的代码,标记为“regenerate”
tree= genTree.regenerate(tree,repRuns, mutRuns, crossRuns,param[7],tournSize,terms,Functs);
其中tree是原始数组
public static genTree[] regenerate(genTree[] original, int repRuns, int mutRuns, int crossRuns, int newDep, int tournSize, Function terms[][], Function Functs[]) {
genTree[] copy = new genTree[original.length];
int trace = 0;
genTree tournResult;
while (trace < repRuns) {
tournResult = genTree.beginTourn(tournSize, original);
genTree reprodOperation = genTree.reprodOperator(tournResult);
copy[trace] = reprodOperation;
trace++;
}
while (trace < (mutRuns + repRuns)) {
tournResult = genTree.beginTourn(tournSize, original);
genTree mutateOP = genTree.mutateOperator(tournResult, newDep, terms, Functs);
copy[trace] = mutateOP;
trace++;
}
while (trace < (repRuns + mutRuns + crossRuns)) {
tournResult = genTree.beginTourn(tournSize, original);
genTree winner2 = genTree.beginTourn(tournSize, original);
genTree[] crossover = genTree.crossoverOperator(tournResult, winner2);
copy[trace] = crossover[0];
trace++;
copy[trace] = crossover[1];
trace++;
}
for (int i = 0; i < copy.length; i++) { //tried deep copy here
original[i] = copy[i];
}
//used to trace through the original array,see what it looks like
System.out.println("ORIGINAL ARRAY : ");
for (int i = 0; i < original.length; i++) {
System.out.println(original[i].displayTreeOnly());
}
//see whhat copy array looks like
System.out.println("COPY ARRAY : ");
for (int i = 0; i < copy.length; i++) {
System.out.println(copy[i].displayTreeOnly());
}
// original =copy.clone();
copy = null;
return original;
}
以上是我创造新人口的尝试。我试过.clone()但我仍然得到相同的结果。作为测试用例,请考虑以下内容。 “Original”包含以下树:
OR AND NOT NOT NOT AND d5 a0 d0 d1
AND AND NOT OR NOT NOT a0 d4 d3 d6
AND NOT AND AND AND OR a3 d1 d3 d1 d4 a3
AND OR AND OR NOT NOT OR d0 d6 d4 d4 d5 a1
OR AND AND AND AND AND AND d0 d2 d2 d7 d2 a1 a3 d7
在运行该方法之后(在复制到原始版本之前),副本应包含
AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT d2 d0 d7 d5 a0 d6
AND OR AND OR NOT NOT OR AND d6 d4 d4 d5 a1 d5 d4
AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
AND NOT AND AND AND OR a3 d1 d3 d1 d4 d2
AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
然而,相反,“原始&#34;和“复制&#34;包含以下内容:
AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
AND OR AND OR NOT NOT OR AND d6 AND d4 d5 a1 d5 d4 NOT NOT d2 d3
AND NOT AND AND AND OR a3 d1 d3 d1 d4 d2
AND AND NOT OR NOT NOT OR d2 d3 d6 OR AND OR OR NOT a3 d0 d7 d5 a0 d6
如果某些树映射到正确的位置,但阵列未完全正确映射
功能类:
public class Function {
private String label;
private int arity;
public Function() {
}
public void setArity(int arty) {
arity = arty;
}
public void setLabel(String lbl) {
label = lbl;
}
public String getLabel() {
return label;
}
public int getArity() {
return arity;
}
}
genTree basic:
public class genTree {
public Node root;
private int hits = 0;
static long seed = System.currentTimeMillis();
static Random rand = new Random(seed);
static Random randBool = new Random();
static genTree[] newPop;
static genTree winner;
private int numberNodes = 0;
private boolean success;
public genTree() {
numberNodes = 0;
}
答案 0 :(得分:0)
确实如此,但是你在不同的元素中重复使用相同的对象,所以对一个元素的更改会影响它们。
答案 1 :(得分:0)
所以基本上我遇到的主要问题是.clone()以及深层复制方法实际上并没有复制我的对象,因此对对象的一个更改是更改对象的所有“副本”的值。解决方案是创建一个复制方法,该方法手动使用get和set方法来创建相关对象的新副本。这解决了这个问题。