RILYBOT 4: Linear Regression  

This description covers just one motor and the light sensor. The same technique is used for the other inputs and outputs.

During training mode the robot continually reads the motor speed and the light level. Each reading generates two values, and these values can be treated as coordinates of a point on a two-dimensional graph. After a few seconds of collecting data, such a graph might look like this:

light | . | . ' | . ' | : ' . | .. | ' .' | ' | . . '. | . ' | +--------------------------- motor

In this example the motor speed goes down as the light goes up. The set of data can be approximated by a straight line, and the common technique called linear regression is used to compute the coefficients of a polynomial that gives the output value as a function of the input value. The line is selected so as to minimize the chi-squared measure, which is the sum of the squares of the errors in the output values that would result from using the line as a substitute for the real data.

The chi-squared linear regression method can compute the coefficients of the line from the following five sums:

- the number of data points n
- the sum of the motor readings Mi
- the sum of the light readings Li
- the sum of the squares of the light readings Li2
- the sum of the products of the two readings Li.Mi

These five sums are all that is required, no matter how many data points are taken. This means that it is not necessary to store multiple data values during training mode. The only concern that limits the number of data points that can be taken is the maximum value of the sum.

Unfortunately, in NQC on the RCX, variables are limited to the range [-32768, 32767]. I could have implemented some multi-precision math (shift and add is all you need) to compute intermediate results and get an acceptable answer, but I decided it wasn't worth the effort.


Robert Munafo's home pages on HostMDS   © 1996-2017 Robert P. Munafo.
aboutcontact    mrob    mrob27    @mrob_27    mrob27
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Details here.

This page was written in the "embarrassingly readable" markup language RHTF, and was last updated on 2017 Feb 02. s.11