This project develops a reinforcement learning (RL) agent that navigates intersections by choosing turn actions (left, right, straight, back) while following a chosen rule set. The agent uses a Deep Q-Network (DQN) and a simulated environment backed by the Google Maps API (geocoding, directions, nearby places).
-
State Representation: Location, direction, and nearby streets are encoded into a fixed-size state vector used for decisions.
-
RL Environment: The environment produces observations from the agent’s location, assigns rewards for rule-compliant turns, and terminates after a set number of intersections.
-
Model: A DQN with a target network approximates Q-values; the agent acts via an epsilon-greedy policy.
-
Training: Experience replay and periodic target updates are used for stability.
-
Evaluation: The trained model is evaluated on test episodes (success rate, average time to destination).
-
Demo: A script lets you pick a rule (right-only, left-only, alternate) and run a driving simulation with the trained model.
-
Clone the repository:
git clone <repository-url> cd inter-sim-rl
-
Install dependencies:
pip install -r requirements.txt
Or install the package in editable mode with dev dependencies:
pip install -e ".[dev]" -
Set the Google Maps API key (required for training and demo). Replace
<key>with your key:export API_KEY=<key>
On Windows (PowerShell):
$env:API_KEY = "<key>"
You can also set
GOOGLE_MAPS_API_KEYinstead ofAPI_KEY.
-
API key: Set the
API_KEYorGOOGLE_MAPS_API_KEYenvironment variable. The app raises an error at startup if the key is missing or invalid. -
Paths and hyperparameters: Defaults are in
inter_sim_rl.config.Config. You can override them in code or by extending the config (e.g. model save directory, number of simulations, epsilon, batch size, gamma, target update frequency). Model and output directories default tomodels/andoutput/under the project root. -
Reproducibility: Set
Config.seedto an integer before training to fix random seeds (Python, NumPy, TensorFlow) for reproducible runs.
-
Demo (default): Run the script and choose a rule set; then a trained model is loaded and a driving simulation is run. You must have trained a model once (see below) and set the API key.
python main.py
-
Training and evaluation: To train the DQN and then run evaluation, use:
python main.py --train
This runs the full training loop, saves the model under
models/, and then runs the test evaluation. Ensure the API key is set.
-
Run tests:
pytest tests -v
-
Lint:
ruff check inter_sim_rl main.py tests
-
Format:
ruff format inter_sim_rl main.py tests
-
Install dev dependencies:
pip install -e ".[dev]"installs pytest and ruff.
-
State:
StateRepresentationholds location, direction, nearby streets, and optional address/instruction;get_state_vector()returns a fixed-size vector. -
Environment:
RLEnvironmentinitializes from a starting address, steps via actions (using the Maps API for transitions), and computes rewards from the chosen rule (right, left, or alternate). -
Agent:
DQNModelprovides the main and target networks; the training loop inmain.pycollects transitions, samples batches, and updates the DQN with Bellman targets. -
Demo: The
simulate_driving_*functions ininter_sim_rl.driving_directionsuse the same state vector format and action space as training so the saved model can be used for inference.
For component-level details, see the docstrings in inter_sim_rl (e.g. config.py, state_representation.py, rl_environment.py, dqn_model.py, driving_directions.py).
This project is licensed under the MIT License. See the LICENSE file for details.
- Fork the repository.
- Create a new branch.
- Make your changes and commit them.
- Push to your fork and submit a pull request.
- Google Maps API
- TensorFlow
If you have any questions, feedback, or issues, feel free to contact us.
