Skip to content

Kminek42/Line_Follower_Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Line Follower Simulation

I created this simulation to train a recursive neural network controlling a line-following robot. All elements in this simulation operate as vector operations, allowing the use of optimizations from the numpy library.

How to run

Before you start:

run pip3 install -r requirements.txt command in terminal to instll all necessary libraries.

Track

The track.py file generates the track procedurally. One can run python3 track.py to see different results. The Track class has a distance_to_chain method that returns the distance between any point and the track. Screenshot 2024-04-03 at 08 26 29

Robot

The robot.py file simulates a robot with two wheels. The Robot class is initialized with the length and wheelbase as a numpy array, allowing testing of many different variants simultaneously. In this presentation, the robot dimensions are constant, and the algorithm only determines the neural network weights. One can use the move(motor_l, motor_r) method to set the target speed, which will be achieved with a specified acceleration to simulate the robot's inertia. The robot also has a get_sensors method that returns readings from all sensors (using distance to the track). image

Learning

Learning is done using a genetic algorithm that adjusts the neural network weights. The quality indicator of the genotype is the average speed at which the robot moved along the track. Turning around, turning at intersections, or leaving the track results in setting the average speed to 0. The mutation rate gradually increases when the next generation is worse than the previous one. After training using the train.py file, One can see progress by running python3 plot_evolution.py BestScore.csv AverageScore.csv MutationRate.csv. image

An example of learning outcome. Despite preserving two individuals from the previous generation, sometimes the result of the next generation worsens. This is due to the added noise to the readings, which introduces some randomness into the simulation.

The learn.py file also creates a genotype.txt file, which can be pasted into the main.py file. One can run the main.py file to see the simulation with the trained NN model.

image

About

Training RNN to control the line follower

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published