**DEEP 2015 - Bio-inspired Robotics - Under construction!**

Declare yourself!

Objectives:

Make an integer, a decimal and a string variable.

Display the values of the variables.

Add, subtract, multiply, divide and square them, and display the resulting values.

Pick an equation, like the Pythagorean Theorem, and calculate and display its value. You can use any numbers for the variables, as long as you calculate the value of an equation.

Create variables A = 5, B = 10. Operator on A and B using less than (<), greater than (>), not equal to (~=) and equal to (==), and make sure the answers make sense (Remember: 0 is FALSE, and 1 is TRUE).

Array your forces

Objectives:

Make two 1D arrays, A and B, with 6 elements. You pick the values.

Change the first and fifth elements in array A to 0.

Change elements 2 through 4 in array A to 0, using the ':' selector.

Make a 2D array, C, with 2 rows and 2 columns. You pick the values.

Change the (1,1) and (2,2) elements in array C to 0.

Change the entire second row of array C to 0.

Create the variable D as the entire first row of C.

Add, subtract and multiply the two arrays, A and B.

In array B, set all elements from the 3rd to the 6th to 0, using the 'end' selector.

Make an array E with the values [2, 4, 5, 6, 5, 3]. Calculate and display E < 5, E > 5, E == 5, and E ~= 5. What do the results mean?

Functions on arrays

Objectives:

Make a 2D array, A, with three rows and two columns. Call the

size

function on A (that is, execute the command

size(A)

). What does the answer mean?

Find the square root of each member of array A.

Make two more arrays the same size as A (call them B and C). All the elements of B should be one, and all the elements of C should be zero (use the

ones

and

zeros

functions).

Using

rand

, make a 3x2 array called D that is full of random decimal numbers between 0 and 1.

Using

randi

, make another 3x2 array called E that is full of random integers between 1 and 10.

BONUS: Make a 3x2 array with random decimal values between -2 and 2.

Using the

find

function, find all the elements of E that are greater than 0.5.

Using the

max

and

min

functions, find the maximum and minimum values of E.

I can function!

Objectives:

Make a variable A = 25.25, and use the

sqrt

function to find the square root.

Make variables B = 25.79, and C=25.25, and try these functions on them:

round

,

ceil

,

floor

. What do you suppose these functions are doing?

Try the commands

min(B,C)

and

max(B,C)

. What happens? What are these functions doing?

I can function - more!

Objectives:

Write a function that multiplies two numbers.

Write a function that adds two arrays.

Pick a complicated mathematical equation and write it in a function.

Write a function that displays your name.

Write a function that takes two 2D points (use two arrays, A and B, that have two values in each, representing the X and Y values), and calculates the distance between them.

BONUS: Write a function that opens a figure and plots the points.

Get the point? See the angle?

Objectives:

Using graph paper, pick an origin O, and plot the points A = (5,5) and B = (6,0).

Measure angle between OA and OB. Is it what you expected?

Calculate the distances from the points A and B to the origin, and measure to make sure you're right.

Calculate the distance from point A to point B, and measure to make sure you're right.

BONUS: Starting with a clean graph, draw a line 30 degrees counterclockwise off of the X-axis. Make the line have a length of 5cm, and put a point (C) at the end of the line. What are the coordinates of C?

Plotting and drawing

Objectives:

Make two points A = (5,5) and B = (6,0) using 2D arrays (with two values each), open a figure, and plot them using the

plot

function. You can plot the origin (0,0) as well.

Draw a line between A and B. Save the 'handle' to the line, so you can delete it (erase it) easily. Remember to use the

hold on

command to allow you to draw without erasing what's there!

Ok, you didn't REALLY want that line. Delete it.

Clear the figure using the

clf

command.

CHALLENGE! - Virtual DNA

Create two virtual DNA segments, with 12 'nucleotides'. DNA is made up of four types of molecules (nucleotides), chained together: A, C, T, and G. We'll use two arrays, A and B, with a size of 12 elements each, to represent the DNA segment. Instead of A, T, C, G, we will use 1, 2, 3, 4. You can set the array values yourself, or make them random (using

randi

).

Write a function

count_nucleotide

that takes in a nucleotide number (either 1, 2, 3 or 4), along with a DNA segment array (for example, A or B), and tells you how many times the nucleotide shows up in the array.

Write a function

crossover

that takes two segments (i.e., the two virtual DNA segments arrays A and B) and gives back two crossed-over segments (two more arrays, C and D, that are the same size as A and B), where the crossover happens between the 7th and 8th position. The picture at the bottom shows what is happening.

Now make that same function cross over at any location (the function takes in two segments - i.e., the two arrays - AND the crossover point).

BIG BONUS: Write a function that takes in a DNA segment array, plus another string, and returns the first place where that string is found in the segment. So, e.g., you give the function the array [1 2 3 4 5 6 7], and the string '34', and it returns the number 3 (since the '34' starts at the 3rd position of '1234567'). You may need to use the

num2str

and the

findstr

functions that are included with Matlab.

BONUS Challenge - What's your vector Victor?

Write a bunch of functions; each takes two 2D vectors (as two arrays, A and B), and a point (as a third array, P). The functions will:

1. Calculate and return the length of each vector.

2. Add A and B together, and give back the length of the sum.

3. Give back the angle between A and B.

4. Give back the angle between the vector A, and the vector from the origin to the point P.

5. BONUS: Write a function that takes X, Y and a radius R, and draws a circle. The function should give back all the handles that are created (e.g., by the

plot

function).

4. BIG BONUS: Write a function,

draw_robot

, that takes X, Y, a radius R, and a pointing angle (call it 'ang'), and draws a robot as a circle at (X,Y) with radius R, AND with a line pointing in the direction of the point angle. Clear as mud? I also put a picture below:

Vectors

Objectives:

On graph paper, draw the vectors A = (5, 5) and B = (6, 0), and draw the sum of the two (call it C).

Measure the angle between the two vectors A and B.

BONUS: Instead of expressing the angle in degrees, express it in radians (a councilor or instructor can explain how).

BIG BONUS: Calculate the angle between the two vectors, if you know how. If you don't know, and want to know, feel free to ask (or Google it).

A 'for'-gone conclusion

Objectives:

Make a 1D array of random numbers, A, with 50 elements. Use a 'for' loop to set all the elements to 1.

Make a 50x50 array of random numbers, B, and use two 'for' loops, one inside the other (this is called

nested loops

), to set all the elements to 0.

Create a drawing figure. Make a 50x3 random array, startPoints, where all the elements in the first two columns are between 0.3 and 0.6 (these are X and Y values), and all of the elements in the last column are between 0 and 6.28 (this is an angle). Write a function called

plot_robot

that takes the startPoints array and plots all of the X and Y points in the array (the first two columns) to the figure. Remember to use the hold on command at the beginning to plot multiple points.

BONUS: Instead of just plotting the points, draw a circle at each robot position. You can use the code you wrote before.

BIG BONUS: Include the 'pointing line' that indicates the robot pointing.

'While' thing, you make my heart sing

Objectives:

Write a script that asks for a number from the user, and displays the square of that number, until the user enters the character 'x'. The Matlab function

input

can be used to read a number (run

help input

command for more info).

Write a script that plays a guessing game with two players. The program generates a random number between 1 and 10, and then asks for Player 1 and Player 2 to guess the number. It displays the secret number, and the two player guesses, and stops playing whenever someone enters an 'x' when prompted to guess.

Making decisions, saving lives

Objectives:

Write a function called

is_in_range

, which takes two numbers (the minimum and maximum of a range of numbers), and a third number, and returns 1 (TRUE) if the third number is between the first two, and 0 (FALSE) otherwise.

Write a function,

get_relation

, that relates two numbers with either the '==', '>', or '<' sign. E.g.:

Write a function called

hard_limiter

that takes an input array and an interval (e.g., [min,max]). It gives back the array, but it sets any elements of the array that are greater than the max to the maximum, and any elements less than min to the minimum.

CHALLENGE! - Mutating Genomes

You need to write a function that will act like mutation on a particular collection of genes in a segment of DNA (the segment is called a

genome

). Mutation is a random process that acts on genetic material like DNA, and occurs due to various different factors. We'll model mutation by assuming there is a small chance that each gene in the genome can be changed a little bit.

In our model, the genome is just a 1-dimensional array of decimal numbers (kind of like our DNA segments were before, expect now the numbers can be decimals, and not just 1, 2, 3 or 4). So, in this model, mutation is a function that will act on a genome (a decimal array), by occasionally changing particular genes (individual elements of the array) by a little bit (the mutation).

So, our function will have the following definition:

outGenome = mutateGenome (inGenome, probMutate, MAX_MUT, MIN_MUT, MAX_GENE_VALUE, MIN_GENE_VALUE)

The outGenome will be the mutated genome,the inGenome is the one that will be mutated, and the probMutate is the probability of mutation (all probabilities are just decimal numbers between 0 and 1). MAX_MUT and MIN_MUT are the maximum and minimum values that the mutation can be, and the MAX_GENE_VALUE and MIN_GENE_VALUE are the maximum and minimum possible value of an individual gene, respectively.

The function will visit each element of the input genome (inGenome). For each element of inGenome, it will generate a random number between 0 and 1. If the random number is less than probMutate, then the current element under consideration will be mutated.

If the element (gene) is going to be mutated, there is another additional small chance (10% chance) that the entire element (gene) will just be completely replaced with a new gene - this is done by setting the gene to a random value between MIN_GENE_VALUE and MAX_GENE_VALUE. Otherwise, a small mutation is generated as a random number between MIN_MUT and MAX_MUT, and this value is added to the gene value.

SO....since this challenge is a bit more involved piece of code, it's a good idea to write pseudocode for it.

Pseudocode

is kind of halfway between English and computer code. It is VERY useful for organizing how the program should be written. The pseudocode for this function might look like this:

for each element of inGenome

a = rand

if a is less than the probMutate then

b = rand

if b is less than 0.1 then

Set the current element to a random value between MIN_GENE_VALUE and MAX_GENE_VALUE

else

Add a random number between MIN_MUT and MAX_MUT to the current element

endif

endif

endfor

Given this pseudocode, and making use of stuff that you've written so far, write the

mutateGenome

function. We will be using this later.

PS: Feel free to ask about probabilities if that seems a bit confusing to you.

Ladies and gentleman, start your bots

Objectives:

Connect an e-puck to your PC, and activate it.

Read the speed, odometer, and proximity sensors. How are the values passed to you?

Set the speed to 100 on both wheels. What happens? Set the speed back to 0.

Set the speed to 100 on one wheel. Now what happens? Set the speed back to 0.

Hold a finger near one of the proximity sensors, and read the values. What do you notice? What is the maximum value of the sensor, and how far is your finger at maximum value? What is the minimum value, and how far away is your finger at the minimum value? This type of activity is called

calibrating the sensor

.

**Day one slides:**

https://www.dropbox.com/s/6txi8gpxet4oyw1/DEEP2015_Day%201_rev15.pdf?dl=0

https://www.dropbox.com/s/6txi8gpxet4oyw1/DEEP2015_Day%201_rev15.pdf?dl=0

CHALLENGE! - I am the very model of a modern...biological network?

A network is an interconnected system (think: the Internet). Networks are EVERYWHERE in biology - they underlie food webs, brains, communication links, social interactions, and so on. Arguably, every system is a network of some degree, since you can always imagine a part of them as being connected to other systems, or other parts. For example, below is a rather complex network. It's actually a network of 4,671 feeding interactions among 68 parasite organisms (in blue) and 117 non-parasitic organisms in the food web of Estero de Punta Banda, Baja California, Mexico. Don't worry, we're not going to work with networks that are this hairy.

(Taken from: http://phys.org/news/2013-06-parasites-food-web-theory.html#jCp)

There are entire fields of study dedicated to understanding networks in general. We won't go into networks in great detail, but we need to understand them a bit in order to work with them.

A much more simple network is shown below.

The bottom row of 'nodes' (the circles in the network diagram) are connected to the top two nodes. The connections each have a 'weight' associated with them. These weights are shown in the array under the network, the red weights correspond to the red connections, from left to right in the diagram and the array, and the blue weight correspond to the blue connections, also left to right. So for example, the weight of the second red connection from the left is -1.2. Each node has an 'activity level' (think of it as the 'energy' of the node), and this is represented by a number inside the node. For example, the top two nodes have activities of 0.21 and 1.15, from left to right.

By now you've noticed that the bottom row of nodes don't have activity level in them (they just have variables h1 through to h6). That's because you're going to calculate them. To calculate them by hand (your first task), you add the input levels coming into each of the h-nodes from each of the two nodes in the layer above. So:

Input into h1 = (weight of connected node coming in) * (activity level of connected node) + (weight of next connected node coming in) * (its activity level) + .... (and so on)

Your tasks are twofold:

1. Compute all the values h1 to h6, by hand.

2. Write a function that takes in a 6-element weight array, and a 2-element input array, and gives back the 6-element array of h values. Test it with the numbers in the diagram above and confirm that it matches your hand-calculated values.

We will be using this function later, so don't lose it.

Roomba

Objectives:

Write a script that drives your robot in a straight line until it detects an object with the proximity sensors, then turn in a random direction, and continue driving. Keep driving until you've gone some distance (you can use the odometer to get the distance travelled). You will need a while loop to keep checking the proximity sensors. Your pseudocode might be:

reset odometer

while (odometer reading is less than maximum)

drive forward (set equal speed on both wheels)

read proximity sensors

if (something detected by proximity sensors)

turn robot (right wheel only for random time)

endif

endwhile

set speed to zero on both wheels to stop

Playing robot games

Objectives:

Start the epuck interface in Matlab (instructor or councilor will show you how). This allows you to see all the sensors and set values easily (I know, you're angry I didn't show you this first, but then you wouldn't have had all that fun doing it by hand!)

Play around a bit by observing the various sensors, and setting wheel speeds. CAREFUL! Ensure the epuck does not drive off of a desk!

If you open the controller.m file in the ePic212 directory, you can very easily write your Roomba code into it. This file runs constantly and controls the epuck (it's already put together for you).

Starting at line 90 in the controller.m file, there is a section that runs over and over again, every 0.1s, when the controller file is activated. This is where you put your controller code. When you activate the controller in the GUI interface, those lines will be executed every 0.1s.

Download the following file to start using epuck robots. You can unzip it anywhere.

https://www.dropbox.com/s/2ys42cvubki1uuo/epic212_DEEP.zip?dl=0

**Day two slides:**

https://www.dropbox.com/s/yohpzwcd029a03k/DEEP2015_Day%202_rev2.pdf?dl=0

https://www.dropbox.com/s/yohpzwcd029a03k/DEEP2015_Day%202_rev2.pdf?dl=0

CHALLENGE! - No one can be told what the Matrix is; you have to see it for yourself.

This challenge gets you started on one of the three main pieces required to evolve a robot brain: The simulator.

The simulator is a virtual world that you create in the computer that provides a place for virtual robots to play. This will make more sense later, but for now, think of this as a computer-generated 2D world where our robots will repeatedly try to execute some task. In this case, the robots are going to move around the world and try to get close to an object located at (0,0).

So you will write a script that starts a virtual robot at a starting position and orientation, and moves the robot around the virtual world for 100 timesteps based on 100 values of the left and right wheel speeds. When the robot is done moving, you'll calculate the distance from the robot to the object at (0,0). That is, you'll code up the following pseudocode:

create a variable to hold the current robot position and angle (a 3x1 array, for x, y and angle)

generate a random array of 50 start positions (x and y) and pointing angle (ang) (did this on day one)

for each starting position and angle

generate a random array of 100 left and right wheel speeds, between 0 and 1

set the current robot position to the current start position

for 100 timesteps (i.e., 100 iterations)

set current robot position = moveRobotStep(current position, current wheel speeds)

endfor

measure and store the distance from the robot to the point (0,0) for this starting position

endfor

You should end up with an array of 50 final distances, one for each of the 50 starting positions. This is not much use now, but later we will put a brain in the robot, and it will start to learn how to move around and make its way to the object at the centre of the map.

BONUS: If you wish, and have time, you can open a figure window and draw the robot path as it moves around the world.

Download the following file to use the moveRobotStep function.

https://www.dropbox.com/s/imyvmmpkefqlntn/moveRobotStep.m?dl=0

**Shared drive:**

https://www.dropbox.com/sh/26ytjy78gb0y989/AABPqxOq9Ci5eBOLUnxdQxq1a?dl=0

https://www.dropbox.com/sh/26ytjy78gb0y989/AABPqxOq9Ci5eBOLUnxdQxq1a?dl=0

CHALLENGE! - Traveling Salesman Problem

The Traveling Salesman Problem is an old combinatorial optimization problem (this means that it involves selecting the best solution from a finite set of solutions). Although these problems sound easy, the

solution set

or

solution space

(that is, the set or space of possible solutions) is usually incredibly large. The gist of the problem is this: You're a traveling salesman that has to visit a number of cities (say, M) located at a specified set of points. For example, the image below shows the point locations of 29 cities in Western Sahara (from www.math.uwaterloo.ca/tsp/world/wipoints.html):

These problems appear all over the place, from laying out transistors on VLSI chips, to operations research and schedule management, to route-planning in internet mapping programs. One way to come up with solutions to this problem is to use a genetic algorithm. This challenge will look at how to do that.

First you need a genetic representation of the problem. An easy one to use is just the 1x29 array of city indexes. The indexes are from 1 to 29, and point to a particular row in the city coordinate list. You can get the Matlab mat-file in the shared folder. Download the 29cities.mat file, copy it to your working directory, and just drag it into the Matlab command window. The 'cities' variable will show up in the workspace. It is a 29x2 array of city coordinates (x and y values). If you plot them, it will look like the plot above.

So, we've chosen the 'genome' for this problem to be the index of the cities in the order they are being visited. For a particular genome, we can calculate the total distance traveled, and we can use this for our fitness. The smaller the distance traveled, the better the fitness.

Have a look at gaTSP.m, where this program is implemented. See if you can follow what the script is doing. Try running the script with different data sets - start with the 5 points in a circle (circlePoints_5.mat) and play with settings. What do you notice when you change the population size and probability of mutation? Do some settings work better than others? Try the circlePoints.mat data set after (10 points), then try the 22cities.mat dataset.

Experiment with the settings in the file as before, to see if you can obtain the optimum shortest path. It looks somewhat like the figure below, and has a path length of 27603.

BONUS: Modify the file so you can select an arrangement of points with the mouse, and run the TSP GA on the points you click in a figure window. You can use the ginput function to return the coordinates of points that you click in a figure window. Or, as a big bonus, see if you can write the file to allow you to add points while it's running, so that the algorithm will accept the new points and find a new short path.

Gauss' GA Practice

Objectives:

Create an array of x values from -10 to 10.

Plot the function gaussian.m with the x values you just created. This is called a Gaussian function.

Let's use a simple GA to find the peak of the Guassian function. Our population will be a collection of x values. Have a look at gaGaussian.m and see if you can follow what the code is doing (you should look back at a standard GA in the slides).

Experiment with the settings for mutation probability (probMut), mutation amplitude (mutAmplitude), number of generations (numGenerations) and population size (popSize). What do you notice? Does the algorithm find the peak easier in each case, or does it have more difficulty? Why?

Surfing the peaks

Objectives:

Now, have a look at gaPeaks.m and see if you can follow what the code is doing (you should look back at a standard GA in the slides). This script implements a GA for finding the peaks of another function (the 'peaks' function, which is in 2D and has a few bumps).

Again, experiment with the settings for mutation probability (probMut), mutation amplitude (mutAmplitude), number of generations (numGenerations) and population size (popSize). What do you notice? Does the algorithm find the peak easier in each case, or does it have more difficulty? Why?

CHALLENGE! - The Robot Simulator

A robot simulator for the epuck robots has been written, and can be found in the runSimulations_template.m.

Open the file and have a look at the steps involved. This file would be used to run virtual robots through their simulated task - to move towards the object located at (0,0) in the simulator.

For now, you can ignore the line that evaluates the neural network. We'll discuss that later. There are a few things that you should think about:

1. How do we calculate the distance from the light?

2. What things would we want to involve in the fitness function? What things would we consider to be a sign that the robot has been successful?

Save a copy of the simulator file, and, using code you may have already written (the functions that draw the robot, and the functions that generate random starting points and wheel angles), see if you can hack together a simulator that will run the robots from your random starting locations/orientation, using generated (possibly random) wheel speeds, while drawing the position at each timestep. Try out some different fitness functions to see if you get values that make sense.

CHALLENGE! - 0/1 Knapsack Problem

The 0/1 Knapsack Problem is another well-studied combinatorial optimization problem.

Imagine you're a burglar, and you've stumbled onto a stash. There are a bunch of objects of varying value (or benefit), and varying volume. You have a knapsack that can only hold up to a maximum volume of objects, and you want to maximize your take without going over the volume.

Let's look at a specific example: There are 22 items. To help you get a feel for the number of possibilities, open and run the file called KPGUI22.fig file in the shared folder. Use the buttons to select items, and keep an eye on your current volume. Try to grab the most total value, while keeping the volume under 400. When you think you've done the best you can, write down the solution as an array of ones and zeros, from left to right, and top to bottom across the items (1 if the item is selected, 0 otherwise). Also note the total volume and the total value.

Now close KPGUI22 and run the gaKP.m file. This will run through a GA that finds solutions to the exact same 22-item knapsack problem. How does it do? How did you do?

Have a look in the gaKP.m file, and examine the part where the fitnesses are calculated (the function calculateKPFitness.m). I have written a rather complex expression that boils down to this:

This is an example of how fitness functions are constructed. In this case, we select based on high fitness, so higher value gives us higher fitness. However, we need to penalize any situations where the volume goes over the maximum, and we do that by including an adjustable penalty to reduce the fitness for genomes where the volume is over the maximum.

CHALLENGE! - If I only had a brain...

In this challenge, you'll write the code to evaluate the outputs of a 25-node simple neural network, which will go in your robot. You've already done a small version of it. In the past exercise you calculated the inputs to 12 hidden layer neurons. A neural network is very similar to this, but in addition to an input layer (with input nodes), and a hidden layer (the second layer in the previous exercise), it also has an output layer which contains the output nodes. In our case, there are two of these, for each of the left and right wheel speeds. So the network looks like this:

The top two nodes are the input nodes. The left one is the input from a single proximity sensor. The other input node is called a 'bias' node, which always has a value of 1.

The second layer is called the hidden layer - there can be many hidden layers with varying numbers of neurons. Notice there is a bias node here is well (there are no connections from the inputs to it). It also has a value of 1.

Finally, there are two output nodes, for the left and right wheel speeds.

Your mission, should you choose to accept it, is to write the function:

o = evaluateNeuralNetwork(weightArray, x)

which takes in the array of weights for all 102 connections (that is, it is a 1x102 array), and the single binary input x (where x = 1 means the sensor sees an object, and 0 means it does not), and produces the 2x1 array of wheel speeds, o.

Remember that the procedure for calculation, as discussed in class, is:

1. Compute the inputs to the hidden neurons by adding up the weights for each connection times the activation of the connected input nodes.

2. Once you have all the input levels for the hidden neurons, calculate the each hidden neuron activation level by putting the neuron input levels through the 'activation' function.

3. Compute the inputs to the output neurons by adding up the weights for each connection times the activation level of the connected hidden neurons.

4. Finally, compute the output neuron activation level by putting the output neuron input level through the activation function.