Skip to content

Instantly share code, notes, and snippets.

View araffin's full-sized avatar

Antonin RAFFIN araffin

View GitHub Profile
@araffin
araffin / optimize_ppo.py
Last active May 18, 2026 01:41
Example on how to use Optuna for automatic hyperparamer optimization with RL and SB3
"""Optuna example that optimizes the hyperparameters of
a reinforcement learning agent using PPO implementation from Stable-Baselines3
on a Gymnasium environment.
This is a simplified version of what can be found in https://github.com/DLR-RM/rl-baselines3-zoo.
You can run this example as follows:
$ python optimize_ppo.py
"""
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from gymnasium import spaces
from stable_baselines3 import PPO
from stable_baselines3.common.env_util import make_vec_env
from stable_baselines3.common.vec_env import VecEnvWrapper
sns.set_theme()
@araffin
araffin / halfcheetah_minimal.py
Last active September 2, 2025 13:09
Minimal implementation to solve the HalfCheetah env using open-loop oscillators
# MIT License Copyright (c) 2024 Antonin Raffin
import gymnasium as gym
import numpy as np
from gymnasium.envs.mujoco.mujoco_env import MujocoEnv
# Env initialization
env = gym.make("HalfCheetah-v4", render_mode="human")
# Wrap to have reward statistics
env = gym.wrappers.RecordEpisodeStatistics(env)
mujoco_env = env.unwrapped
"""
A simple GUI to collect human feedback.
It writes the rating to a file "gui_value.txt" next to the script.
The rating can be reset by removing or emptying the text file.
Nicegui is the only dependency.
If you use `uv` you can do `uv run feedback_gui.py`.
Author: Antonin Raffin (2024)
MIT License
import sbx
import shimmy
import stable_baselines3 as sb3
from dm_control import suite
from gymnasium.wrappers import FlattenObservation
from stable_baselines3.common.env_checker import check_env
# Available envs:
# suite._DOMAINS
# suite.dog.SUITE
import gymnasium as gym
import numpy as np
from gymnasium.envs.mujoco.mujoco_env import MujocoEnv
# Env initialization
env = gym.make("Swimmer-v4", render_mode="human")
# Wrap to have reward statistics
env = gym.wrappers.RecordEpisodeStatistics(env)
mujoco_env = env.unwrapped
n_joints = 2
@araffin
araffin / color_mask.py
Created October 6, 2017 15:56
Find the center of a white line in an image using OpenCV
from __future__ import division
import cv2
import numpy as np
# Input Image
image = cv2.imread("my_image.jpg")
# Convert to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Define range of white color in HSV
lower_white = np.array([0, 0, 212])
@araffin
araffin / example_serial.py
Last active March 3, 2023 16:12
Example Use of Robust Serial in Python
# From https://github.com/araffin/python-arduino-serial
from robust_serial import Order, write_order, write_i8, write_i16
from robust_serial.utils import open_serial_port
# Open serial port with a baudrate of 9600 (bits/s)
serial_file = open_serial_port(baudrate=9600)
# Send the order "MOTOR", i.e. to change the speed of the car
# equivalent to write_i8(serial_file, Order.MOTOR.value)
write_order(serial_file, Order.MOTOR)
@araffin
araffin / download_local_google_fonts.mjs
Created November 3, 2022 14:35
Download Google fonts locally
// https://github.com/datalogix/google-fonts-helper
// npm install google-fonts-helper
import { download } from 'google-fonts-helper'
const downloader = download('https://fonts.googleapis.com/css?family=Montserrat:400,700%7CRoboto:400,400italic,700%7CRoboto+Mono&display=swap', {
base64: false,
overwriting: false,
outputDir: './',
stylePath: 'fonts.css',
fontsDir: 'fonts',
@araffin
araffin / a2c_lunar.py
Last active October 16, 2022 13:53
Training, Saving and Loading an A2C agent
import gym
from stable_baselines import A2C
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
# Create and wrap the environment
env = gym.make('LunarLander-v2')
env = DummyVecEnv([lambda: env])