わかりました、これは少しトリッキーなものです。
次のような式ツリーを取得するコードがたくさんあります。
((a + b)/(c + d) + sqrt(e))
プレフィックス形式のベクトル(C ++を使用していますが、アルゴリズムが必要です)に格納されます。
+(/(+(a,b),+(c,d)),sqrt(e))
//角かっこは読みやすくするためのものです。各演算子と端末は、ベクトルの要素です。
これで、ORFフォームと呼ばれる式ツリーを表す他の方法があります。
(論文の3ページ目:http://arxiv.org/ftp/cs/papers/0102/0102027.pdf)
このフォームでは、ツリーを左から右、上から下に読んでいるかのようにツリーを表現します。
((a + b)/(c + d) + sqrt(e))
今になる:
+/sqrt++eabcd
私が失敗しているのは、変換できるアルゴリズムを作成することです。
+/sqrt++eabcd
// ORF into:
+(/(+(a,b),+(c,d)),sqrt(e))
//プレフィックス
これまでに持っているのは、さまざまなレベルでツリーの幅を取得するためのコードだけです。
bool ConvertPrefixToORF(const std::vector<Node> & individual,
std::vector<Node> & ETindividual){
bool all_terminal = false;
int breadth;
int breadthOfDepth[individual.size()];
int depthCounter = 0;
breadthOfDepth[0] = 1;
//Resize only once.
ETindividual.reserve(individual.size());
while (all_terminal == false) {
//Reset breadth
breadth = 0;
//Iterate over next level and calculate depth of level after that.
for (int i = 0; i < breadthOfDepth[depthCounter]; i++) {
all_terminal = true;
//If the individual is a function...
if (individual[current+i].isFunction()) {
//Get individual from i'th item at this depth
function f = individual[current + i];
//Increment breadth of next level with arity
breadth += f->getArity();
//if a single function is found
all_terminal = false;
}
}
//Go down a level in the tree.
depthCounter++;
//Set the breadth of that tree depth:
breadthOfDepth[depthCounter] = breadth;
}
}
助けてくれてありがとう!これは私の頭を使っています。ああ、これはパフォーマンスが重要です:(