Pages

Showing posts with label Optimisation. Show all posts
Showing posts with label Optimisation. Show all posts

Saturday, 16 July 2011

Perceptron in Actionscript

I've always thought neural networks and the idea of computational learning was pretty cool but until now I'd never actually tried to program one. I decided to start out with something simple so I went for the single layer perceptron with the hope of building it up to something a bit smarter.

For those of you who don't know much about neural networks Wikipedia has done a pretty decent job in teaching me everything I know. All the project files are available here and sample source code is provided below. It is a simple solution to the NAND problem using two inputs.

The perceptron class essentially has two functions, train and evaluate. The train function is called 100 times on the training set which adjusts the respective weights on each input. When evaluate is called on a particular inputs an output is produced. The output trace from the below program looks like this:
Initiating new perceptron:
Training 100 times using training set...
Trained
Evaluating NAND[0,0] : 1
Evaluating NAND[1,0] : 1
Evaluating NAND[0,1] : 1
Evaluating NAND[1,1] : 0

This is a fast solution to these kinds of problems. I plan on implementing optimisations such as exiting training when the weights change by less than some threshold (i.e the solution limit has been reached), but the end goal is to produce a full library with many kinds of next step. I think the next of these will be a multilayer perceptron which initial research suggests is much more complicated! Enjoy the classes and let me know if you do anything interesting with them.

package
{
import flash.display.Sprite;
import org.experimentalized.networks.SingleLayerPerceptron;
import org.experimentalized.networks.objects.Trainer;
import org.experimentalized.networks.objects.TrainerSet;
public class Main extends Sprite
{
public function Main()
{
//NAND example
trace("Initiating new perceptron:");
var perceptron:SingleLayerPerceptron = new SingleLayerPerceptron(0.1);
var training_set:TrainerSet = new TrainerSet([ new Trainer([0,0],1),
new Trainer([1,0],1),
new Trainer([0,1],1),
new Trainer([1,1],0)
]);
trace("Training 100 times using training set...");
perceptron.TrainSet(training_set,100);
trace("Trained");
//Test some evaluations
trace("Evaluating NAND[0,0] :",perceptron.Evaluate([1,0]));
trace("Evaluating NAND[1,0] :",perceptron.Evaluate([1,0]));
trace("Evaluating NAND[0,1] :",perceptron.Evaluate([0,1]));
trace("Evaluating NAND[1,1] :",perceptron.Evaluate([1,1]));
}
}
}


Friday, 26 March 2010

Travelling Salesman Problem

I have been extremely busy over the last few weeks with various University projects, so the genetic algorithm work has moved to the bottom of the stack. The ideas and problems are still quite fresh in my mind, so without going straight into programming the physics based evolving creatures, I'm going to have a look at something else quite closely related. It is linked in to both evolution and optimization. There is a very old mathematical problem, known as the travelling salesman problem (TSP). The TSP involves finding the shortest route between a number of cities, visiting each city only once.

Finding fast solutions to the problem is vital for applications such as route finding on maps. Anyway, I recently read an interesting paper one approach to solving the TSP ( Ant colonies for the traveling salesman problem ). The paper shows that using ant agents, which lay virtual pheremones, a shortest path can be found. Ants prefer to follow paths with more pheremones on them - this means that shorter paths will build up more pheremones because more ants will travel along them.

When I get back to my computer I will have a look at creating a similar solution to the problem in flash, and see the kinds of results that can be produced.

It might also be interesting to look at the effects of changing the properties of ants on how well the system optimizes itself.

I'll get back to you with any results!