我正在尝试建立一棵树(最终用于“神经网络”,并试图使设置尽可能高效。不幸的是,即使是设置树也需要大约3分钟,我不知道是什么让它的效率如此之低。我尝试尽可能地使用指针来最小化负载,但这仍然需要花费很长时间。我做错了什么?
PS。这最终是一个Tic Tac Toe AI (是的,我知道它可以通过看这个愚蠢的游戏来解决,但我想把它作为一个简单的AI来自学。
这棵树的每个分支都有9个节点,每个节点再分出9个节点,最后一组分支大约有4亿个节点。有没有办法更有效地完成这段代码?
#include <iostream>
#include <vector>
using namespace std;
class Node;
class Set;
class Node {
public:
Node(double, Set*);
Node();
double value;
Set * nextSet;
};
class Set {
public:
Set(vector<Node *>);
Set();
vector<Node *> nodes;
};
class NeuralNet {
public:
Set * firstSet;
};
Node::Node(double val, Set * newSet){
value = val;
nextSet = newSet;
}
Set::Set(vector<Node *> input){
nodes = input;
}
Node::Node(){
Set temp;
nextSet = &temp;
}
Set::Set(){
vector<Node *> temp;
nodes = temp;
}
void setUpNeuralNetRecursive(Set * curSet, int curDepth){
if(curDepth<9){
for(int i=0;i<9;i++){
Set newSet;
Node newNode(1,&newSet);
(*curSet).nodes.push_back(&newNode);
setUpNeuralNetRecursive(&newSet, curDepth+1);
}
}
}
void setUpNeuralNet(NeuralNet net){
Set newSet;
net.firstSet=&newSet;
setUpNeuralNetRecursive(&newSet, 0);
}
int main()
{
cout << "Setting up neural network. This may take up to 3 minutes." << endl;
NeuralNet net;
setUpNeuralNet(net);
cout << "Setup ended." << endl;
return 0;
}发布于 2012-11-15 08:53:44
你有一个完全平衡的9元树吗?不要为每个元素分配一个节点!相反,为您的节点分配一个数组,并使用计算在树中导航:
要从节点(i - 1) / 9
i * 9 + 1
(或者像这样的东西;现在是半夜,我还不能做这个算术)。在任何情况下,您都可以使用如下公式导航完全平衡的n元树。例如,该方法用于d-heaps。这种方法的优点是您只有一个大的分配,并且导航树变成了计算而不是内存查找。
也就是说,我怀疑你是否真的想要这样的树:每一次移动,选择的数量都会变少,你可能想要完全杀死某些分支。不过,这项针对树木的技术可能仍然有用。
https://stackoverflow.com/questions/13389891
复制相似问题