{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "**RLeXplore allows you to combine multiple intrinsic rewards to explore the potential assembly advantages.**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Load the libraries**" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import torch as th\n", "\n", "from rllte.env import make_atari_env\n", "from rllte.xplore.reward import Fabric, RE3, ICM\n", "from rllte.agent import PPO" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Use the `Fabric` class to create a mixed intrinsic reward**" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "class TwoMixed(Fabric):\n", " def __init__(self, m1, m2):\n", " super().__init__(m1, m2)\n", " \n", " def compute(self, samples, sync):\n", " rwd1, rwd2 = super().compute(samples, sync)\n", "\n", " return rwd1 + rwd2\n", "\n", "class ThreeMixed(Fabric):\n", " def __init__(self, m1, m2, m3):\n", " super().__init__(m1, m2, m3)\n", " \n", " def compute(self, samples, sync):\n", " rwd1, rwd2, rw3 = super().compute(samples, sync)\n", "\n", " return (rwd1 + rwd2) * rw3" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# set the parameters\n", "device = 'cuda' if th.cuda.is_available() else 'cpu'\n", "# create the vectorized environments\n", "envs = make_atari_env(env_id='BreakoutNoFrameskip-v4', device=device, num_envs=8)\n", "# create two intrinsic reward functions\n", "irs1 = ICM(envs, device)\n", "irs2 = RE3(envs, device)\n", "# create the mixed intrinsic reward function\n", "irs = TwoMixed(irs1, irs2)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# create the PPO agent\n", "agent = PPO(envs, device=device)\n", "# set the intrinsic reward module\n", "agent.set(reward=irs)\n", "# train the agent\n", "agent.train(10000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "marllib", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.18" } }, "nbformat": 4, "nbformat_minor": 2 }