I am new to C and I am trying to build a Monte Carlo Tree Search from scratch. This structure allows me to traverse from root node to leaf node and also from leaf node to root node. So the node that has a NULL parent is the root. Each node can have multiple children. This structure has to be capable of creating and deleting nodes dynamically. For example, I may want to replace the tree root with one of its children. so I have to dynamically delete the old root and its subtree that does not belong to the new root.
I am trying to apply what I learned about dynamic memory allocation in C to this project. This is the code that I implemented so far:
#include <math.h>
#include <vector>
#include <unordered_map>
struct Move
{
int x;
int y;
};
class Node {
private:
Move move;
int Q = 0;
int N = 0;
int N_RAVE = 0;
int Q_RAVE = 0;
int OUTCOME = GameMeta().GAME_ON;
Node* parent;
std::vector <Node> children;
public:
Node(Move move, Node* parent = NULL);
double value(const float EXPLORE_CONST);
void add_children(vector<Node>);
void add_win() { N ; Q ; }
void add_loss() { N ; }
void add_rave_win() { N_RAVE ; Q_RAVE ; }
void add_rave_loss() { N_RAVE ; Q_RAVE--; }
Move get_move() { return move; }
};
Node::Node(Move move, Node* parent_inp){
// Constructor
this->move = move;
this->parent = parent_inp;
this->Q = 0;
this->N = 0;
this->N_RAVE = 0;
this->Q_RAVE = 0;
}
And this is the main
function that throws error:
int main()
{
Move move = { 2, 4 };
Node *root = new Node(move, nullptr);
vector<Node> children;
// Moves from (0, 2) to (20, 2);
for (int i = 0; i <= 20; i ) {
Move move1 = { i,2 };
//Node item = Node(move1, &root);
Node *item = new Node(move1, root);
item->add_win();
children.emplace_back(item);
}
}
This is the error that it throws:
`Error C2664 'Node::Node(Move,Node *)': cannot convert argument 1 from 'Node *' to
'Move' ConsoleApplication1 C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\xmemory 680`
How can I deal with this problem? Thanks
CodePudding user response:
The problem is that children
is a vector with elements of type Node
(and not Node*
) and you're trying to add item
which is of type Node*
into.
To solve(get rid of) the error you can either make children
to be a vector of Node*
or you can dereference the pointer item
before adding it into children
.