## Potential Fields

In addition to the code you will need to communicate with your tank and to determine what it should do (potential fields), you will also need code to visualize your potential fields (later we will also need this to visualize other things). Use gnuplot for this visualization.

See http://aml.cs.byu.edu/~kseppi/cs470sp07files/fields.png for an example. The blue boxes are obstacles.

The Python script fields.py might be helpful as a starting point when you are trying to plot potential fields. The script should make it clear how to write commands that can be given to Gnuplot to get it to draw obstacles and potential fields. Note that you can either write the commands to a file, which can be made into an image later, or it can be displayed on the screen live.

Here is a part of a gnuplot file so that you can see how they are constructed:

# This part sets up the context
set xrange [-400.0: 400.0]
set yrange [-400.0: 400.0]
unset key
set size square

# Draw Obstacles: just a series of 4 line segments for each of the 2 rectangular obstacles
unset arrow
set arrow from 0, 0 to -150, 0 nohead lt 3
set arrow from -150, 0 to -150, -50 nohead lt 3
set arrow from -150, -50 to 0, -50 nohead lt 3
set arrow from 0, -50 to 0, 0 nohead lt 3
set arrow from 200, 100 to 200, 330 nohead lt 3
set arrow from 200, 330 to 300, 330 nohead lt 3
set arrow from 300, 330 to 300, 100 nohead lt 3
set arrow from 300, 100 to 200, 100 nohead lt 3

# A few field lines (see the link below for the complete file)
-381.65625 -381.65625 -4.6875 -4.6875
-381.452830189 -349.66509434 -5.09433962264 -4.66981132075
-381.233606557 -317.694672131 -5.53278688525 -4.6106557377
-381.0 -285.75 -6.0 -4.5
381.0 285.75 6.0 4.5
381.233606557 317.694672131 5.53278688525 4.6106557377
381.452830189 349.66509434 5.09433962264 4.66981132075
381.65625 381.65625 4.6875 4.6875
e

The complete file can be seen at: fields.gpi. This file was created using the python fields script referenced above, and using the following commands:

python fields.py
gnuplot -persist fields.gpi

If you want gnuplot to make a .png image file (like, to include in your report) for you just run “gnuplot” and do the following:

gnuplot> set term png
Terminal type set to 'png'
Options are 'nocrop medium size 640,480 '
gnuplot> set output 'fields.png'
gnuplot> load 'fields.gpi'

If you look at fields.py carefully you will see that it can also do animation, so you can display your fields in real-time. For the potential fields lab, animation is nice, but not required. For the search lab it will be required.

In general it is a good idea to get visualization working early because it will help you in debugging and in understanding what your agents are trying to do.

## Multivariate Normal

For the Kalman lab, you will need to show multivariate normals. The following sample file shows how this should be done. Feel free to play around with the palette and isosamples settings. The complete file can be seen at kalman.gpi and the image is available at kalman.png. Note that the code given below uses sigma-x, sigma-y and rho (where rho ($\rho$) is the correlation between $X$ and $Y$) to represent the covariance matrix which we called $\Sigma$ in class. Remember that $\Sigma$ is symmetric so we really only need three values to represent it. Thus any 2-d $\Sigma$ can be represented by sigma-x, sigma-y and $\rho$ as shown here:

:$\Sigma = \begin{bmatrix} \sigma_x^2 & \rho \sigma_x \sigma_y \rho \sigma_x \sigma_y & \sigma_y^2 \end{bmatrix}.$

and as explained on wikipedia

set xrange [-400.0: 400.0]
set yrange [-400.0: 400.0]
set pm3d
set view map
unset key
set size square

unset arrow
set arrow from 0, 0 to -150, 0 nohead front lt 3
set arrow from -150, 0 to -150, -50 nohead front lt 3
set arrow from -150, -50 to 0, -50 nohead front lt 3
set arrow from 0, -50 to 0, 0 nohead front lt 3
set arrow from 200, 100 to 200, 330 nohead front lt 3
set arrow from 200, 330 to 300, 330 nohead front lt 3
set arrow from 300, 330 to 300, 100 nohead front lt 3
set arrow from 300, 100 to 200, 100 nohead front lt 3

set palette model RGB functions 1-gray, 1-gray, 1-gray
set isosamples 100

sigma_x = 70
sigma_y = 100
rho = 0.3
splot 1.0/(2.0 * pi * sigma_x * sigma_y * sqrt(1 - rho**2) ) \
* exp(-1.0/2.0 * (x**2 / sigma_x**2 + y**2 / sigma_y**2 \
- 2.0*rho*x*y/(sigma_x*sigma_y) ) ) with pm3d