{"id":374,"date":"2019-03-01T18:49:23","date_gmt":"2019-03-01T18:49:23","guid":{"rendered":"https:\/\/tutorials.retopall.com\/?p=374"},"modified":"2019-03-24T21:23:35","modified_gmt":"2019-03-24T21:23:35","slug":"autonomous-driving-simulation","status":"publish","type":"post","link":"https:\/\/tutorials.retopall.com\/index.php\/2019\/03\/01\/autonomous-driving-simulation\/","title":{"rendered":"Autonomous Driving Tutorial"},"content":{"rendered":"\n<p>A Self Driving car simulation will be made in this post. You will understand how Genetic Algorithms and Neural Networks work in a practical way. The environment will be a 3D terrain with a track where the cars should learn how to drive.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/m8fYPy9eiOo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"> <strong>INTRODUCTION<\/strong><\/h4>\n\n\n\n<p>In this tutorials you will learn how to create a self driving simulator where the cars learn to drive using Darwin&#8217;s law of genetic<\/p>\n\n\n\n<p>The project is created in Unity 3D using C# as programming language. You will learn how to design the map in Unity and the algorithms we implement. Also you will be able to program this algorithms in C#<\/p>\n\n\n\n<p>The project can be divided in different aspects we will must focus on.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Neural Network: we will be creating a multilayer neural network<\/li><li>Genetic Algorithm with its respectives crossover and mutation functions<\/li><li>Visual simulator in 3D with Unity: create your track<\/li><li>Lasers: input data of the neural network<\/li><li>Car movement: output actions of the network<\/li><li>Camera: viewing the simulation in an intelligent way<\/li><li>Controller: handles all the executions and the algorithms<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>UNDERSTANDING THE PROJECT<\/strong><\/h4>\n\n\n\n<p>This video explains how all the components work. There are explained the Neural Network, Genetic Algorithm, Camera and Cars.<br>I explain the neccessary maths and algorithms for the simulator<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/iHMnqi_gbPo?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">NEURAL NETWORK<\/h4>\n\n\n\n<p>Neural Networks are computing systems that are used to relate different stimulus with different actions or possible solutions. A neural network is organized in layers and each layer will have a certain amount of neurons. This neurons will be connected to others with weights. Every neuron and weights will store a value. The weight values will be static when the network is being updated in our project. However, the neurons values will be changing during the simulator because they will depend on the input layer and the weights of the network.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tutorials.retopall.com\/wp-content\/uploads\/2019\/03\/NeuralNetwork-2.png\" alt=\"\" class=\"wp-image-375\" width=\"238\" height=\"235\" srcset=\"https:\/\/tutorials.retopall.com\/wp-content\/uploads\/2019\/03\/NeuralNetwork-2.png 376w, https:\/\/tutorials.retopall.com\/wp-content\/uploads\/2019\/03\/NeuralNetwork-2-300x297.png 300w\" sizes=\"auto, (max-width: 238px) 100vw, 238px\" \/><\/figure><\/div>\n\n\n\n<p>In out project each car will have its own neural network and it will be updated every time. This updated algorithm is called feed-forward. The cars will have as the input layer the distances to the walls that surround the cars. With this inputs and the weights we will get some ouputs that would be the rotation and the acceleration.<\/p>\n\n\n\n<p>We will need to put this into practice and create a class in C# called Neural Network that will contain as variables the List of float[][] of weights and the List of neurons organized in layers. It will have two contructors. One for the first generation of cars that will be random weights and the second for assigning the DNA to the weights. It will also have the feed-forward algorithm implemented. You can learn how to program the Neural Network in the next tutorial:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/6BqXj2ymj6Q?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>For a deep explanation of neural networks see <a href=\"https:\/\/tutorials.retopall.com\/index.php\/2019\/02\/17\/neural-networks\/\">Neural Networks<\/a> and <a href=\"https:\/\/tutorials.retopall.com\/index.php\/2019\/03\/01\/feed-forward-neural-network\/\">Feed-Forward Neural Networks<\/a><\/p>\n\n\n\n<p><strong>GENETIC ALGORITHM<\/strong><\/p>\n\n\n\n<p>Instead of using backpropagation for the Neural Network to learn, we will be using reinforcement learning. We will use the Genetic Algorithm applied to every cars. As the Darwin&#8217;s law says the entity (car) with the best genes will survive and it will propagate the genes to the next generation. They will evolve in a better way and survive more time. <\/p>\n\n\n\n<p>If we want to express this matematically for out cars we should then start by creating for each car is own list of genes. There will be as much genes as the quantity of weights in the neural network of each car. When we create the array we will start by creating it in a random way in the first generation.&nbsp;<br>We can divide the Genetic Algorithm in different sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Random Initialization of DNAs<\/li><li>Selection of best cars depending on the fitness function<\/li><li>Crossover of the genes of the parents<\/li><li>Mutation the new DNA<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>INITIALIZATION<\/strong><\/li><\/ul>\n\n\n\n<p>The initialization of the DNAs in a random way will be executed only in the first generation when the Neural Networks of the cars are created. We will create the DNA as a List of length the amount of weights in Neural Network. We will create genes in the interval of [-1,1]<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.retopall.com\/tutorials\/images\/initialize.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>We will create different random DNAs for each car. Then we will assign this DNA to the weights of the Neural Networks of the cars.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>SELECTION<\/strong><\/li><\/ul>\n\n\n\n<p>When the simulator finished because all the cars have collided with a wall, we should choose which have the best genes. You can do this in a lot of different ways. Depending on the distance traveled, the average speed or the time driving. I choosed for this project the time of the car driving, so we will ge the genes for the first and second car that survive more time in the track.&nbsp;<br>All this factors could be joined to obtain a better selection of parents. It can depend also on the diversity we want to achieve.&nbsp;<br>We could create a function that depends on the factors mentioned before in order to get a specific score for each car called&nbsp;<strong>fitness<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.retopall.com\/tutorials\/images\/selection.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>CROSSOVER<\/strong><\/li><\/ul>\n\n\n\n<p>With the parents selected we will swap the genes to create a new DNA with a mix of both parents prefering random fragments from one parent and the other. This random value could be modificated depending if the parents have very different fitness or very similar. As they a similar fitness score they will have more similar probability to be chosen the genes.<\/p>\n\n\n\n<p>You can see here and example of the genes being manipulated:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.retopall.com\/tutorials\/images\/genetic.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>MUTATION<\/strong><\/li><\/ul>\n\n\n\n<p>When we create for each new car for the new generation the mixture between both parents we will create different mutations. This is used because the parents are the best from the generation but not the best driving cars. We must create small mutations in the DNA of each new car to get better cars. <\/p>\n\n\n\n<p>Fo more information about genetic algorithms see <a href=\"https:\/\/tutorials.retopall.com\/index.php\/2019\/03\/01\/genetic-algorithm\/\">Genetic Algorithms<\/a><\/p>\n\n\n\n<p>The tutorial of how to program the genetic algorithm and implement the genes in code is the next video:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/videoseries?list=PLVxq4HUq7dJM9HmxUCcrv3mrFjay_vVd9\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>VISUAL 3D SIMULATOR<\/strong><\/h4>\n\n\n\n<p>Additionaly from the algorithms, we should create all the mesh from the cars, terrain, effects, track and camera.<\/p>\n\n\n\n<p>Each car must be able to move and rotate. Each car will have its own lasers that raycast the distances to the track walls and output a value from 0 to 1.<\/p>\n\n\n\n<p>The camera will behave as a drone that follows one car and will have a smooth movement when changing to other car. It will rotate the cars it is looking to<\/p>\n\n\n\n<p>The terrain, track and effects can be choose by the creator. Nevertheless I have created a video explaining one simple terrain with a track and water:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/videoseries?list=PLVxq4HUq7dJM9HmxUCcrv3mrFjay_vVd9\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>LASERS<\/strong><\/h4>\n\n\n\n<p>The neural networks must have some input data to work and to handle the car movement and rotation. In this case we will store the distances of different rays that raycast to the walls of the track. It will have some parameters such us the angle of view and the number of laser sensors.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/11qGOkvdUu4?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CAR MOVEMENT<\/strong><\/h4>\n\n\n\n<p>The outputs of the neural network will be the rotation and acceleration. We will need to update the car position and rotation depending on this values and the time passed in simulation. The car will have an Accelerated Linear Uniform Movement.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/dWAw_7r7Uo0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">CAMERA<\/h4>\n\n\n\n<p>The camera will automatically move from one car to another. Depending on the cars that haven&#8217;t collided yet the camera will follow one of them. When a car collides and the camera is following it, it will change to another car in a fluent way. Also the camera will rotate slowly around the car.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/Zh7uVVsNEhg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>CONTROLLER<\/strong><\/h4>\n\n\n\n<p>We will finally need to join and interact with all the stuff created before. We will have a class called Controller that will be in charge of controlling every object and algorithm in the simulator.<\/p>\n\n\n\n<p>The lasers will be the inputs of the Neural Network and will be assigned to the input layer of each car with its own lasers. Then we will need to apply the feed-forward algorithm and get the outputs of the Neural Network. These outputs will be the rotation and acceleration of each car that will be used in the physics of the car. This sequence will be updated every time for every car.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/www.retopall.com\/tutorials\/images\/neuralNetwork.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>If it&#8217;s the first generation the DNA of every car will be random. In the next generations the weights will be adjusted by the genetic algorithm.<\/p>\n\n\n\n<p>The next video will be the Car Controller that is the final video where you will be finished all the code and fix some bugs for the project to work. I will also explain how the parameters work and the Car Controller will be coded.<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" width=\"860\" height=\"484\" src=\"https:\/\/www.youtube.com\/embed\/N55co3td6DQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>If you like the projects you can give a comment and if you have some questions tell it to me.<\/p>\n\n\n\n<p>The code is available here: <a href=\"https:\/\/github.com\/dDevTech\/Self-Driving-Car-Tutorial \">Github Repository<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Self Driving car simulation will be made in this post. You will understand how Genetic Algorithms and Neural Networks work in a practical way. [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":42,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,8,5,46,4,10,6,7],"tags":[78,77,80,79,76],"class_list":["post-374","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-c","category-explanation","category-neural-networks","category-programming","category-simulation","category-tutorial","category-unity-3d","tag-3d-environment","tag-3d-track","tag-ai-controller","tag-car-ai","tag-self-driving-car"],"_links":{"self":[{"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/posts\/374","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/comments?post=374"}],"version-history":[{"count":7,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/posts\/374\/revisions\/525"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/media\/42"}],"wp:attachment":[{"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/media?parent=374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/categories?post=374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorials.retopall.com\/index.php\/wp-json\/wp\/v2\/tags?post=374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}