Skip to content

Commit

Permalink
docs: Update usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
strakam committed Jan 16, 2025
1 parent 9fb23a2 commit e8e1652
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 55 deletions.
36 changes: 0 additions & 36 deletions examples/ray_example.py

This file was deleted.

30 changes: 17 additions & 13 deletions examples/record_replay_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import gymnasium as gym

from generals import GridFactory
from generals import GridFactory, PettingZooGenerals
from generals.agents import RandomAgent, ExpanderAgent

# Initialize agents
Expand All @@ -17,22 +15,28 @@
# seed=38, # Seed to generate the same map every time
)

env = gym.make(
"gym-generals-v0", # Environment name
grid_factory=grid_factory, # Grid factory
agent=agent,
npc=npc, # NPC that will play against the agent
agents ={
npc.id: npc,
agent.id: agent
}

env = PettingZooGenerals(
agents=[npc.id, agent.id],
grid_factory=grid_factory,
render_mode=None
)

# Options are used only for the next game
options = {
"replay_file": "my_replay", # Save replay as my_replay.pkl
}

observation, info = env.reset(options=options)

observations, info = env.reset(options=options)
terminated = truncated = False
while not (terminated or truncated):
action = agent.act(observation)
observation, reward, terminated, truncated, info = env.step(action)
env.render()
actions = {}
for agent in env.agents:
# Ask agent for action
actions[agent] = agents[agent].act(observations[agent])
# All agents perform their actions
observations, rewards, terminated, truncated, info = env.step(actions)
8 changes: 2 additions & 6 deletions generals/envs/gymnasium_generals.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def __init__(

# Initialize game state
self.prior_observations: dict[str, Observation] | None = None
self.game = self._create_new_game()
grid = self.grid_factory.generate()
self.game = Game(grid, self.agents)

# Set up spaces
self.observation_space = self._create_observation_space()
Expand All @@ -78,11 +79,6 @@ def _setup_agent_data(self) -> dict[str, dict[str, Any]]:
colors = [(255, 107, 108), (0, 130, 255)]
return {id: {"color": color} for id, color in zip(self.agents, colors)}

def _create_new_game(self) -> Game:
"""Create a new game instance."""
grid = self.grid_factory.generate()
return Game(grid, self.agents)

def _create_observation_space(self) -> spaces.Space:
"""Create the observation space based on grid dimensions."""
dim = self.pad_observations_to
Expand Down

0 comments on commit e8e1652

Please sign in to comment.