我正在生成一个大型游戏树,它描述了两个节点(称为 type1 和 type2)之间的拍卖场景。在我达到第四级节点之前,树的生成非常好。
#include <iostream>
#include <vector>
#include <queue>
#define LEVEL 8
using namespace std;
class payoff_node{
int agent_1;
int agent_2;
};
class node{
public:
/* constructor to set payoff_ptr to NULL */
node() {
payoff_ptr = NULL;
parent = NULL;
}
/* print the information about the node */
void printNode(){
cout << "type: " << type << "\t" << "level: " << level << "\t" << "purse: $" << purse << "\t" << "parent_bid: " << parent_bid << "\t" << "parent:" << parent->getType() << "\t" << endl;
/*for (int i = 0; i < actions.size() ; i++){
cout << actions[i] << endl;
}*/
}
void setType(int t){ type = t; }
void setLevel(int l){ level = l; }
void setPurse(int p){
purse = p;
/* Depending on the purse value the action space is decided */
for(int i = 0; i <= purse ; i++){
actions.push_back(new node);
}
}
void setParentBid(int bid){ parent_bid = bid; }
void setParent(node &parent_node){ parent = &parent_node; }
int getLevel(){ return level; }
int getParentBid(){ return parent_bid; }
int getPurse(){ return purse; }
node** getParent(){ return &parent; }
int getType(){ return type; }
vector<node*> actions;
private:
int type;
int level;
int purse;
int parent_bid;
node* parent;
payoff_node* payoff_ptr;
};
int main(int argc, char* argv[]){
bool D = false;
/* Make the root node and set its properties */
node root_node;
root_node.setType(1);
root_node.setLevel(1);
root_node.setPurse(4);
root_node.setParentBid(0);
// root_node.printNode();
queue<node> myQ;
myQ.push(root_node);
/* BFS like creation of perfect information tree */
while(true){
node &ext = myQ.front();
if(D)
cout << "h1" << endl;
if(ext.getLevel() == LEVEL){
break;
}
if(D)
cout << "h2" << endl;
int type = ext.getType();
if(D)
cout << "h3" << endl;
for(int i = 0; i < ext.actions.size() ; i++){
node &child = *(ext.actions[i]);
if(D)
cout << "h4" << endl;
//process(child);
/* set parent */
child.setParent(ext);
if(D)
cout << "h5" << endl;
/* set type & items won so far */
if((ext.getLevel()) % 2 == 0){ // i.e. even numbered level, then current round has ended
if(i > ext.getParentBid()){
child.setType(type);
}
else{
int type_val = ( *(*(ext.getParent()))).getType() ;
child.setType( type_val );
}
}
else{
if(type == 1){
child.setType(2);
}
else{
child.setType(1);
}
}
if(D)
cout << "h6" << endl;
/* set level */
child.setLevel(ext.getLevel() + 1);
if(D)
cout << "h7" << endl;
/* set purse */
if(child.getType() == ext.getType()){
int val = ext.getPurse() - i;
if(val < 0){
child.setPurse(0);
}
else{
child.setPurse(val);
}
}
else{
if( *(ext.getParent()) != NULL ){
int val = ( *(ext.getParent()))->getPurse() - ext.getParentBid();
if(val < 0){
child.setPurse(0);
}
else{
child.setPurse( val );
}
}
else{
child.setPurse(3);
}
}
if(D)
cout << "h8" << endl;
/* set parent bid */
child.setParentBid(i);
if(D)
cout << "h9" << endl;
/* when the child is ready with all its attributes, add it to the queue */
myQ.push(child);
child.printNode();
}
cout << endl << endl << "----Next Set of Children----" << endl;
myQ.pop();
}
return 0;
}
程序挂在这一行child.setPurse( val );
我相信下一行计算的值
int val = ( *(ext.getParent()))->getPurse() - ext.getParentBid();
是错的。*(ext.getParent())
指向一些垃圾节点的地方。任何帮助将不胜感激,因为我现在已经超过 24 小时无法解决这个问题。谢谢你。