Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(pu): add alphazero ctree unittest #306

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

puyuan1996
Copy link
Collaborator

@puyuan1996 puyuan1996 commented Dec 10, 2024

  • add alphazero ctree unittest
  • *_v0.py is backup files

@puyuan1996 puyuan1996 added the style Code or comments formatting label Dec 10, 2024

sys.path.append('./LightZero/lzero/mcts/ctree/ctree_alphazero/build')

sys.path.append('/Users/puyuan/code/LightZero/lzero/mcts/ctree/ctree_alphazero/build')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path = os.path.joins(os.path.dirname(__file__), 'xxx'))
sys.path.append(path)

@@ -10,7 +10,9 @@
update_per_collect = 50
batch_size = 256
max_env_step = int(2e5)
mcts_ctree = False
# mcts_ctree = False
mcts_ctree = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove commented code

@@ -220,7 +220,7 @@ def _init_collect(self) -> None:
self._collect_model = self._model
if self._cfg.mcts_ctree:
import sys
sys.path.append('/Users/your_user_name/code/LightZero/lzero/mcts/ctree/ctree_alphazero/build')
sys.path.append('/Users/puyuan/code/LightZero/lzero/mcts/ctree/ctree_alphazero/build')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

polish path

@@ -0,0 +1,289 @@
# ./lzero/mcts/ctree/ctree_alphazero/test/eval_alphazero_ctree_zh.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use pytest


class MockEnv:
"""
一个简单的模拟环境类,包含必要的属性和方法。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use English comments

if (battle_mode_in_simulation_env == "self_play_mode") {
update(leaf_value);
if (!is_root()) {
if (!is_root() && parent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a node is not the root node, its parent can't be null.

void update_recursive(float leaf_value, std::string battle_mode_in_simulation_env) {
// If the mode is "self_play_mode", the leaf_value is subtracted from the parent's value
// 递归更新节点和父节点的值
void update_recursive(float leaf_value, const std::string& battle_mode_in_simulation_env) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use const std::string&, add some comments

children[action] = node;
// 获取子节点
const std::map<int, std::shared_ptr<Node>>& get_children() const {
return children;
}

public:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why two public here

self.assertFalse(parent.is_leaf(), "添加子节点后,父节点不应为叶子节点")


class TestMCTSAlphaZero(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add a test method to validate its effectiveness on 50 simulations

self.action_space = type('action_space', (), {'n': 3})()

def reset(self, start_player_index, init_state, katago_policy_init, katago_game_state):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe you should reset something in the reset method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
style Code or comments formatting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants