### Configure and Initialize SMACv2 Environment Source: https://context7.com/oxwhirl/smacv2/llms.txt Defines the capability configuration for procedural generation and initializes the StarCraftCapabilityEnvWrapper. This setup controls team compositions, spawn positions, and observability settings. ```python distribution_config = { "n_units": 5, "n_enemies": 5, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["marine", "marauder", "medivac"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["medivac"], "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } env = StarCraftCapabilityEnvWrapper( capability_config=distribution_config, map_name="10gen_terran", debug=False, conic_fov=False, obs_own_pos=True, use_unit_ranges=True, min_attack_range=2, fully_observable=False ) ``` -------------------------------- ### Initialize and Interact with SMACv2 Environment in Python Source: https://context7.com/oxwhirl/smacv2/llms.txt Demonstrates how to initialize the StarCraftCapabilityEnvWrapper with custom configurations for unit distribution and starting positions. It also shows how to retrieve environment information, available actions for agents, and observation/state structures. ```python from smacv2.env.starcraft2.wrapper import StarCraftCapabilityEnvWrapper import numpy as np # Create environment config = { "n_units": 5, "n_enemies": 5, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["marine", "marauder", "medivac"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["medivac"], "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } env = StarCraftCapabilityEnvWrapper( capability_config=config, map_name="10gen_terran", obs_all_health=True, obs_own_pos=True, use_unit_ranges=True, min_attack_range=2 ) env.reset() env_info = env.get_env_info() # Action space structure: # Action 0: no-op (only valid for dead agents) # Action 1: stop # Action 2: move north # Action 3: move south # Action 4: move east # Action 5: move west # Actions 6+: attack enemy_id (or heal ally_id for Medivacs) print("=== Action Space ===") print(f"Total actions: {env_info['n_actions']}") n_actions_no_attack = 6 # no-op, stop, 4 move directions n_attack_actions = env_info['n_actions'] - n_actions_no_attack print(f"Movement actions: 4 (north, south, east, west)") print(f"Attack/heal targets: {n_attack_actions}") # Get available actions for an agent agent_id = 0 avail_actions = env.get_avail_agent_actions(agent_id) print(f"\nAgent {agent_id} available actions: {avail_actions}") print(f"Can move north: {bool(avail_actions[2])}") print(f"Can move south: {bool(avail_actions[3])}") print(f"Can attack enemy 0: {bool(avail_actions[6]) if len(avail_actions) > 6 else False}") # Observation structure print("\n=== Observation Structure ===") obs_features = env.get_obs_feature_names() print(f"Observation size: {env_info['obs_shape']}") print(f"Feature names ({len(obs_features)} total):") # Group features by type move_features = [f for f in obs_features if 'move' in f or 'pathing' in f or 'terrain' in f] enemy_features = [f for f in obs_features if 'enemy' in f] ally_features = [f for f in obs_features if 'ally' in f] own_features = [f for f in obs_features if 'own' in f] print(f" Movement features: {len(move_features)}") print(f" Enemy features: {len(enemy_features)}") print(f" Ally features: {len(ally_features)}") print(f" Own features: {len(own_features)}") # State structure (for centralized training) print("\n=== State Structure ===") state_features = env.get_state_feature_names() print(f"State size: {env_info['state_shape']}") print(f"State features ({len(state_features)} total)") # Get actual observations obs = env.get_obs() state = env.get_state() print(f"\nObservation shape per agent: {obs[0].shape}") print(f"State shape: {state.shape}") ``` -------------------------------- ### Initialize StarCraftCapabilityEnvWrapper for Procedural Generation Source: https://context7.com/oxwhirl/smacv2/llms.txt Shows how to initialize the StarCraftCapabilityEnvWrapper, which enables procedural content generation by managing capability configurations for team composition and start positions. This wrapper generates new configurations upon each environment reset. ```python from smacv2.env import StarCraft2Env from smacv2.env.starcraft2.wrapper import StarCraftCapabilityEnvWrapper import numpy as np ``` -------------------------------- ### Configure Zerg Units and Environment in SMACv2 Source: https://context7.com/oxwhirl/smacv2/llms.txt Sets up a Zerg environment with specific unit types, weights, and starting positions. It then initializes the StarCraftCapabilityEnvWrapper for Zerg units. This configuration is used for creating custom Zerg-centric scenarios. ```python zerg_config = { "n_units": 10, "n_enemies": 10, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["zergling", "hydralisk", "baneling"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["baneling"], # Exploding units "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } zerg_env = StarCraftCapabilityEnvWrapper( capability_config=zerg_config, map_name="10gen_zerg", use_unit_ranges=True, min_attack_range=2 ) ``` -------------------------------- ### Initialize and Run StarCraft2Env Source: https://context7.com/oxwhirl/smacv2/llms.txt Demonstrates how to initialize the StarCraft2Env with various parameters, get environment information, reset the environment, and run a single episode using random actions. It also shows how to retrieve observations, states, available actions, and process rewards and episode termination. ```python from smacv2.env import StarCraft2Env import numpy as np # Create a basic environment (without procedural generation) env = StarCraft2Env( map_name="10gen_terran", # Map name for the scenario step_mul=8, # Game steps per agent step difficulty="7", # AI difficulty (1-9, A for insane) obs_all_health=True, # Include health in observations obs_own_health=True, # Include own health in observations obs_last_action=False, # Include last actions in observations reward_sparse=False, # Use dense rewards (damage-based) reward_only_positive=True, # Only positive rewards reward_death_value=10, # Reward for killing enemy unit reward_win=200, # Bonus reward for winning use_unit_ranges=True, # Use realistic unit attack/sight ranges min_attack_range=2, # Minimum attack range debug=False # Enable debug logging ) # Get environment information env_info = env.get_env_info() print(f"State shape: {env_info['state_shape']}") print(f"Observation shape: {env_info['obs_shape']}") print(f"Number of actions: {env_info['n_actions']}") print(f"Number of agents: {env_info['n_agents']}") print(f"Episode limit: {env_info['episode_limit']}") # Reset environment and get initial observations obs, state = env.reset() # Run a single episode with random actions terminated = False episode_reward = 0 while not terminated: # Get observations for all agents obs = env.get_obs() state = env.get_state() # Select random valid actions for each agent actions = [] for agent_id in range(env_info['n_agents']): avail_actions = env.get_avail_agent_actions(agent_id) avail_actions_ind = np.nonzero(avail_actions)[0] action = np.random.choice(avail_actions_ind) actions.append(action) # Execute actions reward, terminated, info = env.step(actions) episode_reward += reward # Check battle outcome if terminated: print(f"Battle won: {info.get('battle_won', False)}") print(f"Dead allies: {info.get('dead_allies', 0)}") print(f"Dead enemies: {info.get('dead_enemies', 0)}") print(f"Total episode reward: {episode_reward}") # Get battle statistics stats = env.get_stats() print(f"Win rate: {stats['win_rate']:.2%}") # Save replay and close env.save_replay() env.close() ``` -------------------------------- ### Initialize SMAC Parallel Environment with PettingZoo Source: https://github.com/oxwhirl/smacv2/blob/main/smacv2/examples/pettingzoo/README.rst Initializes a StarCraft II environment using the Parallel API from PettingZoo. This API is suitable for scenarios where agents act simultaneously. PettingZoo must be installed prior to use. ```python from smac.env.pettingzoo import StarCraft2PZEnv env = StarCraft2PZEnv.parallel_env() ``` -------------------------------- ### PettingZoo Integration: Parallel Environment Source: https://context7.com/oxwhirl/smacv2/llms.txt Illustrates using SMACv2 with the PettingZoo parallel API for simultaneous agent actions. It covers environment setup, resetting, and taking steps with a dictionary of actions for all agents. This is ideal for multi-agent systems where agents act concurrently. ```python from smacv2.env.pettingzoo import StarCraft2PZEnv import numpy as np import random # Using parallel environment for simultaneous agent actions parallel_env = StarCraft2PZEnv.parallel_env( map_name="10gen_terran", max_cycles=1000 ) # Parallel API example observations = parallel_env.reset() print(f"\nParallel env agents: {parallel_env.agents}") print(f"Observation spaces: {list(parallel_env.observation_spaces.keys())}") # Step with dictionary of actions actions = {} for agent in parallel_env.agents: obs = observations[agent] if "action_mask" in obs: valid_actions = np.flatnonzero(obs["action_mask"]) actions[agent] = random.choice(valid_actions) if len(valid_actions) > 0 else 0 else: actions[agent] = parallel_env.action_spaces[agent].sample() observations, rewards, dones, infos = parallel_env.step(actions) print(f"Rewards: {rewards}") print(f"Dones: {dones}") parallel_env.close() ``` -------------------------------- ### Initialize SMAC AEC Environment with PettingZoo Source: https://github.com/oxwhirl/smacv2/blob/main/smacv2/examples/pettingzoo/README.rst Initializes a StarCraft II environment using the Agent Environment Cycle (AEC) API from PettingZoo. This is the primary API for PettingZoo, designed for agent-environment interaction cycles. Ensure PettingZoo is installed. ```python from smac.env.pettingzoo import StarCraft2PZEnv env = StarCraft2PZEnv.env() ``` -------------------------------- ### Define Standard SMACv2 Scenarios Source: https://context7.com/oxwhirl/smacv2/llms.txt Defines a dictionary of standard scenarios for Terran, Protoss, and Zerg matchups. Each scenario specifies the number of allied and enemy units, and the map to be used. This allows for easy setup of various combat situations. ```python scenarios = { "terran_5_vs_5": {"n_units": 5, "n_enemies": 5, "map": "10gen_terran"}, "terran_10_vs_10": {"n_units": 10, "n_enemies": 10, "map": "10gen_terran"}, "terran_10_vs_11": {"n_units": 10, "n_enemies": 11, "map": "10gen_terran"}, "terran_20_vs_20": {"n_units": 20, "n_enemies": 20, "map": "10gen_terran"}, "terran_20_vs_23": {"n_units": 20, "n_enemies": 23, "map": "10gen_terran"}, "protoss_5_vs_5": {"n_units": 5, "n_enemies": 5, "map": "10gen_protoss"}, "protoss_10_vs_10": {"n_units": 10, "n_enemies": 10, "map": "10gen_protoss"}, "zerg_5_vs_5": {"n_units": 5, "n_enemies": 5, "map": "10gen_zerg"}, "zerg_10_vs_10": {"n_units": 10, "n_enemies": 10, "map": "10gen_zerg"}, } print("Available SMACv2 scenarios:") for name, params in scenarios.items(): print(f" {name}: {params['n_units']} allies vs {params['n_enemies']} enemies") ``` -------------------------------- ### Race-Specific Configuration: Protoss Units Source: https://context7.com/oxwhirl/smacv2/llms.txt Defines a custom configuration for a Protoss-based SMACv2 environment. This specifies unit compositions with weighted probabilities for Stalkers, Zealots, and Colossi, along with enemy counts and starting position generation. It also sets up unit range parameters. ```python from smacv2.env.starcraft2.wrapper import StarCraftCapabilityEnvWrapper # Protoss configuration # Units: Stalker (0.45), Zealot (0.45), Colossus (0.1) protoss_config = { "n_units": 10, "n_enemies": 10, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["stalker", "zealot", "colossus"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["colossus"], # Too powerful for full team "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } protoss_env = StarCraftCapabilityEnvWrapper( capability_config=protoss_config, map_name="10gen_protoss", use_unit_ranges=True, min_attack_range=2 ) ``` -------------------------------- ### Race-Specific Configuration: Terran Units Source: https://context7.com/oxwhirl/smacv2/llms.txt Defines a custom configuration for a Terran-based SMACv2 environment. This includes specifying the number of units, enemy units, and weighted probabilities for spawning Marines, Marauders, and Medivacs. It also configures starting positions and unit ranges. ```python from smacv2.env.starcraft2.wrapper import StarCraftCapabilityEnvWrapper # Terran configuration # Units: Marine (0.45), Marauder (0.45), Medivac (0.1) terran_config = { "n_units": 10, "n_enemies": 10, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["marine", "marauder", "medivac"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["medivac"], # Healing only, avoid full team "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } terran_env = StarCraftCapabilityEnvWrapper( capability_config=terran_config, map_name="10gen_terran", use_unit_ranges=True, min_attack_range=2 ) ``` -------------------------------- ### Initialize and Run SMACv2 Environment with Random Policies Source: https://github.com/oxwhirl/smacv2/blob/main/README.md Demonstrates how to configure the StarCraftCapabilityEnvWrapper with a custom distribution config and execute a loop where agents perform random valid actions. This script initializes the environment, resets it for multiple episodes, and calculates total rewards. ```python from __future__ import absolute_import from __future__ import division from __future__ import print_function from os import replace from smacv2.env import StarCraft2Env import numpy as np from absl import logging import time from smacv2.env.starcraft2.wrapper import StarCraftCapabilityEnvWrapper logging.set_verbosity(logging.DEBUG) def main(): distribution_config = { "n_units": 5, "n_enemies": 5, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["marine", "marauder", "medivac"], "exception_unit_types": ["medivac"], "weights": [0.45, 0.45, 0.1], "observe": True, }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "n_enemies": 5, "map_x": 32, "map_y": 32, }, } env = StarCraftCapabilityEnvWrapper( capability_config=distribution_config, map_name="10gen_terran", debug=True, conic_fov=False, obs_own_pos=True, use_unit_ranges=True, min_attack_range=2, ) env_info = env.get_env_info() n_actions = env_info["n_actions"] n_agents = env_info["n_agents"] n_episodes = 10 print("Training episodes") for e in range(n_episodes): env.reset() terminated = False episode_reward = 0 while not terminated: obs = env.get_obs() state = env.get_state() actions = [] for agent_id in range(n_agents): avail_actions = env.get_avail_agent_actions(agent_id) avail_actions_ind = np.nonzero(avail_actions)[0] action = np.random.choice(avail_actions_ind) actions.append(action) reward, terminated, _ = env.step(actions) time.sleep(0.15) episode_reward += reward print("Total reward in episode {} = {}".format(e, episode_reward)) if __name__ == "__main__": main() ``` -------------------------------- ### Utilize Procedural Distribution Classes Source: https://context7.com/oxwhirl/smacv2/llms.txt Shows how to manually instantiate and use WeightedTeamsDistribution and SurroundedAndReflectPositionDistribution to generate team compositions and unit positions programmatically. ```python team_config = { "env_key": "team_gen", "dist_type": "weighted_teams", "n_units": 10, "n_enemies": 11, "unit_types": ["stalker", "zealot", "colossus"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["colossus"] } team_dist = WeightedTeamsDistribution(team_config) team_result = team_dist.generate() position_config = { "n_units": 5, "n_enemies": 5, "p": 0.5, "map_x": 32, "map_y": 32 } position_dist = SurroundedAndReflectPositionDistribution(position_config) position_result = position_dist.generate() ``` -------------------------------- ### Execute SMACv2 Training Loop Source: https://context7.com/oxwhirl/smacv2/llms.txt Demonstrates a standard training loop, including resetting the environment, retrieving observations, selecting random actions based on availability, and stepping the environment. ```python n_episodes = 100 for episode in range(n_episodes): obs, state = env.reset() terminated = False episode_reward = 0 while not terminated: obs = env.get_obs() state = env.get_state() actions = [] for agent_id in range(n_agents): avail_actions = env.get_avail_agent_actions(agent_id) valid_actions = np.nonzero(avail_actions)[0] action = np.random.choice(valid_actions) actions.append(action) reward, terminated, info = env.step(actions) episode_reward += reward print(f"Episode {episode}: reward = {episode_reward:.2f}") ``` -------------------------------- ### Compare Dense vs Sparse Rewards in SMACv2 Source: https://context7.com/oxwhirl/smacv2/llms.txt Runs a simulation to compare the performance of dense and sparse reward configurations in SMACv2. It resets the environment, takes random actions, and prints the cumulative reward and steps taken until the episode terminates. ```python print("=== Dense vs Sparse Rewards ===\n") for env_name, env in [("Dense", dense_env), ("Sparse", sparse_env)]: env.reset() total_reward = 0 steps = 0 terminated = False while not terminated: actions = [] for agent_id in range(env.get_env_info()['n_agents']): avail = env.get_avail_agent_actions(agent_id) action = np.random.choice(np.nonzero(avail)[0]) actions.append(action) reward, terminated, info = env.step(actions) total_reward += reward steps += 1 if steps % 20 == 0 and env_name == "Dense": print(f" Step {steps}: cumulative reward = {total_reward:.2f}") outcome = "Won" if info.get('battle_won', False) else "Lost" print(f"{env_name} rewards - {outcome}: total = {total_reward:.2f}, steps = {steps}\n") dense_env.close() sparse_env.close() ``` -------------------------------- ### Demonstrate Action Masking in SMACv2 Source: https://context7.com/oxwhirl/smacv2/llms.txt This snippet shows how to retrieve and display available actions for agents in an SMACv2 environment. It iterates through a specified number of agents and prints the valid actions for each based on the environment's availability information. This is useful for understanding agent capabilities at each step. ```python print("\n=== Action Masking Demo ===") for agent_id in range(min(3, env_info['n_agents'])): avail = env.get_avail_agent_actions(agent_id) valid_actions = np.nonzero(avail)[0] print(f"Agent {agent_id} can take actions: {valid_actions.tolist()}") env.close() ``` -------------------------------- ### PettingZoo Integration: Agent Iterator Pattern Source: https://context7.com/oxwhirl/smacv2/llms.txt Demonstrates using SMACv2 with the PettingZoo API via the agent iterator pattern. It shows environment creation, episode execution, and action selection using the provided action mask. This is suitable for environments where agents act sequentially. ```python from smacv2.env.pettingzoo import StarCraft2PZEnv import numpy as np import random # Create PettingZoo-style environment # Using the wrapped env() function for standard PettingZoo interface env = StarCraft2PZEnv.env( map_name="10gen_terran", difficulty="7" ) # Run episodes with the agent iterator pattern n_episodes = 5 total_rewards = [] for episode in range(n_episodes): env.reset() episode_reward = 0 # PettingZoo agent iteration pattern for agent in env.agent_iter(): # Get observation, reward, done, info for current agent obs, reward, done, info = env.last() episode_reward += reward if done: # Dead agents must pass None action = None elif isinstance(obs, dict) and "action_mask" in obs: # Use action mask to select valid action valid_actions = np.flatnonzero(obs["action_mask"]) action = random.choice(valid_actions) else: # Fallback to random action from action space action = env.action_spaces[agent].sample() env.step(action) total_rewards.append(episode_reward) print(f"Episode {episode}: Total reward = {episode_reward:.2f}") print(f"\nAverage reward: {np.mean(total_rewards):.2f}") env.close() ``` -------------------------------- ### Configure Sparse Rewards in SMACv2 Source: https://context7.com/oxwhirl/smacv2/llms.txt Sets up an environment using sparse rewards, where the agent only receives a reward for winning or losing the battle. This configuration is simpler than dense rewards and focuses on the ultimate outcome of the engagement. ```python sparse_env = StarCraftCapabilityEnvWrapper( capability_config=dense_reward_config, map_name="10gen_terran", reward_sparse=True, # Sparse rewards only use_unit_ranges=True, min_attack_range=2 ) ``` -------------------------------- ### Create Custom Distribution for Balanced Teams in Python Source: https://context7.com/oxwhirl/smacv2/llms.txt Defines a custom distribution class `CustomTeamDistribution` that generates balanced ally and enemy teams based on configuration. It registers this custom distribution and demonstrates its usage. ```python from smacv2.config.distribution_config import register_distribution, get_distribution import numpy as np from typing import Dict, Any class Distribution: def __init__(self, config): pass def generate(self) -> Dict[str, Dict[str, Any]]: pass @property def n_tasks(self) -> int: return 0 # Create custom distribution class CustomTeamDistribution(Distribution): """Custom distribution that creates balanced teams.""" def __init__(self, config): self.config = config self.n_units = config["n_units"] self.n_enemies = config["n_enemies"] self.unit_types = config["unit_types"] self.env_key = config["env_key"] self.rng = np.random.default_rng() def generate(self) -> Dict[str, Dict[str, Any]]: # Create balanced team with equal distribution n_types = len(self.unit_types) units_per_type = self.n_units // n_types remainder = self.n_units % n_types ally_team = [] for i, unit_type in enumerate(self.unit_types): count = units_per_type + (1 if i < remainder else 0) ally_team.extend([unit_type] * count) self.rng.shuffle(ally_team) enemy_team = ally_team.copy() # Add extra enemies if needed if self.n_enemies > self.n_units: extra = self.rng.choice( self.unit_types, size=self.n_enemies - self.n_units ).tolist() enemy_team.extend(extra) return { self.env_key: { "ally_team": ally_team, "enemy_team": enemy_team, "id": 0 } } @property def n_tasks(self) -> int: return float('inf') # Register custom distribution register_distribution("balanced_teams", CustomTeamDistribution) # Use custom distribution custom_config = { "env_key": "team_gen", "dist_type": "balanced_teams", "n_units": 6, "n_enemies": 6, "unit_types": ["zergling", "hydralisk", "baneling"] # Zerg units } custom_dist = get_distribution("balanced_teams")(custom_config) result = custom_dist.generate() print(f"Balanced ally team: {result['team_gen']['ally_team']}") ``` -------------------------------- ### Configure Dense Rewards in SMACv2 Source: https://context7.com/oxwhirl/smacv2/llms.txt Sets up an environment with dense reward configuration, focusing on damage dealt, units killed, and battle outcome. It uses Terran units like Marines, Marauders, and Medivacs. The rewards are scaled and only positive, with bonuses for winning and a penalty for losing. ```python dense_reward_config = { "n_units": 5, "n_enemies": 5, "team_gen": { "dist_type": "weighted_teams", "unit_types": ["marine", "marauder", "medivac"], "weights": [0.45, 0.45, 0.1], "exception_unit_types": ["medivac"], "observe": True }, "start_positions": { "dist_type": "surrounded_and_reflect", "p": 0.5, "map_x": 32, "map_y": 32 } } dense_env = StarCraftCapabilityEnvWrapper( capability_config=dense_reward_config, map_name="10gen_terran", reward_sparse=False, # Use dense rewards reward_only_positive=True, # Only positive rewards reward_death_value=10, # Reward per enemy killed reward_win=200, # Bonus for winning reward_defeat=0, # No penalty for losing reward_scale=True, # Scale rewards reward_scale_rate=20, # Scaling factor use_unit_ranges=True, min_attack_range=2 ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.