我目前正在嘗試一個遺傳算法程序,因此試圖找到一個解決方案,我正在利用遺傳算子的複製,突變和交叉對一組樹對象。我已經根據「FULL」樹方法創建了一個初始種羣,之後我試圖爲N代運行一個while循環。這個想法是採用初始種羣的數組,根據遺傳算子進行操作,然後用新種羣代替原始種羣。爲什麼我的對象數組不保留它被分配的對象?
我遇到的問題是,我的樹對象複製/變異/交叉沒有正確映射到樹數組中正確的行。下面是我的主要方法和我的對象類的樣本以及我使用創建一個新的人口的代碼,標有「再生」
tree= genTree.regenerate(tree,repRuns, mutRuns, crossRuns,param[7],tournSize,terms,Functs);
,其中樹是原來的陣列
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(),但我仍然得到相同的結果。作爲測試案例,請考慮以下幾點。 「原始」包含下面的樹:
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
但是相反,這兩個「原始」和「複製」包含以下內容:
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
當一些樹木被映射到正確的位置,但陣列無法正確映射ç ompletely
功能類:
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基本:
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;
}
試圖看看你的代碼與所有system.out的仍然是在讓我的眼睛受傷。 – hownowbrowncow
我們在這裏有一個很難理解和重現的情況,請問您可以縮小您的代碼並提供一個小的測試用例,我們也可以複製這些用例來檢查發生了什麼問題?注意:genTree是一個類名,如果是的話,它應該以大寫字母開頭。並且更改大量的stuf時,不要使用靜態方法。 – sgpalit
@sgpalit我簡化了我的代碼並提供了一個測試用例。我希望這會幫助你幫助我?大聲笑 –