William Sayers

2009
Milestone 3

05025397

Genetic Algorithms and Neural Networks

Genetic Algorithms and Neural Networks
William Sayers (05025397)
Supervised by: Colin W. Morris (BSc, MSc)
Backpropagation neural networks are usually trained using some iterative method derived from a mathematical analysis of the operation of the network. This technique is time consuming and requires understanding of the mathematical principles involved. This project will investigate the application of genetic algorithms to replace the "normal" training of the network. This involves setting up some candidate networks and then picking the best of these. The best of the population are then combined in some way to produce a new population of candidate networks. This procedure is continued until a satisfactory network is obtained.

William Keith Paul Sayers225 Page 1 of Faculty of Advanced Technology 22/04/2009

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

Contents
Contents ............................................................................................................................ 2 1 - Introduction ............................................................................................................... 10 2 - Research .................................................................................................................... 10 2.1 - Neural Networks ................................................................................................. 10 2.1.1 - The Biological Neuron................................................................................. 10 Figure 1 - A human neuron ............................................................................................ 10 2.1.2 - The Artificial Neuron................................................................................... 11 Figure 2 - An artificial neuron ........................................................................................ 12 Equation 1 - Summation of the inputs and weights ........................................................ 12 Figure 3 - The sigmoid function shape ........................................................................... 13 2.2 - The History of Neural Networks ........................................................................ 13 2.3 - Advantages and Disadvantages of Neural Networks ......................................... 14 2.3.1 - Advantages................................................................................................... 14 2.3.2 - Disadvantages .............................................................................................. 15 2.4 - Current Applications of Neural Networks .......................................................... 17 2.4.1 - Neural Networks in Medicine ...................................................................... 17 2.4.2 - Neural Networks in Business ....................................................................... 18 2.4.3 - Object Trajectories....................................................................................... 19 2.4.4 - Robot Control .............................................................................................. 19 2.5 - Back-propagation ............................................................................................... 20 2.5.1 - Back-propagation overview ......................................................................... 20 Page 2 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3 2.5.2 - Back-propagation in depth ........................................................................... 20 2.5.3 - Back-propagation library ............................................................................. 22 2.6 - The FANN neural network library (Nissen, FANN) .......................................... 22 2.7 - Genetic Algorithms ............................................................................................ 22 2.7.1 - History of Genetic Algorithms .................................................................... 23 2.7.2 - Advantages and Disadvantages of Genetic Algorithms .............................. 24 Figure 4 - local optimum (units are undefined in this case). .......................................... 27 2.8 - Training Neural Networks with Genetic Algorithms ......................................... 28 2.8.1 - Determining Weight Values with Genetic Algorithms................................ 28 2.8.2 - Representation of a Neural Network within a Genetic Algrithm ................ 28 2.8.3 - Using Genetic Algorithms to Determine Neural Network Structure ........... 29 Figure 5 - A feed-forward neural network ..................................................................... 30 2.9 - Cascade Correlation............................................................................................ 32 2.10 - C# User Interface Programming ....................................................................... 37 2.11 - Writing C++/CLI Wrappers for Unmanaged C++ code................................... 37 2.12 - Application of Research ................................................................................... 38 3 - Design ....................................................................................................................... 38 3.1 - Program Requirements ....................................................................................... 39 3.2 - Design of the Class Structure ............................................................................. 39 3.3 - Linking C# code to managed dll’s...................................................................... 40 3.4 - Design of the User Interface ............................................................................... 40 Page 3 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3 3.4.1 - Main Form ................................................................................................... 40 Figure 9 – Network and output tab design (Main form) ................................................ 41 Figure 10 - Mean squared error tab design (Main form) ................................................ 42 Figure 11 - Dataset tab design (Main form) ................................................................... 42 3.4.2 - New Network Wizard .................................................................................. 43 Figure 12 - Design of the training algorithm tab (Wizard form) .................................... 43 Figure 13 - Design of the network settings tab (Wizard form) ...................................... 44 Figure 14 - Dataset selection tab design (Wizard form)................................................. 44 3.4.3 - Working Form ............................................................................................. 44 Figure 15 - Working form design (Working form) ........................................................ 45 3.4.4 - About Form .................................................................................................. 45 Figure 16 - About form design (About form) ................................................................. 45 Figure 17 - Dataset display design (About form) ........................................................... 46 Figure 18 - Licenses display design (About form) ......................................................... 46 4 - Implementation ......................................................................................................... 46 4.1 - User Interface Implementation ........................................................................... 47 4.1.1 - Main Form ................................................................................................... 47 Figure 19 - Network and Output tab............................................................................... 47 Figure 20 - Mean squared error graph tab ..................................................................... 48 Figure 21 - The dataset tab ............................................................................................. 49 4.1.2 - New Neural Network Wizard ...................................................................... 50 Figure 22 - Learning Algorithm tab ............................................................................... 50 Figure 23 - Network Settings tab.................................................................................... 51

Page 4 of 225

.........Back-propagation ................ 54 Figure 28 ...3 ..Keeping the User Interface Active whilst Processing is Occurring.....................Cascade Correlation ....................................................... 58 5............................1.................. Licenses view ............Graphs .....4 ............. 51 4......About Form ............ 54 4....................Data Parser Implementation ......................................... 52 Figure 25 ..2 ..................................Dataset tab ............... 54 4....1 .......................................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 24 .The mean squared error at testing (XOR)..... 52 Figure 26 ........................................Working Form ............ 57 4.............................2 ............................About form..................... 60 5.......................................1.................................................1..Genetic Algorithm Implementation .5 ..........................................Number of iterations to achieve target (XOR) ................. 56 4........Mean squared error upon last training test (XOR) .......................................................................... 61 Figure 30 ...............1..........................................................................................................................................................................................................Passing Information between Forms ................... 53 Figure 27 ......... Datasets view ..........................................................1.Back-propagation and Cascade Correlation Implementation ................. 59 5........... main view ......... 56 4.................................................... 62 Page 5 of 225 ................................................................................................1...................................Neural Network Implementation .................................1 ..............................About form.................... 61 Figure 29 .................1............................XOR .....................................3 ...1 ........... 59 5.. 57 5 ..................................................Genetic Algorithms ... 55 4.................Testing Data .....4 ......... 61 Figure 31 ...................Working form ..........2.....1............................About form..................................4 ................................ 54 4......6 .......3 .................. 58 5...........................

.....Back-propagation and Genetic Algorithms ............................. 68 6..... 69 Page 6 of 225 ...................................................................................................................Cascade Correlation ......2 .4 ....................................3....................... 67 Figure 37 ........ 68 6..... 67 Figure 35 .................Number of iterations to achieve target (Viruses) ...............................................................................................................2 ......................XOR .... 64 Figure 34..Back-propagation ..........................The mean squared error at testing (Iris data) ...............................................................Fishers Iris data............. 68 6 .................................3....................... 63 5........ 69 6.....Comparisons ......................3 ...2..................... 68 6.................................... 66 5................................................Mean squared error at testing (Viruses)............ 69 6.......1...................................................3................................ 65 5............... 63 5........... 64 Figure 33..........................................................1...............Mean squared error upon last training test (Viruses)............1 ................Fishers Iris Data ....Virus Classification........................2 ..........................Mean squared error upon last training test (Iris data) ..2.........4 . 63 5.......................................................................... 66 5.............2.................Cascade Correlation and Genetic Algorithms .....................................Genetic Algorithms ....1 ..........................1.........................Number of iterations to achieve target (Iris data) ............ 65 5.......Cascade Correlation .........................................1 ...........Back-propagation ..................................Graphs ............. 66 5................. 67 Figure 36 ................ 63 5...................................3 ........2 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.....1 .......................3 ......2 ...............Virus Classification .............................................2..Graphs ..........Genetic Algorithms ...................................................................................................3.................................................3 ................................ 64 Figure 32 ....................................................................

Possible Improvements .... 204 9................................................ 70 7.......1.................2 .....................................................................................................................1 ......................Datasets...........................................................................................................................XOR . 70 6.....Source code .... 70 7 ......................................................................1 ....Model ..........1..............................2................................................................................................................................................ 174 9.DLL Wrapper for FANN functions ......................................................................................4 ....1 ................................. 209 9................. 74 9...................................1 .......................................................................... 73 9 ......................1.......................... 210 10 .................................Controller .................Evaluation ................................How many of the original objectives were achieved? .....Possible Training Algorithm Improvements ..........Appendices ............... 69 6.....................................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 6......... 69 6.................2 ....... 207 9.........................................Libraries Researched ..3 ...........................................3 ..................................................... 109 9.............................................1.........3 .....................Fishers Iris Data ..................... 211 11 ......................Testing conclusions ... 75 9..2 ................................................Bibliography............................... 71 8 .............Virus Classification....... 73 8................................................ 158 9. 72 8. 214 Page 7 of 225 ..............................................UML Class Diagram.................4 ...............................................................Possible User Interface Improvements ................................................................................................................2 ..............View ....................2.....................2.....5 ....................Works Cited ............................................Objectives ......................................................3 ................................ 74 9...........................1 .....

..... 219 12................................................3 .........................Diary.....................................................Tables .......................................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 12 ..........................Table of tables ......................................... 217 12...........1 .............................................2 ............Table of Figures ....................................................... 217 12.................................................................. 222 Page 8 of 225 ............................................................................. 220 13 ................Table of Equations ....

...... nor any part of it...................... ………………………………....William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 STATEMENT OF ORIGINALITY SCHOOL OF COMPUTING DEGREE SCHEME IN COMPUTING LEVEL THREE PROJECT This is to certify that......... except where specific reference is made...... ………………………………................ Student Name (Printed) Student Signature Registered Scheme of Study Date of Signing ……………………………….... and that neither this project......... Page 9 of 225 ........ Any material taken from published texts or computerised sources have been fully referenced.......................... ………………………………. the work described within this project is the result of the investigation carried out by myself.. and I fully realise the consequences of plagiarising any of these sources.... has been submitted in candidature for any other award other than this being presently studied............................

the report will investigate. compare and contrast three common learning algorithms that are applicable to neural networks. This report encompasses design. Neurons do not make decisions alone.1.Neural Networks Neural networks are networks of small-specialised units that sum the products of their inputs and the weights on those inputs. development and final prototyping of the program developed for this project.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 1 . 2.The Biological Neuron The human brain is composed of many millions of nerve cells called “Neurons”.Research 2. they can produce immensely complex results. on the course “Computer Games Development”. In addition.1 . Although they are simple in concept. Figure 1 . but in combination with a network of other neurons within the brain.A human neuron Page 10 of 225 .1 . 2 .Introduction The purpose of this report is to bring to a conclusion my final year project on neural networks and genetic algorithms.

2 . a number of weights associated with those inputs. 1. with extensions called “Dendrites” which are the neuronal inputs and a single extension called an “Axon” which is the output for that neuron. as mentioned above). the nucleus of the neuron cell processes them and produces an output. (Barber. a synaptic gap occurs between the axon branch and the synapse. Concerning inputs from the dendrites. 2006) 2. an activation function and a method for representing an output – either to another artificial neuron in the next layer. a summing function.The Artificial Neuron In order to construct an artificial neuron we need a way of representing the same model in data structures and algorithms. AI: Neural Network for Beginners pt. or to another part of the program. If the inputs from the dendrites to the neuron nucleus when summed are past a certain threshold. This action potential extends down the axon to the cells at the end of the axon branches (often synapses connecting to other neuronal cells. In the case of the axon branches connecting to synapses that are part of a second neuron. in order to allow the brain to control cells external to the brain. The usual method for approaching this is to have a number of inputs. the nucleus will generate an “action potential”. (Bourg & Seeman. The axon and dendrites both split before terminating into “Axon branches” and “Synapses”. Page 11 of 225 . in this gap between neurons chemical reactions take place that will inhibit or excite the signal. Axon branches can connect to other types of cells also. 2004). Axon branches connect to synapses to form the neural networks within the brain.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 A neuron is composed of a cell body.1.

or integer).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 2 . or one (activated) if the summed products are above a certain value. a differentiable function of some kind (such as a sigmoid function) which will output values between zero and one depending on the value of the inputs is another possibility. The activation function accepts this stored value. The product of the data item and its associated weighting is then stored before the summation of all the data items and associated weights occurs. Page 12 of 225 .An artificial neuron The system devised with these components is as follows. A specific input accepts a data item (usually a binary digit.Summation of the inputs and weights The next stage depends on the activation function chosen. ℎ = × ℎ Equation 1 . A stepping function can be used which will output zero (or non-activated) if the summed products are below a certain value. Alternatively. real number.

Frank Rosenblatt developed the perceptron model then. In this example “y” is the function output and “t” is the function input.8 0. 2004).The sigmoid function shape = 1 1+ Equation 2 . 2004). 2006) 2. 1. Page 13 of 225 . (Bourg & Seeman. McCulloch and Pitts formulated the first neural network model which featured digital neurons but the neurons had no capability to learn (McCulloch & Pitts. AI: Neural Network for Beginners pt.6 0.4 0.2 . 1958).The History of Neural Networks The foundation of neural network research are in psychology.2 1 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 1.2 0 -6 -4 -2 0 2 4 6 Figure 3 . contributed to the ideas that underpinned early neural network research. the work of 19th century psychologists such as Freud. in 1958. however he was unable to come up with a reliable mathematically accurate mechanism for allowing multilayer perceptrons to learn (Rosenblatt. (Barber. William James and others.A sigmoid function (Bourg & Seeman. 1943). as defined in the graph above.

of “n” nodes. when Werbos (Werbos.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Two layer perceptrons were the subject of several experiments to determine their usage and effectiveness. where “n” is an undefined number.3 . The individual neurons in a neural network have no time dependencies on each other and each can therefore be run on a separate processor (or as separate threads on a single processor) if desired without causing the problems often associated with such parallelism. A good example of this is that neural networks have been implemented to run on architecture originally designed for processing three dimensional computer graphics.Advantages Neural networks make possible or practical many things that with conventional solutions would be extremely difficult. (Blum. taking Page 14 of 225 . 2.3.Advantages and Disadvantages of Neural Networks 2. used on this architecture they become an extremely fast as well as flexible tool. The next major advancement in neural networks was not until 1974. Werbos discovered the algorithm whilst working on his doctoral thesis in statistics and called it “dynamic feedback”. 1982)). 1992).1 . Parker discovered it independently in 1982 whilst performing graduate work at Stanford University in the United States of America and called it “learning logic”. Back-propagation allows for the training of multilayer perceptrons and in particular. perceptrons with “n” hidden layers. 1974) discovered the back-propagation algorithm (independently rediscovered by Parker in 1982 (Parker. One of the benefits of neural networks when compared with other problem solving techniques in that they are inherently parallel and thus can run very effectively and efficiently on parallel hardware.

Table 1 . noisy data is utilised during training upon occasion. Neural networks can excel at determining what data in a particular set is relevant. where explaining why you have arrived at a particular diagnosis is an important part of the process. 5 Build a system that incorporates what we have learned.The statistical approach to deciding relevant data”. 2. in fact.Disadvantages One disadvantage to neural networks can be that it is very difficult for a human being to analyse a trained neural network and describe why it may come up with one answer over another. 3 Run the formulated model. Page 15 of 225 .The statistical approach to deciding relevant data Another advantage of neural networks is that noisy data is not a real problem for them to learn to interpret. 1 Decide on relevant data. 2001). This is a large disadvantage in areas such as medical diagnosis. We can simply present the neural network with all the data as opposed to following the circuitous and overly complex (by comparison) statistical approach demonstrated in “Table 1 . 4 Analyze the results.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 advantage of the massively parallel architecture of this hardware and the programmable sections of the graphics pipeline (vector and fragment shaders) to produce extremely fast neural networks (Davis.2 .3. as irrelevant data will simply end up weighted so that it has zero or close to zero actual effect on the solution produced by the network. to attempt to provide a more robust post-training network. 2 Formulate a statistical model. When using a neural network there's no need to establish before attempting problem solving which data is relevant.

Eventually the original data set was re-examined. requires largely heuristic construction and preparation. Fifty pictures of each set were set aside and the neural network trained with the remaining one hundred images with and without tanks. After the neural network’s training was complete. All the photographs with tanks were from a cloudy day and all the images without. although the length of time the problem would take to solve without employing a neural network must be taken into account when deciding how much of a disadvantage. they tested the network with the remaining fifty images that the neural network had not seen before and the neural network correctly identified most of the tanks. They collected one hundred images with a concealed tank. there is no formal methodology with which to choose the architecture. the network came up with apparently random answers. for obvious purposes. or even to verify the quality of the training. asked to differentiate between the two sets of pictures. a sunny day. When using neural networks is that. Page 16 of 225 . the US military wanted to use a neural network to analyse images and determine whether they contained a concealed tank. the network had trained itself to recognise whether the sky was cloudy or not. with and without tanks.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Once a neural network’s training is complete it is hard to tell why it is coming up with a particular solution. Therefore. A classic example is that in the 1980s. then a further one hundred images without. it can also be hard to tell how well (or how badly) trained a neural network is. The training times on neural networks can also be a disadvantage. train the neural network. only to find that on this round of testing. The US military then tested the neural network with a further set of one hundred images. A tool used to solve heuristic problems.

covering as many relevant situations as possible. One of the main research areas is using neural networks to recognise diseases/conditions via scans. the usual best approach is to simply feed them as much data as can be obtained.Current Applications of Neural Networks 2.1 . the eventual result will be. This drawback is however one that is generally universal to computing (and many other areas) often referred to as “GIGO” or “Garbage in. Page 17 of 225 . This is generalisation and it is far more useful than it at first appears and similar from a very high-level perspective to how the human brain and brains in general function. Testing may then take place with the “testing” set to determine accuracy with unseen data. The more time spent training the neural network on quality data sets.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Another possible criticism is that neural networks are extremely dependant on the quality and content of the original training data set. or it does exacerbate the problem with the time spent training the neural network. Data sets for training neural networks generally separate into two sub-sets.Neural Networks in Medicine There is a large amount of research now covering the applications of neural networks in medicine. in the opinion of the author. Neural networks make it more apparent however because (as in the previous US Military example) it can be difficult sometimes to decide what constitutes good data. “training” and “testing”. lies in their ability for generalisation. 2. The real strength of neural networks. the more accurate and in line with expectations and desires. Garbage out”. Fortunately since neural networks are (as has been mentioned above) good at ignoring irrelevant data. The network training first takes place using the appropriate “training” set.4 . this has to be done within reason however.4.

for example the human cardio-vascular system.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 This is particularly suitable work for neural networks because you do not need perfect examples of how to recognise the diseases and conditions. You can also use the technology to model parts of the human biological system in order to better understand it.2 . Credit scoring. research on using neural networks to diagnose diseases took place. A back-propagation neural net is integrated with the airline marketing tactician which monitored and recommended on booking for each flight. and assessing borrowers have all incorporated neural networks as an integral part of the system. Businesses have used neural networks in the past for applications such as assisting the marketing control of airline seat allocations in the AMT (Airline Marketing Tactician). merely a variety of scans covering all possible permutations of the disease or condition. thus supplying information more or less directly linked to the airlines main income. 1997) Page 18 of 225 . 1997) 2. In the 1980s. (Stergiou & Dimitrios. However. a clear problem with this approach is that when using a neural network it would be difficult to tell how a neural network reached its conclusion.Neural Networks in Business Neural networks. (Stergiou & Dimitrios. mortgage screening.4. can fit very well into most business situations. being good at analysing patterns and predicting future trends. The main reasons for using a neural network to model the human cardio-vascular system are that a neural network is capable of adjusting itself to be relevant to a particular individual and also to adapt by itself to changes in that individual.

(Marshall & Srikanth. Calculate the joint angles required for the arm to be in the appropriate position. a robotic hand or any other similar manipulator) in the appropriate position to grasp or otherwise manipulate an object or part of an object.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 2. When a robot arm is one hundred percent accurate that is a simple task.Object Trajectories Predicting the trajectory of an object in real time is another area where neural networks have been utilised. When controlling manipulators a neural net must deal with four major problems (Robot Control. 2008): • • • Determine the target coordinates relative to the robot. A neural network allows the robotic arms to be very flexible in their operation and perform self-adjustments as time goes by. compensating automatically for wear and tear on Page 19 of 225 . (relatively speaking) involving the following steps (Robot Control. The position. 2008): • • • • Forward Kinematics Inverse Kinematics Dynamics Trajectory generation The ultimate goal of course is to position the “effecter” (I.4 .Robot Control Controlling manipulators (such as robotic arms in a car manufacturing plant) is usually how robotics and neural networks connect.3 . 1999) 2. velocity and acceleration of the object being estimated in those implementations by several neural networks using several of the most recent measurements of the object coordinates.4.4. Adjust the arm to the appropriate joint angles and close the effecter.e.

This is far preferable to having to perform lengthy and expensive re-calibrations on any regular basis (Robot Control. Adjust the weight of the output neurons to lessen the error. In order to use a back-propagation training algorithm you must have a non-linear activation function for your artificial neuron. The sigmoid function is in the source code at 9.5.3. 2. Assign responsibility to each of the neurons in the previous level. 2.1 -“Network. allowing us to apply our learning algorithm. This rule essentially describes adjusting the weight using the difference between the expected and actual outputs.1.2 -The Artificial Neuron. 2008). Page 20 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 themselves. Adjust the weight of the neurons to minimise the responsibility.1.1 . training by back-propagation involves the steps: • • • • • Present a set of training data Compare the network’s output to the desired output at each output neuron and calculate the error. the disadvantage of a differentiable function is that it is by the use of these functions that local optima become apparent in the search space.Back-propagation overview In essence.5.5 .2 . based on the strength of the weights connecting them to the output neurons.cs” lines 130-133.Back-propagation in depth In a single layer neural network (or with a single node) the perceptron rule is applicable.Back-propagation 2. A commonly used function is the sigmoid function as described in section 2. The advantage of using a function like this is that it allows us to differentiate how close we came to the correct result.

2002) Calculating the hidden layer deltas is a little more complicated. Equation 3 – The delta rule (Matthews. = 1− Equation 4 . Provided you are using a differentiable function. as the effect that the change in the weight will have on the rest of the network is a missing factor. Page 21 of 225 . ℎ : = [1 − ] . respectively. the following alteration to the perceptron learning rule would serve our purposes (Matthews. the output layer nodes training function can be written as: = 1− − Equation 6 . such as the sigmoid function (see 2. Back-propagation for the Uninitiated.Altered delta calculation rule for the output layer (Matthews. Back-propagation for the Uninitiated. “n” is the learning rate of the neural network. Back-propagation for the Uninitiated. The effect that the change will have on the following neurons in the network is an extra factor.differentiated sigmoid function (Matthews. The other two values “di and yi” are the actual and desired outputs. Assuming the sigmoid activation function is in use. 2002): ∆ = ℎ = − Equation 5 . 2002) In this equation “w” and “x” are as before.Altered perceptron learning rule (Matthews. 2002) Therefore. 2002) However. Back-propagation for the Uninitiated. The sigmoid function differentiates very neatly. this rule has no positive effect in a multi-layer neural network.1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 ∆ = ℎ = − .2 -The Artificial Neuron) an alteration can be made to the perceptron learning rule allowing this difficulty to be overcome. Back-propagation for the Uninitiated.

it is fast. FANN) The FANN library (see (9.5. in order to apply this learning mechanism you start at the output of the network and work your way towards the inputs. open-source (allowing me to alter it if I needed too. Most organisms evolve by means of sexual reproduction and natural selection (Darwin. Therefore. The FANN library was decided upon for several reasons.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Equation 7 . against which to compare the genetic algorithm trained neural network. it is possible to “breed” solutions to problems.3 -Libraries Researched) for other libraries investigated) implements a back-propagation solution and this library will be used in the project to implement a feed-forward back-propagation neural network. By using similar techniques to natural selection.Altered delta calculation rule for the hidden layer(s) (Matthews.7 . although as it turned out I did not). 2. 2002) As demonstrated above. 1859) very neatly avoids one of the larger problems involved in software design: how to specify in advance all the possible permutations of the original problem and how the program should react to those permutations.Back-propagation library 2.6 . saving me finding a separate library.Genetic Algorithms Genetic algorithms attempt to copy natural laws to a certain extent in order to apply a random search pattern to a defined search space.The FANN neural network library (Nissen.3 . to calculate the delta for a hidden layer requires the deltas for the following layers. 1859) Page 22 of 225 . Natural selection (Darwin. 2. easy to implement. in a language that can be linked to C# fairly easily and it also supports cascade neural networks. Back-propagation for the Uninitiated.

the publication of the book “Adaptation in Natural and Artificial Systems” occurred. or indeed multiple genomes in a population. During the process chromosomes line up together and then cross over partway along their length. a dissertation by Kenneth De Jong established the potential of genetic algorithms by showing that they perform well on a wide variety of data.History of Genetic Algorithms Genetic algorithms first appeared on computers in the late 1950’s and early 1960’s. then of the technical University of Berlin introduced a technique he named evolution strategy. By 1962. Mutation plays a part in this as well. This book presents the concepts of adaptive digital systems using mutation. during this process.7. built on papers both by himself and by other researchers. across a range of subjects. selection and crossover. simulating processes of biological evolution. by the author John Holland. however. to alter slightly in a random way. In 1975. By the early 1980s. but this variety helps to prevent stagnation in the gene pool. many researchers had independently developed evolution-inspired algorithms for function optimisation and machine learning but did not get much follow up to their work.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Sexual reproduction ensures mixing and recombination of genes. At first. swapping genetic material. including noisy and discontinuous data. In 1965 one Ingo Rechenberg. Also in 1975. mostly designed to help evolutionary biologists’ model aspects of natural evolution. this mixing leads to much faster evolution than if off-spring simply copied the genetic material as it is in the parents. 2. genetic algorithms were becoming more widely used. genetic Page 23 of 225 . This could result in either an effective or defective solution. occasionally causing the genetic information. although this technique involved no crossover. this application was largely theoretical.1 .

more conventional.Advantages The primary advantage of genetic algorithms is their inherent parallelism. abandon that search.7. proceeding with other more likely candidates. they are very well suited as a means of exploring search spaces too large to be effectively searched in a reasonable amount of time by other. 2004).Advantages and Disadvantages of Genetic Algorithms 2. This is “Schema Theorem” and allows a genetic algorithm to move towards the searchspace with the most promising individuals in a timely fashion and then select the best member of that group (Marczyk.7.2. Thanks to the parallelism that is a genetic algorithms main advantage. (Marczyk. This leads to the exploration of a large proportion of the search space. These are “non-linear” Page 24 of 225 . can be used to fairly accurately predict national trends. Many other algorithms are largely serial and can only explore a search-tree so far in any one direction before abandoning all progress and starting again from the beginning.1 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 algorithms soon moved into commercial territory and nowadays help solve many problems in many different areas. 2004) 2. The real advantage of parallelism however is that by evaluating the relatively small search-space that it does. Genetic algorithms effectively explore many different branches of the tree at once and when a certain branch turns out to be non-optimal.2 . methods. with processing power exponentially dedicated to better areas as the exploration progresses. or further up the hierarchy. in the same way that the average response of a relatively small percentage of the population of any given country. a genetic algorithm is implicitly evaluating a much larger group of individuals.

Many real life problems are not like this. A third advantage of genetic algorithms is that they do not tend to be easily trapped by local optima. each component’s fitness is individual and any improvement to individual component's fitness is necessarily an improvement to the whole. making genetic algorithms an effective way to search them. Genetic algorithms have no prior knowledge about the problem they are trying to solve. due to their strength in navigating large search spaces (Marczyk. the random nature of the various starting points of the initial population and the other methods they employ. In infinite or very large search spaces. Selection allows the pruning of the least promising searches. crossover allows promising solutions to share their success and mutation allows random changes in the local search space of a given solution.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 problems. each searching their own space for the best solution. selection and mutation a genetic algorithm is metaphorically similar to a large number of parallel depth first algorithms. they tend towards non-linearity where altering one component's fitness positively may make other components less fit and cause a ripple effect across the whole system. Without crossover. This allows them to discover solutions that other algorithms may have over-looked. In a linear problem. such as crossover and mutation. due again to the parallelism of their approach. or merely very good local optima. genetic algorithms tend to give good results compared to other search strategies. This non-linearity results in a huge increase in the search space. One of the largest strengths of genetic algorithms can at first glance appear to be their largest weakness. they produce random changes to their candidates and then use the objective function to determine whether those changes are positive or negative overall. it is hard to know whether we have reached the global optimum. 2004). or never contained in their search space in the first place. A good example of this is the concept Page 25 of 225 . However.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 of negative feedback. etc. in a form that the genetic algorithm can use.2 . integer. if the solutions to various problems are in areas of the search space that the fitness function would not consider likely. the investigation may cover too little of the search space to find the optimum solution (Marczyk. 2004). Page 26 of 225 . however that is only one point. Another method is to use genetic programming. the system will never converge towards a suitable solution. A third issue is setting the correct mutation rate. genetic algorithms use strings of binary. or real-valued numbers. 2004). but denied a patent for several years because it ran counter to established beliefs (Marczyk. population size.Disadvantages One of the disadvantages of a genetic algorithm is the necessity of representing the problem the genetic algorithm is trying to solve. A further problem is that fitness functions can be deceptive. Generally. or adjust them. A second disadvantage of genetic algorithms is that the fitness function is crucial to the development of a suitable solution for the problem. a comparatively smaller segment of the search space will be covered and the eventual solution may take longer to reach or never be reached as a result. where the actual code of the program forms the genomes in the genetic algorithm and it can swap portions in or out. therefore no gradual increase can lead you to it. with each number representing some distinct part of the solution (Marczyk.7. If you have a poorly written fitness function. however if it is too low. 2. the global optima is at the far right. For example in the diagram below. If the mutation is too high. then genetic algorithms (and most other search techniques) are no better than a random search for finding the solution. there is no slope leading to it. if the size of the population is too low. rediscovered by genetic algorithms. 2004). the genetic algorithm may end up solving an entirely different problem from the originally intended one. Likewise.2.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 you either have the solution or you don’t. Page 27 of 225 .local optimum (units are undefined in this case). local optima in the centre of the diagram. including sigma scaling and Boltzmann selection (Mitchell. lowering the diversity of the population and resulting in genetic algorithms possibly falling into the local optima that the mutation represents. Finally. 900 800 700 600 500 400 300 200 100 0 0 2 4 6 8 10 12 Figure 4 . but altogether easier to find. This is because analytical solutions usually produce more accurate results faster than a genetic algorithm (or any heuristic method) is capable of (Marczyk. In this example a properly constructed genetic algorithm would be likely to settle on the marginally less optimal. 1996). where analytical solutions exist they should be take precedence over genetic algorithms. which will then reproduce abundantly. There are various methods for solving this. unless by random chance it discovered the point with the most optimum solution. 2004). A further problem is premature convergence: this occurs when a mutation early in the process produces a large leap in the fitness of that particular genome.

5 -Back-propagation) which is a gradient descent algorithm and as such can be easily trapped in local optima (Branke. to test the appropriateness of a given genome. such as adding a “momentum” to the algorithm. a neural network representation within a genetic algorithm is a concatenation of all the weighs in the neural network.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 2.8 . not altering them besides that in any way.8. to try to help it move beyond local optima. these are not a complete solution (Blum.1 .Determining Weight Values with Genetic Algorithms Genetic algorithms (as described in 2.7 -Genetic Algorithms).Representation of a Neural Network within a Genetic Algrithm As a rule.7 -Genetic Algorithms) are good at avoiding being stuck on local optima due to their searching several regions of search space simultaneously. since you are merely increasing the size of the genomes being worked with. however. Genetic algorithms also have the benefit that they do not place any restrictions whatsoever on the architecture of the neural network. 2. 2. Page 28 of 225 . There are methods to try to avoid this problem when using back propagation. however the method of choice for training neural networks is usually back propagation (see 2. In addition.2 . 1992). involving finding the optimum values for a set of real numbers (connection weights) which will produce the least error in the results from the neural network.Training Neural Networks with Genetic Algorithms Training neural networks is a form of problem solving. 1995).8. The error surfaces associated with these problems tend to be highly variable and contain many local optima. the only information genetic algorithms require to train a neural network is an objective function (as described in 2.

These are all connected.3 . The average neural network has an input layer. Standard genetic algorithms use binary strings. can lead to very large strings (thousands of bits) (Branke. Because of this it is helpful to keep functional units close together (I. it is also possible to use a genetic algorithm to evolve a structure for a neural network. to form a feed forward network. Page 29 of 225 . however representing a large number of real valued numbers as a binary string. 2. a nodes input weights and the nodes of a layer. The problem at hand dictates the input and output layer structures. one or two hidden layers of an indeterminate number of nodes and one output layer. outputs to inputs. One of the most important decisions when deciding how to represent a neural network within a genome is whether to use binary strings or real number strings.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Crossover can disrupt genes far apart.e. but is less likely to do so for genes close together.8. place a neurons input and output weights. side by side). 1995).Using Genetic Algorithms to Determine Neural Network Structure As well as using a genetic algorithm to evolve the weight set for a fixed-structure neural network.

A feed-forward neural network This architecture is then altered (number of nodes in each hidden layer. given a specific problem. There is also no method to check how optimal your solution is (Branke. if the neural network architecture is too complex. Although network architecture plays such a large role in network performance. a neural network topology to deal with that problem. the neural network will never learn to solve the problem (or come close enough to suggest a solution). a neural networks generalisation is its main strength and therefore you do not wish to lose it. On the other hand. Page 30 of 225 .0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 5 . However. this is not necessarily the most efficient form for a network for any given problem and the trial and error method is an inefficient method for determining the correct architecture for an optimally performing neural network for the problem at hand. the neural network will learn fast but will generate too specific a solution and will not generalize between similar inputs. As mentioned in section 1. If neural network architecture is too simplistic for the problem at hand. 1995). number of hidden layers) by trial and error and intuition to establish a neural network that performs well for the problem at hand. there is currently no method by which to establish.

Back propagation learning rate is also a parameter for one of these areas. In direct encoding. In one version of high level encoding of a neural network. the strong. or high-level encoding (Branke.1 .3. Page 31 of 225 . a representation is required which can accommodate all networks that will work for the problem but none that will not. you must remove all connections to or from that node completely. Low-level encodings specify each connection individually.Representing neural network structure Representing the structure of a neural network within a genetic algorithm is not as straightforward as representing the weight values of a neural network within a genetic algorithm. being able to contain the most optimum solution for the problem at hand.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 2.8. In cases where a genetic algorithm determines the number of hidden layers and the number of nodes per hidden layer and the network itself is interconnected fully. 1995). direct. or low-level encoding and the weak. the network is divided into areas and for each area the number of nodes and the density of connections to other nodes is determined (Branke. thus meaning that in order to remove a node completely. Ideally. indirect. whereas high-level encodings group together connections and/or nodes. an exception is possible. There are two methods for representing neural network topologies within a genetic algorithm. 1995). the genetic algorithm represents the neural network by means of its connections. thus eliminating the searching of meaningless space and by definition.

1995). A problem with high level encoding is that regular networks are favoured but not every network is equally probable. will not ever succeed in learning the problem. 2. These are the Step-size problem.Cascade Correlation The cascade learning architecture is a method by which the training algorithm builds the neural network as it proceeds. a balance between speed of learning and generalisation exhibited in the trained network is necessary. The Cascade-Correlation Learning Architecture. Therefore. it is impossible to know whether these large and complex networks may be the most suitable way to solve the problem at hand.9 .2 neural networks that are too large will solve a problem fast but will not generalize well and therefore their usefulness is limited severely. which is obviously undesirable. These mapping methods are therefore more suitable for small networks or networks with a small number of connections. and the moving target problem (Fahlman & Lebiere. Neural networks that are too small on the other hand. by limiting the potential size of the networks evolved and preventing the generation of overly large and complex networks but on the other hand. This section largely drawn from information in: “Curved trajectory prediction using a self-organizing neural network” (Branke. 1991). This could be useful. This can lead to human bias entering the system and affecting the outcome. as the size of the neural network increases. The algorithm attempts to solve the issues associated with back-propagation that produce slow learning.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 One of the reasons why alternatives to low-level encoding arose is the exponential rise in the number of connections. Page 32 of 225 . As mentioned in section 5.

5. The “candidate” nodes take the neural net inputs.2 -Back-propagation in depth) or the quick prop rule (Fahlman. = −` −` . the partial derivative of "s" with respect to each of the candidate unit’s incoming weights "wi" is a necessity. 1991). 'v' is the candidate unit's value. It then trains the adjustable weights using the perceptron rule (as described in 2. Page 33 of 225 . the quantities ‘`v ’ and ‘`Eo’ are the values of v and Eo averaged over all patterns (Fahlman & Lebiere. An Empirical Study of Learning Speed in BackPropagation Networks. with the aim of getting as close to a solution as possible. the initiation of a number of “candidate” units takes place and trained using either the perceptron rule (2. 1988). After a number of training cycles pass with no substantial improvement (the precise number being determined by a user-supplied “patience” parameter).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 The system begins with a basic neural network of inputs and outputs (numbers of each dictated by the task as usual) and no hidden layer. 'p' is the training pattern. the candidate unit’s value. plus the outputs of any previously created units. An Empirical Study of Learning Speed in Back-Propagation Networks.The definition of 's' given that 'o' is the network output at which the error is measured. or if the networks output is of sufficient quality the training is ended. If a trigger occurs for the addition of a unit.5. 1988). or sometimes the quick prop rule (Fahlman. either an extra node is added. and attempt to maximise “s” the sum overall output units “o” of the magnitude of the correlation between “v”. The output units may use either a linear or differentiable activation function. Equation 8 .2 -Back-propagation in depth). The Cascade-Correlation Learning Architecture. and “Eo”. There is also a bias input. with a value of one. In order to maximise "s". The Cascade-Correlation Learning Architecture. 1991). The connection between each input and output is via an adjustable weight. the residual output error at “o” (Fahlman & Lebiere.

−` ′ . Page 34 of 225 . Once the correlation on a candidate has reached an acceptable level. Equation 10 .The partial derivative of ‘s’ with respect to each of the candidates incoming weights ‘wi’. The Cascade-Correlation Learning Architecture. The connection of the best of the candidates to the neural network as it stands takes place. a gradient descent to maximize "s" can take place.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Equation 9 . = . The training iterations for these adjustable weights in the main network then continue. After these computations are complete. 1991). or the completed training epochs reaches a maximum level. where ‘oo’ is the sign of the correlation between the candidates value output ‘o’. ‘f'p’ is the derivative for pattern 'p' of the candidate unit's activation function with respect to the sum of it’s inputs and ‘Ii.The partial derivative of ’s’ (Fahlman & Lebiere. Adjustable weights link the outputs for this node and all the output nodes inputs. . Expansion and differentiation of this calculation can then take place. and the inputs for that node are frozen.p’ is the input the candidate receives from unit ‘i’ for pattern ‘p’.

The second state of a cascade correlation neural network. A first node has been added. 1991) Cascade-Correlation 1991). Figure 7 . The Cascade Correlation Learning Architecture.The initial state of a cascade correlation neural network. The square econd connections are locked weights (Fahlman & Lebiere. The circular connections are adjustable weights (Fahlman & Lebiere.William Sayers 05025397 Genetic Algorithms and Neural Networks Neural Milestone 3 Figure 6 . 1991) Cascade-Correlation 1991). Page 35 of 225 . The Cascade Correlation Learning Architecture.

The third state of a cascade correlation neural network. The Cascade-Correlation Learning Architecture.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 8 . 1991). Page 36 of 225 . with two nodes added (Fahlman & Lebiere.

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

2.10 - C# User Interface Programming
C# uses the Winforms API to create user interfaces. It allows you to “draw” objects on a form in a visual manner to design your layout and then access and alter properties and subproperties of those objects in your programming code. A separate thread for each Window in Winforms exists, in order to facilitate performance on modern multi-core machines, although it does make it more difficult to move data between the forms. A method using “delegate functions” (similar to “function pointers” in C++) and “events” (Lysle, 2007) is the method I will use. Another challenge is making the neural network training independent of the user interface, in order to avoid the interface freezing whilst training a neural network. The solution I plan for this is to use the background worker object provided with C#, which allows you to assign a delegate function to be your background task, and will trigger events upon completion, for which event handlers will be executed (Microsoft, BackgroundWorker Class, 2009). Making a C# user interface interact with C++/CLI dll’s simply involves adding a reference to the dll in your project, via the Visual Studio® user interface.

2.11 - Writing C++/CLI Wrappers for Unmanaged C++ code
The cleanest method of connecting a native C++ or C dll or piece of code to a managed C# interface is via a C++/CLI bridge class. The VC++ compiled can swap between managed and unmanaged code on the fly whilst compiling and allows you to have both managed and unmanaged code (in C++) as part of the same project. Thus, a managed class can communicate with the unmanaged class (performing appropriate marshalling of the non-primitive data types (Microsoft, Using C++ Interop (Implicit PInvoke), 2009)) and the C# code can then access it directly.

Page 37 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3 Although it is possible for a C# program to interact with an un-managed dll directly, this method is less error-prone and more likely to be robust and stable, as well as making for cleaner code.

2.12 - Application of Research
The research presented here will help to produce the deliverables for milestone two and milestone three of my final year project: I plan to combine C# for the user interface and C++ for the neural network back-ends in order to create an efficient, powerful and fast solution. Section 2.11 -Writing C++/CLI Wrappers for Unmanaged C++ code, covers the combination of C# and C++ so I will not bother to cover it here, I plan to use the DLL method, with one DLL combining managed and unmanaged C++ linked into my C #user interface code. The user interface will then have methods to adjust parameters such as the genetic algorithms population size, number of generations, etc. In order to test and see what kind of genetic algorithm learns and develops fastest. It will also allow you to specify the number of back-propagation iterations. The use of this application will be in evaluating the difference in performance between a genetic algorithm, back-propagation algorithm and cascade correlation algorithm as neural network training algorithms.

3 - Design
The purpose of this application will be to allow the user to run a back-propagation trained neural network, or a genetic algorithm trained neural network and present the results of the network in such a fashion as to allow analysis and comparison of the training methods and their suitability in different situations.

Page 38 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

3.1 - Program Requirements
The program must be able to: • • • • • • Run a genetic algorithm on a feed forward fully interconnected neural network to train the network. Run a back propagation algorithm on a similar network to train the network. Run a cascade algorithm on a similar network to train the network. Allow the user to set appropriate variables (see 3.4.2 -New Network Wizard) to adjust the execution of the learning algorithm selected for the neural network. Allow the user to select which learning algorithm they wish to use. Display the results of running that algorithm on a neural network in a meaningful fashion.

3.2 - Design of the Class Structure
Before I discovered a simple to program and effective design, I tried and discarded two designs. I eventually used the third design and it is that third design documented here. The two prior designs to this involved representing neural networks via objects and compositions of objects in vectors, which lead to flexible, but slow and overly complex code upon experimentation. The final solution, the solution documented here, is a far simpler one (in terms of the neural network representation, it is far more detailed in the other areas than the initial designs were). This solution loosely follows a model, view, controller architecture. The solution enables easy replacement of sections of code as long as the interfaces and outputs remain the same. For more information on the class structure, see section 9.2 -UML Class Diagram.

Page 39 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

3.3 - Linking C# code to managed dll’s
Linking C# code and managed dll’s is a simple process (in Visual Studio) as explained in (2.11 -Writing C++/CLI Wrappers for Unmanaged C++ code) of selecting the references section in the C# project in question and adding the dll you wish to refer too. Then you can simply add a “using” directive to use the dll namespace and create, destroy, and use the classes within as if they were native C# classes. This can be seen in (9.1.2.6 “Algorithms\BackProp.cs”) and (9.1.2.4 -“Algorithms\CascadeCorrelation.cs”) with the “using FANN_Wrapper;” directive. This is superior to the direct C# to unmanaged code linking that used in milestone two, since it makes for cleaner and thus more easily maintainable code and the code is more robust in this fashion. The robustness comes from the fact that C++/CLI is far better at interacting with unmanaged C++ code than C# is. Indeed – they can even be in the same source file and compiled into one object. Therefore, by using a C++/CLI wrapper to interface to the unmanaged C++ and then interacting with the C++/CLI wrapper/s errors are less likely to arise.

3.4 - Design of the User Interface
The construction of the user interface utilised the Winforms based tools in visual studio 2008 (see 2.10 -C# User Interface Programming). I also used the ZedGraph control (ZedGraph) to display the graph of my results.

3.4.1 - Main Form
The main form is the crux of the application (as is default in C# Winforms programming) and if the main form is closed, all other forms and classes are disposed and the application terminates.

Page 40 of 225

One display will show network details and one display will show the training and testing output.1.2 .Mean Squared Error Graph tab The mean squared error graph tab is a simple tab. Figure 9 – Network and output tab design (Main form) 3. along with two displays. which simply needs to display the graph of the mean squared error over the current epoch/generation.1.4. Page 41 of 225 .Network and Output tab This tab needs a method by which to initialise the adding of a new network (3.4.4.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 3. Three buttons will therefore be present.1 .2 -New Network Wizard) a method to train the current network and a method to test that training.

Figure 11 .4.3 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 10 .Mean squared error tab design (Main form) 3.1.Dataset tab design (Main form) Page 42 of 225 . to show the training data and to show the testing data.Dataset tab This tab needs two display sections.

2 . Verification of all data takes place before the form passes control back to the main form and triggers a main form event with a parameter that takes a specially constructed class to transfer the data from the wizard. Figure 12 .1 . 2.4.9 -Cascade Correlation). It allows you to move in a logical progression through the set up process of a new neural network and associated training algorithm.1 -Learning Algorithm tab (if no algorithm has yet been selected.Network Settings tab This tab allows you to set appropriate settings for the network and training algorithm you have selected in 3. Page 43 of 225 .Learning Algorithm tab On the learning algorithm tab you select the learning algorithm for the neural network you are creating. the default is genetic algorithm).Design of the training algorithm tab (Wizard form) 3.4.4. 2. 3.New Network Wizard The new network wizard is the form that appears when the user selects the creation of a new network.5 -Back-propagation.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 3.7 Genetic Algorithms.2 . The user can progress through the steps via the “previous” and “next” buttons or via the tabs at the top in whatever order you choose.2. out of three choices previously mentioned (2.2.2.4.

3 . Page 44 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 13 . Figure 14 .Design of the network settings tab (Wizard form) 3. or to choose custom data sets from your hard disk for training and testing data (appropriately formatted in the FANN style).2.Dataset tab The dataset tab allows you either to select from two pre-setup datasets.3 .Working Form The working form is merely a small display shown as a dialog (lock out access to displaying form) which shows whilst a neural network is training.4. both to protect the main form from data inputs that may cause training problems and to provide a visual indicator of work taking place.4.Dataset selection tab design (Wizard form) 3.

About form design (About form) 3.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 15 .Working form design (Working form) 3. clicking the same button again hides that information.4 . Clicking the datasets button or the licenses button displays the appropriate information.4.4. project description). Page 45 of 225 . Figure 16 .About Form The about form displays information about the application (author.4.Datasets display Clicking the data sets button displays a tabbed interface.1 . displays licensing information and displays information on the pre selected datasets. project supervisor. one tab with XOR information and one tab with Fishers Iris Data information.

4.2 .Licenses display Clicking the licenses button displays a small information panel with selection methods.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 17 . Page 46 of 225 .4.Dataset display design (About form) 3. depending on your selection. it will display the appropriate license.Licenses display design (About form) 4 .Implementation The implementation is largely in C#. with small portions in C++/CLI and the back-end FANN (Nissen. Figure 18 . FANN) library written in C and linked as a static library to the managed FANN_Wrapper dynamic link library.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 4.1.1 .1.1.Main Form The main forms code is in section 9.cs”.2 -“FrmMain.1. 4.Network and Output tab Page 47 of 225 .1.1 .User Interface Implementation The implementation of the user interface is mainly in C#. 4. mostly via the visual design tools found in visual studio 2008.Network and Output tab Figure 19 .1 .

Mean Squared Error Graph tab Figure 20 .Mean squared error graph tab Page 48 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 4.1.2 .1.

The dataset tab Page 49 of 225 .1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 4.1.Dataset tab Figure 21 .3 .

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

4.1.2 - New Neural Network Wizard
4.1.2.1 - Learning Algorithm tab

Figure 22 - Learning Algorithm tab

Page 50 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

4.1.2.2 - Network Settings tab

Figure 23 - Network Settings tab

4.1.2.3 - Dataset tab

Figure 24 - Dataset tab

Page 51 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

4.1.3 - About Form

Figure 25 - About form, main view

Page 52 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

4.1.3.1 - Datasets View

Figure 26 - About form, Datasets view

Page 53 of 225

1.5 .About form.Passing Information between Forms Passing the data between the wizard form and the main form takes place as previously described (2.1.3.2 .10 -C# User Interface Programming) via events and delegate functions.Working form 4.4 .Licenses View Figure 27 .Working Form Figure 28 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 4. Page 54 of 225 . Licenses view 4.1.

4. This leads to a user interface that is much more accessible (Microsoft. Events.1.cs” lines 70-119). In the event handler in the main form.1.2 -“FrmMain.1. The declaration and association of a delegate and an event takes place in the form that needs to generate an event (9.1.cs” lines 145-163).1.2 -“FrmMain. Firstly.2 “FrmMain. 2009).2 -“FrmMain.1. The two-event handling functions are at 9.1 -“NetworkUpdatedEventHandler”). The declaration of a new event handler in the main form takes place prior to displaying the new neural network wizard form (see 9. lines 29 – 31).3 -“Wiz1.1.cs”. are associated with a delegate function to respond to that event (Lysle. The “NetworkUpdatedEventArgs” class is a storage class for passing data (see 9.1. the background worker class is instantiated and supplied with the event handler references functions (see 9. 2007).cs”. Then the runworkerasync function executes and then shows the working form.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 An event is a method of notifying a listening object that something has occurred.Keeping the User Interface Active whilst Processing is Occurring The user interface delegates the processing to a background worker class.6 . the processing of the class sent from the Wizard form takes place (see 9. lines 306-353. lines 266-281).cs”.1. (See 9.1.1. lines 29-44).1.1.2 -“FrmMain. when instantiated.cs” lines 120-129). BackgroundWorker Class.1.cs”. The trigger of a new event takes place in the Wizard form when you are on the last tab and the user clicks “next” (see 9.1. Page 55 of 225 .3 -“Wiz1.1.1.

2. FANN) which is a C programming language based neural network library. Once this implementation was complete.5 -“Algorithms\GeneticAlgorithm.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 4.Genetic Algorithm Implementation Although the original design was to implement genetic algorithms in C++.NET sort methods in the sorting part of the genetic algorithm). and didn’t suffer from the memory leaks (being managed code) so that’s the version that exists in the application today (see 9.1.1.3. Therefore.Back-propagation and Cascade Correlation Implementation The FANN library implements both cascade correlation and back propagation (Nissen. • • The sorting implementation in GANetwork.cs” lines 51-126) 4.2 . when the C++ implementation of genetic algorithms that I wrote functioned.4.NET built in functionality (see 9.1. it was both faster than the C++ implementation (due mostly to my use of build in .3 Page 56 of 225 .1 .2.2 -“GANetwork.5 “Algorithms\GeneticAlgorithm. The compilation of the FANN library is static.2 -“GANetwork.2. I decided to try re-implementing it in C#.cs that links into the .cs” lines 176204). and the Dll linked to this to allow access to the functionality. There is an unmanaged C++ wrapper (9.cs” for the C# implementation) The main points to note in the code as it stands are: • The crossover (single-point) and mutation (random increment or decrement) implementations (see 9.3. after having used C# extensively in the UI I felt more confident about the performance loss and the use of the programming language itself.1.cs” lines 32-44). The neural network specific functionality added to the network model in the network specialisation class GANetwork (see 9. but had issues with memory leaks and was slow.1.

which allows it to manipulate the FANN functions in the static library through this class. 4.1. into arrays of strings.4 .h”. number of outputs) separates the data into two-dimensional arrays suitable to be passed to other functions.1. which store respectively. these arrays form parameters for the other functions. the separation of the textual inputs takes place.2 -“FANN_Wrapper. C# accesses the C++/CLI class directly. Page 57 of 225 .cpp”) for the Cbased FANN functions in the DLL and a C++/CLI wrapper (9.Data Parser Implementation The data parser (9.1 -“Network.4.1.h”. via references to these objects passed down via the hierarchy (references are passed to avoid the necessity of passing entire strings down the hierarchy). 4.4. 9.cs”) parses data directly from the rich text boxes that they’re displayed upon on the main form. 9. will also be the coordinates for its corresponding weight (lines 17-19).cpp”) for the unmanaged C++ classes. and nodes) the eventual implementation turned out to be basic yet effective (9.1.4.Neural Network Implementation Although two previous attempts made at programming a neural network.6 -“UnManaged_FANN_CasC. the weights.1.1 -“FANN_Wrapper.3 . 9.1.h”.4. 9. the initialisation of all the arrays to their correct sizes takes place and then the initialisation of the weights to a random double between zero and one. number of inputs. layers. The inputs and weights are structures so that the coordinates for an input.cs”).5 -“UnManaged_FANN_CasC.1. From there. the inputs.2. which using the data from the heading (number of epochs of data.2 -“DatasetParser.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 “UnManaged_FANN_BackProp.cpp”.3.4 “UnManaged_FANN_BackProp. followed an object-based structure (network. and the outputs.4. via a number of designated separator characters. In the constructor (lines 41 – 77). Three jagged arrays are utilised. In the initialisation routines (lines 28-124).

1 . so repetition is instead used. 5 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 The main functionality of the neural network class can be found in the “internal” region (lines 126-235). The genetic algorithm and back-propagation neural networks each had four hidden nodes for each dataset. five times for Virus classification and Fishers iris data). Then entered the weights from the neural network program (by means of using the visual studio debugger to check variable values) into the spreadsheet and checked that the outputs coincided. The target error was 0.02 for Fishers iris data and the virus classification data. Page 58 of 225 . to run the output layer. and creating a neural network with a similar structure on an excel spreadsheet. I tested the neural network implementation by creating a neural network with structure 2. As the program presented the data in each case. recording took place for analysis.01 for XOR and 0. there are three functions. 1.4 -Datasets) (ten times for XOR. and hidden layer which are all called by the public “run()” function (lines 241-270). input layer. 5. the dataset is too small to split.XOR In the XOR dataset. 3. All nodes determine their outputs using the sigmoid function (lines 130-133).Testing Data Testing took place several times for each algorithm and dataset (see 9.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.00998032 0.Back-propagation In the back-propagation testing data.00988863 0.1.009162436 0.009962559 0.006150786 0.00881867 0.009566951 0.006150786 0.00992291 0.00987211 0.00988649 0.00998027 0.1.00997699 0.0099113 0.009375132 0.009566951 0.00957518 0.009885499 Table 3 .Back-propagation testing data (XOR) Page 59 of 225 .0099765 0. because of floating point inaccuracies converting between double precision floating-point numbers in my code and single precision in the FANN code.009375132 0.00992081 0.Genetic Algorithms Iterations (GA) 591 407 500 466 499 534 675 557 357 939 552.00983181 0.00990403 0.009157809 Testing MSqE (GA) 0.009829335 0.009962559 0.00998959 0.5 Result MSqE (BP) 0.00995716 0.00989974 0.2 .00957518 0. the mean squared error from the network result and the testing mean squared error are different despite the data being the same.009829335 0.009888967 0.1 .5 Result MSqE (GA) 0.009888967 0.00999799 0.00881867 0.00991907 0.009248076 0.009157809 Table 2 .009969323 Testing MSqE (BP) 0.009162436 0.00999069 0. Iterations (BP) 345 287 319 327 301 323 421 315 362 325 332.Genetic Algorithm testing data (XOR) 5.00990161 0.009248076 0.0098402 0.

3 .00960884 0.32E-13 2. the network is trained more after that point (output weight training). At each node addition.1.00656719 0.00889787 0.18E-13 7.0086449 0.22E-13 1.14E-14 2.Cascade Correlation In the cascade correlation data. but in the case of cascade training.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.00855044 0. the testing result differs quite widely from the network result.02E-13 2.008039565 Testing MSqE (CC) 7.00992534 0.00585159 0.00798691 0. which is when the program records the mean squared error.Cascade Correlation testing data (XOR) Page 60 of 225 .00573252 0.99E-13 1. the call back function triggers.00863005 0.86E-14 1.83E-13 2.43545E-13 Table 4 .78E-14 1.02E-13 1. Nodes (CC) 2 2 2 2 2 2 2 2 2 2 2 Result MSqE (CC) 0.

Mean squared error upon last training test (XOR) Page 61 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.Graphs 1000 900 800 700 600 500 400 300 200 100 0 0 5 10 15 Iterations (GA) Iterations (BP) Nodes (CC) Figure 29 .004 0.006 0.01 0.1.4 .Number of iterations to achieve target (XOR) 0.008 0.012 0.002 0 0 5 10 15 Result MSqE (GA) Result MSqE (BP) Result MSqE (CC) Figure 30 .

004 0.002 0 0 5 10 15 Testing MSqE (GA) Testing MSqE (BP) Testing MSqE (CC) Figure 31 .The mean squared error at testing (XOR) Page 62 of 225 .008 0.006 0.01 0.012 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 0.

014116 0.1 .0140669 0.0195853 0.0141406 0.019719802 0.019775896 0.0143778 0.014144 0.0139982 0.019751257 0.0164964 0.019775896 0.2.2 .01956255614 Testing mean squared error (GA) 0.Cascade correlation testing data (Iris data) Page 63 of 225 .0189456 0.Genetic algorithm testing data (Iris data) 5.2 .3 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.0196297 0.0160925 Table 7 .019763877 0.Cascade Correlation Nodes (CC) 2 2 2 2 2 2 Mean squared error (CC) 0.019763877 0.0199972 0.Back-propagation Iterations (BP) 434 529 474 391 527 471 Mean squared error (BP) 0.0141935 Table 6 .0199999 0.0198416 0.0199916 0.01950238 Testing mean squared error (CC) 0.018789438 0.Fishers Iris data 5.0143806 0.019719802 0.2.0199719 0.0158679 0.0199918 0.0193595 0.0199892 0.Back-propagation testing data (Iris data) 5.018789438 0.01956255614 Table 5 .2.019751257 0.01998996 Testing mean squared error (BP) 0.Genetic Algorithms Iterations (GA) 1928 1834 1277 2835 8716 3318 Mean squared error (GA) 0.

2.019 0.0192 0.02 0.Number of iterations to achieve target (Iris data) 0.Mean squared error upon last training test (Iris data) Page 64 of 225 .0188 0.0186 0 2 4 6 Mean squared error (GA) Mean squared error (BP) Mean squared error (CC) Figure 33.4 .0194 0.0198 0.0196 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.Graphs 10000 9000 8000 7000 6000 5000 4000 3000 2000 1000 0 0 2 4 6 Iterations (GA) Iterations (BP) Nodes (CC) Figure 32 .0202 0.

and others.015 0. which are the number of amino acid residues per molecule of coat protein.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 0.005 0 0 2 4 6 Figure 34.025 0.01 0. cucumber. The dataset is in order: • • • • Hordeviruses x3 Tobraviruses x6 Tobamoviruses x39 Furoviruses x13 Page 65 of 225 . There are 18 measurements on each virus.3 . tomato.The mean squared error at testing (Iris data) 5.Virus Classification This dataset covers 61 viruses affecting several crops including tobacco.02 Testing mean squared error (GA) Testing mean squared error (BP) Testing mean squared error (CC) 0.

0194383 0.01983722 Mean Squared Error testing (BP) 0.3.019092293 0.0190612094 Table 8 .01870197 0.1 .0105845 0.Cascade Correlation testing data (Viruses) Page 66 of 225 .0391179 0.01870197 0.3 .017723052 0.0199916 0.019927361 0.2 .2 Mean Squared Error training (BP) 0.017043 0.0165979 0.019927361 0.0434423 0.0199621 0.Back-propagation testing data (Viruses) 5.016183342 Mean Squared Error testing (CC) 0.00980208 0.00640291 0.0350318 0.4 Mean Squared Error training (CC) 0.011585152 Table 10 .3.0196959 0.0190612094 Mean Squared Error testing (GA) 0.019092293 0.Genetic Algorithms testing data (Viruses) 5.019861371 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.0111416 0.019743 0.03833646 Table 9 .0197935 0.Back-propagation Iterations (BP) 3275 10922 2800 3744 6295 5407.Genetic Algorithms Iterations (GA) 1818 1421 896 11676 1068 3375.017723052 0.00979968 0.3.0347797 0.0195173 0.Cascade Correlation Nodes (CC) 1 2 1 2 1 1.019861371 0.0185152 0.0393106 0.8 Mean Squared Error training (GA) 0.

Graphs 14000 12000 10000 8000 6000 4000 2000 0 0 2 4 6 Iterations (GA) Iterations (BP) Nodes (CC) Figure 35 .005 0 0 2 4 6 Figure 36 .3.025 0.015 0.02 Mean Squared Error training (GA) Mean Squared Error training (BP) Mean Squared Error training (CC) 0.Mean squared error upon last training test (Viruses) Page 67 of 225 .Number of iterations to achieve target (Viruses) 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.01 0.4 .

015 0.025 0. Genetic algorithms take around five hundred iterations on average to produce a workable solution when aiming for a mean squared error of below 0.03 0.035 0. or possible inefficiencies in my programming. genetic algorithms and back-propagation offer similar performance in solving the problem.05 0.1.1 .005 0 0 2 4 6 Mean Squared Error testing (GA) Mean Squared Error testing (BP) Mean Squared Error testing (CC) Figure 37 . The back-propagation implementation seems to be slightly faster in real-time but this is possibly attributable to managed code inefficiencies versus unmanaged code.04 0.01 0.Mean squared error at testing (Viruses) 6 .1 .XOR As regards to the XOR problem. but to a negligible degree in this scenario. Page 68 of 225 .045 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 0.Comparisons 6.01. versus backpropagations 333 iterations on average.02 0.Back-propagation and Genetic Algorithms 6. Genetic algorithm solutions are slightly more accurate.

Virus Classification The solution of this problem was more effective by genetic algorithms than by backpropagation.Cascade Correlation and Genetic Algorithms 6.2 epochs for backpropagation. genetic algorithms taking 550 generations on average to solve the problem to a suitable accuracy.3 . The genetic algorithm solutions also held their effectiveness through to the testing data with extreme consistency.1. 6. with an average of 3375.2. Page 69 of 225 . 6. whilst the cascade algorithm used 2 nodes to solve the problems. At a maximum of 150 epochs per node added (the default (Nissen.2 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 6.8 generations versus 5407. With an average of 3318 generations for genetic algorithms versus 471 epochs for back-propagation this became clear.1.1 . The back-propagation generated networks were also more accurate overall (in fact on this particular problem analysed the testing data more effectively than the training data). on this data set cascade training came to a satisfactory solution faster and more accurately than genetic algorithms.Fishers Iris Data Again. FANN)) this is around 300 epochs to solve the problem.2 . whereas the back-propagation solutions lost effectiveness on the training data.Fishers Iris Data Genetic algorithms clearly found this a difficult problem to solve as opposed to backpropagation. 6. The cascade algorithm also achieves a higher degree of accuracy.XOR This problem was solved more effectively by the Cascade algorithm than by genetic algorithms.2 .2.

6.Testing conclusions Genetic algorithms are a valuable and effective means of training neural networks. solutions developed using genetic algorithms tend to hold their effectiveness through to the data set effectively. 7 . They are roughly as effective as back-propagation training and although back-propagation is faster for the most part. With more time. with the information presented and discussed here. genetic algorithms fall down as a solution. Page 70 of 225 .2.3 . the unavoidable conclusion is that cascade training is the most effective of the three training methods examined. Compared to cascade correlation however.Virus Classification With this data set cascade training again came to a satisfactory solution faster and more accurately than genetic algorithms did on the same data.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 6. I feel the project has been a success. however. although there are further investigations I would like to continue with given more time (and may continue with in my own time).3 .Evaluation Overall. more in-depth testing using more varied datasets a clearer comparison and possibly drawbacks associated with cascade training may become evident.

Research current information on training neural networks using genetic algorithms. Build an application that is capable of training a neural network with a genetic algorithm and with a backpropagation system. and in which cases these methods are suitable.1 . leading to a more solid conclusion and more information on the various strengths and weaknesses of the various learning algorithms examined. and in which cases these methods are suitable. Page 71 of 225 . evaluate training methods for neural networks. as well as previous research. Using above application.” With more time more advanced tests could be undertaken using larger data sets.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 7.5 -Objectives): Research the fields of genetic algorithms and neural networks. Build a user-interface for the aforementioned application allowing for easy alteration of appropriate variables. The only objective that I feel could be improved upon is the final objective. “Identify the most effective ways of training neural networks using genetic algorithms. Identify the most effective ways of training neural networks using genetic algorithms. and that allows manual adjustment of relevant variables.How many of the original objectives were achieved? The original objectives were as follows (9.

Due to small conflicts between managed and unmanaged memory.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 8 . Some forms of testing that would be possible are: • • • • • Testing with datasets with larger numbers of inputs Testing with large datasets that attempt to train the network to distinguish between small differences in data. Testing with datasets that would take large amounts of time to train. Testing both with and without scaling and utilising different scaling methods. Page 72 of 225 . Improve memory management. the program occasionally causes an exception (most specifically when creating cascade or back-propagation networks in quick succession).Possible Improvements More advanced testing would be the biggest improvement that more time would make possible. These could be resolved fairly simply (I believe) by implementing the iDispose interface in my managed code to allow it to be destroyed upon demand instead of waiting for the garbage collector.

Having the back-propagation algorithm in C# similar to the genetic algorithm would also allow a more “apples to apples” comparison. Page 73 of 225 . experimenting with multiple point crossover and other crossover forms. (the new network wizard and the working form) lock out the rest of the program.Possible Training Algorithm Improvements With more time. for the reasons stated above. Making the forms shown as dialogs. 8. • • Attempt to improve the speed of the genetic algorithm.Possible User Interface Improvements The main improvements to the user interface that I would make if I had more time are: • • Increased number of variable options presented to the user (with appropriate defaults set).1 . but still minimize instead of blocking other computer programs. Contrary to my opinion before undertaking extensive usage of C# I now believe the reduced development time. • Implement the cascade training algorithm in C# instead of C++. Implement the back-propagation algorithm in C# instead of C++.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 8. is worth the possibility of reduced performance. allowing more to be undertaken successfully.2 . I would experiment with the following training algorithm improvements: • Improve the genetic algorithms crossover and mutation algorithms. as well as implementing roulette wheel selection.

The earlier attempts at genetic algorithm and neural network implementations are not included. The code contains line numbering for pinpointing the precise lines in question within the body-text when crossreferencing.Appendices 9. Page 74 of 225 . Although there was another section of the program compiled from source (the FANN library). This section also contains only the final draft of the code. which this code is partially based upon.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9 . and neither is the code for the prototype constructed in milestone two. so that this listing contains only code I have personally written for the project.Source code The source code in this section is organised first by which part of the application it represents and then by what source file it originally resided in. I have not included it in the source code listing.1 .

Classes { public class NetworkUpdateEventArgs : System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.Text. private int _CC_Reports.Controller.Collections. //Cascade Correlation private double _CC_Terr.1. //Dataset identifier private int _TrDataSet.1 .“NetworkUpdatedEventHandler” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 using using using using System. System.EventArgs { #region data /********************\ |* DATA *| \********************/ //Training Algorithm Identifier private int trainingalg. System.1. System. Page 75 of 225 . private double _CC_LearningRate.View 9. private int _CC_MaxNeurons. namespace _05025397.1.Generic. private bool _CC_ITerr.1 .Linq.

private double _BP_LearningRate. private int _GA_Mutation.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 //Genetic Algorithm private int _GA_PopSize. double CCLearningRate. private int _GA_HiddenL. int CCReports. private int _BP_HiddenL. private int _GA_GenLimit. private double _BP_Terr. private double _GA_Terr. bool CCITerr. private bool _BP_ITerr. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ //Constructor to accept all necessary data public NetworkUpdateEventArgs (int Algorithm. int CCMaxNeurons. int GAPopSize. double CCTerr. private int _GA_Crossover. //BackProp private int _BP_EpochLimit. private int _BP_Reports. private int _GA_Reports. Page 76 of 225 . private bool _GA_ITerr.

double BPTerr. _CC_MaxNeurons = CCMaxNeurons. _GA_Terr = GATerr. int GAMutation. int BPReports. _GA_GenLimit = GAGenLimit. double GATerr. _CC_Reports = CCReports. _CC_ITerr = CCITerr. int GAReports. _CC_LearningRate = CCLearningRate. int TrData_Set) { //Algorithm trainingalg = Algorithm.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 int GAGenLimit. bool GAITerr. //Backpropagation Algorithm _BP_EpochLimit = BPEpochLimit. double BPLearningRate. int BPHiddenL. _GA_HiddenL = GAHiddenL. _GA_Crossover = GACrossOver. //Genetic Algorithm _GA_PopSize = GAPopSize. int BPEpochLimit. _GA_Mutation = GAMutation. int GACrossOver. //Cascade Correlation _CC_Terr = CCTerr. bool BPITerr. int GAHiddenL. Page 77 of 225 . _GA_ITerr = GAITerr. _GA_Reports = GAReports.

} } public double CC_LearningRate { get { return _CC_LearningRate.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 _BP_HiddenL = BPHiddenL. } } #endregion #region genetic_algorithms //Genetic Algorithm public int GA_PopSize { get { return _GA_PopSize. } } public int CC_Reports { get { return _CC_Reports. _TrDataSet = TrData_Set. _BP_ITerr = BPITerr. } } public bool CC_ITerr { get { return _CC_ITerr. _BP_Reports = BPReports. } #endregion #region getters/setters #region cascade_correlation //Cascade Correlation public double CC_Terr { get { return _CC_Terr. _BP_LearningRate = BPLearningRate. } } public int GA_GenLimit Page 78 of 225 . } } public int CC_MaxNeurons { get { return _CC_MaxNeurons. _BP_Terr = BPTerr.

} } public bool GA_ITerr { get { return _GA_ITerr.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 { get { return _GA_GenLimit. } } #endregion #region back_propagation //Back propagation public int BP_EpochLimit { get { return _BP_EpochLimit. } } public int GA_Crossover { get { return _GA_Crossover. } } public double BP_LearningRate { get { return _BP_LearningRate. } } public double GA_Terr { get { return _GA_Terr. } } public int GA_HiddenL { get { return _GA_HiddenL. } } #endregion Page 79 of 225 . } } public int BP_HiddenL { get { return _BP_HiddenL. } } public int GA_Mutation { get { return _GA_Mutation. } } public bool BP_ITerr { get { return _BP_ITerr. } } public double BP_Terr { get { return _BP_Terr. } } public int GA_Reports { get { return _GA_Reports. } } public int BP_Reports { get { return _BP_Reports.

IO. } } public int TrainingAlgorithm { get { return trainingalg.Drawing. System.1. System.“FrmMain.Controller { public partial class FrmMain : Form Page 80 of 225 . using ZedGraph.Linq. System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000147 00000148 00000149 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 } #endregion #region external //General public int TrDataSet { get { return _TrDataSet.Forms.ComponentModel.Generic. System. System.Windows.2 .Text. System. using _05025397. System. System. namespace _05025397.Collections.Data.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 using using using using using using using using using System. } } #endregion } 9.1.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 { #region data /********************\ |* DATA *| \********************/ //The neuralnetwork Controller.Resources. bw. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public FrmMain() { InitializeComponent(). bw.Working(). //Working dialog Controller. //Background thread static BackgroundWorker bw = new BackgroundWorker().Working workingdialog = new Controller.DoWork += new DoWorkEventHandler(bw_DoWork). } #endregion #region internal_functions /********************\ Page 81 of 225 .icon.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted).TrainingAlgorithm network.Icon = Properties.Algorithms. this.

SymbolType.zedGraphControl1.TrDataSet).Text = "Mean Squared Error".Title.Red.Text = "Iteration".AddCurve("".Legend.Clear(). this.AxisChange().zedGraphControl1.IsVisible = false. MSEpane.Circle). // Hide the legend MSEpane. Color.XAxis. MSE.Title. } private void updatenetwork(object sender.GraphPane.YAxis.NetworkUpdateEventArgs e) { LoadDataset(e.TrainingAlgorithm == 0) { Page 82 of 225 . MSEpane.Title.GraphPane.zedGraphControl1. Classes.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 |* INTERNAL *| \********************/ private void plotgraphsMSE(PointPairList MSE) { this. //Titles MSEpane. GraphPane MSEpane = this.CurveList. MSEpane.Text = "MSE". try { if (e.

CC_MaxNeurons.CC_Reports. e. e. e.GA_Mutation.TrainingAlgorithm == 2) { network = new Controller.BP_Reports.CC_Terr. e. txtTestData).BP_LearningRate. e. e.GA_GenLimit. } if (e. e. txtTrainData.Algorithms.BP_HiddenL.GA_ITerr. e. txtTrainData.CascadeCorrelation (e. } } catch { Page 83 of 225 .CC_ITerr. txtTestData).BackProp (e.BP_Terr. txtTestData).GA_PopSize. e.CC_LearningRate. e.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 network = new Controller.GA_Terr. e.GA_HiddenL.Algorithms. txtTrainData.BP_EpochLimit. e.GeneticAlgorithm (e. e.Algorithms. } if (e.GA_Crossover.GA_Reports. e.TrainingAlgorithm == 1) { network = new Controller.BP_ITerr. e. e.

} if (network != null) { txtNetSettings. } private void LoadDataset(int Data_set) { switch (Data_set) { case (int)Dataset. MessageBoxButtons.OK.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 MessageBox.NetworkUpdateHandler(updatenetwork). wizard.ShowDialog().Resources.Text = Properties.XORtest. Application.Show("Error creating new network".XORtrain.network_details(). MessageBoxIcon. "Error".Text = Properties. txtTestData. } } private void new_network() { Controller.Resources.Exit().XOR: txtTrainData.Wiz1 wizard = new Controller.Wiz1.NetworkUpdated += new Controller.Text = network.Wiz1(). wizard. Page 84 of 225 .Error).

FISHERtrain.OK.\\".Resources. case (int)Dataset. try { train = LoadTxtFile(trainpath).Error).Text = Properties. "Select Training Data"). } } private void LoadCustomDataSet() { string trainpath = SelectTextFile(". string testpath = SelectTextFile(".\\". break. case (int)Dataset.FISHER: txtTrainData. MessageBoxButtons.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 break. "Select Testing Data"). } catch { MessageBox. string train = "".Text = Properties. txtTestData. string test = "".Resources.CUSTOM: LoadCustomDataSet(). break. "Error!". } Page 85 of 225 . MessageBoxIcon.Show("Error: Problem loading training data".FISHERtest.

MessageBoxButtons. "Error!". FileStream File = null. } private string LoadTxtFile(string path) { string data = "".Read). } catch { MessageBox.OK. MessageBoxIcon.Text = train.Text = test.Error). data = Reader. Reader = new StreamReader(File).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 try { test = LoadTxtFile(testpath). MessageBoxButtons. MessageBoxIcon. FileMode.ReadToEnd(). txtTrainData. } txtTestData.Open. Page 86 of 225 .Error). } catch { MessageBox.OK.Show("Error reading selected file: " + path. try { File = new FileStream(path. FileAccess.Show("Error: Problem loading testing data". "Error!". StreamReader Reader = null.

Title = title.Filter = "txt files (*.FileName : null. } #endregion #region menu_items /********************\ |* MENU ITEMS *| \********************/ Page 87 of 225 .Close().ShowDialog() == DialogResult.txt|All files (*. } return data.InitialDirectory = initialDirectory. } private string SelectTextFile(string initialDirectory. if (File != null) File. dialog.OK) ? dialog. } finally { if (Reader != null) Reader. dialog.txt)|*. return (dialog.Close(). dialog.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 data = "".*)|*.*". string title) { OpenFileDialog dialog = new OpenFileDialog().

Show(). } #endregion #region buttons Page 88 of 225 . EventArgs e) { if (network != null) { if (network.Trained != false) { network. EventArgs e) { this. } } } private void aboutToolStripMenuItem1_Click(object sender. EventArgs e) { FrmAbout About = new FrmAbout(). } private void runToolStripMenuItem_Click(object sender. } private void exitToolStripMenuItem_Click(object sender.Close().Test().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000228 00000229 00000230 00000231 00000232 00000233 00000234 00000235 00000236 00000237 00000238 00000239 00000240 00000241 00000242 00000243 00000244 00000245 00000246 00000247 00000248 00000249 00000250 00000251 00000252 00000253 00000254 00000255 00000256 00000257 private void newNetworkToolStripMenuItem_Click(object sender. EventArgs e) { new_network(). About.

} } } } private void btnTest_Click(object sender.IsBusy) { if (network != null) { if (!network. EventArgs e) { new_network().Trained) Page 89 of 225 . if (!bw. workingdialog. EventArgs e) { //Shouldn't be busy if the user managed to click this //but just make 100% certain. } private void btnTrain_Click(object sender.ShowDialog().Trained) { bw. EventArgs e) { if (network != null) { if (network.RunWorkerAsync().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000258 00000259 00000260 00000261 00000262 00000263 00000264 00000265 00000266 00000267 00000268 00000269 00000270 00000271 00000272 00000273 00000274 00000275 00000276 00000277 00000278 00000279 00000280 00000281 00000282 00000283 00000284 00000285 00000286 00000287 /********************\ |* BUTTONS *| \********************/ private void btnNew_Click(object sender.

ScrollToCaret().Tested) { bool success = network.Refresh(). txtOutput.ReportData.Train().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000288 00000289 00000290 00000291 00000292 00000293 00000294 00000295 00000296 00000297 00000298 00000299 00000300 00000301 00000302 00000303 00000304 00000305 00000306 00000307 00000308 00000309 00000310 00000311 00000312 00000313 00000314 00000315 00000316 00000317 { if (!network.Length. txtOutput.Clear(). DoWorkEventArgs e) { bool success = true.Text. txtOutput.SelectionStart = txtOutput.Test().Text = network. } } } } } #endregion #region bg_worker /********************\ |* BG WORKER *| \********************/ private void bw_DoWork(object sender. if (network != null) { success = network. txtOutput. } Page 90 of 225 . if (success) { txtOutput.

} success = (bool) e.Error).Clear().Result. } private void bw_RunWorkerCompleted(object sender. MessageBoxIcon. if (success) { txtOutput.OK. RunWorkerCompletedEventArgs e) { bool success. plotgraphsMSE(network.Show("Error training network!". MessageBoxIcon.ReportData.Length. MessageBoxButtons. txtOutput.ScrollToCaret(). txtOutput. txtOutput. } Page 91 of 225 .SelectionStart = txtOutput.Error). //Check for errors if (e.OK. MessageBoxButtons.getGraphData()). "Error training network!". } else { MessageBox.Text = network.ToString().Error.Show("Error during network train: " + e.Text.Error != null) { MessageBox.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000318 00000319 00000320 00000321 00000322 00000323 00000324 00000325 00000326 00000327 00000328 00000329 00000330 00000331 00000332 00000333 00000334 00000335 00000336 00000337 00000338 00000339 00000340 00000341 00000342 00000343 00000344 00000345 00000346 00000347 e. "Error".Result = success.

“Wiz1.ComponentModel. System.Linq. System.Collections.Data. } #endregion } 9.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000348 00000349 00000350 00000351 00000352 00000353 00000354 00000355 00000356 00000357 00000358 00000359 00000360 00000361 00000362 00000363 00000364 00000365 00000366 } //Remove the dialog locking out the main form //and showing that we're currently working workingdialog.1. System. System. } #endregion #region misc /********************\ |* MISC *| \********************/ private void FrmMain_Shown(object sender.Generic.1. EventArgs e) { //Run the new neural net wizard new_network().3 .Drawing.cs” 00000001 00000002 00000003 00000004 00000005 00000006 using using using using using using System. Page 92 of 225 .Hide(). System.

using System. public event NetworkUpdateHandler NetworkUpdated.Controller { #region enums /********************\ |* ENUMS *| \********************/ enum Training { GA. Page 93 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 using System. CC }. BP.Forms. CUSTOM }. private int Trainset. private int algorithm. FISHER.Windows. enum Dataset { XOR.Text. #endregion public partial class Wiz1 : Form { #region data /********************\ |* DATA *| \********************/ public delegate void NetworkUpdateHandler(object sender. namespace _05025397.NetworkUpdateEventArgs e). Classes. using _05025397.

icon. EventArgs e) Page 94 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 #endregion data #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public Wiz1() { InitializeComponent(). } #endregion #region control_events /********************\ |* CONTROL EVENTS *| \********************/ #region buttons /********************\ |* BUTTONS *| \********************/ private void btnCancel_Click(object sender. EventArgs e) { this.XOR. } private void rBtnBackprop_CheckedChanged(object sender.GA. Trainset = (int)Dataset.Close(). this. algorithm = (int)Training.Resources.Icon = Properties.

panCC.Visible = false.IMAGECascadeCorrelation.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 { if (rBtnBackprop.Visible = true.Image = _05025397. panGeneticAlgorithm. panBackprop.Resources. panCC. lblBP. lblGA.Properties. panGeneticAlgorithm.Visible = false.Visible = true. algorithm = (int)Training.BP.Visible = true. Page 95 of 225 . panBackprop.Resources.Visible = true.Visible = false.Properties.IMAGEBackProp. lblCC. lblBP.Checked == true) { picAlg. } } private void rBtnCascadeCorrelation_CheckedChanged(object sender.Visible = false.Checked == true) { picAlg.Visible = false.Image = _05025397.CC. lblCC.Visible = false. algorithm = (int)Training.Visible = false. lblGA.Visible = false. EventArgs e) { if (rBtnCascadeCorrelation.

EventArgs e) { Page 96 of 225 . panCC.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 } } private void rBtnGeneticAlgorithm_CheckedChanged(object sender. EventArgs e) { if (rBtnGeneticAlgorithm.GA.Visible = false. panBackprop. } } private void btnNext_Click(object sender.Properties.IMAGEGeneticAlgorithm.1).SelectedIndex > 0) { tabControl1. EventArgs e) { if (tabControl1.Visible = true.Image = _05025397. panGeneticAlgorithm.Visible = false. lblCC.Visible = false.Visible = false.Visible = true.Checked == true) { picAlg. lblBP. } } private void btnBack_Click(object sender. lblGA.SelectedIndex .SelectTab(tabControl1.Resources. algorithm = (int)Training.

txtGA_HiddenNodes.SelectedIndex < tabControl1. check.Text. check. txtBP_EpochLimit.GA_Crossover.Text. } else { VerifyData check = new VerifyData (algorithm.Text.1) { tabControl1. txtGA_Terr.Text. check.Verified) { Classes.GA_Terr.Text.GA_GenLimit. check. txtGA_PopSize. txtCC_LearningRate.CC_ITerr.BP_EpochLimit.Checked. txtBP_LearnRate.Text. check. txtGA_MaxGen.Text.TrainingAlgorithm.CC_MaxNeurons.NetworkUpdateEventArgs (check. check.GA_HiddenL.GA_ITerr.Text. Page 97 of 225 .SelectTab(tabControl1. txtCC_Report.GA_Mutation. txtGA_ProbM.Text.GA_PopSize. check. check. check.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 00000150 00000151 00000152 00000153 00000154 00000155 00000156 if (tabControl1. if (check.BP_HiddenL.CC_Reports. txtBP_Report.Text.GA_Reports. check. Trainset). txtCC_MaxNeurons. check.TabCount . txtBP_HiddenNodes.Text. check. txtGA_ProbX. chkBP_IgnoreTarget. check.SelectedIndex + 1). CC_ITerr. txtCC_TargetError. chkGA_IgnoreTarget.CC_Terr.Checked. txtBP_Terr. check.NetworkUpdateEventArgs args = new Classes.CC_LearningRate.Text.Checked.Text.Text. check.Text.Text. txtGA_Report.

Show("Error Checking Data: Data could not be verified". } } } #endregion #region checkboxes /********************\ |* CHECKBOXES *| \********************/ private void chkDatasetFisher_CheckedChanged(object sender.BP_LearningRate.Checked) { picDataset.Properties.BP_Terr. EventArgs e) Page 98 of 225 . Trainset = (int)Dataset.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 00000180 00000181 00000182 00000183 00000184 00000185 00000186 check. check. "Error Checking Data". this.Exclamation).Dispose(). MessageBoxIcon.Resources.BP_ITerr. MessageBoxButtons. check.FISHER. } else { MessageBox.OK. check.Image = _05025397. NetworkUpdated(this. check.IMAGEIrisFlowers.BP_Reports. EventArgs e) { if (chkDatasetFisher. args).TrDataSet). } } private void chkDatasetXOR_CheckedChanged(object sender.

Trainset = (int)Dataset.XOR.Image = _05025397.Checked) { picDataset. Trainset = (int)Dataset.IMAGEQuestionMark.Checked) { picDataset.Properties.SelectedIndex == 1) { if (panCC.CUSTOM.Focus().Properties.IMAGEXOR. EventArgs e) { if (tabControl1. } } private void chkDatasetCustom_CheckedChanged(object sender.Image = _05025397.Visible == true) { txtCC_TargetError.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 00000210 00000211 00000212 00000213 00000214 00000215 00000216 { if (chkDatasetXOR.Resources.Resources. EventArgs e) { if (chkDatasetCustom. } } #endregion #region misc /********************\ |* MISC *| \********************/ private void tabControl1_SelectedIndexChanged(object sender. } Page 99 of 225 .

Focus(). tabControl1.Visible == true) { txtBP_EpochLimit. } #endregion #endregion } Page 100 of 225 .Focus().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 00000228 00000229 00000230 00000231 00000232 00000233 00000234 00000235 00000236 } if (panBackprop.icon.Icon = Properties. } if (panGeneticAlgorithm.Resources. EventArgs e) { this.Visible == true) { txtGA_PopSize.SelectTab(0). } } } private void Wiz1_Load(object sender.

Forms.Collections.“FrmAbout. System.4 . } #endregion #region form /********************\ |* FORM *| \********************/ private void FrmAbout_Load(object sender. System.1.Linq.Text. EventArgs e) { Page 101 of 225 .Drawing.Controller { public partial class FrmAbout : Form { #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public FrmAbout() { InitializeComponent(). System.Data. System. System. System. System.Windows.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using using using System.Generic.ComponentModel.1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. namespace _05025397.

MessageBoxButtons.Text = Application.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 this.OK.Diagnostics. } } private void linkLabel4_LinkClicked(object sender.Text).Resources. LinkLabelLinkClickedEventArgs e) Page 102 of 225 .Information). label5.Icon = Properties.Text = Properties.Process. textBox5.LinkVisited = true.LICENCE_LGPL21.ProductVersion.Show("No e-mail program defined. LinkLabelLinkClickedEventArgs e) { // Specify that the link was visited. MessageBoxIcon.linkLabel1. } catch { MessageBox. try { // Navigate to a URL.". this. System.Start("mailto:" + linkLabel1. "Undefined Application".Resources.icon. } #endregion #region linklabels /********************\ |* LINKLABELS *| \********************/ private void linkLabel1_LinkClicked(object sender.

MessageBoxIcon. } catch { MessageBox.". LinkLabelLinkClickedEventArgs e) { try { System. MessageBoxIcon. MessageBoxButtons.". } } private void linkLabel5_LinkClicked(object sender. "Error".Diagnostics. "Error".Show("Error opening link.Diagnostics. } } private void linkLabel6_LinkClicked(object sender. MessageBoxButtons.Start (linkLabel4.Show("Error opening link. LinkLabelLinkClickedEventArgs e) Page 103 of 225 .Text).Process.Error).Process.OK.Text).Start (linkLabel5.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 { try { System.OK. } catch { MessageBox.Error).

OK.Show("Error opening link. "Error".Diagnostics.uk").Text). } catch { MessageBox.".Start ("http://www.Error).Process. } } private void linkLabel2_LinkClicked(object sender.Process.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 { try { System. LinkLabelLinkClickedEventArgs e) { try { System.Error). LinkLabelLinkClickedEventArgs e) Page 104 of 225 . MessageBoxButtons. } } private void linkLabel7_LinkClicked(object sender.snikks. MessageBoxButtons.co.OK. MessageBoxIcon. "Error".Start (linkLabel6.Show("Error opening link. MessageBoxIcon.Diagnostics. } catch { MessageBox.".

1.Process.Visible != true) { groupBox1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 { try { System. } } #endregion #region buttons /********************\ |* BUTTONS *| \********************/ private void btnDatasets_Click(object sender.org/licenses/old-licenses/lgpl-2.Error). MessageBoxButtons.Show("Error opening link.OK.Visible = false.Visible = false. EventArgs e) { if (tabControl1.Diagnostics.".html"). tabControl1. "Error".Visible = true. groupBox2. MessageBoxIcon.gnu.Start ("http://www.Visible = false. } catch { MessageBox. } else Page 105 of 225 . pictureBox1.

tabControl1.Visible = groupBox2. Page 106 of 225 .Dispose().Visible pictureBox1.Visible = false. = false. groupBox1.Visible = true. tabControl1. pictureBox1.Visible = false.Visible = false.Visible = true. groupBox2.Visible = true.Visible = tabControl1.Visible = false. } } private void btnOkay_Click(object sender. = true.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 { groupBox1. EventArgs e) { this.Visible = false.Visible != true) { groupBox2. false. } true. pictureBox1.Visible } } private void btnLicences_Click(object sender. } else { groupBox1. EventArgs e) { if (groupBox2.

Text = Properties. EventArgs e) { if (radioButton3.Text = Properties.Checked == true) textBox5.Resources. } private void radioButton3_CheckedChanged(object sender.Resources. EventArgs e) { if (radioButton1. } private void radioButton2_CheckedChanged(object sender.LICENCE_LGPL30. EventArgs e) { if (radioButton2.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 } #endregion #region radiobuttons /********************\ |* RADIOBUTTONS *| \********************/ private void radioButton1_CheckedChanged(object sender.Resources.LICENCE_GPL30.Checked == true) textBox5.Checked == true) textBox5.Text = Properties.LICENCE_LGPL21. } #endregion } Page 107 of 225 .

} private void Working_Click(object sender. System.1.Forms.Data.Windows. System.“Working.Resources. EventArgs e) { System.Collections.Drawing. System.Beep.Play(). } #endregion } Page 108 of 225 .Linq.Icon = Properties.icon.ComponentModel.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using using using System.SystemSounds. this. System.5 . System. System.Text. namespace _05025397.1.Media.Generic.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. System.Controller { public partial class Working : Form { #region functions /********************\ |* FUNCTIONS *| \********************/ public Working() { InitializeComponent().

System. System.Generic. //Training Algorithm Identifier //1 = genetic algorith //2 = backprop //3 = cascade correlation private int trainingalg. System.1.Forms. //Dataset identifer //0 = XOR Page 109 of 225 . System.1.Collections.1 .cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 using using using using using System. namespace _05025397.Windows.Controller 9.Controller { public class VerifyData { #region data /********************\ |* DATA *| \********************/ //Dataverified variable private bool verified.Linq.2 .2.“VerifyData.Text.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 } 9.

private int _BP_HiddenL. private bool _GA_ITerr. //BackProp private int _BP_EpochLimit. private double _CC_LearningRate. #endregion Page 110 of 225 . private int _GA_Reports. private double _BP_Terr. private int _GA_GenLimit. private int _GA_Mutation. private int _CC_Reports. private bool _BP_ITerr. private int _GA_HiddenL. //Cascade Correlation private double _CC_Terr. private int _GA_Crossover. private int _CC_MaxNeurons. private bool _CC_ITerr. //Genetic Algorithm private int _GA_PopSize. private double _GA_Terr.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000026 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 //1 = FISHERS IRIS DATA //2 = CUSTOM private int _TDataSet. private double _BP_LearningRate. private int _BP_Reports.

string GAHiddenL. string CCReports. //DataSet _TDataSet = TData_Set. string BPReports. string GAReports. string BPLearningRate.XOR && _TDataSet != Page 111 of 225 . string GACrossOver. //Algorithm trainingalg = Algorithm. bool CCITerr. bool BPITerr. string CCTerr. string CCLearningRate. string GAPopSize. string BPEpochLimit. string BPHiddenL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000056 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public VerifyData (int Algorithm. string GAMutation. int TData_Set) { //Initially set to true. bool GAITerr. will be changed to //false if any data verification errors occur verified = true. string CCMaxNeurons. if (_TDataSet != (int)Dataset. string GAGenLimit. string BPTerr. string GATerr.

MessageBoxButtons. _CC_ITerr = CCITerr. "Parsing Error".CUSTOM) verified = false. _CC_Reports = Convert.ToSingle(CCTerr). MessageBoxIcon.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000086 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 (int)Dataset.Error). if (verified) { if (trainingalg == 2) { try { //Cascade correlation _CC_Terr = Convert.Show ("Parsing Error: Please check supplied cascade" + "correlation data values". } catch (FormatException) { MessageBox. verified = false.FISHER && _TDataSet != (int)Dataset.ToSingle(CCLearningRate). _CC_MaxNeurons = Convert.OK. } } } Page 112 of 225 .ToInt32(CCMaxNeurons).ToInt32(CCReports). _CC_LearningRate = Convert.

"Parsing Error".Show ("Parsing Error: Please check supplied" +" genetic algorithm data values". MessageBoxIcon.ToInt32(GAHiddenL). } } } if (verified) Page 113 of 225 .ToInt32(GAGenLimit). MessageBoxButtons. _GA_GenLimit = Convert.ToSingle(GATerr).ToInt32(GAReports). _GA_ITerr = GAITerr. verified = false.ToInt32(GAMutation). _GA_Mutation = Convert. } catch (FormatException) { MessageBox. _GA_Terr = Convert. _GA_Crossover = Convert.Error).OK.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000116 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 if (verified) { if (trainingalg == 0) { try { //Genetic algorithm _GA_PopSize = Convert. _GA_Reports = Convert.ToInt32(GACrossOver).ToInt32(GAPopSize). _GA_HiddenL = Convert.

_BP_HiddenL = Convert. verified = false.ToInt32(BPEpochLimit). _BP_ITerr = BPITerr.OK. } Page 114 of 225 .Error).ToDouble(BPTerr).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000146 00000147 00000148 00000149 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 { if (trainingalg == 1) { try { //Back-propagation _BP_EpochLimit = Convert. _BP_Reports = Convert. _BP_LearningRate = Convert. _BP_Terr = Convert. "Parsing Error". MessageBoxIcon. } catch (FormatException) { MessageBox.Show ("Parsing Error: Please check " + "supplied back propagation data values".ToInt32(BPHiddenL).ToInt32(BPReports).ToDouble(BPLearningRate). } } } if (verified) { verified = checkforsanity(). MessageBoxButtons.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000176 00000177 00000178 00000179 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 } #endregion #region internal /********************\ |* INTERNAL *| \********************/ private bool checkGAsanity() { bool sanity = true. Page 115 of 225 . if (_GA_PopSize < 3) sanity = false. } if (_GA_Terr < 0 || _GA_Terr > 1) sanity = false. if (_GA_GenLimit < 1) sanity = false. } if (_GA_Mutation < 0 || _GA_Mutation > 100) { sanity = false. if (_GA_Crossover < 0 || _GA_Crossover > 100) { sanity = false.

MessageBoxButtons. } private bool checkBPsanity() { bool sanity = true. Page 116 of 225 . if (!sanity) MessageBox. if (_GA_HiddenL < 1) sanity = false.Show ("Sanity Check Fail: Please check supplied " + "genetic algorithm data values".Error).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000206 00000207 00000208 00000209 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 00000228 00000229 00000230 00000231 00000232 00000233 00000234 00000235 if (_GA_Reports < 1 || _GA_Reports > _GA_GenLimit) sanity = false. if (_BP_HiddenL < 1) sanity = false. if (_BP_EpochLimit < 1) sanity = false. return sanity. "Sanity Check Fail". MessageBoxIcon. if (_BP_Reports < 1 || _BP_Reports > _BP_EpochLimit) sanity = false.OK.

if (!sanity) MessageBox.Error). if (_CC_Terr < 0 || _CC_Terr > 1) sanity = false. "Sanity Check Fail". MessageBoxButtons.Show ("Sanity Check Fail: Please check supplied " + "back propagation data values". return sanity. } private bool checkCCsanity() { bool sanity = true.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000236 00000237 00000238 00000239 00000240 00000241 00000242 00000243 00000244 00000245 00000246 00000247 00000248 00000249 00000250 00000251 00000252 00000253 00000254 00000255 00000256 00000257 00000258 00000259 00000260 00000261 00000262 00000263 00000264 00000265 if (_BP_Terr < 0 || _BP_Terr > 1) sanity = false. if (_CC_MaxNeurons < 1) sanity = false. if (_CC_Reports < 1 || _CC_Reports > _CC_MaxNeurons) sanity = false.OK. MessageBoxIcon. Page 117 of 225 . if (_BP_LearningRate < 0 || _BP_LearningRate > 1) sanity = false.

return sanity. MessageBoxIcon. if they're greater than 1 but less than 100 scale them appropriately. "Sanity Check Fail". // // // // // bool Basically we're making sure the data is clean before it goes anywhere near a DLL. } private bool checkforsanity() { //Some of the values that are allowed through here would not produce //good results .OK.Show ("Sanity Check Fail: Please check supplied " + "cascade correlation data values".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000266 00000267 00000268 00000269 00000270 00000271 00000272 00000273 00000274 00000275 00000276 00000277 00000278 00000279 00000280 00000281 00000282 00000283 00000284 00000285 00000286 00000287 00000288 00000289 00000290 00000291 00000292 00000293 00000294 00000295 if (_CC_LearningRate < 0 || _CC_LearningRate > 1) sanity = false. sanity = true. only //halt any values from proceeding that are so wrong they could cause //serious errors later down the line. MessageBoxButtons. We're also doing some very minor processing of the probability values.however I don't want to prevent any flexibility. if (!sanity) MessageBox.Error). if (trainingalg < 0 || trainingalg > 2) Page 118 of 225 .

} } return sanity. } if (sanity) { if (trainingalg == 0) { sanity = checkGAsanity(). MessageBoxIcon. "Sanity Check Fail". MessageBoxButtons.Error). MessageBox.Show ("Sanity Check Fail: Training Algorithm not " + "selected or selected incorrectly". } Page 119 of 225 . } if (trainingalg == 2) { sanity = checkCCsanity().OK. } if (trainingalg == 1) { sanity = checkBPsanity().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000296 00000297 00000298 00000299 00000300 00000301 00000302 00000303 00000304 00000305 00000306 00000307 00000308 00000309 00000310 00000311 00000312 00000313 00000314 00000315 00000316 00000317 00000318 00000319 00000320 00000321 00000322 00000323 00000324 00000325 { sanity = false.

} } public double CC_LearningRate { get { return _CC_LearningRate. } } public int GA_GenLimit { get { return _GA_GenLimit.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000326 00000327 00000328 00000329 00000330 00000331 00000332 00000333 00000334 00000335 00000336 00000337 00000338 00000339 00000340 00000341 00000342 00000343 00000344 00000345 00000346 00000347 00000348 00000349 00000350 00000351 00000352 00000353 00000354 00000355 #endregion #region getters/setters /********************\ |* EXTERNAL *| \********************/ #region cascade_correlation //Cascade Correlation public double CC_Terr { get { return _CC_Terr. } } public int CC_Reports { get { return _CC_Reports. } } public int GA_Mutation Page 120 of 225 . } } #endregion #region genetic_algorithm //Genetic Algorithm public int GA_PopSize { get { return _GA_PopSize. } } public bool CC_ITerr { get { return _CC_ITerr. } } public int GA_Crossover { get { return _GA_Crossover. } } public int CC_MaxNeurons { get { return _CC_MaxNeurons.

} } #region back_propagation //Back propagation public int BP_EpochLimit { get { return _BP_EpochLimit. } } _GA_ITerr. } } #endregion #endregion #region external //Verification Page 121 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000356 00000357 00000358 00000359 00000360 00000361 00000362 00000363 00000364 00000365 00000366 00000367 00000368 00000369 00000370 00000371 00000372 00000373 00000374 00000375 00000376 00000377 00000378 00000379 00000380 00000381 00000382 00000383 00000384 00000385 { get { return public double GA_Terr { get { return public int GA_Reports { get { return public int GA_HiddenL { get { return public bool GA_ITerr { get { return #endregion _GA_Mutation. } } public double BP_Terr { get { return _BP_Terr. } } public bool BP_ITerr { get { return _BP_ITerr. } } public int BP_Reports { get { return _BP_Reports. } } public int BP_HiddenL { get { return _BP_HiddenL. } } _GA_Terr. } } _GA_Reports. } } public double BP_LearningRate { get { return _BP_LearningRate. } } _GA_HiddenL.

} } //TrainingAlgorithm public int TrainingAlgorithm { get { return trainingalg.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000386 00000387 00000388 00000389 00000390 00000391 00000392 00000393 00000394 00000395 00000396 00000397 00000398 } public bool Verified { get { return verified. } } #endregion } Page 122 of 225 . } } //Dataset public int TrDataSet { get { return _TDataSet.

private ArrayList teinputs.Collections. System. System. private ArrayList aldata. Page 123 of 225 .1.“DatasetParser. private ArrayList trinputs.Forms. private string[] test. private ArrayList altest.2 .2.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. private string[] data.Text. System. System.Collections.Linq.Windows. System.Controller { public class DatasetParser { #region data /********************\ |* DATA *| \********************/ private bool _Verified. private ArrayList troutputs.Generic. private ArrayList teoutputs. namespace _05025397.

txtTest.Text. ""). '\r'.Text.Text = txtTest.'.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 //private Scaler scale.' }.Text. private double Noutputs. teoutputs = new ArrayList(). trinputs = new ArrayList(). aldata = new ArrayList(). private double Ninputs. Page 124 of 225 . teinputs = new ArrayList(). RichTextBox txtTest) { //Data init bool Verified = true. //Strip carriage returns and trim txtData. txtTest. '.Text = txtData. ' '. char[] delimiterChars = { '\n'. ""). txtData. private double Niterations.Text = txtData. troutputs = new ArrayList().Replace("\r".Trim().Text. altest = new ArrayList(). #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public DatasetParser(RichTextBox txtData.Text = txtTest.Replace("\r". '.Trim(). '\t'.

Noutputs = System.Split(delimiterChars). number of inputs //and number of outputs Niterations = System.ToDouble(data[0]).Text. i++) Page 125 of 225 . //Get the number of iterations. try { for (int i = 3.Convert.Length. Ninputs = System. } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private bool parseTrainingData() { bool success = true. i < data.Convert.Text.ToDouble(data[2]).ToDouble(data[1]).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 //Split at the chars specified above data = txtData.Split(delimiterChars). Verified = parseTestingData(). //parse the data into appropriately //structured arrays Verified = parseTrainingData().Convert. _Verified = Verified. test = txtTest.

"Error".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 { aldata. j++) { troutputs. j < Noutputs. MessageBoxIcon. aldata. } return success. Page 126 of 225 . MessageBoxButtons.Show("Error parsing training data".OK.Convert.RemoveAt(0). } for (int j = 0. aldata. i++) { trinputs. i < Ninputs.Error).RemoveAt(0). } while (aldata. } } } catch { MessageBox. success = false.Count > 0) { for (int i = 0. } private bool parseTestingData() { bool success = true.Add(System.ToDouble(data[i])).Add(aldata[0]).Add(aldata[0]).

altest. j++) { teoutputs. altest. i < Ninputs. "Error".Add(System. } } } catch { MessageBox.Length.Add(altest[0]). i < data. } return success. MessageBoxButtons. } while (altest.ToDouble(data[i])).RemoveAt(0). j < Noutputs. success = false.RemoveAt(0). i++) { altest. MessageBoxIcon.Error). } for (int j = 0.Count > 0) { for (int i = 0.Show("Error parsing testing data".Add(altest[0]). i++) { teinputs. } Page 127 of 225 .OK.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 try { for (int i = 3.Convert.

out double Nout) { Ninp = Ninputs.Count / (int)Noutputs).Count / (int) Ninputs][]. j+=(int) Ninputs.Count. l = 0. k++) { Page 128 of 225 . i++) trinputdata[i] = new double[(int) Ninputs]. for (int i = 0. for (int i = 0. for (int j = 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public void GetStructure(out double iter. k < Ninputs. i < (trinputs. } public void GetTrainingData (out double[][] trinputdata. troutputdata = new double[troutputs. iter = Niterations.Count / (int)Ninputs). out double Ninp. Nout = Noutputs. l++) { for (int k = 0. i < (troutputs. j < trinputs. i++) troutputdata[i] = new double[(int) Noutputs]. out double[][] troutputdata) { trinputdata = new double[trinputs.Count / (int) Noutputs][].

j < troutputs. i < (teinputs.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 trinputdata[l][k] = (double) trinputs[j + } } for (int j = 0.Count. teoutputdata = new double[teoutputs. j < teinputs. j += (int)Noutputs. k++) { teinputdata[l][k] = (double)teinputs[j + k]. k < Ninputs. l = 0. l++) { for (int k = 0. l++) { for (int k = 0.Count / (int)Noutputs). i < (teoutputs. l = 0. i++) teoutputdata[i] = new double[(int)Noutputs]. out double[][] teoutputdata) { teinputdata = new double[teinputs. Page 129 of 225 .Count.Count / (int)Noutputs][]. for (int j = 0.Count / (int)Ninputs][]. k < Noutputs.Count / (int)Ninputs). for (int i = 0. k++) { troutputdata[l][k] = (double)troutputs[j + k]. j += (int)Ninputs. } } } public void GetTestingData (out double[][] teinputdata. for (int i = 0. i++) teinputdata[i] = new double[(int)Ninputs]. k].

k++) { teoutputdata[l][k] = (double)teoutputs[j + k].3 .“Algorithms\TrainingAlgorithm. j += (int)Noutputs.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 00000228 00000229 00000230 00000231 00000232 00000233 00000234 00000235 00000236 } } } for (int j = 0. } public bool Verified { get { return _Verified. k < Noutputs. j < teoutputs.2.Count. l++) { for (int k = 0.1. out ArrayList teoutputdata) { teinputdata = teinputs. teoutputdata = teoutputs. } } #endregion } 9. l = 0. } } } public void GetTestingDataAL (out ArrayList teinputdata.cs” 00000001 using System. Page 130 of 225 .

Generic.Controller. System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 00000030 00000031 using using using using using using System. namespace _05025397. private string _ReportData. private int _ReportInterval. System. protected PointPairList BestErrList.Text. protected bool _ITerr. ZedGraph. Page 131 of 225 .Collections. _05025397.Forms.Linq. protected int CurIt.Algorithms { public abstract class TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _TargetError. private int _IterMax.Windows. protected bool _Tested. System. protected bool _Trained.

0 //this is impossible to reach. int Reports. RichTextBox txtData. protected DatasetParser getdata. _IterMax = Iter_Max. int Iter_Max. bool ITerr) { _TargetError = Target_Error. getdata = new DatasetParser(txtData. _ITerr = ITerr. //If the ignore target error is true //then set the target error to -1.00. if (_ITerr) _TargetError = -1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 00000060 00000061 protected Random Rand = new Random(). txtTest). #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public TrainingAlgorithm(double Target_Error. RichTextBox txtTest. so the //network will loop to completion. } #endregion Page 132 of 225 . _ReportInterval = Reports. BestErrList = new PointPairList().

} } Page 133 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 00000090 00000091 #region internal /********************\ |* INTERNAL *| \********************/ protected void Report(string output) { _ReportData += output. } public double TargetError { set { _TargetError = value. public abstract string network_details(). } get { return _TargetError. public abstract bool Test(). } #endregion #region external /********************\ |* EXTERNAL *| \********************/ public abstract bool Train(). public PointPairList getGraphData() { return BestErrList.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 } public int ReportInterval { get { return _ReportInterval. } } #endregion } Page 134 of 225 . } } public bool Tested { get { return _Tested. } } public int IterMax { get { return _IterMax. } } public bool Trained { get { return _Trained. } } public string ReportData { get { return _ReportData. } set { _Trained = value.

ZedGraph.Algorithms { public class CascadeCorrelation : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _InputL.Linq.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using using using using using System.Controller.Forms. System. System. FANN_Cascade CCNet. private double _LearnRate.Generic.1. System.Windows. System.“Algorithms\CascadeCorrelation.IO.Text. #endregion #region constructor Page 135 of 225 .2. FANN_Wrapper.4 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. private double _OutputL.Collections. _05025397. System.Collections. namespace _05025397. System.

getdata. ITerr) { _LearnRate = LearnRate. double iterations. saveDllData(txtData. } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private void saveDllData(RichTextBox tr. out _OutputL). txtData. base. RichTextBox txtData. int Iter_Max. RichTextBox te) { Page 136 of 225 . base. out _InputL. Iter_Max. txtTest).TargetError.ReportInterval.IterMax). RichTextBox txtTest) : base(TargetError. int Reports. (int)_OutputL. _LearnRate.GetStructure(out iterations. txtTest. double TargetError. Reports. base. bool ITerr. CCNet = new FANN_Cascade((int)_InputL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 /********************\ |* CONSTRUCTOR *| \********************/ public CascadeCorrelation(double LearnRate.

dat").Append( Page 137 of 225 . tw.Format ("Network Details: \n Input Layer: {0:d}". (int)_InputL)). tw.00000}".Format ("\t\t\t\t\tOutput Layer: {0:d}". tw.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 TextWriter tw = new StreamWriter("dlltrdata.Write(tr. } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { StringBuilder output = new StringBuilder().Close(). output.Append( string. _LearnRate)). tw = new StreamWriter("dlltedata.dat").Close().Format ("\nLearning Rate: {0:0.Append( string.Append( string. output. tw.Text).Text). (int)_OutputL)).Write(te. output. output.

_tr_output).Train().ReportInterval)).Format ("\nReport every {0:d} generations.Format ("\t\t\t\t\t\tIgnore target error " + "(process all iterations): {0}".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 string.". base. return output. base. output. output.Append( string. base.IterMax)).Format ("\t\t\tTarget Error: {0:0.TargetError)). } public override bool Train() { bool success = true.00000}".Append( string.ToString().Append( string. _ITerr)). Report(CCNet. } catch { Page 138 of 225 .Format ("\t\tMaximum Nodes: {0:d}". output. try { success = CCNet.

BestErrList.OK. return success. besterrlist[i]). MessageBoxIcon. } } catch { MessageBox. } Page 139 of 225 . for (int i = 0.Show("Error getting graph data from DLL". MessageBoxButtons. i < besterrlist. MessageBoxIcon. try { double[] besterrlist = CCNet. success = false. } success = getGraphDataFromDll().OK. } public bool getGraphDataFromDll() { bool success = true.Show("Error running Cascade training!". "Error".besterrorlist. success = false.Add((double)i. base. MessageBoxButtons.Error). i++) { base.Error)._Trained = success. "Error".Length.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 MessageBox.

MessageBoxButtons. } #endregion } Page 140 of 225 ._Tested = success.OK. success = false. Report(CCNet.Error). } public override bool Test() { bool success = true. MessageBoxIcon. "Error". return success. try { success = CCNet.Show("Error running Cascade testing!". } base.Test()._te_output).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 } return success. } catch { MessageBox.

_05025397. private double _OutputL.Generic.Forms. System. //Population Page 141 of 225 .Windows.Collections.Text.2. //Network Data private double _InputL.5 .1.Controller.“Algorithms\GeneticAlgorithm. System. namespace _05025397.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using using using System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. private int _Mutation. System.Algorithms { public class GeneticAlgorithm : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ //GA Data private int _PopSize. private int _Crossover.Linq. ZedGraph. System. System.Collections. private int _HiddenL.

Iter_Max. i < _PopSize. int Crossover.Add Page 142 of 225 . int Mutation. i++) { Population. ITerr) { _PopSize = PopSize. Reports. _Mutation = Mutation. out _InputL. _Crossover = Crossover. int Reports. int HiddenL. RichTextBox txtTest) : base(TargetError. //Create new population references Population = new ArrayList(_PopSize). for (int i = 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 ArrayList Population. bool ITerr. int Iter_Max. txtData. _HiddenL = HiddenL. //Init Network params double iterations. double TargetError. out _OutputL). RichTextBox txtData. txtTest. getdata. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public GeneticAlgorithm(int PopSize.GetStructure(out iterations.

} //Sort the functions according to fitness now. results).getMsQE(inputs. i < base.GANetwork( (int)_InputL. j < _PopSize.Next())). Page 143 of 225 .GetScaledTrainingData(out inputs.GANetwork) Population[j]). out results). double[][] inputs. j++) { ((Model. getdata. for (int i = 0.IterMax. success = sort_fitnesses().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 (new Model. } } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private bool run() { bool success = true. Rand. _HiddenL. i++) { //Get the msqe for (int j = 0. double[][] results. (int)_OutputL.

CurIt++.\n"). //Check for having reached the target if (((Model. break. } base. } private bool sort_fitnesses() { Page 144 of 225 . base. base.CurIt == ReportInterval) { base. } return success.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 //Report if we're at a report iteration //Also update the besterrlist (for graphing) if (base. ((Model.TargetError) { Report("\nNetwork matching or improving upon target error"+ "found at iteration [ " + i + " ].MSqE <= base.Add ((double)i.GANetwork)Population[0]).BestErrList. } success = build_generation().MSqE).MSqE + ".GANetwork)Population[0]).Report("Best error at iteration [ " + i + " ] was " + ((Model.").GANetwork)Population[0]).CurIt = 0.

int selection1.Sort(). double[] weightarray_f. double[] weightarray1. try { Population. } catch { MessageBox. i > (int)(_PopSize / 2). MessageBoxButtons. } private bool build_generation() { bool success = true. double[] weightarray2. //It selects two points from the start of the population //Then applies crossover if necessary (based on probability) Page 145 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 bool success = true.Show("Error sorting population". } return success.Error).1). selection2. for (int i = (_PopSize .OK. i--) { //This algorithm is altered from previous versions. MessageBoxIcon. "Error". success = false.

"Error". selection2 = Rand.OK. if (Rand. success = false. the least fit) with this new string. //Next iteration it replaces the next to last.Length]. //Just a quick error check if (weightarray1.Next((int)_PopSize / 2). selection1 = Rand.getWeights().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 //applies mutation if necessary (same again) //then if neither mutation or Xover have been applied //chooses position 1 or 2.Show("Error: 2D weight array length unequal. weightarray2 = ((Model.Length) { MessageBox. MessageBoxIcon.Error).Next((int)_PopSize / 2). weightarray1 = ((Model. //Then it replaces the last position in the population //(ie.GANetwork)Population[selection1]).".GANetwork)Population[selection1]).getWeights().Next(100) < _Crossover) { //Choose a random position in the weight array //to be our crossover point Page 146 of 225 . MessageBoxButtons. and //so on. } try { weightarray_f = new double[weightarray1. all the way to the middle of the population.Length != weightarray2.

(weightarray1. //Simple single point crossover of the weights for (int j = 0.0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 int XPos = Rand. j < weightarray1.5.1)). j++) { weightarray_f[j] += Rand.Length.Length .setWeights(weightarray_f). } else { weightarray_f[j] = weightarray2[j]. } Page 147 of 225 . } } } if (Rand. j++) { if (j < XPos) { weightarray_f[j] = weightarray1[j].Next(1.GANetwork)Population[i]).Length.5 for (int j = 0. } } //Set the weights of the current member of the //population we're on ((Model.NextDouble() . j < weightarray_f.Next(100) < _Mutation) { //Jiggle the weights in the array //up or down by 0.

Format("Network Details: \n Population: {0:d}". "Error".Append( string. } } return success.Format("\t\t\tCrossover Probability: {0:d}".Append( Page 148 of 225 .Show("Error building new generation".OK. output. output.Error). MessageBoxButtons.Format("\t\t\tMutation Probability: {0:d}". output. } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { //Add stringbuilder for efficiency here StringBuilder output = new StringBuilder(). MessageBoxIcon. _Crossover)). _PopSize)). _Mutation)).Append( string. success = false.Append( string.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 00000228 00000229 00000230 00000231 00000232 00000233 00000234 00000235 00000236 00000237 00000238 00000239 catch { MessageBox. output.

". base. _HiddenL)). return success._Trained = success.Append( string.Append( string.Format("\t\t\tTarget Error: {0:0. return output. output.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000240 00000241 00000242 00000243 00000244 00000245 00000246 00000247 00000248 00000249 00000250 00000251 00000252 00000253 00000254 00000255 00000256 00000257 00000258 00000259 00000260 00000261 00000262 00000263 00000264 00000265 00000266 00000267 00000268 00000269 string.ToString().Format("\t\t\tMaximum Generation: {0:d}".IterMax)). base. output. } public override bool Train() { bool success = true.00000}".Format("\nHidden layer: {0:d}".Append( string. _ITerr)). success = run(). output.Append( string. } public override bool Test() Page 149 of 225 .Format("\t\t\t\t\t\t\tIgnore target error (process all iterations): {0}". output. base. base.TargetError)).Format("\nReport every {0:d} generations.ReportInterval)).

Append("\nWith inputs:"). j < netoutput.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000270 00000271 00000272 00000273 00000274 00000275 00000276 00000277 00000278 00000279 00000280 00000281 00000282 00000283 00000284 00000285 00000286 00000287 00000288 00000289 00000290 00000291 00000292 00000293 00000294 00000295 00000296 00000297 00000298 00000299 { bool success = true.Length. try { for (int i = 0.Append(string.Length.Length. i < tstinput. } output. j < tstinput[i]. i++) { ((Model. double[] netoutput. netoutput[j])). output. double[][] tstoutput. getdata.Append("\n\n\t\t\t~~Network Testing~~"). j++) { output. out tstoutput).Append(string.Run(tstinput[i]).Append("\nOutput achieved was:"). Page 150 of 225 . StringBuilder output = new StringBuilder().GetScaledTestingData(out tstinput. tstinput[i][j])). for (int j = 0.Format(" [{0:g}] ".GANetwork)Population[0]). j++) { output. double[][] tstinput.GetOutput().Format(" [{0:g}] ". for (int j = 0.GANetwork)Population[0]). netoutput = ((Model. output.

GANetwork)Population[0]).Append(string.Format ("{0:g}".Append("\nMean Squared Error with these inputs and outputs is:"). } } output.getMsQE(tstinput. MessageBoxIcon.GANetwork)Population[0]). base. ((Model. } #endregion } Page 151 of 225 . output.".OK.Format(" [{0:g}] ". j++) { output. for (int j = 0. "Error".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000300 00000301 00000302 00000303 00000304 00000305 00000306 00000307 00000308 00000309 00000310 00000311 00000312 00000313 00000314 00000315 00000316 00000317 00000318 00000319 00000320 00000321 00000322 00000323 00000324 00000325 00000326 00000327 00000328 } } output.Error). MessageBoxButtons.tstoutput). (double) ((Model.ToString()). j < tstoutput[i]. } catch { MessageBox.Show("Error Running Test.Append("\nOutput Desired was:"). } base.Length.Append( string._Tested = success.Report(output. return success.MSqE)). success = false. tstoutput[i][j])).

Collections. private int _HiddenL.Forms. System.“Algorithms\BackProp. System.Collections. private double _LearnRate.Controller.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.Algorithms { public class BackProp : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _InputL. _05025397.Text. System.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 using using using using using using using using using using System. private double _OutputL. ZedGraph. FANN_BackProp BPnet.Generic.1. System.Windows. FANN_Wrapper. namespace _05025397.2.Linq.6 .IO. System. #endregion #region constructor Page 152 of 225 . System.

int Iter_Max. HiddenL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 /********************\ |* CONSTRUCTOR *| \********************/ public BackProp (int HiddenL. double iterations.IterMax).ReportInterval.TargetError. } #endregion #region internal_functions /********************\ Page 153 of 225 . _LearnRate = LearnRate. _LearnRate. (int)_OutputL. BPnet = new FANN_BackProp((int)_InputL. txtTest. out _OutputL). //Copy the training and testing data to the dat files //ready for the DLL's to access. out _InputL. double LearnRate. txtTest). double TargetError. base. saveDllData(txtData. RichTextBox txtTest) : base(TargetError. Iter_Max. base. bool ITerr. Reports. int Reports. ITerr) { _HiddenL = HiddenL. txtData.GetStructure(out iterations. getdata. RichTextBox txtData. base.

tw.Write(te.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 |* INTERNAL *| \********************/ private void saveDllData(RichTextBox tr.Format ("\t\t\tOutput Layer: {0:d}". RichTextBox te) { TextWriter tw = new StreamWriter("dlltrdata. (int) _OutputL)). output. tw = new StreamWriter("dlltedata. (int) _InputL)). tw.Close(). tw.dat").Close(). output.Format ("Network Details: \n Input Layer: {0:d}".Text). } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { StringBuilder output = new StringBuilder().Append( string.Text).Write(tr.dat").Append( string. output.Append( Page 154 of 225 . tw.

base.Format ("\nLearning Rate: {0:0.Format ("\t\t\tHidden Layer: {0:d}".Append( string.". _LearnRate)).Append( string.Format ("\t\tMaximum Epoch: {0:d}". output.ReportInterval)). output. base. base.Format ("\t\t\t\t\t\tIgnore target error " + "(process all iterations): {0}".Append( string. output.00000}". output. _ITerr)).ToString().Append( string.00000}".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 string.TargetError)). } public override bool Train() { Page 155 of 225 .Format ("\t\t\tTarget Error: {0:0. (int) _HiddenL)). output.IterMax)).Append( string. return output.Format ("\nReport every {0:d} generations.

Page 156 of 225 .besterrorlist.Length._Trained = success. "Error". } success = getGraphDataFromDll(). MessageBoxButtons.BestErrList.OK. try { success = BPnet.Error). } public bool getGraphDataFromDll() { bool success = true.Add((double)i. success = false. i < besterrlist._tr_output).Train(). for (int i = 0. besterrlist[i]). try { double[] besterrlist = BPnet. return success. Report(BPnet. } catch { MessageBox. base. i++) { base.Show("Error running Backprop training!". MessageBoxIcon.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 bool success = true.

OK. MessageBoxIcon. MessageBoxIcon. MessageBoxButtons.OK. MessageBoxButtons.Show("Error running Backprop testing!". "Error". success = false. } #endregion Page 157 of 225 . "Error". success = false. } return success.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 } } catch { MessageBox.Error)._Tested = success. } catch { MessageBox. } base.Show("Error getting graph data from DLL". Report(BPnet. try { success = BPnet. return success. } public override bool Test() { bool success = true.Test()._te_output).Error).

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 } } 9. protected int _hiddennodes. protected int _numoutputs. Page 158 of 225 .“Network. System.1.3.Text.Model 9. _05025397.Collections.Linq. System.Forms. namespace _05025397.cs” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 using using using using using using System. protected double[][] _outputs. System.1 . protected int _numinputs.Generic.3 .Windows. System.Model { public class Network { #region data /********************\ |* DATA *| \********************/ //[layer][node][input] protected double[][][] _inputs. protected double[][][] _weights.1.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000025 00000026 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 protected Random Rand. for (int i = 0. _weights[0][i] = new double[2]. Page 159 of 225 . _outputs[1] = new double[_hiddennodes]. _weights[0] = new double[_numinputs][]. _outputs[0] = new double[_numinputs]. #endregion #region init /********************\ |* INIT *| \********************/ private void init_inputlayer() { _inputs[0] = new double[_numinputs][]. } } private void init_hiddenlayer() { _inputs[1] = new double[_hiddennodes][]. i < _hiddennodes. i++) { _inputs[0][i] = new double[2]. _weights[1][i] = new double[_numinputs + 1]. i < _numinputs. for (int i = 0. i++) { _inputs[1][i] = new double[_numinputs + 1]. _weights[1] = new double[_hiddennodes][].

i++) { _inputs[0][i][1] = 1. _weights[1][i][_numinputs] = 1.0. i++) { _inputs[2][i] = new double[_hiddennodes + 1]. } } private void init_bias() { //Input bias for (int i = 0.0. i < _numinputs.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000055 00000056 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 } } private void init_outputlayer() { _inputs[2] = new double[_numoutputs][]. } //Hidden layer bias for (int i = 0. _weights[0][i][1] = 1. i++) { _inputs[1][i][_numinputs] = 1.0. for (int i = 0.0. _outputs[2] = new double[_numoutputs]. _weights[2][i] = new double[_hiddennodes + 1]. i < _numoutputs. Page 160 of 225 . _weights[2] = new double[_numoutputs][]. i < _hiddennodes.

i < _numoutputs. i++) { _weights[0][i][0] = Rand. i < _hiddennodes.NextDouble(). i < _numinputs.NextDouble(). } } private void init_hiddenweights() { for (int i = 0. } } private void init_inputweights() { for (int i = 0. j < _numinputs.0. i++) { _inputs[2][i][_hiddennodes] = 1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000085 00000086 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 } //Output layer bias for (int i = 0. _weights[2][i][_hiddennodes] = 1. } } } private void init_outputweights() Page 161 of 225 . i++) { for (int j = 0. j++) { _weights[1][i][j] = Rand.0.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000115 00000116 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 { for (int i = 0. try { //Calculate results for this layer for (int i = 0. } private bool Runinput() { bool success = true.NextDouble(). i < _numinputs. j++) { _weights[2][i][j] = Rand. double sum = 0. -input))). i++) { for (int j = 0.Pow(Math. j < _hiddennodes. i < _numoutputs. } } } #endregion #region internal /********************\ |* INTERNAL *| \********************/ private double sigmoid(double input) { return (1 / (1 + Math.E. i++) { Page 162 of 225 .0.

success = false. sum = 0. j < 2.0. j++) { sum += (_inputs[0][i][j] * _weights[0][i][j]). try { //Feed forward the results from input layer for (int i = 0. "Error". } } catch { MessageBox. double sum = 0. Page 163 of 225 . i < _hiddennodes. } return success. j < _numinputs. i++) { for (int j = 0.Error).Show("Error processing input layer". MessageBoxIcon. } _outputs[0][i] = sigmoid(sum). j++) { _inputs[1][i][j] = _outputs[0][j].0. MessageBoxButtons.OK.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000145 00000146 00000147 00000148 00000149 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 for (int j = 0. } private bool Runhidden() { bool success = true.

} _outputs[1][i] = sigmoid(sum). j++) { sum += (_inputs[1][i][j] * _weights[1][i][j]). j < _numinputs + 1. } } catch { MessageBox. try Page 164 of 225 . } private bool Runoutput() { bool success = true.Error).Show("Error processing hidden layer".0. sum = 0. MessageBoxIcon. success = false. } return success.0. double sum = 0.OK. i < _hiddennodes. i++) { for (int j = 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000175 00000176 00000177 00000178 00000179 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 } } //Calculate results for this layer for (int i = 0. "Error". MessageBoxButtons.

i < _numoutputs. success = false. j++) { sum += (_inputs[2][i][j] * _weights[2][i][j]). MessageBoxIcon. } } //Calculate results for this layer for (int i = 0. j < _hiddennodes. } Page 165 of 225 . MessageBoxButtons. i++) { for (int j = 0.OK. } return success.0. "Error".Error). } _outputs[2][i] = sigmoid(sum). j++) { _inputs[2][i][j] = _outputs[1][j]. j < _hiddennodes + 1.Show("Error processing output layer". i++) { for (int j = 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000205 00000206 00000207 00000208 00000209 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 00000220 00000221 00000222 00000223 00000224 00000225 00000226 00000227 00000228 00000229 00000230 00000231 00000232 00000233 00000234 { //Feed forward the results from hidden layer for (int i = 0. sum = 0. } } catch { MessageBox. i < _numoutputs.

"Incorrect number of inputs". MessageBoxIcon. //The numbers of inputs must match up if (inputs.Length != _numinputs) { MessageBox.Show("Error: Incorrect number of inputs supplied to NN".OK.the number of nodes corresponds //to the number of inputs accepted. if(success) success = Runinput().William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000235 00000236 00000237 00000238 00000239 00000240 00000241 00000242 00000243 00000244 00000245 00000246 00000247 00000248 00000249 00000250 00000251 00000252 00000253 00000254 00000255 00000256 00000257 00000258 00000259 00000260 00000261 00000262 00000263 00000264 #endregion #region external /********************\ |* EXTERNAL *| \********************/ public bool Run(double[] inputs) { bool success = true.Error). i++) //Each input node has only one real input //and a bias . } if (success) for (int i = 0. _inputs[0][i][0] = inputs[i]. if(success) success = Runhidden(). success = false. Page 166 of 225 . i < _numinputs. MessageBoxButtons.

} public double[] GetOutput() { //Return the outputs from the //output layer return _outputs[2]. //We'll always have 3 layers Page 167 of 225 . _numoutputs = numoutputs. //Set network structure descriptors _numinputs = numinputs. _hiddennodes = hiddennodes. int numoutputs. } #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public Network(int numinputs. return success. int hiddennodes.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000265 00000266 00000267 00000268 00000269 00000270 00000271 00000272 00000273 00000274 00000275 00000276 00000277 00000278 00000279 00000280 00000281 00000282 00000283 00000284 00000285 00000286 00000287 00000288 00000289 00000290 00000291 00000292 00000293 00000294 if(success) success = Runoutput(). int SetRandom) { //Set random number generator Rand = new Random(SetRandom).

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000295 00000296 00000297 00000298 00000299 00000300 00000301 00000302 00000303 00000304 00000305 00000306 00000307 00000308 00000309 00000310 } //Input, Hidden, and Output _inputs = new double[3][][]; _weights = new double[3][][]; _outputs = new double[3][]; init_inputlayer(); init_hiddenlayer(); init_outputlayer(); init_bias(); init_inputweights(); init_hiddenweights(); init_outputweights(); } #endregion }

9.1.3.2 - “GANetwork.cs”
00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Windows.Forms; _05025397; System.Collections;

//This class contains Genetic Algorithm specific functions //in addition to the basic feed foward neural net functionality. namespace _05025397.Model

Page 168 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000013 { 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042

public class GANetwork : Network, IComparable { #region data /********************\ |* DATA *| \********************/ double _MSqE; #endregion #region .NET /********************\ |* .NET *| \********************/ //Interface_Implementation //Return Value Meanings: //-<Zero: x < y //-Zero: x == y //->Zero: x > y public int CompareTo(object a) { GANetwork b; if (a is GANetwork) { b = a as GANetwork; return _MSqE.CompareTo(b.MSqE); }

Page 169 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 return 0; } #endregion #region external /********************\ |* EXTERNAL *| \********************/ public void getMsQE(double[][] inputs, double[][] outputs) { double sum = 0.0; int counter = 0; double[] netoutputs = new double[_numoutputs]; for (int i = 0; i < inputs.Length; i++) { base.Run(inputs[i]); netoutputs = base.GetOutput(); for (int j = 0; j < netoutputs.Length; j++) { sum += (outputs[i][j] - netoutputs[j]) * (outputs[i][j] - netoutputs[j]); counter++; } } _MSqE = (sum / counter); }

Page 170 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 public double[] getWeights() { ArrayList collater = new ArrayList(); //Collect all the weights in an array list //then spit them out as a 1D array of doubles try { for (int i = 0; i < _weights.Length; i++) { for (int j = 0; j < _weights[i].Length; j++) { for (int k = 0; k < _weights[i][j].Length; k++) { collater.Add(_weights[i][j][k]); } } } } catch { MessageBox.Show("Fatal Error collating weights to 1D array", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } return (double[])collater.ToArray(typeof(double)); } public void setWeights(double[] weights)

Page 171 of 225

MessageBoxIcon.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 { //Take a 1D array of doubles and apply //to the correct positions in our weights //array try { for (int i = 0. } } /********************\ |* GETTERS/SETTERS *| \********************/ public double MSqE { Page 172 of 225 .Exit(). k < _weights[i][j]. "Error".Show("Fatal Error adding 1D weight array to 3D weight array".OK.Length. wc++) { _weights[i][j][k] = weights[wc].Length. k++.Error). Application. } } } } catch { MessageBox. wc = 0. j++) { for (int k = 0. j < _weights[i]. MessageBoxButtons. i < _weights.Length. i++) { for (int j = 0.

Noutputs.0. int Noutputs. int SetRandom) : base(Ninputs. SetRandom) { MSqE = 0. int Nhnodes. } } #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public GANetwork(int Ninputs. } set { _MSqE = value. Nhnodes. } #endregion } Page 173 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 00000150 00000151 } get { return _MSqE.

4.1 .“FANN_Wrapper.DLL Wrapper for FANN functions 9. { public: /********************\ |* DATA *| \********************/ UnManaged_FANN_CasC* UMWrapper. public: /********************\ |* EXTERNAL *| \********************/ FANN_Cascade(int InputL. namespace FANN_Wrapper { public ref class FANN_Cascade //Managed wrapper for UnManaged_FANN_CasC //which is a C++ interface to the C programmed //FANN dll.h" using namespace System. int OutputL. using namespace System::Runtime::InteropServices. double LearnRate.4 . double TargetErr.1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. Page 174 of 225 .1.h” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 #pragma once #include "UnManaged_FANN_BackProp.h" #include "UnManaged_FANN_CasC.

} } property array<double>^ besterrorlist { array<double>^ get() MaxNeurons). int ~FANN_Cascade(void). bool Test(void). } } property String^ _tr_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_train()). !FANN_Cascade(void). bool Train(void). public: /********************\ |* GETTERS/SETTERS *| \********************/ property String^ _te_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_test()).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000027 00000028 00000029 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 int ReportInterval. Page 175 of 225 .

} } }. public: Page 176 of 225 . for(int i = 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000057 00000058 00000059 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 { int size. array<double>^ arout = gcnew array<double>(size). i++) { arout[i] = ar[i]. const double *ar = UMWrapper->get_besterrs(size). public ref class FANN_BackProp //Managed wrapper for UnManaged_FANN_Backprop //which is a C++ interface to the C programmed //FANN dll. { public: /********************\ |* DATA *| \********************/ UnManaged_FANN_Backprop* UMWrapper. } return arout. i < size.

~FANN_BackProp(void). !FANN_BackProp(void). int MaximumIteration). double LearnRate. public: /********************\ |* GETTERS/SETTERS *| \********************/ property String^ _te_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_test()). int HiddenL. double TargetErr. int OutputL. bool Test(void). } } property String^ _tr_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_train()).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000087 00000088 00000089 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 /********************\ |* EXTERNAL *| \********************/ FANN_BackProp(int InputL. int ReportInterval. } Page 177 of 225 . bool Train(void).

for(int i = 0. } } }. Page 178 of 225 . array<double>^ arout = gcnew array<double>(size). i++) { arout[i] = ar[i]. const double *ar = UMWrapper->get_besterrs(size).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000117 00000118 00000119 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 } } property array<double>^ besterrorlist { array<double>^ get() { int size. i < size. } return arout.

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3

9.1.4.2 - “FANN_Wrapper.cpp”
00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 #include "stdafx.h" #include "FANN_Wrapper.h" namespace FANN_Wrapper { /********************\ |* FANN_BackProp *| \********************/ //Constructor FANN_BackProp::FANN_BackProp(int InputL, int HiddenL, int OutputL, double LearnRate, double TargetErr, int ReportInterval, int MaximumIteration) { UMWrapper = new UnManaged_FANN_Backprop( InputL, HiddenL, OutputL, LearnRate, TargetErr, ReportInterval, MaximumIteration); } //Destructor FANN_BackProp::~FANN_BackProp(void) { if (UMWrapper) { delete UMWrapper; UMWrapper = 0; } }

Page 179 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059

//Finalizer FANN_BackProp::!FANN_BackProp(void) { if (UMWrapper) { delete UMWrapper; UMWrapper = 0; } }

//Train bool FANN_BackProp::Train(void) { return UMWrapper->Train(); } //Test bool FANN_BackProp::Test(void) { return UMWrapper->Test(); } /********************\ |* FANN_Cascade *| \********************/ //Constructor FANN_Cascade::FANN_Cascade(int InputL, int OutputL,

Page 180 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 double LearnRate, double TargetErr, int ReportInterval, int MaxNeurons) { UMWrapper = new UnManaged_FANN_CasC( InputL, OutputL, LearnRate, TargetErr, ReportInterval, MaxNeurons); } //Destructor FANN_Cascade::~FANN_Cascade(void) { if (UMWrapper) { delete UMWrapper; UMWrapper = 0; } } //Finalizer FANN_Cascade::!FANN_Cascade(void) { if (UMWrapper) { delete UMWrapper; UMWrapper = 0; } } //Train

Page 181 of 225

William Sayers

05025397

Genetic Algorithms and Neural Networks
Milestone 3
00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 } bool FANN_Cascade::Train(void) { return UMWrapper->Train(); } //Test bool FANN_Cascade::Test(void) { return UMWrapper->Test(); }

Page 182 of 225

int _ReportInterval.“UnManaged_FANN_BackProp.3 . int _MaximumIteration. double _Momentum. double _TargetErr.1. //Output for C# Page 183 of 225 .h> class UnManaged_FANN_Backprop //An interface to the C programmed //FANN dll.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.h” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 #pragma once #include <iostream> #include <string> #include <sstream> #include <vector> #include <doublefann. int _HiddenL. int _OutputL.4. { private: /********************\ |* DATA *| \********************/ //Network Structure int _InputL. //Training Parameters double _LearnRate.h> #include <fann_cpp.

int MaximumIteration). 00000032 wchar_t *wchoutput. 00000047 double LearnRate. 00000052 00000053 //Interface functions 00000054 //accessed from C# 00000055 bool Train(void). 00000035 double *bsterr. double TargetErr. 00000036 00000037 //FANN Data 00000038 struct fann *ann. int OutputL. 00000049 00000050 //Destructor 00000051 ~UnManaged_FANN_Backprop(void).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 std::wstringstream tr_output. 00000056 bool Test(void). 00000039 struct fann_train_data *data. 00000048 int ReportInterval. 00000057 00000058 public: 00000059 /********************\ Page 184 of 225 . int HiddenL. 00000031 std::wstringstream te_output. 00000040 00000041 public: 00000042 /********************\ 00000043 |* EXTERNAL *| 00000044 \********************/ 00000045 //Constructor 00000046 UnManaged_FANN_Backprop(int InputL. 00000033 00000034 std::vector<double> vecbsterr.

Page 185 of 225 . |* GETTERS/SETTERS *| \********************/ const wchar_t* get_report_train(void). const double* get_besterrs(int &size). const wchar_t* get_report_test(void).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 }.

Page 186 of 225 .cpp” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 #include "StdAfx. int HiddenL. double TargetErr. _LearnRate = LearnRate.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.“UnManaged_FANN_BackProp. bsterr = NULL. int ReportInterval. int MaximumIteration) { _InputL = InputL. _HiddenL = HiddenL.h" #include "UnManaged_FANN_BackProp. int OutputL. double LearnRate. wchoutput = NULL. _OutputL = OutputL.h" //Constructor UnManaged_FANN_Backprop::UnManaged_FANN_Backprop (int InputL. ann = NULL. _TargetErr = TargetErr. _MaximumIteration = MaximumIteration.4 .1. _ReportInterval = ReportInterval.4. data = NULL. } //Destructor UnManaged_FANN_Backprop::~UnManaged_FANN_Backprop() { fann_destroy_train(data).

double error. success = false. _HiddenL. } if (fann_num_input_train_data(data) != _InputL) Page 187 of 225 . int reportcounter = 0.. } //Train bool UnManaged_FANN_Backprop::Train(void) { static int firstrun = false. _OutputL).) { throw("").William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 delete bsterr.. ann = fann_create_standard(3. try { data = fann_read_train_from_file("dlltrdata.dat"). wchoutput = NULL. } catch(. bsterr = NULL. bool success = true. _InputL. delete wchoutput.

//Standard backprop fann_set_training_algorithm(ann. FANN_SIGMOID). (float) _LearnRate). } fann_set_activation_steepness_hidden(ann. } if (fann_num_output_train_data(data) != _OutputL) { throw(""). FANN_SIGMOID). fann_set_activation_steepness_output(ann. fann_set_activation_function_hidden (ann. success = false.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 { throw(""). //Set the learning rate fann_set_learning_rate(ann. //Sigmoid Activation Functions (the same one //the GA uses). FANN_TRAIN_BATCH). 1). 1). //Same range the GA's weights are //initialised too Page 188 of 225 . success = false. fann_set_activation_function_output (ann.

i++) { //Train one epoch then check the //mean squared error error = (double) fann_train_epoch(ann.0). if (error < _TargetErr) { tr_output << "\nNetwork matching or improving upon target error" << "found at iteration [ " << i << " ].0. i < _MaximumIteration. } } fann_destroy_train(data). reportcounter = 0. Page 189 of 225 . if (reportcounter == _ReportInterval) { tr_output << "Best error at iteration [ " << i << " ] was " << error << ".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 00000091 00000092 00000093 00000094 00000095 00000096 00000097 00000098 00000099 00000100 00000101 00000102 00000103 00000104 00000105 00000106 00000107 00000108 00000109 00000110 00000111 00000112 00000113 00000114 00000115 00000116 00000117 00000118 00000119 fann_randomize_weights(ann. 1.". data). vecbsterr. } reportcounter++. //Training Loop for (int i = 0.\n". 0.push_back(error). i = _MaximumIteration + 1.

0. } //Test bool UnManaged_FANN_Backprop::Test(void) { fann_type *calc_out = NULL. data = fann_read_train_from_file("dlltedata. data->input[i]). error = new double[fann_length_train_data(data)]. te_output << "\n\n\t\t\t~~Network Testing~~". fann_scale_train_data(data. } te_output << "\nOutput achieved was". for (unsigned int i = 0. i < fann_length_train_data(data). j++) { te_output << " [" << data->input[i][j] << "] ". i++) { calc_out = fann_run(ann. double *error = NULL. for (int j = 0. j < _OutputL. return true.dat"). j++) { Page 190 of 225 . 1). j < _InputL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 00000122 00000123 00000124 00000125 00000126 00000127 00000128 00000129 00000130 00000131 00000132 00000133 00000134 00000135 00000136 00000137 00000138 00000139 00000140 00000141 00000142 00000143 00000144 00000145 00000146 00000147 00000148 00000149 firstrun = true. for (int j = 0. te_output << "\nWith inputs".

j < _OutputL. delete calc_out. return true. te_output << fann_test_data(ann. } //get_report const wchar_t* UnManaged_FANN_Backprop::get_report_train(void) { if (wchoutput != NULL) { delete wchoutput.str(). } } te_output << "\nMean Squared Error with these inputs and outputs is:". wcscpy(wchoutput. tr_output. } te_output << "\nOutput Desired was". Page 191 of 225 . wchoutput = NULL. } wchoutput = new wchar_t[tr_output.length() + 1].William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 te_output << " [" << calc_out[j] << "] ". data).c_str()). delete error.str(). for (int j = 0. j++) { te_output << " [" << data->output[i][j] << "] ".

} //get_besterrs const double* UnManaged_FANN_Backprop::get_besterrs(int &size) { if (bsterr == NULL) { delete bsterr.str(). Page 192 of 225 . } bsterr = new double[vecbsterr. size = vecbsterr.size(). } const wchar_t* UnManaged_FANN_Backprop::get_report_test(void) { if (wchoutput != NULL) { delete wchoutput. wchoutput = NULL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 return wchoutput. bsterr = NULL.length() + 1].size()].str(). te_output. return wchoutput.c_str()). wcscpy(wchoutput. } wchoutput = new wchar_t[te_output.

“UnManaged_FANN_CasC. i++) { bsterr[i] = vecbsterr[i].4. 9.1. } vecbsterr.clear().h> class UnManaged_FANN_CasC { public: /********************\ |* DATA *| \********************/ //Network Structure int _InputL. return bsterr.h” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 #pragma once #include <iostream> #include <string> #include <sstream> #include <vector> #include <doublefann.size(). //Training Parameters Page 193 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000210 00000211 00000212 00000213 00000214 00000215 00000216 00000217 00000218 00000219 } for (unsigned int i = 0. i < vecbsterr. int _OutputL.5 .

00000031 00000032 //FANN Data 00000033 struct fann *ann. 00000030 double *bsterr. 00000035 00000036 public: 00000037 /********************\ 00000038 |* EXTERNAL *| 00000039 \********************/ 00000040 00000041 //Destructor 00000042 ~UnManaged_FANN_CasC(void). 00000028 00000029 static std::vector<double> vecbsterr. 00000034 struct fann_train_data *data. Page 194 of 225 . int MaxNeurons). 00000020 double _TargetErr. 00000021 int _ReportInterval. int OutputL. 00000048 int ReportInterval. 00000022 int _MaxNeurons. 00000023 00000024 //Output for C# 00000025 static std::wstringstream tr_output. 00000027 wchar_t *wchoutput.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000019 double _LearnRate. double TargetErr. 00000026 std::wstringstream te_output. 00000047 double LearnRate. 00000043 00000044 //Constructor 00000045 UnManaged_FANN_CasC 00000046 (int InputL.

00000053 bool Test(void). Page 195 of 225 . unsigned int epochs). 00000065 const wchar_t* get_report_test(void). 00000066 00000067 const double* get_besterrs(int &size). 00000054 00000055 static int FANN_API Report_Callback 00000056 (struct fann *ann. 00000058 float desired_error. 00000068 }. 00000057 unsigned int max_epochs. unsigned int epochs_between_reports.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000049 00000050 //Interface functions 00000051 //accessed from C# 00000052 bool Train(void). 00000059 00000060 public: 00000061 /********************\ 00000062 |* GETTERS/SETTERS *| 00000063 \********************/ 00000064 const wchar_t* get_report_train(void). struct fann_train_data *train.

data = NULL. std::vector<double> UnManaged_FANN_CasC::vecbsterr. //Constructor UnManaged_FANN_CasC::UnManaged_FANN_CasC (int InputL. _ReportInterval = ReportInterval. _OutputL = OutputL.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.4. double LearnRate.h" //Static variable declarations std::wstringstream UnManaged_FANN_CasC::tr_output.1.h" #include "UnManaged_FANN_CasC. _MaxNeurons = MaxNeurons. _LearnRate = LearnRate. double TargetErr. int ReportInterval.“UnManaged_FANN_CasC.6 . } //Destructor UnManaged_FANN_CasC::~UnManaged_FANN_CasC(void) Page 196 of 225 . int OutputL. wchoutput = NULL. ann = NULL. int MaxNeurons) { _InputL = InputL. bsterr = NULL.cpp” 00000001 00000002 00000003 00000004 00000005 00000006 00000007 00000008 00000009 00000010 00000011 00000012 00000013 00000014 00000015 00000016 00000017 00000018 00000019 00000020 00000021 00000022 00000023 00000024 00000025 00000026 00000027 00000028 00000029 #include "StdAfx. _TargetErr = TargetErr.

wcscpy(wchoutput.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 00000031 00000032 00000033 00000034 00000035 00000036 00000037 00000038 00000039 00000040 00000041 00000042 00000043 00000044 00000045 00000046 00000047 00000048 00000049 00000050 00000051 00000052 00000053 00000054 00000055 00000056 00000057 00000058 00000059 { delete bsterr. wchoutput = NULL. tr_output.length() + 1]. Page 197 of 225 .c_str()). } //get_report const wchar_t* UnManaged_FANN_CasC::get_report_train(void) { if (wchoutput != NULL) { delete wchoutput. delete wchoutput. fann_destroy_train(data). } wchoutput = new wchar_t[tr_output. bsterr = NULL. wchoutput = NULL.str(). } const wchar_t* UnManaged_FANN_CasC::get_report_test(void) { if (wchoutput != NULL) { delete wchoutput. return wchoutput.str().

size()]. } bsterr = new double[vecbsterr. te_output. } vecbsterr.size().c_str()). bsterr = NULL.str(). } //get_besterrs const double* UnManaged_FANN_CasC::get_besterrs(int &size) { if (bsterr == NULL) { delete bsterr. for (unsigned int i = 0.size(). i++) { bsterr[i] = vecbsterr[i]. return wchoutput.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000060 00000061 00000062 00000063 00000064 00000065 00000066 00000067 00000068 00000069 00000070 00000071 00000072 00000073 00000074 00000075 00000076 00000077 00000078 00000079 00000080 00000081 00000082 00000083 00000084 00000085 00000086 00000087 00000088 00000089 wchoutput = NULL. return bsterr.length() + 1]. } wchoutput = new wchar_t[te_output.str(). size = vecbsterr.clear(). wcscpy(wchoutput. i < vecbsterr. } Page 198 of 225 .

2. 00000113 00000114 //Some more network customisation here 00000115 //might be nice in future. Page 199 of 225 .0). 00000111 00000112 fann_set_learning_rate(ann. _InputL. 00000108 00000109 if (fann_num_output_train_data(data) != _OutputL) 00000110 throw("").0). 00000096 00000097 try 00000098 { 00000099 data = fann_read_train_from_file("dlltrdata. (float)_LearnRate). 00000116 00000117 fann_set_quickprop_decay(ann.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000090 //Train 00000091 bool UnManaged_FANN_CasC::Train() 00000092 { 00000093 int reportcounter = 0. 00000094 00000095 ann = fann_create_shortcut(2. _OutputL). 00000118 00000119 fann_set_quickprop_mu(ann. 00000100 } 00000101 catch(. 0.) 00000102 { 00000103 throw(""). 00000104 } 00000105 00000106 if (fann_num_input_train_data(data) != _InputL) 00000107 throw("").dat")...

0. 00000124 00000125 fann_set_activation_steepness_output(ann. FANN_TRAIN_QUICKPROP). 00000126 fann_set_activation_steepness_hidden(ann. 00000146 } 00000147 00000148 //Callback for reporting 00000149 int FANN_API UnManaged_FANN_CasC::Report_Callback Page 200 of 225 . 00000133 00000134 fann_set_activation_function_output(ann. FANN_SIGMOID). 1). 00000137 fann_set_cascade_output_change_fraction(ann.0). 00000140 00000141 fann_set_callback(ann.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000120 00000121 fann_set_cascade_weight_multiplier(ann. 00000122 00000123 fann_set_cascade_max_out_epochs(ann.01f). 1. data. 150). FANN_STOPFUNC_MSE). 1). _ReportInterval. 00000131 00000132 fann_set_activation_function_hidden(ann. 00000138 fann_set_cascade_candidate_change_fraction(ann. (float) _TargetErr). (float) _LearnRate).01f).0. 0. 00000144 00000145 return true. _MaxNeurons. 00000129 00000130 fann_set_training_algorithm(ann. 0. 00000142 00000143 fann_cascadetrain_on_data(ann. FANN_SIGMOID). UnManaged_FANN_CasC::Report_Callback). 00000139 fann_set_train_stop_function(ann. 1). 00000135 00000136 fann_randomize_weights(ann. 00000127 00000128 fann_set_learning_rate(ann.

unsigned int max_epochs.\n". struct fann_train_data *train. if (MSqE <= desired_error) { tr_output << "\nNetwork matching or improving upon target error" << "found at node [ " << node << " ]. float desired_error. tr_output << "Best error at node [ " << node << " ] was " << MSqE << ". } node++. unsigned int epochs) { double MSqE = fann_get_MSE(ann). static int node = 1. double *error = NULL.dat"). vecbsterr. fann_scale_train_data(data. } //Test bool UnManaged_FANN_CasC::Test() { fann_type *calc_out = NULL. return 0.push_back(MSqE).William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000150 00000151 00000152 00000153 00000154 00000155 00000156 00000157 00000158 00000159 00000160 00000161 00000162 00000163 00000164 00000165 00000166 00000167 00000168 00000169 00000170 00000171 00000172 00000173 00000174 00000175 00000176 00000177 00000178 00000179 (struct fann *ann. 0. 1). unsigned int epochs_between_reports. data = fann_read_train_from_file("dlltedata.". Page 201 of 225 .

j < _OutputL. i++) { calc_out = fann_run(ann. for (unsigned int i = 0. j++) { te_output << " [" << data->output[i][j] << "] ". j++) { te_output << " [" << data->input[i][j] << "] ". } te_output << "\nOutput achieved was:". NULL). for (int j = 0. for (int j = 0. i < fann_length_train_data(data). data). j++) { te_output << " [" << calc_out[j] << "] ".William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 00000182 00000183 00000184 00000185 00000186 00000187 00000188 00000189 00000190 00000191 00000192 00000193 00000194 00000195 00000196 00000197 00000198 00000199 00000200 00000201 00000202 00000203 00000204 00000205 00000206 00000207 00000208 00000209 fann_set_callback(ann. te_output << "\nWith inputs:". te_output << fann_test_data(ann. error = new double[fann_length_train_data(data)]. data->input[i]). j < _OutputL. Page 202 of 225 . for (int j = 0. } } te_output << "\nMean Squared Error with these inputs and outputs is:". te_output << "\n\n\t\t\t~~Network Testing~~". j < _InputL. } te_output << "\nOutput Desired was:".

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000210 00000211 00000212 00000213 00000214 00000215 } delete error. Page 203 of 225 . delete calc_out. return true.

2 .UML Class Diagram The class diagram on the following pages represents the structure of the various classes within the program and how they are combined together to create a whole.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. Page 204 of 225 .

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Page 205 of 225 .

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Page 206 of 225 .

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 -BackProp Previous Page -CascadeCorrelation 1 FANN_Cascade -UMWrapper +FANN_Cascade(in AllDataToFillAttributes) +Train() : bool +Test() : bool 1 -UMWrapper 1 FANN_BackProp +FANN_BackProp(in AllDataToFillAttributes) +Train() : bool +Test() : bool 1 1 1 UnManaged_FANN_BackProp -_InputL : int -_HiddenL : int -_OutputL : int -_LearnRate : double -_TargetErr : double -_ReportInterval : int -_MaximumIteration : int -_Momentum : double -tr_output -te_output -wchoutput -vecbsterr -bsterr -ann -data +UnManaged_FANN_Backprop(in AddDataToFillAttributes) +~UnManaged_FANN_Backprop() +Train() : bool +Test() : bool UnManaged_FANN_CasC -_InputL : int -_OutputL : int -_LearnRate : double -_TargetErr : double -_ReportInterval : int -_MaxNeurons : int -tr_output -te_output -wchoutput -vecbsterr -bsterr -ann -data +UnManaged_FANN_CasC(in AllDataToAFillAttributes) +~UnManaged_FANN_CasC() +Train() : bool +Test() : bool +Report_Callback(in Attributes) : int 1 1 1 1 FANN_Library 9.mit.Libraries Researched • GALib o Matthew Wall o http://lancet.edu/ga/ o Accessed: 31/10/2008 Page 207 of 225 .3 .

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 • Libneural o D.cimne.sourceforge.com/flood/ o Accessed: 31/10/2008 • Annie o Asim Shankar o http://annie.org/libann/index.html o Accessed: 31/10/2008 • Flood o Roberto Lopez o http://www.edu/au/~daniel/software/libneural/ o Accessed: 31/10/2008 • Libann o Anonymous o http://www. Franklin o http://ieee.nongnu.uow.net/ o Accessed: 31/10/2008 • FANN (Fast Artificial Neural Network Library) o Steffen Nissen Page 208 of 225 .

Datasets • XOR 1 0 1 0 1 1 1 1 0 0 0 0 • • Fishers Iris Data o http://www.stats.xhtml Virus Classification o http://www.uk/pub/PRNN/README.html Page 209 of 225 .ac.uah.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 o http://leenissen.ox.php o Accessed: 31/10/2008 9.math.dk/fann/index.edu/stat/data/Fisher.4 .

Research current information on training neural networks using genetic algorithms. evaluate training methods for neural networks. Deliverable three: A document detailing and explaining the evaluation of different training techniques. Build an application that is capable of training a neural network with a genetic algorithm and with a backpropagation system.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. Using above application. Identify the most effective ways of training neural networks using genetic algorithms. Deliverable two: An application meeting the requirements of objectives three and four. and that allows manual adjustment of relevant variables. and a document describing its implementation and usage. Build a user-interface for the aforementioned application allowing for easy alteration of appropriate variables. and in which cases these methods are suitable. Page 210 of 225 .5 . and the conclusions derived from that evaluation. as well as previous research.Objectives FINAL YEAR DEGREE PROJECT Project Objectives & Deliverables • • • • • • Research the fields of genetic algorithms and neural networks. • • • Deliverable one: A research document describing and detailing the research done for objectives one and two.

aspx Page 211 of 225 . S. AI for Game Developers (pp. 2009. Neural Network in C++.. E..Works Cited Barber.edu/user/sef/www/publications/qp-tr. January 16). Retrieved April 16.codeproject. from Codeproject: www. O'Reilly. Carnegie Mellon: http://www-2. & Seeman.unikarlsruhe. from http://www. 2009. S. On the origin of the species by means of natural selection. AI: Neural Network for Beginners pt. C. from C# Corner: http://www. & G.de/~jbr/Papers/GaNNOverview. Bourg. Curved trajectory prediction using a self-organizing neural network.edu/user/sef/www/publications/cascor-tr. from http://www. Graphics processing unit computation of neural networks. (2007. (2004).cs. Retrieved April 13.cmu.cs.edu/afs/cs. (1859). Retrieved April 16.edu/afs/cs. Passing Data between Windows Forms.com/KB/recipes/NeuralNetwork_1. Retrieved April 16. J.ps Lysle. 2009. M. 356-358).cmu. 2009. In D.csharpcorner. 2009. Branke.gz Darwin. or the preservation of favoured races in the struggle for life. Davis. (1988).pdf Fahlman.cs.aifb. S. Wiley Professional Computing.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 10 . Carnegie Mellon: http://www2. Chapter 14: Neural Networks. London: John Murray. The Cascade-Correlation Learning Architecture. S. C. Retrieved April 13. from School of Computer Science. E. D. (2001).aspx Blum. An Empirical Study of Learning Speed in Back-Propagation Networks.unm. C. Bourg.cmu.com/UploadFile/scottlysle/PassData01142007013005AM/PassData. from School of Computer Science. (1991). M. (1995). 1.cmu. (2006). Retrieved April 13. G.edu/~chris2d/papers/CED_thesis. 2009. A.ps Fahlman. Seeman. & Lebiere.ps. E. (1992).

V.learnartificialneuralnetworks. W. S. from Computer Science . W. (2009).cs.com/robotcontrol. D. Retrieved 21 01. (1982). Retrieved April 15. A. Curved trajectory prediction using a selforganizing neural network.asp McCulloch. Retrieved April 16. 115-133. 2009. BackgroundWorker Class.microsoft.). F. from MSDN: http://msdn. 386-408.componentmodel. 2009. from MSDN: http://msdn. Bulletin of Mathematical Biophysics .UNC-Chapel Hill: http://www. from Artificial Neural Networks: http://www. Retrieved April 15. J.htm Rosenblatt. Stanford University.microsoft. M.com/en-us/library/system.aspx Mitchell. 5.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Marczyk. (1999). Learning Logic. Retrieved April 15. Using C++ Interop (Implicit PInvoke). from http://www.. (2004). & Pitts. Robot Control.ps.com/faqs/genalg/genalg. from FANN: http://leenissen.edu/~marshall/WWW/PAPERS/curve9912. J. Genetic algorithms and evolutionary computation. A logical calculus of the ideas immanent in nervous activity. 2009. An introduction to genetic algorithms. Back-propagation for the Uninitiated. Page 212 of 225 . 65.com/en-us/library/2x8kf7zx. The perceptron: A probabilistic theory for information storage and organization in the brain. Retrieved April 16.talkorigins.backgroundworker.generation5. Office of Technology Licensing. (2009). 2009.html Marshall. (1943). (1958).org/content/2002/bp. Nissen. 2009.dk/fann/ Parker. Invention Report S81-64. Microsoft. 2009.d. (2008).gz Matthews.aspx Microsoft. Massachusetts: MIT Press. Psychological Review . B. (n. (1996). Retrieved April 13.. & Srikanth. A. 2009.unc. from generation5: http://www. (2002).

Retrieved April 15.d.doc.php?title=Main_Page Page 213 of 225 . from http://zedgraph. Neural Networks. & Dimitrios. Harvard University.uk/~nd/surprise_96/journal/vol4/cs11/report. (1997).). P.. Retrieved 21 01. from Imperial College London: http://www.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Stergiou. (1974). 2009.org/wiki/index.ic. S. (n. C. ZedGraph.ac.html#Contents Werbos. Beyond Regression: New tools for prediction and analysis in the behavioral sciences. 2009. PhD.

S. Davis.pdf DiLascia. 3.com/Cpp/Cpp/cpp_mfc/tutorials/article. 2009. (1992). E.aifb. 1.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 11 . & G. Curved trajectory prediction using a self-organizing neural network. M. Neural Network in C++. 2009. London: John Murray. Chapter 14: Neural Networks. C. Bourg. 356-358). from http://www. A. On the origin of the species by means of natural selection. Branke. S. from MSDN: http://msdn. 2009. Bourg.edu/~chris2d/papers/CED_thesis. & Seeman.cs. 2009.aspx Barber. Retrieved April 13.Bibliography Barber. G. Retrieved April 13. from http://www. In D.codeproject. (2002). S. Retrieved April 13. (2004). O'Reilly. M. (1995). Retrieved April 13. 2009.codeguru. 2.php/c9855/ Page 214 of 225 . (2006). AI: Neural Network for Beginners pt. from Codeproject: http://www. from Codeguru: http://www. from Codeproject: http://www. AI: Neural Network for Beginners pt.com/enus/magazine/cc301501. 2009.unikarlsruhe. August 6). Call Unmanaged DLLs from C#. (2001). J.microsoft.aspx Barber.gz Darwin. Seeman.de/~jbr/Papers/GaNNOverview..com/KB/cs/GA_ANN_XOR.com/KB/recipes/Backprop_ANN. Wiley Professional Computing. (2006). (2007. Graphics processing unit computation of neural networks.codeproject. AI for Game Developers (pp.unm. (1859). or the preservation of favoured races in the struggle for life.aspx Blum.ps. D.com/KB/recipes/NeuralNetwork_1.codeproject. P. AI: Neural Network for Beginners pt. Killing Processes Cleanly. Retrieved April 13. from Codeproject: www. Retrieved April 13. (2006).aspx DLL Tutorial for Beginners. 2009. Retrieved April 13. C.

A logical calculus of the ideas immanent in nervous activity. & Lebiere. (2000).cmu. S.ps.edu/~marshall/WWW/PAPERS/curve9912. (2002).cmu.d. (1991).asp Matthews. from http://code.unc. (2004). Retrieved 26 01.gz Matthews. The Cascade-Correlation Learning Architecture. 2009. C. A. Retrieved 21 01.). (n. from generation5: http://www. Retrieved April 15. Back-Propagation: CBPNet. S.edu/user/sef/www/publications/qp-tr. 2009. Retrieved April 15. Retrieved April 16. & Srikanth.cmu.).cmu.org/content/2000/xorga.com/faqs/genalg/genalg. A. E. (1999).edu/afs/cs. generation5. 2009. from School of Computer Science. 2009.org/content/2000/cbpnet. Genetic algorithms and evolutionary computation. (n. E. J.UNC-Chapel Hill: http://www.generation5. Retrieved April 16. 115-133.com/p/googlechartsharp/ Lysle.d. from C# Corner: http://www. from Computer Science .asp McCulloch.google.edu/afs/cs. from XORGA: http://www.cs. Back-propagation for the Uninitiated. Retrieved April 16. Page 215 of 225 .talkorigins.ps GoogleChartSharp.aspx Marczyk. An Empirical Study of Learning Speed in Back-Propagation Networks. J. & Pitts.cs.. Carnegie Mellon: http://www2. Curved trajectory prediction using a selforganizing neural network. Carnegie Mellon: http://www-2.csharpcorner.cs. J. (1943). (2007. 5..html Marshall.asp Matthews.. from generation5: http://www. 2009. January 16). 2009. 2009. Retrieved April 15.org/content/2002/bp.generation5.com/UploadFile/scottlysle/PassData01142007013005AM/PassData. 2009. V. from School of Computer Science. Retrieved April 13. Passing Data between Windows Forms. Bulletin of Mathematical Biophysics . S.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Fahlman.generation5.edu/user/sef/www/publications/cascor-tr. (1988). W. 2009. from http://www. W.ps Fahlman. J.

). (n. Retrieved 21 01. Retrieved April 16.aspx Microsoft.d.backgroundworker. S.html Nissen.d.dk/fann/fann_1_2_0/r1597. 2009. B.org/nplot/wiki/ Parker.d.dk/fann/html/files/fann-h.com/en-us/library/2x8kf7zx. (n. Reference Manual.htm Page 216 of 225 . (n. Office of Technology Licensing.). from Datatypes: http://leenissen.). 21-26. from FANN: http://leenissen. Invention Report S81-64. 2009. Retrieved April 16. (2009). 2009. (1996). from FANN: http://leenissen.learnartificialneuralnetworks.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Microsoft. (1982). Massachusetts: MIT Press. from MSDN: http://msdn.html#fann_train_enum Nissen. Robot Control. 2009. Using C++ Interop (Implicit PInvoke). Retrieved 21 01. 2009. from Artificial Neural Networks: http://www.NET..html Nplot Charting Library for . Retrieved 21 01.com/robotcontrol.dk/fann/ Nissen. & Widrow. S. Retrieved 21 01.). M. Neural Networks.html Nissen..dk/fann/html/files/fann_data-h.d. (1990). S. from http://netcontrols. D. 2009. (n. from MSDN: http://msdn. D. (n. struct fann_train_data. from FANN: http://leenissen. from FANN: http://leenissen. Retrieved 21 01.). (2008).). (n.microsoft. Learning Logic. (2009).aspx Mitchell. S.d.com/en-us/library/system. S. 2009. An introduction to genetic algorithms. 1990. Retrieved April 15. Retrieved 26 01. Improving the learning speed of 2-layer neural networks by choosinginitial values of the adaptive weights.microsoft.dk/fann/fann_1_2_0/r1837. BackgroundWorker Class. B. 1990 IJCNN International Joint Conference on .d. struct fann. 2009.componentmodel. 2009. Stanford University. Nguyen. Nissen.

...... ......................uk/~nd/surprise_96/journal/vol4/cs11/report......d..........................org/wiki/index... Retrieved 21 01......... 386-408...... The circular connections are adjustable weights (Fahlman & Lebiere. 27 Figure 5 ....1 ...An artificial neuron .....................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Rosenblatt... (1958)......A feed-forward neural network ................ Stergiou.. 1991).Tables 12.... P................................................................. The Cascade-Correlation Learning Architecture.........local optimum (units are undefined in this case).... 2009................... 65.................................................... PhD............ac..... Harvard University....... The perceptron: A probabilistic theory for information storage and organization in the brain.. Psychological Review . (n...ic. (1974)... from http://zedgraph........................ Retrieved April 15...........The sigmoid function shape . 35 Page 217 of 225 ...)....... ZedGraph.. (1997).. 2009..... Beyond Regression: New tools for prediction and analysis in the behavioral sciences........ C.doc...........html#Contents Werbos.........................php?title=Main_Page 12 . 10 Figure 2 . F..... S... & Dimitrios.................... from Imperial College London: http://www.A human neuron .The initial state of a cascade correlation neural network........ ... 12 Figure 3 .............Table of Figures Figure 1 ................................... 13 Figure 4 .... 30 Figure 6 ........ Neural Networks....

................................................... 45 Figure 16 ....The second state of a cascade correlation neural network................................. The CascadeCorrelation Learning Architecture.........................................Dataset selection tab design (Wizard form).................................Design of the network settings tab (Wizard form) ..Dataset tab design (Main form) ..........Licenses display design (About form) .. 45 Figure 17 .. .................................................................... The square connections are locked weights(Fahlman & Lebiere... 44 Figure 15 .......................Design of the training algorithm tab (Wizard form) ..............................................The third state of a cascade correlation neural network.. 46 Figure 19 ....................... 1991)................. 36 Figure 9 – Network and output tab design (Main form) .Dataset display design (About form) .........Mean squared error tab design (Main form) ...... The Cascade-Correlation Learning Architecture....... 44 Figure 14 .................................... with two nodes added (Fahlman & Lebiere....... A first node has been added.......Mean squared error graph tab ..............................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 7 ................................................ 35 Figure 8 .........About form design (About form) ...... 42 Figure 12 ........................................................... 1991).............Network and Output tab................Learning Algorithm tab ........................................................... 49 Figure 22 .......... 51 Page 218 of 225 ............................................... 42 Figure 11 ........................................... 41 Figure 10 .Network Settings tab............................. 47 Figure 20 ............................................................................................................................................................ 48 Figure 21 .............Working form design (Working form) ................. 43 Figure 13 ....... 50 Figure 23 .......................................... 46 Figure 18 ...............The dataset tab .

........The mean squared error at testing (Iris data) ............................................................Mean squared error at testing (Viruses)............................................... 61 Figure 30 ........................Working form .. 51 Figure 25 ....... 67 Figure 37 ................. ...............................................Dataset tab ........................................................................ Licenses view .................. 12 Equation 2 ... 61 Figure 31 ...............Summation of the inputs and weights ................................................ 54 Figure 28 ...........................Mean squared error upon last training test (XOR) ....... main view ...............Number of iterations to achieve target (Iris data) . 2004).................Mean squared error upon last training test (Iris data) ....................... 53 Figure 27 ..About form..... 64 Figure 33............................................ 62 Figure 32 ..................................A sigmoid function(Bourg & Seeman.................About form....... 64 Figure 34................................ Datasets view ........................................................... 67 Figure 36 ................ 52 Figure 26 . 21 Page 219 of 225 ......................The mean squared error at testing (XOR)........................ 65 Figure 35 ........... 68 12... 13 Equation 3 – The delta rule(Matthews..Mean squared error upon last training test (Viruses)...................................... Back-propagation for the Uninitiated...........About form....... 54 Figure 29 ......Table of Equations Equation 1 .2 ...........William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 24 .......................... In this example “y” is the function output and “t” is the function input.......... 2002) ............................................ as defined in the graph above....................Number of iterations to achieve target (Viruses) ...Number of iterations to achieve target (XOR) .......

............. .......................................The partial derivative of ‘s’ with respect to each of the candidates incoming weights ‘wi’................. 33 Equation 9 ...................... 'p' is the training pattern. .................. 21 Equation 6 ............ The CascadeCorrelation Learning Architecture... 21 Equation 7 ............................................. Backpropagation for the Uninitiated....... 21 Equation 5 ........................... 34 12.The partial derivative of ’s’ (Fahlman & Lebiere............... Backpropagation for the Uninitiated................ 'v' is the candidate unit's value................Altered perceptron learning rule (Matthews.......................Back-propagation testing data (XOR)................. Back-propagation for the Uninitiated........The statistical approach to deciding relevant data .............................................................................. the quantities ‘`v ’ and ‘`Eo’ are the values of v and Eo averaged over all patterns (Fahlman & Lebiere...................3 ............................................... The Cascade-Correlation Learning Architecture..... 2002) ...................... 15 Table 2 .................... where ‘oo’ is the sign of the correlation between the candidates value output ‘o’.................. ‘f'p’ is the derivative for pattern 'p' of the candidate unit's activation function with respect to the sum of it’s inputs and ‘Ii........... 59 Table 3 ..............................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Equation 4 ..........p’ is the input the candidate receives from unit ‘i’ for pattern ‘p’....... Back-propagation for the Uninitiated......................................... 59 Page 220 of 225 ........... 2002) . 2002)......................Table of tables Table 1 .............................differentiated sigmoid function (Matthews....................................... 2002). 1991)............................ 1991)..............................Altered delta calculation rule for the hidden layer(s) (Matthews..... 22 Equation 8 ........The definition of 's' given that 'o' is the network output at which the error is measured........................Altered delta calculation rule for the output layer(Matthews.................................... 34 Equation 10 ..........................Genetic Algorithm testing data (XOR) ...................................

. 63 Table 7 .........Back-propagation testing data (Viruses) .......Cascade correlation testing data (Iris data) .......................... 66 Page 221 of 225 ...............................................................................................Genetic algorithm testing data (Iris data) ..........................................William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Table 4 ................ 66 Table 10 ...............Cascade Correlation testing data (XOR) ... 63 Table 6 ............................................................ 66 Table 9 ........ 63 Table 8 .................Back-propagation testing data (Iris data) ............................ 60 Table 5 ...................Cascade Correlation testing data (Viruses) .....Genetic Algorithms testing data (Viruses) .........................................

o Decision made to adjust the weights of the neural network during training as opposed to using a genetic algorithm to establish network structure. • 10/10/2008 o GALib decided as a genetic algorithm starting point o Further non-web references as a goal • • 16/10/2008 o Investigation into various neural network libraries taking place. o Submitted objectives. • 31/10/2008 o First presentation complete and ready o References building up o Project plan complete • 10/11/2008 o Presentation went well o Progress satisfactory • 14/11/2008 o Discussed document formatting and layout Page 222 of 225 . o Decided on XOR as a starting point. • 24/10/2008 o University computers were down so code demo was impossible. however. back-propagation network was running and learning XOR.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 13 . 17/10/2008 o Investigating more deeply into journals based on research already conducted. o Agreed on further investigation into GALib and genetic algorithms.Diary • 01/10/2008 – Research progress satisfactory.

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 o Progress satisfactory. • • • • • • • • • 19/11/2008 o Milestone one submitted successfully. • • 06/02/2009 o Still exploring extra areas for project to cover. 21/11/2008 o Progress satisfactory 28/11/2008 o Progress satisfactory 05/12/2008 o Progress satisfactory 12/12/2008 o Progress satisfactory 09/01/2009 o Progress satisfactory 16/01/2009 o Progress satisfactory 23/01/2009 o Progress satisfactory 30/01/2009 o Milestone two has been successfully submitted o Started investigating other areas to explore outside of project requirements. o Progress satisfactory • 20/02/2009 o Progress satisfactory Page 223 of 225 . 13/02/2009 o Have decided to extend the project by including cascade correlation learning algorithm and comparison to genetic algorithms.

catch-up time for previous two weeks will be used over Easter break.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 • • • • 27/02/2009 o Progress satisfactory 06/03/2009 o Project progress delayed by other coursework 13/03/2009 o Project progress delayed by other coursework 20/03/2009 o Project progressing. is much faster (fixed several bugs along the way) and retains none of the problematic memory leaks. o Report satisfactory • 05/04/2009 o Further work on user interface needed o Finish C# genetic algorithm code (C++ code does work but is very slow) o Report satisfactory • 06/04/2009 o C# genetic algorithm code is complete. o Have started report • 02/04/2009 o Main problems experienced with genetic algorithm seem related to memory leaks. have decided to implement in managed language (C#) to try to solve this o Report continues satisfactorily • 04/04/2009 o To do: About box Add dataset notes Decided on virus classification as a third dataset via the custom dataset functionality. Page 224 of 225 .

Page 225 of 225 . o Report satisfactory.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 o Report satisfactory • 10/04/2009 o Fully functional project – commencing report.