我正在尝试在 C++ 中进行一些基因编程。我已经用 Python 编写了这个代码的一个版本,它运行良好(它只是太慢了)。基本前提是将程序视为树状表达式并对其进行进化。
这是仅创建一棵候选树的代码(树存储为节点的向量,这些节点通过同一向量中子节点的索引指向其子节点)(请参阅grow_tree 函数):
这段代码编译得很好:) 我正在使用 g++ 进行编译以获取我使用 ./a.out 运行的 a.out 文件
我的问题是有时它运行并完成得很好,有时它只是挂起并且不做任何事情(也不给我一个错误)
对此的任何帮助将不胜感激!谢谢!
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <math.h>
const int MAX_TREE_DEPTH = 5;
int randint(int min, int max) {
return (rand() % ((max - min) + 1)) + min;
}
struct node {
int type, op, req_ch;
std::vector<int> ch;
std::map<std::string, float> params;
node(int p, int rem_depth) {
if ((p == -1) || (p == 0)) {
req_ch = 2;
if (rem_depth >= 2) {
type = randint(0, 1);
if (type == 0) {
op = randint(0, 1);
} else if (type == 1) {
op = randint(2, 5);
}
} else {
type = 1;
op = randint(2, 5);
}
} else if (p == 1) {
type = 2;
if (rem_depth >= 1) {
op = randint(6, 11);
} else { op = 11; }
if ((op >= 6) && (op <= 10)) {
req_ch = 2;
} else if (op >= 11) { req_ch = 0; }
}
}
};
void grow_tree(std::vector<node>* func, int p, int rem_depth) {
node n(p, rem_depth);
func->push_back(n);
int i = func->size() - 1;
for (int j = 0; j < n.req_ch; j++) {
func->at(i).ch.push_back(func->size());
grow_tree(func, n.type, rem_depth - 1);
}
}
struct rule {
float score;
bool scored;
std::vector<node> func;
rule(int m) {
if (m == 0) { // Random initialisation
int depth = randint(2, MAX_TREE_DEPTH);
grow_tree(&func, -1, depth);
}
}
};
int main(void) {
srand(time(NULL)); // Seed the random number generator
int depth = randint(2, MAX_TREE_DEPTH);
std::vector<node> f;
grow_tree(&f, -1, depth);
return 0;
}