感谢您花时间阅读本文(对不起文字之墙)。
是的,这是一个学校项目。不,我不是要求你为我这样做。我自己完成了所有的工作,我刚刚被Java本身所困扰,而不是问题。
该项目涉及遗传规划的基础知识。我随机生成树(代表基本的数学表达式),评估它们对目标函数(x,y值)的数据的适应性,然后通过消除一些真正遥远的东西来操纵集合,稍微改变一些,然后执行对两个最合适的表达式进行“交叉”操作。交叉是个问题。我已经创建了一个工作的算法,但是它会永久地更改两个原始树,我想要保留这些树,以防交叉生成两个远离的树。
为了解决这个问题,我研究了复制树结构,结果很难实现。我研究了几种不同的方法,首先是序列化。在尝试并阅读了一些examples之后,我自己尝试了并且失败了。我意识到我没有足够的Java掌握使用该技术。所以我决定在Node类上使用递归复制构造函数,并在GPTree类上有一个简单的构造函数(见下文)。
我无法解释出现了什么问题,如果你看不到它会特别困难,但要忍受我。在Eclipse中花了一段时间调试后,问题似乎从GPTree.crossover
方法开始。具体如下:
Node t1Node = getRandomNode(t1.root);
Node t1NodeParent = t1Node.parent;
Node t2Node = getRandomNode(t2.root);
Node t2NodeParent = t2Node.parent;
例如,当我查看前两个节点(t1Node
和t1NodeParent
)的调试器信息时,t1Node
的id将是(为示例编写的所有数字) 18,它的父节点的id将是22.然后,在分配给t1NodeParent
之后,t1NodeParent
的id将是22(就像它应该是的那样),但它的左或右孩子都不会拥有18的id(就像孩子一样)!因此,这些赋值下面的if
子句搞砸了,然后树也搞砸了。有时(不是每次,奇怪的是)原始树木也会被改变。
我认为这与我试图复制树的方式有关,因为这是唯一发生变化的事情。但是,对于我的生活,我无法告诉你为什么这样做。
所以,谢谢你阅读这个冗长的问题。 。 。我希望你能提供帮助。 :/
这是我严格删除的代码,用于演示我的问题:
import java.util.ArrayList;
public class GPEnv {
private final int MAX_HEIGHT = 4;
private ArrayList<GPTree> trees;
public GPEnv(int numTrees) {
trees = new ArrayList<GPTree>();
for (int i = 0; i < numTrees; i++) {
trees.add(new GPTree(MAX_HEIGHT));
}
}
public void crossover() {
System.out.println(trees.get(0));
System.out.println(trees.get(1));
System.out.println();
/*
This commented version works, but it permanently alters the original trees.
GPTree[] res = GPTree.crossover(
trees.get(0),
trees.get(1)
);
*/
GPTree[] res = GPTree.crossover(
trees.get(0).copy(),
trees.get(1).copy()
);
System.out.println(res[0]);
System.out.println(res[1]);
System.out.println();
System.out.println(trees.get(0));
System.out.println(trees.get(1));
}
public static void main(String[] args) {
GPEnv env = new GPEnv(2);
env.crossover();
}
}
public abstract class Expression {
protected static enum Token {
PLUS("+"),
MINUS("-"),
TIMES("*"),
DIVIDE("/"),
NUMBER(""),
VARIABLE("x");
private final String symbol;
Token(String symbol) {
this.symbol = symbol;
}
public String toString() {
return this.symbol;
}
}
protected static class Node {
protected Token token;
protected Node parent, left, right;
protected double value;
public Node() {
this.parent = null;
this.left = this.right = null;
}
public Node(int number) {
this();
this.token = Token.NUMBER;
this.value = number;
}
public Node(Token token) {
this();
this.token = token;
this.value = Double.NaN;
}
private Node(Token token, double number, Node parent, Node left, Node right) {
switch (token) {
case PLUS:
this.token = Token.PLUS;
this.value = Double.NaN;
break;
case MINUS:
this.token = Token.MINUS;
this.value = Double.NaN;
break;
case TIMES:
this.token = Token.TIMES;
this.value = Double.NaN;
break;
case DIVIDE:
this.token = Token.DIVIDE;
this.value = Double.NaN;
break;
case NUMBER:
this.token = Token.NUMBER;
this.value = Double.parseDouble(number + "");
break;
case VARIABLE:
this.token = Token.VARIABLE;
this.value = Double.NaN;
break;
}
this.parent = parent;
this.left = left;
this.right = right;
}
public void setParent(Node rent) {
this.parent = rent;
}
public boolean isOperator() {
switch (this.token) {
case PLUS:
case MINUS:
case TIMES: // intentional fall-throughs
case DIVIDE:
return true;
default:
return false;
}
}
public String toString() {
if (this.token == Token.NUMBER) {
return this.value + "";
}
return this.token.toString();
}
public Node copy() {
Node left = null;
Node right = null;
if (this.left != null) {
left = this.left.copy();
}
if (this.right != null) {
right = this.right.copy();
}
return new Node(token, value, parent, left, right);
}
}
protected Node root;
private void postOrderTraverse(Node node, StringBuilder sb) {
if (node != null) {
postOrderTraverse(node.left, sb);
postOrderTraverse(node.right, sb);
sb.append(node + " ");
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
postOrderTraverse(this.root, sb);
return sb.toString();
}
}
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Random;
public class GPTree extends Expression {
private static final int MIN_HEIGHT = 2;
private static final int MAX_MUTATED = 4;
private static final Random rand = new Random();
private int maxHeight;
public GPTree(int maxHeight) {
super();
this.maxHeight = maxHeight;
generateRandomTree();
}
private GPTree(Node root, int maxHeight) {
this.root = root;
this.maxHeight = maxHeight;
}
private static Node getRandomNode(Node node) {
if (node.left == null && node.right == null) {
return node;
} else if (rand.nextInt(10) > 6) {
return node;
}
if (rand.nextInt(2) == 0) {
return getRandomNode(node.left);
} else {
return getRandomNode(node.right);
}
}
private void generateRandomTree(Node node, int depth) {
if (depth == maxHeight) {
node.left = new Node(rand.nextInt(10));
node.left.setParent(node);
node.right = new Node(rand.nextInt(10));
node.right.setParent(node);
return;
}
if (rand.nextInt(2) == 0) {
node.left = new Node(Token.values()[rand.nextInt(4)]);
generateRandomTree(node.left, depth + 1);
} else {
// give numbers an increased chance of occuring (60%)
if (rand.nextInt(10) > 3) {
node.left = new Node(rand.nextInt(10));
} else {
node.left = new Node(Token.VARIABLE);
}
}
if (rand.nextInt(2) == 0) {
node.right = new Node(Token.values()[rand.nextInt(4)]);
generateRandomTree(node.right, depth + 1);
} else {
// give numbers an increased chance of occuring (60%)
if (rand.nextInt(10) > 3) {
node.right = new Node(rand.nextInt(10));
} else {
node.right = new Node(Token.VARIABLE);
}
}
if (depth < MIN_HEIGHT && (!node.left.isOperator() && !node.right.isOperator())) {
if (rand.nextInt(2) == 0) {
node.left = new Node(Token.values()[rand.nextInt(4)]);
generateRandomTree(node.left, depth + 1);
} else {
node.right = new Node(Token.values()[rand.nextInt(4)]);
generateRandomTree(node.right, depth + 1);
}
}
node.left.setParent(node);
node.right.setParent(node);
}
public void generateRandomTree() {
if (this.root == null) {
this.root = new Node(Token.values()[rand.nextInt(4)]);
}
generateRandomTree(this.root, 1);
}
public static GPTree[] crossover(GPTree t1, GPTree t2) {
GPTree[] result = new GPTree[2];
Node t1Node = getRandomNode(t1.root);
Node t1NodeParent = t1Node.parent;
Node t2Node = getRandomNode(t2.root);
Node t2NodeParent = t2Node.parent;
t2Node.parent = t1NodeParent;
if (t1NodeParent == null) {
t1.root = t2Node;
} else {
if (t1NodeParent.left == t1Node) {
t1NodeParent.left = t2Node;
} else {
t1NodeParent.right = t2Node;
}
}
t1Node.parent = t2NodeParent;
if (t2NodeParent == null) {
t2.root = t1Node;
} else {
if (t2NodeParent.left == t2Node) {
t2NodeParent.left = t1Node;
} else {
t2NodeParent.right = t1Node;
}
}
result[0] = t1;
result[1] = t2;
return result;
}
public GPTree copy() {
return new GPTree(root.copy(), maxHeight);
}
}
答案 0 :(得分:1)
Node.copy()
方法 - 您实际上可以将其命名为Name.clone()
,然后制作全班实现Cloneable
。更重要的是:你正在克隆和设置孩子,但你忘了那些孩子会有新的父母。克隆后,您应该将父级设置为新的,例如:
setParent(this)
方法。没有它,您只是创建使用克隆节点的父节点作为父节点的新节点,这可能是出错的原因。
答案 1 :(得分:1)
在制作副本时,您似乎没有尝试修复父链接。
想象一下基本的表达式1 + 2。当你复制'+',比如id 22时,你创建一个新的'+'节点,其中包含两个子节点的副本和对其原始父节点的引用(null)。假设原始的'1'节点具有id 5,并且新创建的'1'节点具有id 18.当你构建新的'1'节点时,你将父链接保留回22,因为在制作副本时你没有但是可以访问新的父母。
深度复制循环数据结构时,解决此问题的方法是提供将新父级作为参数的特殊复制操作。深层副本应该为每个子节点使用不完整的新父节点调用此特殊副本。
像
这样的东西public Node copy() {
return copyWithParent( parent );
}
public Node copyWithParent( Node parentOverride ) {
Node out = new Node( token, value, parentOverride, null, null );
if (this.left != null) {
out.left = this.left.copyWithParent( out );
}
if (this.right != null) {
out.right = this.right.copyWithParent( out );
}
return out;
}