### Install DouZero Source: https://github.com/kwai/douzero/blob/main/README.md Commands to clone the repository and install the necessary dependencies or the package itself. ```bash git clone https://github.com/kwai/DouZero.git ``` ```bash cd douzero pip3 install -r requirements.txt ``` ```bash pip3 install douzero ``` ```bash pip3 install douzero -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ```bash pip3 install -e . ``` -------------------------------- ### Install DouZero Dependencies Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Navigate to the douzero directory and install the required Python packages. Ensure Python 3.6 or higher is installed. ```bash cd douzero pip3 install -r requirements.txt ``` -------------------------------- ### Start DouZero Training Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Begin training the DouZero model. This command assumes you have at least one available GPU. For Windows users, only CPU simulation is supported. ```bash python3 train.py ``` -------------------------------- ### Evaluate DouZero-ADP as Farmer vs. RLCard Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Example command to evaluate the DouZero-ADP agent as a farmer (up-player) against an RLCard agent as the landlord. ```bash python3 evaluate.py --landlord rlcard --landlord_up baselines/douzero_ADP/landlord_up.ckpt --landlord_down baselines/douzero_ADP/landlord_down.ckpt ``` -------------------------------- ### Install Latest DouZero Version Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Install the latest version of Douzero from the source. This version may be unstable. ```bash pip3 install -e . ``` -------------------------------- ### DouZero Environment Game Loop Example Source: https://context7.com/kwai/douzero/llms.txt Example of a game loop using the DouZero environment. It demonstrates selecting a legal action and stepping through the environment until the game is over. ```python # Game loop example done = False while not done: # Select an action from legal actions action = obs['legal_actions'][0] # Example: pick first legal action # Step through environment obs, reward, done, info = env.step(action) if done: # Reward is based on objective: # 'wp': +1 for win, -1 for loss # 'adp': ±(2^bomb_num) based on win/loss print(f"Game over! Reward: {reward}") ``` -------------------------------- ### Evaluate DouZero-ADP vs. Random Agents Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Example command to evaluate the DouZero-ADP agent as the landlord against random agents for the other two players. ```bash python3 evaluate.py --landlord baselines/douzero_ADP/landlord.ckpt --landlord_up random --landlord_down random ``` -------------------------------- ### Install Stable DouZero Package Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Install the stable version of the Douzero package using pip. A Tsinghua mirror is suggested for faster downloads in China. ```bash pip3 install douzero ``` ```bash pip3 install douzero -i https://pypi.tuna.tsinghua.edu.cn/simple ``` -------------------------------- ### Initialize GameEnv with Random Agents Source: https://context7.com/kwai/douzero/llms.txt Initialize the core DouZero game engine with specified player agents. This example uses RandomAgent for all player roles. ```python from douzero.env.game import GameEnv, InfoSet # Card representation mappings EnvCard2RealCard = { 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', 11: 'J', 12: 'Q', 13: 'K', 14: 'A', 17: '2', 20: 'X', 30: 'D' } # Initialize game with player agents from douzero.evaluation.random_agent import RandomAgent players = { 'landlord': RandomAgent(), 'landlord_up': RandomAgent(), 'landlord_down': RandomAgent() } env = GameEnv(players) ``` -------------------------------- ### Evaluate DouZero-ADP Peasants vs RLCard Landlord Source: https://context7.com/kwai/douzero/llms.txt Evaluate DouZero-ADP peasants against an RLCard landlord agent. Ensure RLCard is installed and configured. ```bash python3 evaluate.py \ --landlord rlcard \ --landlord_up baselines/douzero_ADP/landlord_up.ckpt \ --landlord_down baselines/douzero_ADP/landlord_down.ckpt ``` -------------------------------- ### Reset DouZero Environment and Get Initial Observation Source: https://context7.com/kwai/douzero/llms.txt Reset the environment to its initial state and retrieve the first observation. This includes game position, legal actions, and feature batch shapes. ```python # Reset environment and get initial observation obs = env.reset() print(f"Position: {obs['position']}") print(f"Legal actions: {len(obs['legal_actions'])} available moves") print(f"Feature batch shape (x_batch): {obs['x_batch'].shape}") print(f"Historical moves shape (z_batch): {obs['z_batch'].shape}") ``` -------------------------------- ### Run Game and Get Results in GameEnv Source: https://context7.com/kwai/douzero/llms.txt Execute the game within the GameEnv until completion and retrieve game statistics such as the winner, number of bombs, and win counts for landlord and farmers. ```python # Run game until completion while not env.game_over: env.step() # Get results print(f"Winner: {env.get_winner()}") # 'landlord' or 'farmer' print(f"Bombs played: {env.get_bomb_num()}") print(f"Landlord wins: {env.num_wins['landlord']}") print(f"Farmer wins: {env.num_wins['farmer']}") # Reset for next game env.reset() ``` -------------------------------- ### Get Most Recent Checkpoint Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Use this script to locate the most recently saved model checkpoint in the specified directory. The path to the most recent model will be found in the 'most_recent_model' file. ```bash sh get_most_recent.sh douzero_checkpoints/douzero/ ``` -------------------------------- ### Configure CPU Training and Simulation Source: https://github.com/kwai/douzero/blob/main/README.md Commands to force training and simulation to run on CPU. ```bash python3 train.py --actor_device_cpu --training_device cpu ``` ```bash python3 train.py --actor_device_cpu ``` -------------------------------- ### Set Up Initial Card Distribution in GameEnv Source: https://context7.com/kwai/douzero/llms.txt Configure the initial card distribution for players and the three landlord cards within the GameEnv. This is crucial for setting up a specific game scenario. ```python # Set up initial card distribution card_play_data = { 'landlord': [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 17, 17, 17, 20, 30, 3, 4], 'landlord_up': [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 5, 6, 7, 8, 9], 'landlord_down': [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 10, 11, 12, 13, 14], 'three_landlord_cards': [20, 30, 17] } env.card_play_init(card_play_data) ``` -------------------------------- ### Training Configuration Arguments Source: https://github.com/kwai/douzero/blob/main/README.md List of available command-line arguments for customizing the training process. ```bash --xpid XPID Experiment id (default: douzero) --save_interval SAVE_INTERVAL Time interval (in minutes) at which to save the model --objective {adp,wp} Use ADP or WP as reward (default: ADP) --actor_device_cpu Use CPU as actor device --gpu_devices GPU_DEVICES Which GPUs to be used for training --num_actor_devices NUM_ACTOR_DEVICES The number of devices used for simulation --num_actors NUM_ACTORS The number of actors for each simulation device --training_device TRAINING_DEVICE The index of the GPU used for training models. `cpu` means using cpu --load_model Load an existing model --disable_checkpoint Disable saving checkpoint --savedir SAVEDIR Root dir where experiment data will be saved --total_frames TOTAL_FRAMES Total environment frames to train for --exp_epsilon EXP_EPSILON The probability for exploration --batch_size BATCH_SIZE Learner batch size --unroll_length UNROLL_LENGTH The unroll length (time dimension) --num_buffers NUM_BUFFERS Number of shared-memory buffers --num_threads NUM_THREADS Number learner threads --max_grad_norm MAX_GRAD_NORM Max norm of gradients --learning_rate LEARNING_RATE Learning rate --alpha ALPHA RMSProp smoothing constant --momentum MOMENTUM RMSProp momentum --epsilon EPSILON RMSProp epsilon ``` -------------------------------- ### Multi-GPU Training Configuration Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Configure training with multiple GPUs using specific arguments for GPU devices, actor devices, number of actors, and training device. ```bash python3 train.py --gpu_devices 0,1,2,3 --num_actor_devices 3 --num_actors 15 --training_device 3 ``` -------------------------------- ### Customizable Training Options Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md A list of optional arguments for customizing the training process, including experiment ID, save interval, objective function, device selection, and various training hyperparameters. ```bash --xpid XPID --save_interval SAVE_INTERVAL --objective {adp,wp} --actor_device_cpu --gpu_devices GPU_DEVICES --num_actor_devices NUM_ACTOR_DEVICES --num_actors NUM_ACTORS --training_device TRAINING_DEVICE --load_model --disable_checkpoint --savedir SAVEDIR --total_frames TOTAL_FRAMES --exp_epsilon EXP_EPSILON --batch_size BATCH_SIZE --unroll_length UNROLL_LENGTH --num_buffers NUM_BUFFERS --num_threads NUM_THREADS --max_grad_norm MAX_GRAD_NORM --learning_rate LEARNING_RATE --alpha ALPHA --momentum MOMENTUM --epsilon EPSILON ``` -------------------------------- ### Initialize DouZero Environment with Objective Source: https://context7.com/kwai/douzero/llms.txt Initialize the DouZero game environment with a specified reward objective. Supported objectives include 'adp', 'wp', and 'logadp'. ```python from douzero.env.env import Env # Initialize environment with reward objective # objective options: 'adp' (Average Difference Points), 'wp' (Win Percentage), 'logadp' env = Env(objective='adp') ``` -------------------------------- ### CPU Simulation Only Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Run simulation using the CPU while training might still utilize other available devices if not specified. ```bash python3 train.py --actor_device_cpu ``` -------------------------------- ### Evaluate DouZero-ADP vs Random Peasants Source: https://context7.com/kwai/douzero/llms.txt Run an evaluation script for DouZero-ADP as the landlord against random player agents. ```bash python3 evaluate.py \ --landlord baselines/douzero_ADP/landlord.ckpt \ --landlord_up random \ --landlord_down random ``` -------------------------------- ### CPU-Only Training and Simulation Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Run training and simulation entirely on the CPU. This is the only option for Windows users for simulation. ```bash python3 train.py --actor_device_cpu --training_device cpu ``` -------------------------------- ### Configure Training Arguments Source: https://context7.com/kwai/douzero/llms.txt Parse command-line arguments to configure the DMC training algorithm. ```python from douzero.dmc import parser # Parse all available arguments flags = parser.parse_args() ``` -------------------------------- ### Training Arguments Reference Source: https://context7.com/kwai/douzero/llms.txt Lists and describes the configuration options available for the DMC (DouZero Model Configuration) training algorithm. ```APIDOC ## Training Arguments Reference ### Description Complete configuration options for the DMC training algorithm. These arguments can be parsed using the `douzero.dmc.parser` module. ### Usage ```python from douzero.dmc import parser flags = parser.parse_args() ``` ### Key Arguments - **--xpid** (string): Experiment name. Default: 'douzero'. - **--save_interval** (int): Save model checkpoint every N minutes. Default: 30. - **--objective** (string): Reward function: 'adp', 'wp', or 'logadp'. Default: 'adp'. - **--actor_device_cpu** (boolean): Use CPU for actor processes. Default: False. - **--gpu_devices** (string): Comma-separated list of GPU IDs to use. Default: '0'. - **--num_actor_devices** (int): Number of GPU devices for simulation. Default: 1. - **--num_actors** (int): Number of actors per GPU device. Default: 5. - **--training_device** (string): GPU ID or 'cpu' for training. Default: '0'. - **--load_model** (string): Path to a checkpoint to resume training. - **--savedir** (string): Directory to save models and logs. Default: 'douzero_checkpoints'. - **--total_frames** (int): Total training duration in frames. Default: 100,000,000,000. - **--exp_epsilon** (float): Exploration rate for epsilon-greedy strategy. Default: 0.01. - **--batch_size** (int): Training batch size. Default: 32. - **--unroll_length** (int): Trajectory length for unrolling. Default: 100. - **--learning_rate** (float): Optimizer learning rate. Default: 0.0001. ``` -------------------------------- ### Train DouZero Models Source: https://context7.com/kwai/douzero/llms.txt The main entry point for training agents using the Deep Monte Carlo algorithm. Requires configuring GPU or CPU devices via command-line arguments. ```python # train.py - Main training script import os from douzero.dmc import parser, train if __name__ == '__main__': flags = parser.parse_args() os.environ["CUDA_VISIBLE_DEVICES"] = flags.gpu_devices train(flags) ``` ```bash # Basic training on single GPU python3 train.py # Multi-GPU training: 3 GPUs for simulation (15 actors each), 1 GPU for training python3 train.py --gpu_devices 0,1,2,3 --num_actor_devices 3 --num_actors 15 --training_device 3 # CPU-only training (for Windows or machines without GPU) python3 train.py --actor_device_cpu --training_device cpu # Custom training with all hyperparameters python3 train.py \ --xpid my_experiment \ --save_interval 30 \ --objective adp \ --gpu_devices 0,1 \ --num_actor_devices 1 \ --num_actors 5 \ --training_device 1 \ --total_frames 100000000000 \ --exp_epsilon 0.01 \ --batch_size 32 \ --unroll_length 100 \ --num_buffers 50 \ --num_threads 4 \ --learning_rate 0.0001 \ --max_grad_norm 40.0 ``` -------------------------------- ### Initialize DeepAgent for DouZero Source: https://context7.com/kwai/douzero/llms.txt Instantiate agents for specific game positions using pretrained model checkpoints. ```python landlord_agent = DeepAgent( position='landlord', model_path='baselines/douzero_ADP/landlord.ckpt' ) peasant_up_agent = DeepAgent( position='landlord_up', model_path='baselines/douzero_ADP/landlord_up.ckpt' ) peasant_down_agent = DeepAgent( position='landlord_down', model_path='baselines/douzero_ADP/landlord_down.ckpt' ) # Use agent to select action given game infoset # infoset contains: player_hand_cards, legal_actions, last_move, etc. action = landlord_agent.act(infoset) print(f"Selected action: {action}") # Returns list of card values ``` -------------------------------- ### Perform Self-Play Evaluation Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Run the evaluation script for self-play. Specify agents for landlord, landlord's up-player, and landlord's down-player using predefined options or paths to pre-trained models. The evaluation data file and number of workers can also be set. ```bash python3 evaluate.py ``` -------------------------------- ### Enable Shared Memory for Multi-Process Training Source: https://context7.com/kwai/douzero/llms.txt Prepare the model for multi-process training by enabling shared memory and setting the model to evaluation mode. ```python # Enable shared memory for multi-process training model.share_memory() model.eval() ``` -------------------------------- ### Full Evaluation with GPU and Multiple Workers Source: https://context7.com/kwai/douzero/llms.txt Perform a comprehensive evaluation using specified checkpoints, evaluation data, multiple workers, and a GPU device. ```bash python3 evaluate.py \ --landlord baselines/douzero_WP/landlord.ckpt \ --landlord_up baselines/douzero_WP/landlord_up.ckpt \ --landlord_down baselines/douzero_WP/landlord_down.ckpt \ --eval_data eval_data.pkl \ --num_workers 8 \ --gpu_device 0 ``` -------------------------------- ### Initialize Individual Position Models Source: https://context7.com/kwai/douzero/llms.txt Instantiate individual position-specific LSTM models for Landlord and Farmer roles. Note the expected input feature dimensions for each. ```python # Or use individual position models directly landlord_model = LandlordLstmModel() # Input: 373 + 128 features farmer_model = FarmerLstmModel() # Input: 484 + 128 features ``` -------------------------------- ### Initialize DouZero Model Wrapper Source: https://context7.com/kwai/douzero/llms.txt Initialize the main Model wrapper for DouZero, which handles position-specific LSTM models. It defaults to using GPU device 0, but can be set to 'cpu'. ```python import torch from douzero.dmc.models import Model, LandlordLstmModel, FarmerLstmModel # Initialize model wrapper (uses GPU by default) model = Model(device=0) # Use 'cpu' for CPU-only ``` -------------------------------- ### Evaluate DouZero models Source: https://github.com/kwai/douzero/blob/main/README.md Run the evaluation script to assess model performance against various agent types. ```bash python3 evaluate.py ``` ```bash python3 evaluate.py --landlord baselines/douzero_ADP/landlord.ckpt --landlord_up random --landlord_down random ``` ```bash python3 evaluate.py --landlord rlcard --landlord_up baselines/douzero_ADP/landlord_up.ckpt --landlord_down baselines/douzero_ADP/landlord_down.ckpt ``` -------------------------------- ### Use Baseline Agents Source: https://context7.com/kwai/douzero/llms.txt Implement random or rule-based heuristic agents for benchmarking purposes. ```python from douzero.evaluation.random_agent import RandomAgent from douzero.evaluation.rlcard_agent import RLCardAgent # Random agent - selects uniformly from legal actions random_agent = RandomAgent() action = random_agent.act(infoset) # RLCard rule-based agent - uses heuristics for leading and following rlcard_agent = RLCardAgent(position='landlord') # or 'landlord_up', 'landlord_down' action = rlcard_agent.act(infoset) ``` -------------------------------- ### Run Multi-Process Simulation Source: https://context7.com/kwai/douzero/llms.txt Execute parallel game simulations to evaluate model performance or load models for custom evaluation. ```python from douzero.evaluation.simulation import evaluate, load_card_play_models, mp_simulate # Run full evaluation evaluate( landlord='baselines/douzero_ADP/landlord.ckpt', # or 'random', 'rlcard' landlord_up='baselines/douzero_ADP/landlord_up.ckpt', landlord_down='baselines/douzero_ADP/landlord_down.ckpt', eval_data='eval_data.pkl', num_workers=5 ) # Load models manually for custom evaluation card_play_model_path_dict = { 'landlord': 'baselines/douzero_ADP/landlord.ckpt', 'landlord_up': 'random', 'landlord_down': 'random' } players = load_card_play_models(card_play_model_path_dict) ``` -------------------------------- ### Clone DouZero Repository Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Clone the DouZero repository to your local machine. For faster access in China, a Gitee mirror is provided. ```bash git clone https://github.com/kwai/DouZero.git ``` -------------------------------- ### Evaluate Trained Models Source: https://context7.com/kwai/douzero/llms.txt Simulates games using specified model checkpoints to calculate win rates. Requires a pre-generated evaluation dataset. ```python # evaluate.py import os import argparse from douzero.evaluation.simulation import evaluate if __name__ == '__main__': parser = argparse.ArgumentParser('Dou Dizhu Evaluation') parser.add_argument('--landlord', type=str, default='baselines/douzero_ADP/landlord.ckpt') parser.add_argument('--landlord_up', type=str, default='baselines/sl/landlord_up.ckpt') parser.add_argument('--landlord_down', type=str, default='baselines/sl/landlord_down.ckpt') parser.add_argument('--eval_data', type=str, default='eval_data.pkl') parser.add_argument('--num_workers', type=int, default=5) parser.add_argument('--gpu_device', type=str, default='') args = parser.parse_args() os.environ['KMP_DUPLICATE_LIB_OK'] = 'True' os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_device evaluate(args.landlord, args.landlord_up, args.landlord_down, args.eval_data, args.num_workers) ``` -------------------------------- ### Generate Evaluation Data Source: https://github.com/kwai/douzero/blob/main/README.zh-CN.md Execute the script to generate evaluation data. Key parameters include the output path for pickle data and the number of games to simulate. ```bash python3 generate_eval_data.py ``` -------------------------------- ### Access Position-Specific Models from Wrapper Source: https://context7.com/kwai/douzero/llms.txt Retrieve individual position-specific models (landlord, landlord_up, landlord_down) from the main Model wrapper. ```python # Access position-specific models landlord = model.get_model('landlord') landlord_up = model.get_model('landlord_up') landlord_down = model.get_model('landlord_down') ``` -------------------------------- ### Generate Evaluation Data Source: https://context7.com/kwai/douzero/llms.txt Creates randomized card distributions for consistent model evaluation. Outputs a pickle file containing game scenarios. ```python # generate_eval_data.py import argparse import pickle import numpy as np # Standard 54-card deck representation deck = [] for i in range(3, 15): # 3-A (values 3-14) deck.extend([i for _ in range(4)]) deck.extend([17 for _ in range(4)]) # Four 2s deck.extend([20, 30]) # Small joker (20), Big joker (30) def generate(): """Generate random card distribution for one game""" _deck = deck.copy() np.random.shuffle(_deck) card_play_data = { 'landlord': _deck[:20], # Landlord gets 20 cards 'landlord_up': _deck[20:37], # Peasant up gets 17 cards 'landlord_down': _deck[37:54], # Peasant down gets 17 cards 'three_landlord_cards': _deck[17:20], # Three bonus cards } for key in card_play_data: card_play_data[key].sort() return card_play_data if __name__ == '__main__': parser = argparse.ArgumentParser(description='DouZero: random data generator') parser.add_argument('--output', default='eval_data', type=str) parser.add_argument('--num_games', default=10000, type=int) flags = parser.parse_args() data = [generate() for _ in range(flags.num_games)] with open(flags.output + '.pkl', 'wb') as f: pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) ``` ```bash # Generate 10,000 random game scenarios (default) python3 generate_eval_data.py # Generate 50,000 games with custom output path python3 generate_eval_data.py --output my_eval_data --num_games 50000 ``` -------------------------------- ### Baseline Agents - RandomAgent and RLCardAgent Source: https://context7.com/kwai/douzero/llms.txt Provides baseline agents for benchmarking: RandomAgent for random action selection and RLCardAgent for rule-based heuristics. ```APIDOC ## RandomAgent and RLCardAgent - Baseline Agents ### Description Simple baseline agents for benchmarking: RandomAgent selects uniformly from legal actions, and RLCardAgent uses heuristics for leading and following. ### Usage ```python from douzero.evaluation.random_agent import RandomAgent from douzero.evaluation.rlcard_agent import RLCardAgent # Random agent random_agent = RandomAgent() action = random_agent.act(infoset) # RLCard rule-based agent rlcard_agent = RLCardAgent(position='landlord') # or 'landlord_up', 'landlord_down' action = rlcard_agent.act(infoset) ``` ### Parameters - **position** (string) - Optional - The player position for RLCardAgent ('landlord', 'landlord_up', 'landlord_down'). Defaults to 'landlord'. ### Heuristic Rules (RLCardAgent) - **Leading**: Plays the smallest card in an optimal combination. - **Following**: Plays the smallest card that beats the rival's move. - **Passing**: Peasants may pass to assist their teammate. ``` -------------------------------- ### DeepAgent - Load Pretrained Agent Source: https://context7.com/kwai/douzero/llms.txt Loads a pretrained agent for a specific player position (landlord, landlord_up, landlord_down) using a specified model path. ```APIDOC ## Load Pretrained Agent ### Description Loads a pretrained agent for a specific player position (e.g., 'landlord', 'landlord_up', 'landlord_down') using a specified model path. ### Usage ```python from douzero.env.env import DeepAgent landlord_agent = DeepAgent(position='landlord', model_path='baselines/douzero_ADP/landlord.ckpt') peasant_up_agent = DeepAgent(position='landlord_up', model_path='baselines/douzero_ADP/landlord_up.ckpt') peasant_down_agent = DeepAgent(position='landlord_down', model_path='baselines/douzero_ADP/landlord_down.ckpt') action = landlord_agent.act(infoset) print(f"Selected action: {action}") ``` ### Parameters - **position** (string) - Required - The player position ('landlord', 'landlord_up', 'landlord_down'). - **model_path** (string) - Required - Path to the pretrained model checkpoint. ### Request Example ```json { "infoset": { "player_hand_cards": ["3S", "3H", ...], "legal_actions": [["3S"], ["3H"], ...], "last_move": null } } ``` ### Response Example ```json { "action": ["3S"] } ``` ``` -------------------------------- ### Load and Run DeepAgent for Inference Source: https://context7.com/kwai/douzero/llms.txt Load a pretrained DouZero model using the DeepAgent class and run inference. This is typically used for evaluation or deployment. ```python import torch from douzero.evaluation.deep_agent import DeepAgent ``` -------------------------------- ### evaluate() Function - Multi-Process Simulation Source: https://context7.com/kwai/douzero/llms.txt Runs parallel game simulations using multiple processes to compute win percentages and average difference points. ```APIDOC ## evaluate() Function - Multi-Process Simulation ### Description Runs parallel game simulations using multiple processes to compute win percentages and average difference points. It can use pretrained models, random agents, or RLCard agents for evaluation. ### Usage ```python from douzero.evaluation.simulation import evaluate, load_card_play_models # Run full evaluation with pretrained models evaluate( landlord='baselines/douzero_ADP/landlord.ckpt', landlord_up='baselines/douzero_ADP/landlord_up.ckpt', landlord_down='baselines/douzero_ADP/landlord_down.ckpt', eval_data='eval_data.pkl', num_workers=5 ) # Load models manually for custom evaluation card_play_model_path_dict = { 'landlord': 'baselines/douzero_ADP/landlord.ckpt', 'landlord_up': 'random', 'landlord_down': 'random' } players = load_card_play_models(card_play_model_path_dict) ``` ### Parameters for `evaluate()` - **landlord** (string) - Path to landlord model or 'random'/'rlcard'. - **landlord_up** (string) - Path to landlord_up model or 'random'/'rlcard'. - **landlord_down** (string) - Path to landlord_down model or 'random'/'rlcard'. - **eval_data** (string) - Path to evaluation data file. - **num_workers** (int) - Number of worker processes for simulation. ### Output Format ``` WP results: landlord : Farmers - 0.65 : 0.35 ADP results: landlord : Farmers - 1.82 : 0.91 ``` ``` -------------------------------- ### Forward Pass for Training (Landlord Model) Source: https://context7.com/kwai/douzero/llms.txt Perform a forward pass on the Landlord LSTM model for training, returning value predictions. Requires historical moves (z_batch) and current state features (x_batch). ```python # Forward pass for training (returns values) z_batch = torch.randn(32, 5, 162) # Historical moves: [batch, 5 rounds, 162 features] x_batch = torch.randn(32, 373) # Current state features for landlord output = landlord_model(z_batch, x_batch, return_value=True) print(f"Value predictions shape: {output['values'].shape}") # [32, 1] ``` -------------------------------- ### Forward Pass for Inference (Landlord Model) Source: https://context7.com/kwai/douzero/llms.txt Perform a forward pass on the Landlord LSTM model for inference, returning the selected action index. Set 'return_value' to False. ```python # Forward pass for inference (returns action) output = landlord_model(z_batch, x_batch, return_value=False) print(f"Selected action index: {output['action']}") ``` -------------------------------- ### get_obs() Function - Observation Extraction Source: https://context7.com/kwai/douzero/llms.txt Extracts position-specific features from the game state (infoset) to be used as input for neural networks. ```APIDOC ## get_obs() Function - Observation Extraction ### Description Extracts position-specific features from the game state (infoset) for neural network input. This function is typically called automatically by the environment. ### Usage ```python from douzero.env.env import get_obs obs = get_obs(infoset) ``` ### Observation Dictionary Contents - **position** (string): The player's position ('landlord', 'landlord_up', 'landlord_down'). - **x_batch** (list): State and action features. Shape: `[num_legal_actions, feature_dim]`. - **z_batch** (list): Historical move encoding for LSTM input. Shape: `[num_legal_actions, 5, 162]`. - **legal_actions** (list): A list of valid moves, where each move is a list of card values. - **x_no_action** (list): State features without action encoding. - **z** (list): Historical move encoding for a single sample. ### Feature Dimensions - **Landlord `x_batch`**: 373 features (includes card counts, bomb counts, action features). - **Farmer `x_batch`**: 484 features (includes teammate information). ``` -------------------------------- ### Cite the DouZero Paper Source: https://github.com/kwai/douzero/blob/main/README.md BibTeX entry for citing the DouZero research paper in academic work. ```bibtex @InProceedings{pmlr-v139-zha21a, title = {DouZero: Mastering DouDizhu with Self-Play Deep Reinforcement Learning}, author = {Zha, Daochen and Xie, Jingru and Ma, Wenye and Zhang, Sheng and Lian, Xiangru and Hu, Xia and Liu, Ji}, booktitle = {Proceedings of the 38th International Conference on Machine Learning}, pages = {12333--12344}, year = {2021}, editor = {Meila, Marina and Zhang, Tong}, volume = {139}, series = {Proceedings of Machine Learning Research}, month = {18--24 Jul}, publisher = {PMLR}, pdf = {http://proceedings.mlr.press/v139/zha21a/zha21a.pdf}, url = {http://proceedings.mlr.press/v139/zha21a.html}, abstract = {Games are abstractions of the real world, where artificial agents learn to compete and cooperate with other agents. While significant achievements have been made in various perfect- and imperfect-information games, DouDizhu (a.k.a. Fighting the Landlord), a three-player card game, is still unsolved. DouDizhu is a very challenging domain with competition, collaboration, imperfect information, large state space, and particularly a massive set of possible actions where the legal actions vary significantly from turn to turn. Unfortunately, modern reinforcement learning algorithms mainly focus on simple and small action spaces, and not surprisingly, are shown not to make satisfactory progress in DouDizhu. In this work, we propose a conceptually simple yet effective DouDizhu AI system, namely DouZero, which enhances traditional Monte-Carlo methods with deep neural networks, action encoding, and parallel actors. Starting from scratch in a single server with four GPUs, DouZero outperformed all the existing DouDizhu AI programs in days of training and was ranked the first in the Botzone leaderboard among 344 AI agents. Through building DouZero, we show that classic Monte-Carlo methods can be made to deliver strong results in a hard domain with a complex action space. The code and an online demo are released at https://github.com/kwai/DouZero with the hope that this insight could motivate future work.} } ``` -------------------------------- ### Extract Observations Source: https://context7.com/kwai/douzero/llms.txt Convert game state information into neural network-compatible features. ```python from douzero.env.env import get_obs # Extract observations from infoset (called automatically by Env) obs = get_obs(infoset) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.