A gymnasium environment for laser pulse shaping
Project description
gym-laser
gym-laser is a physics-informed simulated environment for laser pulse optimization using []gymnasium](LINK_TO_GYMNASIUM).
Features
- Physics-informed environments: Based on real laser physics simulations
- Two environment variants:
FROGLaserEnv: Environment for standard RL trainingRandomFROGLaserEnv: Environment enabling domain randomization environment for robust training
Installation
We recommend installing this environment within a Conda environment. Then, simply run:
conda create -n rlaser python=3.11 -y
conda activate rlaser
pip install gym-laser
Quick Start
Basic Usage
import numpy as np
from gym_laser.env_utils import EnvParametrization
from gym_laser.LaserEnv import FROGLaserEnv
# Create environment with default parameters
params = EnvParametrization().get_parametrization_dict()
env = FROGLaserEnv(**params)
# Reset environment
obs, info = env.reset()
# Take a random action
action = env.action_space.sample()
obs, reward, terminated, truncated, info = env.step(action)
# Access physics properties
print(f"Pulse FWHM: {env.pulse_FWHM:.2f} ps")
print(f"Peak intensity: {env.peak_intensity:.2e}")
env.close()
Domain Randomization
from gym_laser.RandomLaserEnv import RandomFROGLaserEnv
# Create environment with domain randomization
env = RandomFROGLaserEnv(**params)
# Enable domain randomization
env.set_dr_training(True)
# Environment will sample different dynamics at each reset
for episode in range(5):
obs, info = env.reset()
print(f"Episode {episode} task: {env.get_task()}")
env.close()
Training with Stable Baselines3
from stable_baselines3 import PPO
from gym_laser.LaserEnv import FROGLaserEnv
from gym_laser.env_utils import EnvParametrization
# Create environment
params = EnvParametrization().get_parametrization_dict()
env = FROGLaserEnv(**params)
# Train agent
model = PPO("MultiInputPolicy", env, verbose=1)
model.learn(total_timesteps=10000)
# Test trained agent
obs, info = env.reset()
for _ in range(100):
action, _states = model.predict(obs)
obs, reward, terminated, truncated, info = env.step(action)
if terminated or truncated:
obs, info = env.reset()
env.close()
Environment Details
Observation Space
The environments provide dict observations containing:
frog_trace: FROG trace as uint8 image (shape:(1, window_size, window_size))psi: Current control parameters in [0, 1] range (shape:(3,))action: Last applied action (shape:(3,))
Additional for RandomFROGLaserEnv:
B_integral: Current B-integral value (shape:(1,))compressor_GDD: Current compressor GDD parameter (shape:(1,))
Action Space
3-dimensional continuous action space representing changes to:
- GDD (Group Delay Dispersion)
- TOD (Third Order Dispersion)
- FOD (Fourth Order Dispersion)
Actions are in the range [-1, 1] and are applied as deltas to current control parameters.
Reward Function
The reward function combines multiple components:
- Intensity component: Encourages higher pulse intensity
- Duration component: Penalizes longer pulse durations
- Alive bonus: Reward for not terminating episode
Testing
This project includes a comprehensive test suite covering:
- Environment interface compliance with Gymnasium
- Physics calculations accuracy
- Domain randomization functionality
- Environment semantics and behavior
Running Tests
# Run all tests
python run_tests.py --all
# Run specific test categories
python run_tests.py --unit # Unit tests only
python run_tests.py --integration # Integration tests only
python run_tests.py --physics # Physics tests only
python run_tests.py --fast # Fast tests only
# Run with coverage
python run_tests.py --coverage
# Run linting and formatting checks
python run_tests.py --lint --format
Test Structure
tests/
├── __init__.py
├── conftest.py # Test fixtures
├── test_env_interface.py # Gymnasium interface tests
├── test_env_semantics.py # Environment behavior tests
├── test_physics_calculations.py # Physics accuracy tests
└── test_domain_randomization.py # Domain randomization tests
Development
Setting up development environment
git clone https://github.com/fracapuano/gym-laser.git
cd gym-laser
pip install -e ".[dev]"
Code Quality
The project uses several tools to maintain code quality:
- pytest: Testing framework
- black: Code formatting
- isort: Import sorting
- flake8: Linting
- mypy: Type checking
- bandit: Security checking
CI/CD Pipeline
The project includes a comprehensive GitHub Actions CI/CD pipeline that:
- Tests on Python 3.8, 3.9, 3.10, and 3.11
- Runs all test categories
- Checks code quality and security
- Generates coverage reports
- Tests installation and basic functionality
- Includes performance benchmarks
Physics Background
The environments simulate ultrashort laser pulse propagation and control using:
- FROG (Frequency-Resolved Optical Gating): For pulse characterization
- Dispersion control: Via GDD, TOD, and FOD parameters
- Non-linear effects: Modeled through B-integral
- Transform-limited pulses: As optimization targets
Citation
If you use this environment in your research, please cite:
@software{gym_laser,
title={Gym-Laser: A Reinforcement Learning Environment for Laser Pulse Optimization},
author={Francesco Capuano},
year={2024},
url={https://github.com/fracapuano/gym-laser}
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. Make sure to:
- Add tests for new functionality
- Update documentation as needed
- Follow the existing code style
- Ensure all CI checks pass
Support
If you encounter any issues or have questions, please open an issue on GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file gym_laser-0.1.0.tar.gz.
File metadata
- Download URL: gym_laser-0.1.0.tar.gz
- Upload date:
- Size: 114.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ca294cf4239bd3190764e7b42f619321e65707a10b7cdce1f6f765ccbd4311c
|
|
| MD5 |
c1a78f56fa79f6d4aa43da264027621f
|
|
| BLAKE2b-256 |
2265a9cae5e2cf214dd5239e9fc03ed45a0234c429f3739b22c3ad9140407834
|
File details
Details for the file gym_laser-0.1.0-py3-none-any.whl.
File metadata
- Download URL: gym_laser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 116.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6297e9e87d54fb72313857e545f18a9be32c12536995c74fc9cd0bd5eade629c
|
|
| MD5 |
9cd4e59adb28865a55a21b153d8f58e0
|
|
| BLAKE2b-256 |
ecf0bf117e4711c00c0ca692961c11a83ec59f616fd6af6bd8de48d34cf830c9
|