| ||||||||||||||
| ||||||||||||||
|
||||||||||||||
|
Simple neural network as robot brain
Hardware: None
Software: Visual Basic Date: 1999 This page gives a short introduction to the design of neural networks and how to use them as a robot brain, as used in some projects on this website. Let's look at a simple neural network consisting of only two inputs and two outputs.
Since this is a website on robotics we'll call the inputs 'sensors' and the outputs 'motors'. The relationship between the sensors and the motors can be described in the following table (where +1 means on and -1 means off). If a sensor is on it means the switch is activated or pressed. Sensor1 sensor2 motor1 motor2 +1 +1 -1 -1 +1 -1 +1 -1 -1 +1 -1 +1 -1 -1 +1 +1 This is of course not a random table. Imagine the sensors are on the front of a robot vehicle, and the motors drive this robot. If both sensors are pressed the robot has made a frontal collision and should stop both motors. If the left front sensor is pressed the right motor has to stop (thus the robot turns right), similar for the right front sensor. If none of the sensors is pressed the motors should run. Of course this table is simple enough to be programmed into the robot, but for the sake of this discussion we'll put it into a neural network. We will treat each line in the table as a vector (yes, you should have paid attention in math). This vector will have the format: Input (sensor 1) Input (sensor 2) Output (motor 1) Output (motor 2) So: +1 +1 -1 -1 A vector can be seen as a 'pathway' in the neural brain. The brain itself is a matrix (a mathematical representation of all possible pathways between inputs, outputs and neurons), so in order to turn a single vector into a matrix we'll have to multiply it with itself. For the first line of the table this yields:
+1 +1 -1 -1
+1 +1 +1 -1 -1
+1 +1 +1 -1 -1
-1 -1 -1 +1 +1
-1 -1 -1 +1 +1
The brain should now have a 'pathway' which leads from 'both sensors pressed' to the solution 'stop both motors'. In order to check this we first have to turn our question into a vector again +1 The outputs in this vector are question marks, because that's the answer we'd like the neural brain to give us. To get this answer we'll multiply the brain matrix with the question vector. Ignore any calculation which has the question mark in it. So the first number of the resulting vector would be: (1 x 1) + (1 x 1) + (-1 x ?) + (-1 x ?) = (1 x 1) + (1 x 1) = 2. For the total matrix this would look like this: +1 +1 -1 -1 +1 +2 +1 +1 -1 -1 +1 +2 -1 -1 +1 +1 x ? = -2 -1 -1 +1 +1 ? -2 Now these results need to be normalised, which means anything larger than zero becomes +1 and anything smaller than zero becomes -1. This results in: +1 +1 -1 -1 And if we compare this result with the vector layout (so we remember what is what): Input (sensor 1) +1 Input (sensor 2) +1 Output (motor 1) -1 Output (motor 2) -1 we can see that the neural brain just told us to turn off the motors (both get -1 as answer), which is correct. So the neural brain has correctly remembered that is both sensors are activated both motors should stop. Quite remarkable for such a simple matrix. The fact that the calculated inputs (+1/+1) match the inputs from the question means that this is a valid ansers as far as the neural network is concerned. If this wouldn't match we know we've come across something the brain hasn't learned yet! We could do the same thing for the second line of the table, which would result in: +1 -1 +1 -1 -1 +1 -1 +1 +1 -1 +1 -1 -1 +1 -1 +1 This new matrix can be added to our original neural brain by simply adding the numbers together, which results in:
2 0 0 -2
0 2 -2 0
0 -2 2 0
-2 0 0 2
By doing this the original brain has in fact 'learned' a new question and answer (if the left sensor is pushed and the right sensor is not, then the left motor should run and the left motor should not). You can keep on doing that for each line in the table and eventually you'll end up with a finished neural brain which can answer all questions you could possibly ask regarding the two sensors. The completed brain by the way looks like this:
4 0 0 -4
0 4 -4 0
0 -4 4 0
-4 0 0 4
A lot of people use a reduced size matrix (in this case a 2x2 rather than a 4x4). I prefer the large size because it's much easier to work with and it allows you to find the question for an answer. If for example you wonder what you would have to to to get both motors running simple use the vector (? ? 1 1) as input and you'll get the right answer. Here's a small program program (Visual Basic source) to help you test this neural brain. In this explanation we have started with a table, and added all questions and answers to the neural brain. This could be done because we know all questions, and perhaps more important, we know all answers. In real life the questions may not be so obvious, and the answers unknown. In that case you would start with an empty neural brain (consisting of all zero's). Now there are basically two ways for this empty brain to learn: 1. You give the answers. Every time the sensors reach a state the brain has never seen before it stops and asks you to give the correct answer for that given sensor state. For example both inputs would be off and the neural brain asks you what to do in a case like that. You tell it to switch on both motors, and the brain adds this information (if both sensors are off both motors must be on) as a vector to it's knowlegde. This is called training the neural brain and it can be done if you know all the answers (for example when you train the brain to recognize the letters of the alphabet (for the sake of argument we'll assume that you can read)). This is the fastest way to teach a neural brain, and it can be compared with a child going to school. 2. The brain figures out the answers on it's own. Every time the sensors reach a state the brain has never seen before it selects a random answer, and tries that. It it proves to be succesfull the question/answer combination is added to the brain as a vector. If it is not succesfull it will randomly select another answer and repeat that procedure untill it finds a correct one. This can be done if you can provide a way of checking wether the answer selected is actually correct. This is more difficult than you might expect because very often it is not easy for a brain to tell wether the answer is right or not, and if it guesses wrong there may not be a second chance (such as when your robot decides to drive out of the 10th floor window). This is the slowest way of learning something, but the advantage is that it can be done unsupervised. You can compare this with giving a box of Lego's to a child and wait to see what it does with it. It depends on the situation which method you select.
Hardware: Cybermaster
Software: Visual Basic Date: 1999 Until now you had to actually program your robots, but this is no longer necessary. This robot, if left alone, will program itself! If you want to understand how the neural brain of this robot works I suggest you read the page on neural networks first. Ofcourse you don't need to understand it, simply put it on the floor and see what happens. The bumper design still poses me difficulties, it is possible for the robot to get stuck (even though it is performing the right actions) simply because the bumpers are stuck.
(picture of the neural robot) This robot is equipped with a self learning neural network. The network consists of two neurons, two inputs (sensors) and two outputs (motors). Due to the way the brain matrix is set up the motors and sensors act as neurons themselves too, so if you want to be precise the network has 6 neurons, hence the 16 values in the matrix. It is however called a two neuron network because there are two 'units' that act as neurons only. From a control point of view the connections are:
(virtual connections) In reality each unit is connected to each other unit. The neural network is actually in the PC because programming it in the Cybermaster may very well be possible but I thought that was much to much work. Besides if you want to expand the limited intelligence of this particular model you'll have to use the power of the PC anyway. The problem with a self learning robot is ofcourse that it has to learn something. As soon as an unknown input combination is encountered the brain doesn't know what to do. So it starts to generate random solutions (random on/off states for the motors). Each one is tried, and if it is succesfull the current motor status is added to the brain as solution (so in fact: learned). If the input combination is known (it has learned this before) it can of course take the appropriate action immediately. The flow of the program is as follows:
(program flow) The difficulty lies in determining wether a randomly chosen solution is correct. I have chosen to use the following rule for this: if within 2 seconds after trying the solution the input status changes the solution is correct. This means that the specific motor on/off combination managed to change the position of the robot enough to change the number of bumper switches pressed. In program code:
'Advise user a solution could not be found
Form1.Label23.Caption = "Unknown bumper status"
Succes = False
Do
'Generate a random solution for the two
'motors
sol1 = Rnd(1)
sol2 = Rnd(1)
If sol1 > 0.5 Then v3 = 1 Else v3 = -1
If sol2 > 0.5 Then v4 = 1 Else v4 = -1
'Try this solution
Form1.Label23.Caption = "Testing possible answer."
If v3 = 1 Then
motor0 = MaxSpeed
Form1.Shape5.FillColor = &HC0C0C0
Form1.Shape6.FillColor = &HFF00&
Else
motor0 = 0
Form1.Shape5.FillColor = &HFF&
Form1.Shape6.FillColor = &HC0C0C0
End If
If v4 = 1 Then
motor1 = MaxSpeed
Form1.Shape7.FillColor = &HC0C0C0
Form1.Shape8.FillColor = &HFF00&
Else
motor1 = 0
Form1.Shape7.FillColor = &HFF&
Form1.Shape8.FillColor = &HC0C0C0
End If
Form1.Spirit1.Drive motor0, motor1
'Now figure out wether this is succesfull by
'waiting for a given time so see if the
'bumber status has improved (eg less switches
'activated). The timeloop routine fails at
'midnight rollover by the way, couldn't be
'bothered to make a real one.
StartTime = Timer
Do
'Check the bumper status
Call CHECKBUMPERS(nv1, nv2)
'Give windows some time to refresh the
'screen otherwise you won't see anything
Form1.Refresh
If nv1 + nv2 < NumAct Then
'This is succesvol, add to the
'neural brain
Form1.Label23.Caption = "Problem solved."
Call VECTORtimesVECTOR
Call MATRIXaddMATRIC
Call DISPLAYMATRIX
Succes = True
End If
'Repeat until 2 seconds have passed (should
'be enough time for the robot to get itself
'out of trouble, if not modify this time
'period) or until as solution has been
'found.
Loop Until (Timer - StartTime > 2) Or (Succes = True)
'repeat the loop (the random search for a correct
'answer) until one is found
Loop Until Succes = True
This strategy doesn't always yield the result that you might expect, although you'll usually get something that works. Try this yourself and see that the robot can learn some strange behaviour this way. Of course you can also hold the robot in your hand, press the bumpers one by one and release once the brain has found the proper solution. That'll go fast, as is normal in 'supervised training'. And here's a screenshot of the program in action:
(screenshot of the program in action, robot driving straight ahead) The program is written in Visual Basic, you can download the source by clicking here. The .zip file includes a saved neural brain which is the desired end result. In the source you'll want to adapt the load/save routines to point to your own directories. As you can see I didn't make the actual robot very complex. The only thing you need to take care of is that it moves slowly enough, and doesn't have the strength of the catarpillar tracks (which will simply drive over anything or, if that fails, push everything out of the way, which is a solution also but not the one we're looking for). If you want to know how I built it go to the next page for the details. The November 1999 issue of KIJK (a Dutch magazine on popular science) ran an article on Lego Robots.
It wasn't very in-depth but they did mention the robot decribed above:
Advanced neural network
Hardware: None
Software: Quick Basic Date: 1999
TheoryOn the previous page you've seen an example of a very simple neural brain. The basic theory however was covered. Now we'll move on to something with a lot more power (relatively speaking): a network with 25 neurons! In real neural networks it's rare for inputs and outputs to be connected directly to the network. Normally speaking a design such as this is more appropriate:
SensorThis can be any type of sensor such as external ones (limit switch, light sensor) or internal ones (tacho, motor power etc). Pre processingIn this step the sensor data are modified to fit into an input vector. This usually means a conversion from one type of data to another (eq from raw measurement to real resistance) or a treshold limit (eq a resistance over 250 means 'on') or a fit to a different range.. Input vectorThis is a list of inputs for the neural network. From a math point of view neural nets are matrixes, thus the inputs are called vectors. If you need less than the complete vector (meaning you have less sensor points than actual points in the vector) playing around with the location of the data in relation to the unused vector points may be important to improve the network results. Very often it is better to separate the 'real data' as much as possible to prevent one datapoint from influencing the others. Neural brainThis is where the real knowledge is stored. It consists of neural cells which are interconnected. Each cell is connected to the other cells. A (mathematical) matrix is used to describe these connections, the values indicate the relative strength. Apart from that each cell has a treshold which determines when it will fire, and thus pass a signal to the next cell. Output vectorThis is a list of outputs from the neural network, the result of the multiplication of the input vector with the brain matrix, taking for example individual cell treshold limits into account. Please note that not all points in this vector are relevant, that depends on the traing data set. Action functionUsually the neural network is not capable to yield results which are directly usable, so a separate function is required to translate the output vector to a specific reaction. This can for example be to combine a number of data points of the output vector into the speed of a motor. MotorsThis can be any kind of output ofcourse, but since we built robots the outputs are usually motors. How to design a neural brainThe advanced brain featured on this page is based on a neural network demonstration program written by C.J. Matheus and C.C. Jorgensen in 1986 in GWBasic. It's difficult to get a hold of this program, so in order to give proper credit to them you can download it here. It'll run in Qbasic (the free basic that comes with W95 and DOS) and it's really worthwhile to study it. Designing and using neural brains is an art and not a science. Even though the brain itself is fixed (you 'get' a 25 node brain) getting the proper data configuration and variable settings is still difficult and requires trail and error (do play around with the software to get a feel for this). The following instructions describe the process, and a working example is given at the end.
Light seeking robot exampleLet's look at a real example: a light seeking robot with a neural brain (ofcourse there's a much simpler way to get a behaviour like that but that makes it such an easy to understand example). We'll follow the steps above one by one:
Light seeker: an advanced neural robot
Hardware: Cybermaster
Software: Visual Basic Date: 1999
(screenshot of the application) This is the end result of the example that was discussed on the 'advanced neural brain' page. At this moment the robot is facing the light with only one eye (light sensor) so one motor has been switched off to allow it to turn. The screen shows all stages of the signal, from sensor to motor. Prior to loading the training file some neural network parameters can be set. Don't forget to select a training file (with extension .ful) before pressing the 'load data and start program' button. You can download the source in Visual Basic here. This zipfile also contains a trining file for this application. With this example it should be possible for you to design your own neural brain robot. If you have an interesting design using this (or another) neural brain I'd be happy to hear from you, because I can't find too many people who actively interested in this field. The robot itself is very simple (I'm not very mechanically inclined as you may have noticed):
(frontal view of the robot, note the two home made sensors on top)
(home made light sensor) Neural Robot PicturesHere are some pictures to help you recreate the neural robot:
(the completed model) Note the geared down big wheels in the back. They provide enough traction to drive the robot over the carpet (even at the lowest speed) but not enough to push things out of the way.
(pivot wheel) This wheel allows the robot to turn. The negative end result is a very low ground clearance. If I had to built this robot again (but I never do a project twice) I'd make it drive backwards in order to move the bumpers to the turning point.
(geared down powered wheels) This shows the gears on the backwheels, the picture below shows the other side.
(inside view of the powered wheels)
(bumper, right hand side) These are the bumpers. It's the best model so far, but still not satisfactory. I'm beginning to think that mechanical bumpers are not the way to go, perhaps optical (or ultrasonic) bumpers are better.
(bumper, left hand side)
Submitted: 06/02/2005 Article content copyright © Bert van Dam, 2005.
|
|
|||||||||||||
All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -