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

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

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

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

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

...... the work described within this project is the result of the investigation carried out by myself............ nor any part of it...................... ………………………………... and that neither this project......... Any material taken from published texts or computerised sources have been fully referenced. 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........... ………………………………... Page 9 of 225 ............................. except where specific reference is made......... ………………………………................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.............. Student Name (Printed) Student Signature Registered Scheme of Study Date of Signing ………………………………....

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

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

ℎ = × ℎ Equation 1 . 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. or one (activated) if the summed products are above a certain value. Page 12 of 225 . A specific input accepts a data item (usually a binary digit. The activation function accepts this stored value. real number.An artificial neuron The system devised with these components is as follows.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 2 . Alternatively. or integer). 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.Summation of the inputs and weights The next stage depends on the activation function chosen. A stepping function can be used which will output zero (or non-activated) if the summed products are below a certain value.

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

of “n” nodes. 1992). taking Page 14 of 225 .Advantages Neural networks make possible or practical many things that with conventional solutions would be extremely difficult.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. Werbos discovered the algorithm whilst working on his doctoral thesis in statistics and called it “dynamic feedback”. 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. 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. 2. where “n” is an undefined number.3. used on this architecture they become an extremely fast as well as flexible tool. 1982)). 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. perceptrons with “n” hidden layers. A good example of this is that neural networks have been implemented to run on architecture originally designed for processing three dimensional computer graphics.3 .Advantages and Disadvantages of Neural Networks 2.1 . (Blum. when Werbos (Werbos. The next major advancement in neural networks was not until 1974. 1974) discovered the back-propagation algorithm (independently rediscovered by Parker in 1982 (Parker.

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. 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 . in fact. 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.The statistical approach to deciding relevant data”. 2 Formulate a statistical model.2 . Page 15 of 225 . 4 Analyze the results. When using a neural network there's no need to establish before attempting problem solving which data is relevant. 1 Decide on relevant data.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. Table 1 . to attempt to provide a more robust post-training network. Neural networks can excel at determining what data in a particular set is relevant. 2001).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. 5 Build a system that incorporates what we have learned. noisy data is utilised during training upon occasion. where explaining why you have arrived at a particular diagnosis is an important part of the process. 2. This is a large disadvantage in areas such as medical diagnosis. 3 Run the formulated model.

or even to verify the quality of the training. The training times on neural networks can also be a disadvantage. When using neural networks is that. then a further one hundred images without. A classic example is that in the 1980s. a sunny day. Therefore. the US military wanted to use a neural network to analyse images and determine whether they contained a concealed tank. it can also be hard to tell how well (or how badly) trained a neural network is. They collected one hundred images with a concealed tank. train the neural network. only to find that on this round of testing. requires largely heuristic construction and preparation. The US military then tested the neural network with a further set of one hundred images. Eventually the original data set was re-examined. there is no formal methodology with which to choose the architecture. with and without tanks. After the neural network’s training was complete. A tool used to solve heuristic problems.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. the network had trained itself to recognise whether the sky was cloudy or not. for obvious purposes. Page 16 of 225 . 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. 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. the network came up with apparently random answers. asked to differentiate between the two sets of pictures. Fifty pictures of each set were set aside and the neural network trained with the remaining one hundred images with and without tanks. All the photographs with tanks were from a cloudy day and all the images without.

One of the main research areas is using neural networks to recognise diseases/conditions via scans. covering as many relevant situations as possible. This drawback is however one that is generally universal to computing (and many other areas) often referred to as “GIGO” or “Garbage in. the more accurate and in line with expectations and desires.Neural Networks in Medicine There is a large amount of research now covering the applications of neural networks in medicine. Garbage out”. 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. Page 17 of 225 . the usual best approach is to simply feed them as much data as can be obtained. The real strength of neural networks.1 . lies in their ability for generalisation.4 . this has to be done within reason however.4. Fortunately since neural networks are (as has been mentioned above) good at ignoring irrelevant data.Current Applications of Neural Networks 2.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. Data sets for training neural networks generally separate into two sub-sets. in the opinion of the author. 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. or it does exacerbate the problem with the time spent training the neural network. The network training first takes place using the appropriate “training” set. 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. “training” and “testing”. 2. the eventual result will be.

However. A back-propagation neural net is integrated with the airline marketing tactician which monitored and recommended on booking for each flight. merely a variety of scans covering all possible permutations of the disease or condition. Credit scoring. for example the human cardio-vascular system. being good at analysing patterns and predicting future trends.2 . 1997) 2. 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). You can also use the technology to model parts of the human biological system in order to better understand it. (Stergiou & Dimitrios.Neural Networks in Business Neural networks. 1997) Page 18 of 225 . In the 1980s.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. research on using neural networks to diagnose diseases took place. 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. and assessing borrowers have all incorporated neural networks as an integral part of the system. thus supplying information more or less directly linked to the airlines main income. (Stergiou & Dimitrios. 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. mortgage screening.4. can fit very well into most business situations.

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

This rule essentially describes adjusting the weight using the difference between the expected and actual outputs.1 -“Network.Back-propagation 2. 2.1. Adjust the weight of the neurons to minimise the responsibility. based on the strength of the weights connecting them to the output neurons. This is far preferable to having to perform lengthy and expensive re-calibrations on any regular basis (Robot Control.3.2 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 themselves. Assign responsibility to each of the neurons in the previous level. 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.cs” lines 130-133.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. Adjust the weight of the output neurons to lessen the error. Page 20 of 225 .Back-propagation in depth In a single layer neural network (or with a single node) the perceptron rule is applicable.5. The sigmoid function is in the source code at 9.5 . In order to use a back-propagation training algorithm you must have a non-linear activation function for your artificial neuron. A commonly used function is the sigmoid function as described in section 2.2 -The Artificial Neuron.5.1. 2008). The advantage of using a function like this is that it allows us to differentiate how close we came to the correct result.Back-propagation overview In essence. 2. allowing us to apply our learning algorithm.

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

in a language that can be linked to C# fairly easily and it also supports cascade neural networks. it is fast.Back-propagation library 2. 1859) Page 22 of 225 . Natural selection (Darwin. 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. The FANN library was decided upon for several reasons. to calculate the delta for a hidden layer requires the deltas for the following layers. Back-propagation for the Uninitiated.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.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Equation 7 . against which to compare the genetic algorithm trained neural network. 2. it is possible to “breed” solutions to problems. in order to apply this learning mechanism you start at the output of the network and work your way towards the inputs.5. By using similar techniques to natural selection.Altered delta calculation rule for the hidden layer(s) (Matthews.3 . 2002) As demonstrated above. although as it turned out I did not).6 . FANN) The FANN library (see (9. easy to implement. open-source (allowing me to alter it if I needed too. Most organisms evolve by means of sexual reproduction and natural selection (Darwin.7 . saving me finding a separate library.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. Therefore. 2.

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

2. Thanks to the parallelism that is a genetic algorithms main advantage. 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.2 .7.Advantages The primary advantage of genetic algorithms is their inherent parallelism. (Marczyk.Advantages and Disadvantages of Genetic Algorithms 2. abandon that search.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. with processing power exponentially dedicated to better areas as the exploration progresses. This leads to the exploration of a large proportion of the search space. proceeding with other more likely candidates. 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.7. can be used to fairly accurately predict national trends. a genetic algorithm is implicitly evaluating a much larger group of individuals. more conventional. 2004). These are “non-linear” Page 24 of 225 . Genetic algorithms effectively explore many different branches of the tree at once and when a certain branch turns out to be non-optimal. or further up the hierarchy. 2004) 2. 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.1 . methods. in the same way that the average response of a relatively small percentage of the population of any given country. The real advantage of parallelism however is that by evaluating the relatively small search-space that it does.

Genetic algorithms have no prior knowledge about the problem they are trying to solve. each component’s fitness is individual and any improvement to individual component's fitness is necessarily an improvement to the whole. A good example of this is the concept Page 25 of 225 . A third advantage of genetic algorithms is that they do not tend to be easily trapped by local optima. 2004). This non-linearity results in a huge increase in the search space. genetic algorithms tend to give good results compared to other search strategies. making genetic algorithms an effective way to search them. due to their strength in navigating large search spaces (Marczyk. In infinite or very large search spaces. Without crossover. However. Many real life problems are not like this.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 problems. 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. each searching their own space for the best solution. 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. or merely very good local optima. 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. This allows them to discover solutions that other algorithms may have over-looked. or never contained in their search space in the first place. In a linear problem. the random nature of the various starting points of the initial population and the other methods they employ. selection and mutation a genetic algorithm is metaphorically similar to a large number of parallel depth first algorithms. such as crossover and mutation. One of the largest strengths of genetic algorithms can at first glance appear to be their largest weakness. due again to the parallelism of their approach.

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

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

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

Because of this it is helpful to keep functional units close together (I. 1995). These are all connected. place a neurons input and output weights. however representing a large number of real valued numbers as a binary string. can lead to very large strings (thousands of bits) (Branke. a nodes input weights and the nodes of a layer. one or two hidden layers of an indeterminate number of nodes and one output layer. Page 29 of 225 .3 . The average neural network has an input layer. side by side). to form a feed forward network. 2.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Crossover can disrupt genes far apart.e. it is also possible to use a genetic algorithm to evolve a structure for a neural network. The problem at hand dictates the input and output layer structures. Standard genetic algorithms use binary strings.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.8. but is less likely to do so for genes close together. 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. outputs to inputs.

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

8. thus eliminating the searching of meaningless space and by definition. being able to contain the most optimum solution for the problem at hand. 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.3. whereas high-level encodings group together connections and/or nodes. an exception is possible. In one version of high level encoding of a neural network. 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. direct. Low-level encodings specify each connection individually.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 2. 1995). 1995). In direct encoding. or low-level encoding and the weak. the genetic algorithm represents the neural network by means of its connections. you must remove all connections to or from that node completely. Page 31 of 225 . the strong. Ideally. thus meaning that in order to remove a node completely. Back propagation learning rate is also a parameter for one of these areas. a representation is required which can accommodate all networks that will work for the problem but none that will not.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. or high-level encoding (Branke. indirect. There are two methods for representing neural network topologies within a genetic algorithm.1 .

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

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

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

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

Page 36 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 8 .The third state of a cascade correlation neural network. 1991). The Cascade-Correlation Learning Architecture. 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

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

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

New Network Wizard The new network wizard is the form that appears when the user selects the creation of a new network.Learning Algorithm tab On the learning algorithm tab you select the learning algorithm for the neural network you are creating.4.4.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.5 -Back-propagation.2 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 3. 3. Figure 12 .9 -Cascade Correlation).7 Genetic Algorithms. 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.2.4.4.2.1 -Learning Algorithm tab (if no algorithm has yet been selected. 2.Network Settings tab This tab allows you to set appropriate settings for the network and training algorithm you have selected in 3. the default is genetic algorithm). Page 43 of 225 . 2.2 . It allows you to move in a logical progression through the set up process of a new neural network and associated training algorithm. out of three choices previously mentioned (2.1 .Design of the training algorithm tab (Wizard form) 3.

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.Design of the network settings tab (Wizard form) 3.Dataset selection tab design (Wizard form) 3.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 Figure 13 . or to choose custom data sets from your hard disk for training and testing data (appropriately formatted in the FANN style). Figure 14 . both to protect the main form from data inputs that may cause training problems and to provide a visual indicator of work taking place.Dataset tab The dataset tab allows you either to select from two pre-setup datasets.3 .4.2. Page 44 of 225 .4.

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

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

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

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

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

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

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

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

• • The sorting implementation in GANetwork.2. and didn’t suffer from the memory leaks (being managed code) so that’s the version that exists in the application today (see 9.5 “Algorithms\GeneticAlgorithm.NET built in functionality (see 9.1. There is an unmanaged C++ wrapper (9.cs” lines 51-126) 4.cs” lines 176204).NET sort methods in the sorting part of the genetic algorithm).Back-propagation and Cascade Correlation Implementation The FANN library implements both cascade correlation and back propagation (Nissen. I decided to try re-implementing it in C#. after having used C# extensively in the UI I felt more confident about the performance loss and the use of the programming language itself.3 Page 56 of 225 .2 -“GANetwork.3.1.cs” lines 32-44).3.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++. Once this implementation was complete.cs that links into the .1 . it was both faster than the C++ implementation (due mostly to my use of build in . The neural network specific functionality added to the network model in the network specialisation class GANetwork (see 9.2 .1. Therefore. The compilation of the FANN library is static.1.2 -“GANetwork.2. and the Dll linked to this to allow access to the functionality. when the C++ implementation of genetic algorithms that I wrote functioned.1. but had issues with memory leaks and was slow.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.5 -“Algorithms\GeneticAlgorithm.4. FANN) which is a C programming language based neural network library.2.

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

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).02 for Fishers iris data and the virus classification data. 3.XOR In the XOR dataset. As the program presented the data in each case. Page 58 of 225 . to run the output layer. the dataset is too small to split.1 . The genetic algorithm and back-propagation neural networks each had four hidden nodes for each dataset. there are three functions. and creating a neural network with a similar structure on an excel spreadsheet. and hidden layer which are all called by the public “run()” function (lines 241-270). input layer. recording took place for analysis.4 -Datasets) (ten times for XOR. 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. 1.Testing Data Testing took place several times for each algorithm and dataset (see 9. five times for Virus classification and Fishers iris data).01 for XOR and 0. so repetition is instead used. I tested the neural network implementation by creating a neural network with structure 2. All nodes determine their outputs using the sigmoid function (lines 130-133). 5 .

009375132 0.00998959 0.0098402 0.009162436 0.009888967 0.00991907 0.0099765 0.00989974 0.009157809 Testing MSqE (GA) 0.00957518 0. because of floating point inaccuracies converting between double precision floating-point numbers in my code and single precision in the FANN code.00998027 0.009829335 0.00881867 0.009888967 0.009248076 0.00983181 0.Back-propagation testing data (XOR) Page 59 of 225 .009829335 0. Iterations (BP) 345 287 319 327 301 323 421 315 362 325 332.00992291 0.00987211 0.1.00990403 0.Genetic Algorithms Iterations (GA) 591 407 500 466 499 534 675 557 357 939 552.009375132 0.009162436 0.009969323 Testing MSqE (BP) 0.1 .00881867 0. the mean squared error from the network result and the testing mean squared error are different despite the data being the same.00992081 0.5 Result MSqE (GA) 0.0099113 0.006150786 0.00999799 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.006150786 0.5 Result MSqE (BP) 0.00990161 0.2 .009566951 0.009248076 0.00995716 0.009962559 0.009566951 0.009885499 Table 3 .Genetic Algorithm testing data (XOR) 5.00988863 0.00999069 0.00988649 0.009962559 0.009157809 Table 2 .00957518 0.Back-propagation In the back-propagation testing data.00997699 0.00998032 0.1.

the testing result differs quite widely from the network result.00992534 0.02E-13 1.99E-13 1. Nodes (CC) 2 2 2 2 2 2 2 2 2 2 2 Result MSqE (CC) 0.00656719 0.14E-14 2. At each node addition.22E-13 1.00889787 0.43545E-13 Table 4 .Cascade Correlation testing data (XOR) Page 60 of 225 . the call back function triggers.Cascade Correlation In the cascade correlation data.0086449 0.83E-13 2.00798691 0. but in the case of cascade training.18E-13 7.00573252 0.00960884 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.02E-13 2.78E-14 1.00585159 0.1.86E-14 1.00863005 0. the network is trained more after that point (output weight training).3 . which is when the program records the mean squared error.32E-13 2.008039565 Testing MSqE (CC) 7.00855044 0.

01 0.Mean squared error upon last training test (XOR) Page 61 of 225 .4 .012 0.1.Graphs 1000 900 800 700 600 500 400 300 200 100 0 0 5 10 15 Iterations (GA) Iterations (BP) Nodes (CC) Figure 29 .002 0 0 5 10 15 Result MSqE (GA) Result MSqE (BP) Result MSqE (CC) Figure 30 .006 0.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.008 0.004 0.Number of iterations to achieve target (XOR) 0.

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

0164964 0.0139982 0.01950238 Testing mean squared error (CC) 0.Cascade Correlation Nodes (CC) 2 2 2 2 2 2 Mean squared error (CC) 0.Back-propagation Iterations (BP) 434 529 474 391 527 471 Mean squared error (BP) 0.0193595 0.Genetic Algorithms Iterations (GA) 1928 1834 1277 2835 8716 3318 Mean squared error (GA) 0.01956255614 Table 5 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.0158679 0.1 .0189456 0.019775896 0.0141935 Table 6 .0199972 0.0143806 0.019751257 0.0199719 0.019719802 0.018789438 0.0199916 0.0140669 0.2.2.01956255614 Testing mean squared error (GA) 0.0199892 0.Fishers Iris data 5.014144 0.2 .018789438 0.0199918 0.Cascade correlation testing data (Iris data) Page 63 of 225 .019719802 0.01998996 Testing mean squared error (BP) 0.Back-propagation testing data (Iris data) 5.3 .014116 0.0198416 0.0196297 0.0160925 Table 7 .Genetic algorithm testing data (Iris data) 5.0141406 0.019775896 0.0195853 0.2 .2.019763877 0.019751257 0.0143778 0.0199999 0.019763877 0.

Graphs 10000 9000 8000 7000 6000 5000 4000 3000 2000 1000 0 0 2 4 6 Iterations (GA) Iterations (BP) Nodes (CC) Figure 32 .019 0.2.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.0202 0.4 .0196 0.Mean squared error upon last training test (Iris data) Page 64 of 225 .0192 0.Number of iterations to achieve target (Iris data) 0.02 0.0198 0.0188 0.0186 0 2 4 6 Mean squared error (GA) Mean squared error (BP) Mean squared error (CC) Figure 33.0194 0.

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

3.00980208 0.Genetic Algorithms Iterations (GA) 1818 1421 896 11676 1068 3375.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 5.0165979 0.019092293 0.Cascade Correlation Nodes (CC) 1 2 1 2 1 1.0199621 0.0393106 0.2 .3 .4 Mean Squared Error training (CC) 0.0190612094 Mean Squared Error testing (GA) 0.017043 0.01870197 0.019743 0.0105845 0.01983722 Mean Squared Error testing (BP) 0.01870197 0.0199916 0.019861371 0.0350318 0.016183342 Mean Squared Error testing (CC) 0.011585152 Table 10 .3.0194383 0.0185152 0.2 Mean Squared Error training (BP) 0.1 .Back-propagation testing data (Viruses) 5.00979968 0.00640291 0.Cascade Correlation testing data (Viruses) Page 66 of 225 .0190612094 Table 8 .0196959 0.0391179 0.0347797 0.019927361 0.8 Mean Squared Error training (GA) 0.017723052 0.019927361 0.0197935 0.Genetic Algorithms testing data (Viruses) 5.017723052 0.3.0195173 0.019861371 0.0111416 0.019092293 0.Back-propagation Iterations (BP) 3275 10922 2800 3744 6295 5407.0434423 0.03833646 Table 9 .

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

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

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

With more time.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 6. more in-depth testing using more varied datasets a clearer comparison and possibly drawbacks associated with cascade training may become evident. Compared to cascade correlation however.3 . the unavoidable conclusion is that cascade training is the most effective of the three training methods examined. They are roughly as effective as back-propagation training and although back-propagation is faster for the most part. with the information presented and discussed here. however. genetic algorithms fall down as a solution. 6. although there are further investigations I would like to continue with given more time (and may continue with in my own time).Evaluation Overall.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.3 .2.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. Page 70 of 225 . I feel the project has been a success. 7 .

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

the program occasionally causes an exception (most specifically when creating cascade or back-propagation networks in quick succession). 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.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 8 . Testing both with and without scaling and utilising different scaling methods. Due to small conflicts between managed and unmanaged memory. 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. Testing with datasets that would take large amounts of time to train.Possible Improvements More advanced testing would be the biggest improvement that more time would make possible. Improve memory management. Page 72 of 225 .

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

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

1 .Text. private int _CC_MaxNeurons.Linq. namespace _05025397. private int _CC_Reports.1.Controller.Generic. System. System.View 9.1. //Cascade Correlation private double _CC_Terr.“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.Classes { public class NetworkUpdateEventArgs : System. private bool _CC_ITerr.1 . System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.Collections. //Dataset identifier private int _TrDataSet. Page 75 of 225 .1.EventArgs { #region data /********************\ |* DATA *| \********************/ //Training Algorithm Identifier private int trainingalg. private double _CC_LearningRate.

private int _GA_HiddenL. private int _GA_Crossover. double CCTerr. private int _GA_Mutation. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ //Constructor to accept all necessary data public NetworkUpdateEventArgs (int Algorithm. bool CCITerr. int CCReports. private bool _BP_ITerr. Page 76 of 225 . int CCMaxNeurons. private double _BP_Terr. private bool _GA_ITerr. private int _GA_Reports. private int _GA_GenLimit.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. private double _GA_Terr. //BackProp private int _BP_EpochLimit. private double _BP_LearningRate. int GAPopSize. double CCLearningRate. private int _BP_HiddenL. private int _BP_Reports.

//Cascade Correlation _CC_Terr = CCTerr. //Backpropagation Algorithm _BP_EpochLimit = BPEpochLimit. int BPHiddenL. double BPLearningRate. _GA_Mutation = GAMutation. _GA_GenLimit = GAGenLimit. _GA_HiddenL = GAHiddenL. double GATerr. //Genetic Algorithm _GA_PopSize = GAPopSize. double BPTerr. _CC_LearningRate = CCLearningRate. _GA_Terr = GATerr. bool GAITerr. _CC_MaxNeurons = CCMaxNeurons. bool BPITerr. _CC_Reports = CCReports. int GAHiddenL. _GA_ITerr = GAITerr. int GACrossOver. int TrData_Set) { //Algorithm trainingalg = Algorithm. Page 77 of 225 . int BPReports. int GAReports. int GAMutation. _GA_Reports = GAReports. _CC_ITerr = CCITerr. _GA_Crossover = GACrossOver.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. int BPEpochLimit.

} } public double CC_LearningRate { get { return _CC_LearningRate. _BP_LearningRate = BPLearningRate. } #endregion #region getters/setters #region cascade_correlation //Cascade Correlation public double CC_Terr { get { return _CC_Terr. } } public int GA_GenLimit Page 78 of 225 .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. } } public int CC_MaxNeurons { get { return _CC_MaxNeurons. _BP_ITerr = BPITerr. _BP_Terr = BPTerr. _BP_Reports = BPReports. } } #endregion #region genetic_algorithms //Genetic Algorithm public int GA_PopSize { get { return _GA_PopSize. _TrDataSet = TrData_Set. } } public bool CC_ITerr { get { return _CC_ITerr. } } public int CC_Reports { get { return _CC_Reports.

} } public int BP_HiddenL { get { return _BP_HiddenL. } } #endregion Page 79 of 225 . } } public double GA_Terr { get { return _GA_Terr. } } public int GA_HiddenL { get { return _GA_HiddenL. } } public bool GA_ITerr { get { return _GA_ITerr. } } public bool BP_ITerr { get { return _BP_ITerr. } } public int GA_Crossover { get { return _GA_Crossover. } } #endregion #region back_propagation //Back propagation public int BP_EpochLimit { get { return _BP_EpochLimit. } } public int GA_Reports { get { return _GA_Reports. } } public double BP_LearningRate { get { return _BP_LearningRate. } } public double BP_Terr { get { return _BP_Terr. } } public int BP_Reports { get { return _BP_Reports.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. } } public int GA_Mutation { get { return _GA_Mutation.

} } #endregion } 9.Collections. System.2 . System. System. System.Forms.1.Data. } } public int TrainingAlgorithm { get { return trainingalg. System.Drawing.Controller { public partial class FrmMain : Form Page 80 of 225 . namespace _05025397. using _05025397.ComponentModel.Text.Windows. System.“FrmMain.1.IO.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. System. using ZedGraph. System.Generic.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.Linq.

TrainingAlgorithm network.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted).Working workingdialog = new Controller.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.DoWork += new DoWorkEventHandler(bw_DoWork). #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public FrmMain() { InitializeComponent().Algorithms. this. bw. } #endregion #region internal_functions /********************\ Page 81 of 225 .Working(). bw.icon. //Working dialog Controller.Icon = Properties. //Background thread static BackgroundWorker bw = new BackgroundWorker().

MSEpane.Clear().AddCurve("".zedGraphControl1.GraphPane. this.NetworkUpdateEventArgs e) { LoadDataset(e. GraphPane MSEpane = this.CurveList.Text = "MSE".TrainingAlgorithm == 0) { Page 82 of 225 . Color.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.Legend. //Titles MSEpane.Title.IsVisible = false.zedGraphControl1. MSEpane. MSEpane.AxisChange(). MSE.Text = "Iteration". // Hide the legend MSEpane.Title.YAxis.XAxis. try { if (e.Title.zedGraphControl1.Red.Circle). Classes. SymbolType. } private void updatenetwork(object sender.TrDataSet).GraphPane.Text = "Mean Squared Error".

e. e. txtTrainData.CC_LearningRate.Algorithms.BP_ITerr.BP_Terr. e. e. txtTrainData. e.GA_GenLimit.GA_Terr.GeneticAlgorithm (e. } if (e.GA_Mutation. e.BackProp (e.CC_Reports.Algorithms.CC_ITerr. txtTrainData. e. e.BP_HiddenL. e.CC_Terr.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.BP_LearningRate. txtTestData).CascadeCorrelation (e. txtTestData).Algorithms.GA_HiddenL. e. e. txtTestData).CC_MaxNeurons.GA_Crossover. e.BP_EpochLimit. } if (e. } } catch { Page 83 of 225 . e. e. e.TrainingAlgorithm == 2) { network = new Controller.GA_Reports. e.BP_Reports.GA_PopSize.TrainingAlgorithm == 1) { network = new Controller.GA_ITerr.

Text = network.Wiz1. wizard.NetworkUpdateHandler(updatenetwork).Error).Text = Properties.Resources.XOR: txtTrainData.Text = Properties. wizard.XORtrain. } } private void new_network() { Controller.Resources.Wiz1 wizard = new Controller.ShowDialog().Exit(). "Error".NetworkUpdated += new Controller. Page 84 of 225 .OK.XORtest. Application.Wiz1().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. MessageBoxButtons. MessageBoxIcon.Show("Error creating new network". } if (network != null) { txtNetSettings. } private void LoadDataset(int Data_set) { switch (Data_set) { case (int)Dataset.network_details(). txtTestData.

Error). try { train = LoadTxtFile(trainpath).Show("Error: Problem loading training data". } Page 85 of 225 .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.FISHERtest. case (int)Dataset.Text = Properties.FISHER: txtTrainData.\\". "Select Testing Data"). break. "Error!".Resources. break.OK. string testpath = SelectTextFile(". string test = "".FISHERtrain. txtTestData.\\". } catch { MessageBox. } } private void LoadCustomDataSet() { string trainpath = SelectTextFile(".Text = Properties.CUSTOM: LoadCustomDataSet().Resources. string train = "". case (int)Dataset. "Select Training Data"). MessageBoxButtons. MessageBoxIcon.

"Error!". try { File = new FileStream(path. MessageBoxIcon.OK. MessageBoxButtons.Error).Show("Error: Problem loading testing data". MessageBoxButtons. } private string LoadTxtFile(string path) { string data = "". StreamReader Reader = null.Read).Error). "Error!". FileAccess. txtTrainData.Open.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). data = Reader. } txtTestData. FileStream File = null.Text = train. FileMode.OK. Page 86 of 225 . MessageBoxIcon.Show("Error reading selected file: " + path. } catch { MessageBox. Reader = new StreamReader(File).ReadToEnd(). } catch { MessageBox.Text = test.

Close().Close().Title = title. } private string SelectTextFile(string initialDirectory. if (File != null) File. dialog.FileName : null. } finally { if (Reader != null) Reader. } #endregion #region menu_items /********************\ |* MENU ITEMS *| \********************/ Page 87 of 225 . } return data.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 = "".*". dialog. return (dialog.ShowDialog() == DialogResult.*)|*.OK) ? dialog.Filter = "txt files (*.InitialDirectory = initialDirectory. dialog.txt)|*.txt|All files (*. string title) { OpenFileDialog dialog = new OpenFileDialog().

Show(). EventArgs e) { FrmAbout About = new FrmAbout(). EventArgs e) { if (network != null) { if (network. } private void runToolStripMenuItem_Click(object sender.Test(). } } } private void aboutToolStripMenuItem1_Click(object sender.Close(). About. EventArgs e) { new_network(). EventArgs e) { this.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. } #endregion #region buttons Page 88 of 225 .Trained != false) { network. } private void exitToolStripMenuItem_Click(object sender.

RunWorkerAsync().Trained) { bw. EventArgs e) { new_network(). if (!bw. } } } } private void btnTest_Click(object sender.ShowDialog().Trained) Page 89 of 225 . EventArgs e) { //Shouldn't be busy if the user managed to click this //but just make 100% certain. workingdialog.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. EventArgs e) { if (network != null) { if (network. } private void btnTrain_Click(object sender.IsBusy) { if (network != null) { if (!network.

Text. txtOutput. txtOutput. } Page 90 of 225 .Length.ReportData.Text = network.ScrollToCaret().Test(). DoWorkEventArgs e) { bool success = true.SelectionStart = txtOutput. txtOutput.Refresh(). txtOutput.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. } } } } } #endregion #region bg_worker /********************\ |* BG WORKER *| \********************/ private void bw_DoWork(object sender. if (success) { txtOutput.Clear().Tested) { bool success = network. if (network != null) { success = network.Train().

MessageBoxIcon.Length. plotgraphsMSE(network.Error).Result = success.Show("Error during network train: " + e.Text = network. txtOutput.Error != null) { MessageBox. MessageBoxButtons.ToString(). MessageBoxIcon.ScrollToCaret().Show("Error training network!".Result. } else { MessageBox. "Error training network!". RunWorkerCompletedEventArgs e) { bool success. if (success) { txtOutput.Clear().Error. MessageBoxButtons.OK. txtOutput.Error). //Check for errors if (e.ReportData. txtOutput.OK. } Page 91 of 225 .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.SelectionStart = txtOutput.Text. } private void bw_RunWorkerCompleted(object sender. "Error".getGraphData()). } success = (bool) e.

} #endregion } 9.Linq.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.Data.ComponentModel. System.1.Hide().“Wiz1.1. System. } #endregion #region misc /********************\ |* MISC *| \********************/ private void FrmMain_Shown(object sender.Generic.Drawing.3 . System.Collections. Page 92 of 225 . EventArgs e) { //Run the new neural net wizard new_network(). System. System.cs” 00000001 00000002 00000003 00000004 00000005 00000006 using using using using using using System.

Classes.Windows. BP.Forms. using _05025397. #endregion public partial class Wiz1 : Form { #region data /********************\ |* DATA *| \********************/ public delegate void NetworkUpdateHandler(object sender. FISHER. Page 93 of 225 . namespace _05025397. using System. public event NetworkUpdateHandler NetworkUpdated.Controller { #region enums /********************\ |* ENUMS *| \********************/ enum Training { GA. enum Dataset { XOR.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.NetworkUpdateEventArgs e).Text. private int Trainset. CC }. CUSTOM }. private int algorithm.

Icon = Properties. EventArgs e) Page 94 of 225 .GA. algorithm = (int)Training.Resources.Close(). Trainset = (int)Dataset. } #endregion #region control_events /********************\ |* CONTROL EVENTS *| \********************/ #region buttons /********************\ |* BUTTONS *| \********************/ private void btnCancel_Click(object sender.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().icon. } private void rBtnBackprop_CheckedChanged(object sender. this.XOR. EventArgs e) { this.

CC.Checked == true) { picAlg.Visible = true. EventArgs e) { if (rBtnCascadeCorrelation.Visible = false.Resources.Visible = false. lblBP.Visible = false. algorithm = (int)Training. algorithm = (int)Training. lblCC. panCC. lblCC.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.Image = _05025397.Visible = false.Properties.IMAGECascadeCorrelation. panGeneticAlgorithm.Visible = false. panBackprop. Page 95 of 225 . panBackprop. panGeneticAlgorithm.IMAGEBackProp.Checked == true) { picAlg. lblGA.Resources.Visible = true.Visible = true. lblBP. } } private void rBtnCascadeCorrelation_CheckedChanged(object sender.Image = _05025397.Visible = false.BP.Properties.Visible = false. lblGA.Visible = false.Visible = true. panCC.

SelectedIndex > 0) { tabControl1.Image = _05025397.Visible = false.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. algorithm = (int)Training. } } private void btnNext_Click(object sender.Visible = false.Checked == true) { picAlg.1). } } private void btnBack_Click(object sender. lblBP.IMAGEGeneticAlgorithm. lblGA. panBackprop. EventArgs e) { if (tabControl1.Visible = true. EventArgs e) { Page 96 of 225 .Properties.Visible = true. panGeneticAlgorithm.Resources. lblCC.SelectedIndex .GA.SelectTab(tabControl1.Visible = false. EventArgs e) { if (rBtnGeneticAlgorithm.Visible = false. panCC.

SelectTab(tabControl1. txtCC_Report.Text. txtCC_MaxNeurons. check.Text.CC_Reports.Verified) { Classes.NetworkUpdateEventArgs args = new Classes. txtCC_TargetError. check.Text. txtBP_EpochLimit.Text.GA_Terr.NetworkUpdateEventArgs (check.BP_EpochLimit. check. check.Checked.TrainingAlgorithm. check. check.Checked. check. txtCC_LearningRate.Text.GA_HiddenL. check. check. check.GA_Mutation. } else { VerifyData check = new VerifyData (algorithm. Page 97 of 225 .Text.TabCount . txtGA_ProbM. check.CC_ITerr. if (check. txtGA_Terr.1) { tabControl1.Text.Text. txtBP_Terr.Text.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.Text. check. txtGA_Report.Text.Text.GA_ITerr.GA_Reports.CC_Terr. txtBP_LearnRate.Text. txtGA_ProbX. txtBP_Report. txtGA_PopSize.GA_Crossover. check. check.SelectedIndex < tabControl1.GA_GenLimit.Text. CC_ITerr.BP_HiddenL.CC_MaxNeurons. chkGA_IgnoreTarget. txtGA_HiddenNodes.CC_LearningRate.Text.Checked.SelectedIndex + 1).Text. chkBP_IgnoreTarget. check. Trainset). txtGA_MaxGen. txtBP_HiddenNodes.GA_PopSize.

BP_LearningRate.OK. check.IMAGEIrisFlowers. MessageBoxIcon.Resources. check. } } } #endregion #region checkboxes /********************\ |* CHECKBOXES *| \********************/ private void chkDatasetFisher_CheckedChanged(object sender.Checked) { picDataset.BP_Terr. check.FISHER. this. EventArgs e) Page 98 of 225 .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. EventArgs e) { if (chkDatasetFisher. } else { MessageBox.BP_Reports. "Error Checking Data".Properties.TrDataSet). args).Exclamation). Trainset = (int)Dataset.Show("Error Checking Data: Data could not be verified".Image = _05025397. check.Dispose(). MessageBoxButtons. } } private void chkDatasetXOR_CheckedChanged(object sender. NetworkUpdated(this.BP_ITerr.

Focus(). EventArgs e) { if (chkDatasetCustom. Trainset = (int)Dataset.Properties.Resources. } Page 99 of 225 .XOR.Visible == true) { txtCC_TargetError. } } #endregion #region misc /********************\ |* MISC *| \********************/ private void tabControl1_SelectedIndexChanged(object sender. } } private void chkDatasetCustom_CheckedChanged(object sender.IMAGEXOR.SelectedIndex == 1) { if (panCC.Resources.Image = _05025397.Properties. Trainset = (int)Dataset.Image = _05025397.IMAGEQuestionMark.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.Checked) { picDataset.CUSTOM. EventArgs e) { if (tabControl1.Checked) { picDataset.

Icon = Properties. } } } private void Wiz1_Load(object sender. } if (panGeneticAlgorithm.Focus().SelectTab(0).Resources.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.Focus().icon. EventArgs e) { this.Visible == true) { txtGA_PopSize. tabControl1.Visible == true) { txtBP_EpochLimit. } #endregion #endregion } Page 100 of 225 .

ComponentModel. System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. System.Data.1.Controller { public partial class FrmAbout : Form { #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public FrmAbout() { InitializeComponent(). System.4 .Windows. } #endregion #region form /********************\ |* FORM *| \********************/ private void FrmAbout_Load(object sender.1.Collections. EventArgs e) { Page 101 of 225 . namespace _05025397. System.Generic.“FrmAbout.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.Drawing. System.Forms. System. System.Text.Linq.

LinkVisited = true.Process. try { // Navigate to a URL.ProductVersion.Diagnostics.Text = Properties.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.Resources. LinkLabelLinkClickedEventArgs e) Page 102 of 225 . } #endregion #region linklabels /********************\ |* LINKLABELS *| \********************/ private void linkLabel1_LinkClicked(object sender.Icon = Properties.linkLabel1.". "Undefined Application".Text). } } private void linkLabel4_LinkClicked(object sender.Text = Application.Show("No e-mail program defined.LICENCE_LGPL21.OK. textBox5. label5. System.icon.Start("mailto:" + linkLabel1. this. LinkLabelLinkClickedEventArgs e) { // Specify that the link was visited. MessageBoxIcon. } catch { MessageBox.Resources.Information). MessageBoxButtons.

Start (linkLabel5.OK.Show("Error opening link. "Error". "Error". MessageBoxIcon. } } private void linkLabel6_LinkClicked(object sender.Text). MessageBoxButtons. } } private void linkLabel5_LinkClicked(object sender.".Diagnostics.Show("Error opening link.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.".Error).Process. MessageBoxIcon. LinkLabelLinkClickedEventArgs e) { try { System.Process.OK. } catch { MessageBox. MessageBoxButtons. } catch { MessageBox.Start (linkLabel4.Text).Diagnostics. LinkLabelLinkClickedEventArgs e) Page 103 of 225 .Error).

OK.Text).Process.Show("Error opening link.Start ("http://www.snikks. } catch { MessageBox. MessageBoxIcon. LinkLabelLinkClickedEventArgs e) { try { System.Diagnostics.uk").Diagnostics.Error).Process.Error). MessageBoxButtons. LinkLabelLinkClickedEventArgs e) Page 104 of 225 .co. } catch { MessageBox.Start (linkLabel6. } } private void linkLabel7_LinkClicked(object sender.OK. "Error".Show("Error opening link. MessageBoxIcon.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. MessageBoxButtons. } } private void linkLabel2_LinkClicked(object sender. "Error".".".

Show("Error opening link.Error).html").Visible = false.Visible = true.Diagnostics.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.Start ("http://www. } } #endregion #region buttons /********************\ |* BUTTONS *| \********************/ private void btnDatasets_Click(object sender.gnu. pictureBox1.".org/licenses/old-licenses/lgpl-2.Process. } catch { MessageBox. tabControl1. groupBox2. "Error".1.Visible = false.Visible = false. MessageBoxIcon. } else Page 105 of 225 .Visible != true) { groupBox1. MessageBoxButtons. EventArgs e) { if (tabControl1.OK.

false.Visible = false. } } private void btnOkay_Click(object sender.Visible = false.Visible } } private void btnLicences_Click(object sender. pictureBox1.Visible != true) { groupBox2. } else { groupBox1.Visible = groupBox2.Visible = false. EventArgs e) { this. tabControl1.Dispose().Visible = true. = false.Visible = true. tabControl1.Visible pictureBox1. Page 106 of 225 .Visible = true. groupBox1. EventArgs e) { if (groupBox2.Visible = tabControl1. pictureBox1.Visible = false. groupBox2.Visible = false.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. } true. = true.

} private void radioButton3_CheckedChanged(object sender. EventArgs e) { if (radioButton3.LICENCE_LGPL21. } private void radioButton2_CheckedChanged(object sender. EventArgs e) { if (radioButton1. } #endregion } Page 107 of 225 .Checked == true) textBox5. EventArgs e) { if (radioButton2.Text = Properties.Text = Properties.LICENCE_GPL30.Resources.Resources.Checked == true) textBox5.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.Checked == true) textBox5.Text = Properties.LICENCE_LGPL30.

SystemSounds.Media.“Working.Data.Forms.Resources. System.1.Drawing.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. } #endregion } Page 108 of 225 . } private void Working_Click(object sender. System. System.Beep.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.Controller { public partial class Working : Form { #region functions /********************\ |* FUNCTIONS *| \********************/ public Working() { InitializeComponent(). System.Icon = Properties. this.Windows.Linq.1.icon. namespace _05025397.Play(). System.Generic. EventArgs e) { System. System.Collections.5 .Text.ComponentModel. System.

Linq.Controller { public class VerifyData { #region data /********************\ |* DATA *| \********************/ //Dataverified variable private bool verified.“VerifyData.Generic. System.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 using using using using using System.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000030 } 9.Text. System.2 . namespace _05025397.Forms.Collections.Windows. System.1 .1. System. //Training Algorithm Identifier //1 = genetic algorith //2 = backprop //3 = cascade correlation private int trainingalg.Controller 9.1. //Dataset identifer //0 = XOR Page 109 of 225 .

private int _GA_HiddenL. //BackProp private int _BP_EpochLimit. private bool _GA_ITerr. private double _CC_LearningRate.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. //Genetic Algorithm private int _GA_PopSize. private double _GA_Terr. #endregion Page 110 of 225 . private double _BP_LearningRate. private int _GA_GenLimit. //Cascade Correlation private double _CC_Terr. private int _GA_Mutation. private int _CC_MaxNeurons. private bool _CC_ITerr. private int _GA_Crossover. private double _BP_Terr. private int _CC_Reports. private bool _BP_ITerr. private int _BP_Reports. private int _GA_Reports. private int _BP_HiddenL.

string BPReports. int TData_Set) { //Initially set to true. string CCMaxNeurons. string BPEpochLimit.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 CCLearningRate. string GAHiddenL. string BPLearningRate. string GACrossOver. will be changed to //false if any data verification errors occur verified = true. //Algorithm trainingalg = Algorithm. string BPHiddenL. bool GAITerr. //DataSet _TDataSet = TData_Set. string GATerr. string BPTerr.XOR && _TDataSet != Page 111 of 225 . string GAGenLimit. string CCReports. string CCTerr. string GAPopSize. string GAReports. bool BPITerr. if (_TDataSet != (int)Dataset. string GAMutation. bool CCITerr.

ToSingle(CCLearningRate). _CC_MaxNeurons = Convert. verified = false.Show ("Parsing Error: Please check supplied cascade" + "correlation data values".FISHER && _TDataSet != (int)Dataset. } catch (FormatException) { MessageBox.OK. MessageBoxButtons. MessageBoxIcon.ToInt32(CCMaxNeurons). _CC_Reports = Convert. } } } Page 112 of 225 .ToInt32(CCReports). _CC_ITerr = CCITerr.CUSTOM) verified = false.ToSingle(CCTerr).Error). _CC_LearningRate = Convert.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. if (verified) { if (trainingalg == 2) { try { //Cascade correlation _CC_Terr = Convert. "Parsing Error".

ToInt32(GAReports). MessageBoxButtons. _GA_HiddenL = Convert.ToSingle(GATerr). } } } if (verified) Page 113 of 225 . _GA_Mutation = Convert. MessageBoxIcon. } catch (FormatException) { MessageBox. _GA_Reports = Convert.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_Crossover = Convert.ToInt32(GACrossOver). "Parsing Error".ToInt32(GAGenLimit).OK. _GA_ITerr = GAITerr.ToInt32(GAHiddenL). _GA_Terr = Convert.Error).Show ("Parsing Error: Please check supplied" +" genetic algorithm data values". verified = false.ToInt32(GAPopSize). _GA_GenLimit = Convert.ToInt32(GAMutation).

OK. _BP_LearningRate = Convert. verified = false. _BP_Reports = Convert.ToInt32(BPHiddenL). _BP_HiddenL = Convert. } Page 114 of 225 .ToInt32(BPEpochLimit). } } } if (verified) { verified = checkforsanity().ToInt32(BPReports).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. MessageBoxIcon. "Parsing Error".ToDouble(BPLearningRate).Show ("Parsing Error: Please check " + "supplied back propagation data values".Error). _BP_ITerr = BPITerr.ToDouble(BPTerr). _BP_Terr = Convert. } catch (FormatException) { MessageBox. MessageBoxButtons.

if (_GA_GenLimit < 1) sanity = false. } if (_GA_Terr < 0 || _GA_Terr > 1) sanity = false. Page 115 of 225 . if (_GA_PopSize < 3) sanity = false.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. if (_GA_Crossover < 0 || _GA_Crossover > 100) { sanity = false. } if (_GA_Mutation < 0 || _GA_Mutation > 100) { sanity = false.

if (_BP_Reports < 1 || _BP_Reports > _BP_EpochLimit) sanity = false. Page 116 of 225 . } private bool checkBPsanity() { bool sanity = true. MessageBoxIcon. if (_BP_HiddenL < 1) sanity = false. if (_BP_EpochLimit < 1) sanity = false. MessageBoxButtons. "Sanity Check Fail".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. return sanity. if (_GA_HiddenL < 1) sanity = false.OK.Show ("Sanity Check Fail: Please check supplied " + "genetic algorithm data values". if (!sanity) MessageBox.Error).

"Sanity Check Fail". return sanity. Page 117 of 225 . if (_CC_Terr < 0 || _CC_Terr > 1) sanity = false.OK. MessageBoxIcon. if (_BP_LearningRate < 0 || _BP_LearningRate > 1) sanity = false. if (_CC_Reports < 1 || _CC_Reports > _CC_MaxNeurons) sanity = false. MessageBoxButtons.Show ("Sanity Check Fail: Please check supplied " + "back propagation data values". if (_CC_MaxNeurons < 1) sanity = false.Error). if (!sanity) MessageBox. } 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.

only //halt any values from proceeding that are so wrong they could cause //serious errors later down the line. We're also doing some very minor processing of the probability values.Error).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. if (!sanity) MessageBox. MessageBoxButtons.however I don't want to prevent any flexibility. return sanity. if (trainingalg < 0 || trainingalg > 2) Page 118 of 225 . } private bool checkforsanity() { //Some of the values that are allowed through here would not produce //good results . if they're greater than 1 but less than 100 scale them appropriately. "Sanity Check Fail".OK.Show ("Sanity Check Fail: Please check supplied " + "cascade correlation data values". MessageBoxIcon. // // // // // bool Basically we're making sure the data is clean before it goes anywhere near a DLL. sanity = true.

MessageBoxIcon. } if (sanity) { if (trainingalg == 0) { sanity = checkGAsanity(). MessageBoxButtons.Show ("Sanity Check Fail: Training Algorithm not " + "selected or selected incorrectly". MessageBox. } Page 119 of 225 .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. } if (trainingalg == 1) { sanity = checkBPsanity(). "Sanity Check Fail".Error).OK. } } return sanity. } if (trainingalg == 2) { sanity = checkCCsanity().

} } public int CC_Reports { get { return _CC_Reports. } } public int GA_Crossover { get { return _GA_Crossover. } } public int CC_MaxNeurons { get { return _CC_MaxNeurons. } } public bool CC_ITerr { get { return _CC_ITerr. } } public int GA_Mutation Page 120 of 225 .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 double CC_LearningRate { get { return _CC_LearningRate. } } #endregion #region genetic_algorithm //Genetic Algorithm public int GA_PopSize { get { return _GA_PopSize. } } public int GA_GenLimit { get { return _GA_GenLimit.

} } #endregion #endregion #region external //Verification Page 121 of 225 . } } public bool BP_ITerr { get { return _BP_ITerr. } } _GA_ITerr. } } public double BP_Terr { get { return _BP_Terr. } } _GA_Terr. } } public int BP_Reports { get { return _BP_Reports. } } _GA_Reports. } } public int BP_HiddenL { get { return _BP_HiddenL. } } public double BP_LearningRate { get { return _BP_LearningRate.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. } } #region back_propagation //Back propagation public int BP_EpochLimit { get { return _BP_EpochLimit. } } _GA_HiddenL.

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. } } //TrainingAlgorithm public int TrainingAlgorithm { get { return trainingalg. } } #endregion } Page 122 of 225 . } } //Dataset public int TrDataSet { get { return _TDataSet.

private ArrayList trinputs.2.Windows.Collections.Collections. private ArrayList teoutputs. private string[] data.Generic.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.Forms. private ArrayList aldata. System. namespace _05025397.“DatasetParser. System. private string[] test.1. private ArrayList teinputs. System.Linq. private ArrayList troutputs.Controller { public class DatasetParser { #region data /********************\ |* DATA *| \********************/ private bool _Verified. System.2 .Text. Page 123 of 225 . 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 System. private ArrayList altest.

'\t'.Text. trinputs = new ArrayList(). aldata = new ArrayList().Trim().Replace("\r".Trim().Text = txtData. RichTextBox txtTest) { //Data init bool Verified = true. '. altest = new ArrayList(). txtData. '\r'.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 = txtTest. "").Replace("\r".Text. ' '.'. private double Niterations. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public DatasetParser(RichTextBox txtData.Text. txtTest. "").Text = txtTest.Text = txtData.' }. teinputs = new ArrayList(). private double Ninputs. txtTest. char[] delimiterChars = { '\n'. teoutputs = new ArrayList(). private double Noutputs. //Strip carriage returns and trim txtData. troutputs = new ArrayList().Text. '. Page 124 of 225 .

Text.Length. test = txtTest.Convert.Split(delimiterChars). //Get the number of iterations.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.Convert.ToDouble(data[0]).ToDouble(data[1]). //parse the data into appropriately //structured arrays Verified = parseTrainingData(). i++) Page 125 of 225 . number of inputs //and number of outputs Niterations = System.Text. try { for (int i = 3.ToDouble(data[2]). i < data. Ninputs = System.Split(delimiterChars). Noutputs = System. Verified = parseTestingData(). _Verified = Verified.Convert. } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private bool parseTrainingData() { bool success = true.

aldata.Add(aldata[0]). j++) { troutputs. i++) { trinputs. i < Ninputs.Error).OK. "Error".ToDouble(data[i])).RemoveAt(0). } for (int j = 0. MessageBoxIcon. } private bool parseTestingData() { bool success = true.Convert.Add(System. aldata.Count > 0) { for (int i = 0.RemoveAt(0).Add(aldata[0]). } return success. j < Noutputs. Page 126 of 225 .Show("Error parsing training data". MessageBoxButtons.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. success = false. } } } catch { MessageBox. } while (aldata.

"Error". } } } catch { MessageBox. MessageBoxButtons.OK. altest.Count > 0) { for (int i = 0.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. } while (altest.RemoveAt(0). j++) { teoutputs. j < Noutputs.Add(altest[0]). i++) { teinputs. MessageBoxIcon.Convert. i < data. } for (int j = 0.Error).Length. i < Ninputs.Add(altest[0]).RemoveAt(0).Add(System. } Page 127 of 225 .Show("Error parsing testing data". i++) { altest.ToDouble(data[i])). } return success. success = false. altest.

Count / (int) Noutputs][].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. i < (trinputs. out double Nout) { Ninp = Ninputs. k++) { Page 128 of 225 . } public void GetTrainingData (out double[][] trinputdata. out double[][] troutputdata) { trinputdata = new double[trinputs. i++) troutputdata[i] = new double[(int) Noutputs]. k < Ninputs. for (int j = 0. iter = Niterations.Count / (int)Noutputs). l = 0.Count. out double Ninp. j+=(int) Ninputs.Count / (int)Ninputs). i++) trinputdata[i] = new double[(int) Ninputs]. for (int i = 0. troutputdata = new double[troutputs. j < trinputs.Count / (int) Ninputs][]. for (int i = 0. i < (troutputs. Nout = Noutputs. l++) { for (int k = 0.

i++) teoutputdata[i] = new double[(int)Noutputs]. j < teinputs.Count / (int)Ninputs). for (int i = 0. k++) { troutputdata[l][k] = (double)troutputs[j + k].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. j += (int)Ninputs. i < (teinputs.Count. j += (int)Noutputs. l = 0. k < Ninputs. Page 129 of 225 . teoutputdata = new double[teoutputs. } } } public void GetTestingData (out double[][] teinputdata. k++) { teinputdata[l][k] = (double)teinputs[j + k]. l++) { for (int k = 0.Count. j < troutputs. i < (teoutputs. l++) { for (int k = 0. i++) teinputdata[i] = new double[(int)Ninputs]. for (int i = 0.Count / (int)Noutputs). for (int j = 0.Count / (int)Noutputs][]. out double[][] teoutputdata) { teinputdata = new double[teinputs. k < Noutputs.Count / (int)Ninputs][]. l = 0. k].

j < teoutputs. Page 130 of 225 . out ArrayList teoutputdata) { teinputdata = teinputs. k < Noutputs.cs” 00000001 using System. teoutputdata = teoutputs.“Algorithms\TrainingAlgorithm. j += (int)Noutputs.2. } public bool Verified { get { return _Verified. } } #endregion } 9.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.1. l = 0. l++) { for (int k = 0.3 .Count. } } } public void GetTestingDataAL (out ArrayList teinputdata. k++) { teoutputdata[l][k] = (double)teoutputs[j + k].

private int _ReportInterval.Collections. private string _ReportData. Page 131 of 225 . protected PointPairList BestErrList. System. private int _IterMax. protected bool _ITerr.Controller.Algorithms { public abstract class TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _TargetError. protected int CurIt. ZedGraph. protected bool _Tested. 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.Windows.Text.Forms. System. protected bool _Trained. _05025397.Generic.Linq. namespace _05025397.

protected DatasetParser getdata. getdata = new DatasetParser(txtData.00. BestErrList = new PointPairList(). if (_ITerr) _TargetError = -1. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public TrainingAlgorithm(double Target_Error. int Iter_Max. _ITerr = ITerr.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(). } #endregion Page 132 of 225 . _IterMax = Iter_Max. RichTextBox txtTest. txtTest). int Reports. so the //network will loop to completion. bool ITerr) { _TargetError = Target_Error. RichTextBox txtData. //If the ignore target error is true //then set the target error to -1.0 //this is impossible to reach. _ReportInterval = Reports.

} #endregion #region external /********************\ |* EXTERNAL *| \********************/ public abstract bool Train(). public abstract bool Test(). } get { return _TargetError.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. } } Page 133 of 225 . public PointPairList getGraphData() { return BestErrList. public abstract string network_details(). } public double TargetError { set { _TargetError = value.

} } public bool Tested { get { return _Tested.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. } } public bool Trained { get { return _Trained. } } public string ReportData { get { return _ReportData. } } public int IterMax { get { return _IterMax. } set { _Trained = value. } } #endregion } Page 134 of 225 .

William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.1. System. namespace _05025397. #endregion #region constructor Page 135 of 225 .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.2. FANN_Cascade CCNet.Windows.Collections. FANN_Wrapper. System.Algorithms { public class CascadeCorrelation : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _InputL.Linq. System. System.Controller. System.Collections. System. private double _LearnRate.Generic.4 . _05025397. ZedGraph.Forms.IO. private double _OutputL.“Algorithms\CascadeCorrelation.Text.

double iterations.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. int Iter_Max. RichTextBox txtData. _LearnRate. Reports.GetStructure(out iterations. out _InputL.TargetError. out _OutputL).ReportInterval. getdata. int Reports. base. base. txtTest.IterMax). (int)_OutputL. bool ITerr. RichTextBox te) { Page 136 of 225 . base. ITerr) { _LearnRate = LearnRate. } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private void saveDllData(RichTextBox tr. Iter_Max. double TargetError. RichTextBox txtTest) : base(TargetError. saveDllData(txtData. CCNet = new FANN_Cascade((int)_InputL. txtData. txtTest).

output. (int)_InputL)). output.Text).Format ("\nLearning Rate: {0:0.Append( Page 137 of 225 . (int)_OutputL)).Format ("\t\t\t\t\tOutput Layer: {0:d}". _LearnRate)). tw.dat"). } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { StringBuilder output = new StringBuilder().Close(). output.Close(). tw.Text).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. tw. tw = new StreamWriter("dlltedata.Write(te. tw.Append( string.Format ("Network Details: \n Input Layer: {0:d}".Append( string.Write(tr.dat"). output.00000}".Append( string.

base. output.ReportInterval)).TargetError)).Append( string. _ITerr)). try { success = CCNet. base.Format ("\t\tMaximum Nodes: {0:d}".IterMax)). return output.Format ("\t\t\tTarget Error: {0:0. } public override bool Train() { bool success = true.Append( string.ToString(). output.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. Report(CCNet.".Format ("\nReport every {0:d} generations.Append( string.Format ("\t\t\t\t\t\tIgnore target error " + "(process all iterations): {0}"._tr_output).Train(). output. } catch { Page 138 of 225 .00000}".

Error).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. base._Trained = success. MessageBoxButtons. } public bool getGraphDataFromDll() { bool success = true.OK. "Error".besterrorlist.Error). MessageBoxIcon.Show("Error running Cascade training!". MessageBoxIcon. success = false. success = false.Add((double)i. for (int i = 0. } } catch { MessageBox. } Page 139 of 225 . i < besterrlist. "Error". } success = getGraphDataFromDll().OK.Show("Error getting graph data from DLL". i++) { base. try { double[] besterrlist = CCNet.Length. besterrlist[i]). return success.BestErrList. MessageBoxButtons.

OK.Show("Error running Cascade testing!". try { success = CCNet. } catch { MessageBox.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.Error)._te_output). } #endregion } Page 140 of 225 ._Tested = success. Report(CCNet. success = false. "Error". } base. } public override bool Test() { bool success = true.Test(). MessageBoxIcon. return success. MessageBoxButtons.

//Network Data private double _InputL.Controller.1. System. System.Collections. _05025397. System.5 . ZedGraph.Forms.Collections. private int _Mutation. System. //Population Page 141 of 225 .Linq. private int _HiddenL.Algorithms { public class GeneticAlgorithm : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ //GA Data private int _PopSize. namespace _05025397.Windows. System.“Algorithms\GeneticAlgorithm.Generic. private int _Crossover.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.2.Text.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. private double _OutputL.

ITerr) { _PopSize = PopSize. _HiddenL = HiddenL. int Reports. //Init Network params double iterations. int HiddenL. i++) { Population. for (int i = 0. int Iter_Max. out _OutputL). txtData. RichTextBox txtTest) : base(TargetError. int Crossover. getdata. double TargetError. i < _PopSize. #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public GeneticAlgorithm(int PopSize. _Mutation = Mutation. _Crossover = Crossover. out _InputL.Add Page 142 of 225 . Iter_Max. RichTextBox txtData.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. Reports.GetStructure(out iterations. txtTest. int Mutation. //Create new population references Population = new ArrayList(_PopSize).

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.IterMax. double[][] results. i++) { //Get the msqe for (int j = 0.getMsQE(inputs.GANetwork( (int)_InputL. j++) { ((Model. Rand. i < base.GetScaledTrainingData(out inputs. _HiddenL. success = sort_fitnesses(). Page 143 of 225 . getdata. j < _PopSize. for (int i = 0. out results). double[][] inputs. } //Sort the functions according to fitness now. (int)_OutputL.Next())).GANetwork) Population[j]). results). } } #endregion #region internal_functions /********************\ |* INTERNAL *| \********************/ private bool run() { bool success = true.

base.GANetwork)Population[0]).").BestErrList.CurIt = 0. break.MSqE + ".Report("Best error at iteration [ " + i + " ] was " + ((Model. //Check for having reached the target if (((Model.GANetwork)Population[0]).TargetError) { Report("\nNetwork matching or improving upon target error"+ "found at iteration [ " + i + " ].CurIt == ReportInterval) { base.Add ((double)i.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.\n"). } success = build_generation(). } private bool sort_fitnesses() { Page 144 of 225 .GANetwork)Population[0]).MSqE).MSqE <= base. } base. } return success.CurIt++. ((Model. base.

for (int i = (_PopSize . "Error". int selection1. selection2. } catch { MessageBox. double[] weightarray2. double[] weightarray_f. MessageBoxIcon. MessageBoxButtons. success = false. } return success.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. double[] weightarray1. //It selects two points from the start of the population //Then applies crossover if necessary (based on probability) Page 145 of 225 .OK.Sort().Show("Error sorting population".1).Error). i--) { //This algorithm is altered from previous versions. try { Population. i > (int)(_PopSize / 2). } private bool build_generation() { bool success = true.

MessageBoxIcon.". MessageBoxButtons.Next((int)_PopSize / 2). selection1 = Rand. success = false.getWeights(). selection2 = Rand. weightarray1 = ((Model.Show("Error: 2D weight array length unequal.OK.Next((int)_PopSize / 2).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.GANetwork)Population[selection1]). //Next iteration it replaces the next to last.Error).Length]. the least fit) with this new string. if (Rand. weightarray2 = ((Model. //Just a quick error check if (weightarray1. "Error". all the way to the middle of the population.GANetwork)Population[selection1]). and //so on.Next(100) < _Crossover) { //Choose a random position in the weight array //to be our crossover point Page 146 of 225 .getWeights(). } try { weightarray_f = new double[weightarray1.Length) { MessageBox.Length != weightarray2. //Then it replaces the last position in the population //(ie.

Length .1)).setWeights(weightarray_f). } Page 147 of 225 .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.5. //Simple single point crossover of the weights for (int j = 0. (weightarray1. } } } if (Rand. } } //Set the weights of the current member of the //population we're on ((Model.GANetwork)Population[i]).Next(1. } else { weightarray_f[j] = weightarray2[j].Length.0. j++) { weightarray_f[j] += Rand.5 for (int j = 0.Next(100) < _Mutation) { //Jiggle the weights in the array //up or down by 0.NextDouble() . j++) { if (j < XPos) { weightarray_f[j] = weightarray1[j].Length. j < weightarray_f. j < weightarray1.

_PopSize)). _Mutation)). _Crossover)).Error).Format("\t\t\tCrossover Probability: {0:d}". output.Append( string.Append( Page 148 of 225 . success = false.Append( string. output. } } return success.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.Format("\t\t\tMutation Probability: {0:d}". output. "Error".Show("Error building new generation".Format("Network Details: \n Population: {0:d}". MessageBoxIcon. MessageBoxButtons. } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { //Add stringbuilder for efficiency here StringBuilder output = new StringBuilder().OK. output.Append( string.

". base. success = run(). _ITerr)).Format("\t\t\tTarget Error: {0:0.Format("\nHidden layer: {0:d}".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.00000}". _HiddenL)).ToString(). output. } public override bool Train() { bool success = true. base._Trained = success.Format("\t\t\t\t\t\t\tIgnore target error (process all iterations): {0}". output. base.Append( string.TargetError)). output.ReportInterval)).Append( string.Format("\t\t\tMaximum Generation: {0:d}". } public override bool Test() Page 149 of 225 . output.Format("\nReport every {0:d} generations.Append( string. return output. base. return success.IterMax)).Append( string.

j++) { output. out tstoutput).GetOutput().Format(" [{0:g}] ".Format(" [{0:g}] ".Append("\n\n\t\t\t~~Network Testing~~"). double[] 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. for (int j = 0. j < tstinput[i]. netoutput[j])). for (int j = 0. output. netoutput = ((Model. Page 150 of 225 . double[][] tstoutput. tstinput[i][j])). getdata. i < tstinput.Append("\nWith inputs:"). try { for (int i = 0.Length.Append(string. i++) { ((Model.GANetwork)Population[0]). double[][] tstinput. j++) { output.GANetwork)Population[0]).Append(string. j < netoutput. StringBuilder output = new StringBuilder().Append("\nOutput achieved was:"). output. } output.Length.Run(tstinput[i]).GetScaledTestingData(out tstinput.Length.

output.GANetwork)Population[0]). tstoutput[i][j])).Format(" [{0:g}] ".MSqE)).Append("\nOutput Desired was:"). j++) { output.tstoutput).Error).Length.Append(string.Report(output.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.Format ("{0:g}"._Tested = success. } base. (double) ((Model. return success.OK.". j < tstoutput[i]. } } output.Append("\nMean Squared Error with these inputs and outputs is:"). success = false.getMsQE(tstinput.GANetwork)Population[0]). for (int j = 0. MessageBoxButtons.Show("Error Running Test.ToString()). } #endregion } Page 151 of 225 . "Error". ((Model.Append( string. } catch { MessageBox. base. MessageBoxIcon.

Forms. FANN_Wrapper. private int _HiddenL. System. System. private double _LearnRate. System.Collections. System.IO.Controller. #endregion #region constructor Page 152 of 225 .6 . private double _OutputL. ZedGraph. FANN_BackProp BPnet.1.Linq.Generic.2. namespace _05025397. _05025397. System.Text.Windows.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.“Algorithms\BackProp.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.Collections.Algorithms { public class BackProp : TrainingAlgorithm { #region data /********************\ |* DATA *| \********************/ private double _InputL. System.

txtTest). ITerr) { _HiddenL = HiddenL. txtTest. base. txtData. double TargetError. out _InputL. 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. Reports. out _OutputL). _LearnRate = LearnRate. } #endregion #region internal_functions /********************\ Page 153 of 225 . double LearnRate. saveDllData(txtData.IterMax). //Copy the training and testing data to the dat files //ready for the DLL's to access. _LearnRate. double iterations. int Reports.ReportInterval. base.TargetError.GetStructure(out iterations. RichTextBox txtTest) : base(TargetError. BPnet = new FANN_BackProp((int)_InputL. int Iter_Max. bool ITerr. Iter_Max. base. RichTextBox txtData. getdata. (int)_OutputL.

RichTextBox te) { TextWriter tw = new StreamWriter("dlltrdata. tw.Write(tr.Text). } #endregion #region external_functions /********************\ |* EXTERNAL *| \********************/ public override string network_details() { StringBuilder output = new StringBuilder(). tw = new StreamWriter("dlltedata.Format ("\t\t\tOutput Layer: {0:d}". output. tw.Close(). tw.dat").Close().Text).Write(te.dat").Append( Page 154 of 225 . output. tw.Format ("Network Details: \n Input Layer: {0:d}". (int) _InputL)). output.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.Append( string. (int) _OutputL)).Append( string.

Format ("\t\tMaximum Epoch: {0:d}".TargetError)).00000}". output. base.Format ("\t\t\t\t\t\tIgnore target error " + "(process all iterations): {0}".Format ("\nReport every {0:d} generations. output. output.Append( string.Format ("\t\t\tTarget Error: {0: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.Format ("\nLearning Rate: {0:0.". output.IterMax)). output. _LearnRate)). base. (int) _HiddenL)). } public override bool Train() { Page 155 of 225 . _ITerr)).ToString(). base.Append( string. return output.Format ("\t\t\tHidden Layer: {0:d}".00000}".Append( string.Append( string.ReportInterval)).Append( string.

_tr_output).Length. base._Trained = success. Report(BPnet.Train().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 bool success = true. MessageBoxButtons.Error). besterrlist[i]).besterrorlist.Add((double)i. "Error". for (int i = 0. } catch { MessageBox. return success.Show("Error running Backprop training!". } success = getGraphDataFromDll(). try { success = BPnet. try { double[] besterrlist = BPnet.BestErrList. success = false. i < besterrlist. MessageBoxIcon. i++) { base. } public bool getGraphDataFromDll() { bool success = true. Page 156 of 225 .

try { success = BPnet. Report(BPnet. } base.Error). MessageBoxIcon._te_output). success = false. "Error".Test(). MessageBoxButtons. } public override bool Test() { bool success = true. } catch { MessageBox.Show("Error getting graph data from DLL". MessageBoxButtons. } #endregion Page 157 of 225 .OK. } return success.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. MessageBoxIcon. "Error".Show("Error running Backprop testing!".OK._Tested = success.

Collections.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.Linq.“Network.Windows.Model 9. _05025397.3.Model { public class Network { #region data /********************\ |* DATA *| \********************/ //[layer][node][input] protected double[][][] _inputs.1 . protected int _hiddennodes. System. protected double[][] _outputs. System.3 . protected double[][][] _weights. System. namespace _05025397. System.1. protected int _numoutputs.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 00000180 00000181 } } 9.1. protected int _numinputs. Page 158 of 225 .Forms.Text.Generic.

i++) { _inputs[0][i] = new double[2]. for (int i = 0. _weights[0] = new double[_numinputs][]. _weights[0][i] = new double[2]. _outputs[0] = new double[_numinputs]. _outputs[1] = new double[_hiddennodes]. i < _hiddennodes. _weights[1] = new double[_hiddennodes][]. _weights[1][i] = new double[_numinputs + 1]. for (int i = 0. } } private void init_hiddenlayer() { _inputs[1] = new double[_hiddennodes][]. i < _numinputs. #endregion #region init /********************\ |* INIT *| \********************/ private void init_inputlayer() { _inputs[0] = new double[_numinputs][]. Page 159 of 225 .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. i++) { _inputs[1][i] = new double[_numinputs + 1].

i < _hiddennodes.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][]. i < _numinputs. _weights[0][i][1] = 1. } } private void init_bias() { //Input bias for (int i = 0. i++) { _inputs[0][i][1] = 1. for (int i = 0. _weights[2][i] = new double[_hiddennodes + 1]. Page 160 of 225 . i++) { _inputs[1][i][_numinputs] = 1. i < _numoutputs. _outputs[2] = new double[_numoutputs].0. } //Hidden layer bias for (int i = 0. _weights[2] = new double[_numoutputs][].0.0. i++) { _inputs[2][i] = new double[_hiddennodes + 1].0. _weights[1][i][_numinputs] = 1.

j++) { _weights[1][i][j] = Rand.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. i++) { _inputs[2][i][_hiddennodes] = 1. i < _hiddennodes. j < _numinputs. i < _numoutputs. i++) { _weights[0][i][0] = Rand. i < _numinputs. _weights[2][i][_hiddennodes] = 1. } } } private void init_outputweights() Page 161 of 225 .NextDouble().0.0. } } private void init_inputweights() { for (int i = 0.NextDouble(). } } private void init_hiddenweights() { for (int i = 0. i++) { for (int j = 0.

i++) { Page 162 of 225 . j < _hiddennodes. i < _numinputs.NextDouble(). } } } #endregion #region internal /********************\ |* INTERNAL *| \********************/ private double sigmoid(double input) { return (1 / (1 + Math. double sum = 0.E. } private bool Runinput() { bool success = true. i < _numoutputs. j++) { _weights[2][i][j] = Rand.Pow(Math.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.0. try { //Calculate results for this layer for (int i = 0. -input))). i++) { for (int j = 0.

} _outputs[0][i] = sigmoid(sum). sum = 0. j < _numinputs.Error). Page 163 of 225 .OK. success = false. MessageBoxButtons. MessageBoxIcon. i++) { for (int j = 0. } return success. i < _hiddennodes.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. j < 2.Show("Error processing input layer". j++) { _inputs[1][i][j] = _outputs[0][j]. double sum = 0. } private bool Runhidden() { bool success = true. } } catch { MessageBox. "Error". j++) { sum += (_inputs[0][i][j] * _weights[0][i][j]).0.0. try { //Feed forward the results from input layer for (int i = 0.

j++) { sum += (_inputs[1][i][j] * _weights[1][i][j]). try Page 164 of 225 . "Error". } return success. sum = 0.0.0. success = false. } private bool Runoutput() { bool success = true. 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). j < _numinputs + 1. MessageBoxIcon.Show("Error processing hidden layer". } _outputs[1][i] = sigmoid(sum). } } catch { MessageBox. i < _hiddennodes. MessageBoxButtons.OK. double sum = 0.

"Error". MessageBoxIcon. i++) { for (int j = 0. i < _numoutputs. success = false.0. } _outputs[2][i] = sigmoid(sum). j++) { _inputs[2][i][j] = _outputs[1][j]. MessageBoxButtons. } return success.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.Error). sum = 0. i++) { for (int j = 0. j++) { sum += (_inputs[2][i][j] * _weights[2][i][j]).Show("Error processing output layer". } Page 165 of 225 . i < _numoutputs. j < _hiddennodes + 1. } } catch { MessageBox. j < _hiddennodes.OK. } } //Calculate results for this layer for (int i = 0.

} if (success) for (int i = 0. Page 166 of 225 .Show("Error: Incorrect number of inputs supplied to NN". //The numbers of inputs must match up if (inputs.Error). if(success) success = Runinput(). if(success) success = Runhidden().Length != _numinputs) { MessageBox. MessageBoxButtons. _inputs[0][i][0] = inputs[i]. i++) //Each input node has only one real input //and a bias . "Incorrect number of inputs". i < _numinputs.OK. MessageBoxIcon. success = false.the number of nodes corresponds //to the number of inputs accepted.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.

//Set network structure descriptors _numinputs = numinputs. } #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public Network(int numinputs. int numoutputs. _hiddennodes = hiddennodes. } public double[] GetOutput() { //Return the outputs from the //output layer return _outputs[2]. return success. int SetRandom) { //Set random number generator Rand = new Random(SetRandom). _numoutputs = numoutputs. //We'll always have 3 layers Page 167 of 225 .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 hiddennodes.

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

Show("Fatal Error adding 1D weight array to 3D weight array".OK.Error).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. "Error". } } } } catch { MessageBox. j++) { for (int k = 0. MessageBoxButtons. } } /********************\ |* GETTERS/SETTERS *| \********************/ public double MSqE { Page 172 of 225 . k++. k < _weights[i][j]. j < _weights[i]. Application. i < _weights.Length.Length. MessageBoxIcon. i++) { for (int j = 0.Exit(). wc++) { _weights[i][j][k] = weights[wc].Length. wc = 0.

Nhnodes. int Noutputs. int Nhnodes. Noutputs. int SetRandom) : base(Ninputs.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. } set { _MSqE = value.0. SetRandom) { MSqE = 0. } #endregion } Page 173 of 225 . } } #endregion #region constructor /********************\ |* CONSTRUCTOR *| \********************/ public GANetwork(int Ninputs.

1 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. Page 174 of 225 .1. double TargetErr.h" #include "UnManaged_FANN_CasC. public: /********************\ |* EXTERNAL *| \********************/ FANN_Cascade(int InputL. using namespace System::Runtime::InteropServices.4 . 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. { public: /********************\ |* DATA *| \********************/ UnManaged_FANN_CasC* UMWrapper.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.DLL Wrapper for FANN functions 9. int OutputL.“FANN_Wrapper.1. double LearnRate.4.h" using namespace System.

bool Test(void). } } property array<double>^ besterrorlist { array<double>^ get() MaxNeurons). bool Train(void).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. public: /********************\ |* GETTERS/SETTERS *| \********************/ property String^ _te_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_test()). !FANN_Cascade(void). } } property String^ _tr_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_train()). int ~FANN_Cascade(void). Page 175 of 225 .

} return arout. { public: /********************\ |* DATA *| \********************/ UnManaged_FANN_Backprop* UMWrapper. array<double>^ arout = gcnew array<double>(size). i++) { arout[i] = ar[i]. const double *ar = UMWrapper->get_besterrs(size). for(int i = 0. } } }. public: Page 176 of 225 . public ref class FANN_BackProp //Managed wrapper for UnManaged_FANN_Backprop //which is a C++ interface to the C programmed //FANN dll.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. i < size.

!FANN_BackProp(void). public: /********************\ |* GETTERS/SETTERS *| \********************/ property String^ _te_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_test()). } } property String^ _tr_output { String^ get() { return gcnew String((wchar_t *) UMWrapper->get_report_train()). ~FANN_BackProp(void). int MaximumIteration).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 OutputL. } Page 177 of 225 . int HiddenL. bool Train(void). double TargetErr. int ReportInterval. double LearnRate. bool Test(void).

array<double>^ arout = gcnew array<double>(size). i < size. for(int i = 0. const double *ar = UMWrapper->get_besterrs(size). i++) { arout[i] = ar[i]. Page 178 of 225 . } return arout. } } }.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.

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

3 . //Training Parameters double _LearnRate.h> #include <fann_cpp.h> class UnManaged_FANN_Backprop //An interface to the C programmed //FANN dll. //Output for C# Page 183 of 225 . int _ReportInterval.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. double _TargetErr.“UnManaged_FANN_BackProp. int _OutputL.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 00000027 00000028 00000029 #pragma once #include <iostream> #include <string> #include <sstream> #include <vector> #include <doublefann. { private: /********************\ |* DATA *| \********************/ //Network Structure int _InputL. int _HiddenL.4. double _Momentum. int _MaximumIteration.

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

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

int MaximumIteration) { _InputL = InputL.“UnManaged_FANN_BackProp.4 . _MaximumIteration = MaximumIteration.h" //Constructor UnManaged_FANN_Backprop::UnManaged_FANN_Backprop (int InputL. int OutputL. int HiddenL. data = NULL. ann = NULL. bsterr = NULL. } //Destructor UnManaged_FANN_Backprop::~UnManaged_FANN_Backprop() { fann_destroy_train(data). _TargetErr = TargetErr.1. int ReportInterval. double TargetErr.h" #include "UnManaged_FANN_BackProp. _OutputL = OutputL. _LearnRate = LearnRate.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. double LearnRate.4. Page 186 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. _ReportInterval = ReportInterval. _HiddenL = HiddenL. wchoutput = NULL.

int reportcounter = 0.. ann = fann_create_standard(3.) { throw(""). } catch(.dat"). delete wchoutput. } //Train bool UnManaged_FANN_Backprop::Train(void) { static int firstrun = false. _InputL. bsterr = NULL. success = false. wchoutput = NULL. try { data = fann_read_train_from_file("dlltrdata.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. bool success = true. } if (fann_num_input_train_data(data) != _InputL) Page 187 of 225 . double error. _OutputL).. _HiddenL.

} fann_set_activation_steepness_hidden(ann. //Set the learning rate fann_set_learning_rate(ann. //Standard backprop fann_set_training_algorithm(ann. FANN_SIGMOID). FANN_SIGMOID). //Same range the GA's weights are //initialised too Page 188 of 225 . fann_set_activation_function_hidden (ann. success = false. (float) _LearnRate).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(""). FANN_TRAIN_BATCH). fann_set_activation_function_output (ann. 1). success = false. 1). fann_set_activation_steepness_output(ann. //Sigmoid Activation Functions (the same one //the GA uses). } if (fann_num_output_train_data(data) != _OutputL) { throw("").

i = _MaximumIteration + 1. reportcounter = 0. } reportcounter++. i < _MaximumIteration.0). 1.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.\n". if (error < _TargetErr) { tr_output << "\nNetwork matching or improving upon target error" << "found at iteration [ " << i << " ]. i++) { //Train one epoch then check the //mean squared error error = (double) fann_train_epoch(ann. Page 189 of 225 . } } fann_destroy_train(data). if (reportcounter == _ReportInterval) { tr_output << "Best error at iteration [ " << i << " ] was " << error << ".".0. vecbsterr. data). //Training Loop for (int i = 0. 0.push_back(error).

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. return true. for (int j = 0. } //Test bool UnManaged_FANN_Backprop::Test(void) { fann_type *calc_out = NULL. error = new double[fann_length_train_data(data)]. 0. j < _OutputL. } te_output << "\nOutput achieved was". j++) { Page 190 of 225 . 1). i < fann_length_train_data(data). for (unsigned int i = 0. double *error = NULL.dat"). te_output << "\nWith inputs". te_output << "\n\n\t\t\t~~Network Testing~~". data->input[i]). data = fann_read_train_from_file("dlltedata. for (int j = 0. j++) { te_output << " [" << data->input[i][j] << "] ". j < _InputL. i++) { calc_out = fann_run(ann. fann_scale_train_data(data.

Page 191 of 225 . delete error. return true. tr_output. te_output << fann_test_data(ann. delete calc_out. wcscpy(wchoutput. } wchoutput = new wchar_t[tr_output.str(). j++) { te_output << " [" << data->output[i][j] << "] ".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] << "] ". } te_output << "\nOutput Desired was".c_str()).str(). j < _OutputL. data). } //get_report const wchar_t* UnManaged_FANN_Backprop::get_report_train(void) { if (wchoutput != NULL) { delete wchoutput. } } te_output << "\nMean Squared Error with these inputs and outputs is:". for (int j = 0. wchoutput = NULL.length() + 1].

return wchoutput.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 = new double[vecbsterr. Page 192 of 225 .size()]. bsterr = NULL.size(). wchoutput = NULL. te_output.length() + 1]. wcscpy(wchoutput.str().str(). } const wchar_t* UnManaged_FANN_Backprop::get_report_test(void) { if (wchoutput != NULL) { delete wchoutput.c_str()). } wchoutput = new wchar_t[te_output. } //get_besterrs const double* UnManaged_FANN_Backprop::get_besterrs(int &size) { if (bsterr == NULL) { delete bsterr. size = vecbsterr.

} vecbsterr.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.5 . 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. int _OutputL.size().1.“UnManaged_FANN_CasC.h> class UnManaged_FANN_CasC { public: /********************\ |* DATA *| \********************/ //Network Structure int _InputL. 9. //Training Parameters Page 193 of 225 .clear(). i < vecbsterr. i++) { bsterr[i] = vecbsterr[i].4.

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

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

_ReportInterval = ReportInterval. data = NULL.“UnManaged_FANN_CasC.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9. double TargetErr. double LearnRate. //Constructor UnManaged_FANN_CasC::UnManaged_FANN_CasC (int InputL.6 . ann = NULL. bsterr = NULL. int OutputL.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. _MaxNeurons = MaxNeurons. } //Destructor UnManaged_FANN_CasC::~UnManaged_FANN_CasC(void) Page 196 of 225 . wchoutput = NULL. std::vector<double> UnManaged_FANN_CasC::vecbsterr. _TargetErr = TargetErr.h" #include "UnManaged_FANN_CasC. int ReportInterval.h" //Static variable declarations std::wstringstream UnManaged_FANN_CasC::tr_output. int MaxNeurons) { _InputL = InputL.1. _OutputL = OutputL. _LearnRate = LearnRate.4.

tr_output.c_str()).str(). delete wchoutput. wcscpy(wchoutput. return wchoutput. fann_destroy_train(data). Page 197 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 { delete bsterr. } wchoutput = new wchar_t[tr_output. } const wchar_t* UnManaged_FANN_CasC::get_report_test(void) { if (wchoutput != NULL) { delete wchoutput.length() + 1]. wchoutput = NULL. } //get_report const wchar_t* UnManaged_FANN_CasC::get_report_train(void) { if (wchoutput != NULL) { delete wchoutput. bsterr = NULL. wchoutput = NULL.str().

length() + 1]. i < vecbsterr. } vecbsterr. return bsterr. bsterr = NULL. i++) { bsterr[i] = vecbsterr[i].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. } bsterr = new double[vecbsterr.str(). wcscpy(wchoutput.clear(). } wchoutput = new wchar_t[te_output. te_output. size = vecbsterr. } //get_besterrs const double* UnManaged_FANN_CasC::get_besterrs(int &size) { if (bsterr == NULL) { delete bsterr.size()]. return wchoutput.size(). for (unsigned int i = 0. } Page 198 of 225 .size().str().c_str()).

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

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

double *error = NULL. } node++. struct fann_train_data *train.". static int node = 1. 1). float desired_error.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.dat").\n". if (MSqE <= desired_error) { tr_output << "\nNetwork matching or improving upon target error" << "found at node [ " << node << " ]. 0. data = fann_read_train_from_file("dlltedata. unsigned int epochs_between_reports. tr_output << "Best error at node [ " << node << " ] was " << MSqE << ".push_back(MSqE). unsigned int epochs) { double MSqE = fann_get_MSE(ann). vecbsterr. unsigned int max_epochs. return 0. Page 201 of 225 . } //Test bool UnManaged_FANN_CasC::Test() { fann_type *calc_out = NULL. fann_scale_train_data(data.

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. for (int j = 0. data). j < _InputL. NULL). for (int j = 0. j++) { te_output << " [" << calc_out[j] << "] ". j < _OutputL. i < fann_length_train_data(data). j++) { te_output << " [" << data->input[i][j] << "] ". data->input[i]). error = new double[fann_length_train_data(data)]. j < _OutputL. } te_output << "\nOutput Desired was:". te_output << "\n\n\t\t\t~~Network Testing~~". te_output << "\nWith inputs:". for (unsigned int i = 0. } } te_output << "\nMean Squared Error with these inputs and outputs is:". j++) { te_output << " [" << data->output[i][j] << "] ". Page 202 of 225 . for (int j = 0. } te_output << "\nOutput achieved was:". i++) { calc_out = fann_run(ann. te_output << fann_test_data(ann.

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.

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.2 . Page 204 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 9.

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 .

mit.Libraries Researched • GALib o Matthew Wall o http://lancet.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.3 .edu/ga/ o Accessed: 31/10/2008 Page 207 of 225 .

org/libann/index.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 • Libneural o D.sourceforge.com/flood/ o Accessed: 31/10/2008 • Annie o Asim Shankar o http://annie. Franklin o http://ieee.uow.edu/au/~daniel/software/libneural/ o Accessed: 31/10/2008 • Libann o Anonymous o http://www.cimne.nongnu.html o Accessed: 31/10/2008 • Flood o Roberto Lopez o http://www.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.William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 o http://leenissen.stats.4 .uah.ac.math.xhtml Virus Classification o http://www.php o Accessed: 31/10/2008 9.uk/pub/PRNN/README.html Page 209 of 225 .ox.dk/fann/index.edu/stat/data/Fisher.

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

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

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

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

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

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

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

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

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

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

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

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

• 24/10/2008 o University computers were down so code demo was impossible. o Agreed on further investigation into GALib and genetic algorithms. • 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 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 13 . o Decision made to adjust the weights of the neural network during training as opposed to using a genetic algorithm to establish network structure. o Decided on XOR as a starting point. • 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. back-propagation network was running and learning XOR. 17/10/2008 o Investigating more deeply into journals based on research already conducted. o Submitted objectives. however.Diary • 01/10/2008 – Research progress satisfactory.

• • 06/02/2009 o Still exploring extra areas for project to cover. • • • • • • • • • 19/11/2008 o Milestone one submitted successfully. o Progress satisfactory • 20/02/2009 o Progress satisfactory Page 223 of 225 .William Sayers 05025397 Genetic Algorithms and Neural Networks Milestone 3 o Progress satisfactory. 13/02/2009 o Have decided to extend the project by including cascade correlation learning algorithm and comparison to genetic algorithms. 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.

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. o Have started report • 02/04/2009 o Main problems experienced with genetic algorithm seem related to memory leaks. is much faster (fixed several bugs along the way) and retains none of the problematic memory leaks.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. Page 224 of 225 . 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. catch-up time for previous two weeks will be used over Easter break.

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