Skip to content

Ppo

mighty.mighty_agents.ppo #

MightyPPOAgent #

MightyPPOAgent(
    output_dir,
    env: MIGHTYENV,
    eval_env: Optional[MIGHTYENV] = None,
    seed: Optional[int] = None,
    learning_rate: float = 0.001,
    gamma: float = 0.99,
    batch_size: int = 64,
    learning_starts: int = 1,
    render_progress: bool = True,
    log_wandb: bool = False,
    wandb_kwargs: Optional[Dict] = None,
    rollout_buffer_class: Optional[
        str | DictConfig | Type[MightyRolloutBuffer]
    ] = MightyRolloutBuffer,
    rollout_buffer_kwargs: Optional[TypeKwargs] = None,
    meta_methods: Optional[List[str | type]] = None,
    meta_kwargs: Optional[List[TypeKwargs]] = None,
    n_policy_units: int = 8,
    n_critic_units: int = 8,
    soft_update_weight: float = 0.01,
    policy_class: Optional[
        Union[
            str, DictConfig, Type[MightyExplorationPolicy]
        ]
    ] = None,
    policy_kwargs: Optional[Dict] = None,
    ppo_clip: float = 0.2,
    value_loss_coef: float = 0.5,
    entropy_coef: float = 0.01,
    max_grad_norm: float = 0.5,
    n_gradient_steps: int = 10,
)

Bases: MightyAgent

Creates all relevant class variables and calls the agent-specific init function.

:param env: Train environment :param eval_env: Evaluation environment :param seed: Seed for random number generators :param learning_rate: Learning rate for training :param gamma: Discount factor :param batch_size: Batch size for training :param learning_starts: Number of steps before learning starts :param render_progress: Whether to render progress :param log_tensorboard: Log to TensorBoard as well as to file :param log_wandb: Log to Weights and Biases :param wandb_kwargs: Arguments for Weights and Biases logging :param rollout_buffer_class: Rollout buffer class :param rollout_buffer_kwargs: Arguments for the rollout buffer :param meta_methods: Meta methods for the agent :param meta_kwargs: Arguments for meta methods :param n_policy_units: Number of units for the policy network :param n_critic_units: Number of units for the critic network :param soft_update_weight: Size of soft updates for the target network :param policy_class: Policy class :param policy_kwargs: Arguments for the policy :param ppo_clip: Clipping parameter for PPO :param value_loss_coef: Coefficient for the value loss :param entropy_coef: Coefficient for the entropy loss :param max_grad_norm: Maximum gradient norm :param n_gradient_steps: Number of gradient steps per update

Source code in mighty/mighty_agents/ppo.py
def __init__(
    self,
    output_dir,
    env: MIGHTYENV,  # type: ignore
    eval_env: Optional[MIGHTYENV] = None,  # type: ignore
    seed: Optional[int] = None,
    learning_rate: float = 0.001,
    gamma: float = 0.99,
    batch_size: int = 64,
    learning_starts: int = 1,
    render_progress: bool = True,
    log_wandb: bool = False,
    wandb_kwargs: Optional[Dict] = None,
    rollout_buffer_class: Optional[
        str | DictConfig | Type[MightyRolloutBuffer]
    ] = MightyRolloutBuffer,
    rollout_buffer_kwargs: Optional[TypeKwargs] = None,
    meta_methods: Optional[List[str | type]] = None,
    meta_kwargs: Optional[List[TypeKwargs]] = None,
    n_policy_units: int = 8,
    n_critic_units: int = 8,
    soft_update_weight: float = 0.01,
    policy_class: Optional[
        Union[str, DictConfig, Type[MightyExplorationPolicy]]
    ] = None,
    policy_kwargs: Optional[Dict] = None,
    ppo_clip: float = 0.2,
    value_loss_coef: float = 0.5,
    entropy_coef: float = 0.01,
    max_grad_norm: float = 0.5,
    n_gradient_steps: int = 10,
):
    """Initialize the PPO agent.

    Creates all relevant class variables and calls the agent-specific init function.

    :param env: Train environment
    :param eval_env: Evaluation environment
    :param seed: Seed for random number generators
    :param learning_rate: Learning rate for training
    :param gamma: Discount factor
    :param batch_size: Batch size for training
    :param learning_starts: Number of steps before learning starts
    :param render_progress: Whether to render progress
    :param log_tensorboard: Log to TensorBoard as well as to file
    :param log_wandb: Log to Weights and Biases
    :param wandb_kwargs: Arguments for Weights and Biases logging
    :param rollout_buffer_class: Rollout buffer class
    :param rollout_buffer_kwargs: Arguments for the rollout buffer
    :param meta_methods: Meta methods for the agent
    :param meta_kwargs: Arguments for meta methods
    :param n_policy_units: Number of units for the policy network
    :param n_critic_units: Number of units for the critic network
    :param soft_update_weight: Size of soft updates for the target network
    :param policy_class: Policy class
    :param policy_kwargs: Arguments for the policy
    :param ppo_clip: Clipping parameter for PPO
    :param value_loss_coef: Coefficient for the value loss
    :param entropy_coef: Coefficient for the entropy loss
    :param max_grad_norm: Maximum gradient norm
    :param n_gradient_steps: Number of gradient steps per update
    """

    self.gamma = gamma
    self.n_policy_units = n_policy_units
    self.n_critic_units = n_critic_units
    self.soft_update_weight = soft_update_weight
    self.ppo_clip = ppo_clip
    self.value_loss_coef = value_loss_coef
    self.entropy_coef = entropy_coef
    self.max_grad_norm = max_grad_norm
    self.n_gradient_steps = n_gradient_steps

    # Placeholder variables which are filled in self._initialize_agent
    self.model: PPOModel | None = None
    self.update_fn: PPOUpdate | None = None

    # Policy class
    policy_class = retrieve_class(cls=policy_class, default_cls=StochasticPolicy)  # type: ignore
    if policy_kwargs is None:
        policy_kwargs = {}
    self.policy_class = policy_class
    self.policy_kwargs = policy_kwargs

    super().__init__(
        env=env,
        output_dir=output_dir,
        seed=seed,
        eval_env=eval_env,
        learning_rate=learning_rate,
        batch_size=batch_size,
        learning_starts=learning_starts,
        render_progress=render_progress,
        log_wandb=log_wandb,
        wandb_kwargs=wandb_kwargs,
        replay_buffer_class=rollout_buffer_class,
        replay_buffer_kwargs=rollout_buffer_kwargs,
        meta_methods=meta_methods,
        meta_kwargs=meta_kwargs,
    )

    self.loss_buffer = {
        "Update/policy_loss": [],
        "Update/value_loss": [],
        "Update/entropy": [],
        "step": [],
    }

agent_type property #

agent_type: str

Return the type of the agent.

value_function property #

value_function: Module

Return the value function model.

__del__ #

__del__() -> None

Close wandb upon deletion.

Source code in mighty/mighty_agents/base_agent.py
def __del__(self) -> None:
    """Close wandb upon deletion."""
    self.env.close()  # type: ignore
    if self.log_wandb:
        wandb.finish()

adapt_hps #

adapt_hps(metrics: Dict) -> None

Set hyperparameters.

Source code in mighty/mighty_agents/base_agent.py
def adapt_hps(self, metrics: Dict) -> None:
    """Set hyperparameters."""
    old_hps = {
        "step": self.steps,
        "hp/lr": self.learning_rate,
        "hp/pi_epsilon": self._epsilon,
        "hp/batch_size": self._batch_size,
        "hp/learning_starts": self._learning_starts,
        "meta_modules": list(self.meta_modules.keys()),
    }
    self.learning_rate = metrics["hp/lr"]
    self._epsilon = metrics["hp/pi_epsilon"]
    self._batch_size = metrics["hp/batch_size"]
    self._learning_starts = metrics["hp/learning_starts"]

    updated_hps = {
        "step": self.steps,
        "hp/lr": self.learning_rate,
        "hp/pi_epsilon": self._epsilon,
        "hp/batch_size": self._batch_size,
        "hp/learning_starts": self._learning_starts,
        "meta_modules": list(self.meta_modules.keys()),
    }
    # TODO: this probably always fails
    if old_hps != updated_hps:
        self.hp_buffer = update_buffer(self.hp_buffer, updated_hps)

apply_config #

apply_config(config: Dict) -> None

Apply config to agent.

Source code in mighty/mighty_agents/base_agent.py
def apply_config(self, config: Dict) -> None:
    """Apply config to agent."""
    for n in config:
        algo_name = n.split(".")[-1]
        if hasattr(self, algo_name):
            setattr(self, algo_name, config[n])
        elif hasattr(self, "_" + algo_name):
            setattr(self, "_" + algo_name, config[n])
        elif n in ["architecture", "n_units", "n_layers", "size"]:
            pass
        else:
            print(f"Trying to set hyperparameter {algo_name} which does not exist.")

evaluate #

evaluate(eval_env: MIGHTYENV | None = None) -> Dict

Eval agent on an environment. (Full rollouts).

:param env: The environment to evaluate on :param episodes: The number of episodes to evaluate :return:

Source code in mighty/mighty_agents/base_agent.py
def evaluate(self, eval_env: MIGHTYENV | None = None) -> Dict:  # type: ignore
    """Eval agent on an environment. (Full rollouts).

    :param env: The environment to evaluate on
    :param episodes: The number of episodes to evaluate
    :return:
    """

    terminated, truncated = False, False
    options: Dict = {}
    if eval_env is None:
        eval_env = self.eval_env

    state, _ = eval_env.reset(options=options)  # type: ignore
    rewards = np.zeros(eval_env.num_envs)  # type: ignore
    steps = np.zeros(eval_env.num_envs)  # type: ignore
    mask = np.zeros(eval_env.num_envs)  # type: ignore
    while not np.all(mask):
        action = self.policy(state, evaluate=True)  # type: ignore
        state, reward, terminated, truncated, _ = eval_env.step(action)  # type: ignore
        rewards += reward * (1 - mask)
        steps += 1 * (1 - mask)
        dones = np.logical_or(terminated, truncated)
        mask = np.where(dones, 1, mask)

    eval_env.close()  # type: ignore

    if isinstance(self.eval_env, DACENV) or isinstance(self.env, CARLENV):
        instance = eval_env.instance  # type: ignore
    else:
        instance = "None"

    eval_metrics = {
        "step": self.steps,
        "seed": self.seed,
        "eval_episodes": np.array(rewards) / steps,
        "mean_eval_step_reward": np.mean(rewards) / steps,
        "mean_eval_reward": np.mean(rewards),
        "instance": instance,
    }
    self.eval_buffer = update_buffer(self.eval_buffer, eval_metrics)

    # FIXME: this is the ugly I'm talking about
    if self.verbose:
        print("")
        print(
            "------------------------------------------------------------------------------"
        )
        print(
            f"""Evaluation performance after {self.steps} steps:
            {np.round(np.mean(rewards), decimals=2)}"""
        )
        print(
            f"""Evaluation performance per step after {self.steps} steps:
            {np.round(np.mean(rewards / steps), decimals=2)}"""
        )
        print(
            "------------------------------------------------------------------------------"
        )
        print("")

    if self.log_wandb:
        wandb.log(eval_metrics)

    return eval_metrics

initialize_agent #

initialize_agent() -> None

General initialization of tracer and buffer for all agents.

Algorithm specific initialization like policies etc. are done in _initialize_agent

Source code in mighty/mighty_agents/base_agent.py
def initialize_agent(self) -> None:
    """General initialization of tracer and buffer for all agents.

    Algorithm specific initialization like policies etc.
    are done in _initialize_agent
    """
    self._initialize_agent()
    self.buffer = self.buffer_class(**self.buffer_kwargs)  # type: ignore

load #

load(path: str) -> None

Load the internal state of the agent.

Source code in mighty/mighty_agents/ppo.py
def load(self, path: str) -> None:
    """Load the internal state of the agent."""
    base_path = Path(path)
    self.model.policy_net.load_state_dict(torch.load(base_path / "policy_net.pt"))  # type: ignore
    self.model.value_net.load_state_dict(torch.load(base_path / "value_net.pt"))  # type: ignore
    self.update_fn.policy_optimizer.load_state_dict(  # type: ignore
        torch.load(base_path / "policy_optimizer.pt")
    )
    self.update_fn.value_optimizer.load_state_dict(  # type: ignore
        torch.load(base_path / "value_optimizer.pt")
    )

    if self.verbose:
        print(f"Loaded checkpoint at {path}")

make_checkpoint_dir #

make_checkpoint_dir(t: int) -> None

Checkpoint model.

:param T: Current timestep :return:

Source code in mighty/mighty_agents/base_agent.py
def make_checkpoint_dir(self, t: int) -> None:
    """Checkpoint model.

    :param T: Current timestep
    :return:
    """
    self.upper_checkpoint_dir = Path(self.output_dir) / Path("checkpoints")
    if not self.upper_checkpoint_dir.exists():
        Path(self.upper_checkpoint_dir).mkdir()
    self.checkpoint_dir = self.upper_checkpoint_dir / f"{t}"
    if not self.checkpoint_dir.exists():
        Path(self.checkpoint_dir).mkdir()

run #

run(
    n_steps: int,
    eval_every_n_steps: int = 1000,
    human_log_every_n_steps: int = 5000,
    save_model_every_n_steps: int | None = 5000,
    env: MIGHTYENV = None,
) -> Dict

Run agent.

Source code in mighty/mighty_agents/base_agent.py
def run(  # noqa: PLR0915
    self,
    n_steps: int,
    eval_every_n_steps: int = 1_000,
    human_log_every_n_steps: int = 5000,
    save_model_every_n_steps: int | None = 5000,
    env: MIGHTYENV = None,  # type: ignore
) -> Dict:
    """Run agent."""
    episodes = 0
    if env is not None:
        self.env = env
    # FIXME: can we add the eval result here? Else the evals spam the command line in a pretty ugly way
    with Progress(
        "[progress.description]{task.description}",
        BarColumn(),
        "[progress.percentage]{task.percentage:>3.0f}%",
        "Remaining:",
        TimeRemainingColumn(),
        "Elapsed:",
        TimeElapsedColumn(),
        disable=not self.render_progress,
    ) as progress:
        steps_task = progress.add_task(
            "Train Steps",
            total=n_steps - self.steps,
            start=False,
            visible=False,
        )
        steps_since_eval = 0
        progress.start_task(steps_task)
        # FIXME: this is more of a question: are there cases where we don't want to reset this completely?
        # I can't think of any, can you? If yes, we should maybe add this as an optional argument
        metrics = {
            "env": self.env,
            "vf": self.value_function,  # type: ignore
            "policy": self.policy,
            "step": self.steps,
            "hp/lr": self.learning_rate,
            "hp/pi_epsilon": self._epsilon,
            "hp/batch_size": self._batch_size,
            "hp/learning_starts": self._learning_starts,
        }

        # Reset env and initialize reward sum
        curr_s, _ = self.env.reset()  # type: ignore
        if len(curr_s.squeeze().shape) == 0:
            episode_reward = [0]
        else:
            episode_reward = np.zeros(curr_s.squeeze().shape[0])  # type: ignore

        last_episode_reward = episode_reward
        if not torch.is_tensor(last_episode_reward):
            last_episode_reward = torch.tensor(last_episode_reward).float()
        progress.update(steps_task, visible=True)

        # Main loop: rollouts, training and evaluation
        while self.steps < n_steps:
            metrics["episode_reward"] = episode_reward

            # TODO Remove
            progress.stop()

            action, log_prob = self.step(curr_s, metrics)

            next_s, reward, terminated, truncated, _ = self.env.step(action)  # type: ignore
            dones = np.logical_or(terminated, truncated)

            transition_metrics = self.process_transition(
                curr_s, action, reward, next_s, dones, log_prob, metrics
            )

            metrics.update(transition_metrics)

            episode_reward += reward

            # Log everything
            t = {
                "seed": self.seed,
                "step": self.steps,
                "reward": reward,
                "action": action,
                "state": curr_s,
                "next_state": next_s,
                "terminated": terminated.astype(int),
                "truncated": truncated.astype(int),
                "mean_episode_reward": last_episode_reward.mean(),
            }
            metrics["episode_reward"] = episode_reward
            self.result_buffer = update_buffer(self.result_buffer, t)

            if self.log_wandb:
                wandb.log(t)

            for k in self.meta_modules:
                self.meta_modules[k].post_step(metrics)

            self.steps += len(action)
            metrics["step"] = self.steps
            steps_since_eval += len(action)
            for _ in range(len(action)):
                progress.advance(steps_task)

            # Update agent
            if (
                len(self.buffer) >= self._batch_size  # type: ignore
                and self.steps >= self._learning_starts
            ):
                update_kwargs = {"next_s": next_s, "dones": dones}

                metrics = self.update(metrics, update_kwargs)

            # End step
            self.last_state = curr_s
            curr_s = next_s

            # Evaluate
            if eval_every_n_steps and steps_since_eval >= eval_every_n_steps:
                steps_since_eval = 0
                self.evaluate()

            # Log to command line
            if self.steps % human_log_every_n_steps == 0 and self.verbose:
                mean_last_ep_reward = np.round(
                    np.mean(last_episode_reward), decimals=2
                )
                mean_last_step_reward = np.round(
                    np.mean(mean_last_ep_reward / len(last_episode_reward)),
                    decimals=2,
                )
                print(
                    f"""Steps: {self.steps}, Latest Episode Reward: {mean_last_ep_reward}, Latest Step Reward: {mean_last_step_reward}"""  # noqa: E501
                )

            # Save
            if (
                save_model_every_n_steps
                and self.steps % save_model_every_n_steps == 0
            ):
                self.save(self.steps)
                log_to_file(
                    self.output_dir,
                    self.result_buffer,
                    self.hp_buffer,
                    self.eval_buffer,
                    self.loss_buffer,
                )

            if np.any(dones):
                last_episode_reward = np.where(  # type: ignore
                    dones, episode_reward, last_episode_reward
                )
                episode_reward = np.where(dones, 0, episode_reward)  # type: ignore
                # End episode
                if isinstance(self.env, DACENV) or isinstance(self.env, CARLENV):
                    instance = self.env.instance  # type: ignore
                else:
                    instance = None
                metrics["instance"] = instance
                episodes += 1
                for k in self.meta_modules:
                    self.meta_modules[k].post_episode(metrics)

                # Remove rollout data from last episode
                # TODO: only do this for finished envs
                # FIXME: open todo, I think we need to use dones as a mask here
                # Proposed fix: metrics[k][:, dones] = 0
                # I don't think this is correct masking and I think we have to check the size of zeros
                for k in list(metrics.keys()):
                    if "rollout" in k:
                        del metrics[k]

                # Meta Module hooks
                for k in self.meta_modules:
                    self.meta_modules[k].pre_episode(metrics)
    log_to_file(
        self.output_dir,
        self.result_buffer,
        self.hp_buffer,
        self.eval_buffer,
        self.loss_buffer,
    )
    return metrics

save #

save(t: int) -> None

Save current agent state.

Source code in mighty/mighty_agents/ppo.py
def save(self, t: int) -> None:
    """Save current agent state."""
    super().make_checkpoint_dir(t)
    torch.save(
        self.model.policy_net.state_dict(),  # type: ignore
        self.checkpoint_dir / "policy_net.pt",
    )
    torch.save(
        self.model.value_net.state_dict(),  # type: ignore
        self.checkpoint_dir / "value_net.pt",
    )
    torch.save(
        self.update_fn.policy_optimizer.state_dict(),  # type: ignore
        self.checkpoint_dir / "policy_optimizer.pt",
    )
    torch.save(
        self.update_fn.value_optimizer.state_dict(),  # type: ignore
        self.checkpoint_dir / "value_optimizer.pt",
    )

    if self.verbose:
        print(f"Saved checkpoint at {self.checkpoint_dir}")

update #

update(metrics: Dict, update_kwargs: Dict) -> Dict

Update agent.

Source code in mighty/mighty_agents/base_agent.py
def update(self, metrics: Dict, update_kwargs: Dict) -> Dict:
    """Update agent."""
    for k in self.meta_modules:
        self.meta_modules[k].pre_update(metrics)

    agent_update_metrics = self.update_agent(**update_kwargs)
    metrics.update(agent_update_metrics)
    metrics = {k: np.array(v) for k, v in metrics.items()}
    metrics["step"] = self.steps

    if self.log_wandb:
        wandb.log(metrics)

    metrics["env"] = self.env
    metrics["vf"] = self.value_function  # type: ignore
    metrics["policy"] = self.policy
    for k in self.meta_modules:
        self.meta_modules[k].post_update(metrics)
    return metrics

update_agent #

update_agent(next_s, dones, **kwargs) -> Dict

Update the agent using PPO.

:return: Dictionary containing the update metrics.

Source code in mighty/mighty_agents/ppo.py
def update_agent(self, next_s, dones, **kwargs) -> Dict:  # type: ignore
    """Update the agent using PPO.

    :return: Dictionary containing the update metrics.
    """
    if len(self.buffer) < self._learning_starts:  # type: ignore
        return {}

    # Compute returns and advantages for PPO
    last_values = self.value_function(
        torch.as_tensor(next_s, dtype=torch.float32)
    ).detach()

    self.buffer.compute_returns_and_advantage(last_values, dones)  # type: ignore

    metrics: Dict = {}
    for _ in range(self.n_gradient_steps):
        for batch in self.buffer.sample(self._batch_size):  # type: ignore
            metrics.update(self.update_fn.update(batch))  # type: ignore

    for key, value in metrics.items():
        self.loss_buffer[key].append(value)
    self.loss_buffer["step"].append(self.steps)

    self.buffer.reset()  # type: ignore

    return metrics