GAF Lab

From gaframework.org
Jump to: navigation, search

Please note that GAF Lab is a collection of ideas rather than a fully developed product. I would welcome comments and requirements from GAF users. - John

Gaflab large.png

Introduction

Gaf Lab is an application that is designed to host a genetic algorithm created using the Genetic Algorithm Framework for .Net. Existing GAF based GAs can, with some minor modifications, be used with GAF Lab.

The application allows GA parameters to be adjusted whilst the GA is running. In addition, statistics are reported to aid in the parameter selection process and to evaluate the GAs performance.

The product is a cross platform .Net/Mono application written using the Gtk toolkit and runs on Linux, OSX and Windows. See GAF Lab Installation for details.

Features

  • Reatime adjustment of GA and operator parameters.
  • Genetic Operators can be enabled and disabled whilst the GA is running.
  • The product is designed to work with any existing GAF based GA.
  • The application constantly reports on fitness, convergence and diversity.
  • Code generation provides the code required to implement the GA, once defined.
  • Cross platform, tested on Debian, Mint, Arch Linux, OSX 10.11.5 and Windows 10, see GAF Lab Installation.

Realtime Adjustment of Parameters

Most of the parameters of the GA and genetic operators can be adjusted whilst the GA is running. This can speed up the process of determining the optimum settings for a particular GA.

Adding and Removing Operators

Gaflab operators.png

Each operator can be added or removed as required by clicking the 'enabled' checkbox. This can be done even when the GA is running, making it a simple task to experiment with the available operators.

Monitoring the GA

The monitor pane shows the status of the GA during its run. For binary chromosome types the average hamming distance (the average distance between all pairs of binary strings in a population) is used to derive indicators of diversity and convergence. Fitness diversity is also shown and used to derive alternative indicators for diversity and fitness. More measures will be added as they are developed.

The Distribution of Fitness display gives a visual representation of where the concentration of fitness values lie, within the fitness value range (0.0 - 1.0).

Gaflab monitoring.png

The Code Generator

The Source Code pane shows the source code required to implement the currently configured GA. This means that once the GA has been optimised for use within a particular domain, the source can be copied directly into a Visual Studio or Xamarin Studio project.

Gaflab source.png

GAF Lab Installation

GAF Lab can be installed on Linux, OSX and Windows.

Linux and OSX

GAF Lab is a .Net/Mono application and requires Mono to be installed. The directions for this can be found on the Mono website by following the links below.

Once Mono has been installed, download GAF Lab from the link below into a suitable directory/folder.

Use the following command to run the application.

   mono GAFLab.exe

Windows

GAF Lab can be installed on Windows using the installer available for download at the link below.

The installer will install the GAF Lab files, in addition, the installer for Gtk Sharp will also be launched.

Use the following command to run the application.

   GAFLab.exe

Getting Started with GAF Lab

Gaflab large.png

The Basics

GAF Lab is a GUI application that uses the Genetic Algorithm Framework to run a genetic algorithm. It uses the normal GAF.dll assembly that is available via Nuget or Bitbucket to run the GA, however, it takes control of the running GA in order that it can be monitored and changed whilst it is running.

The aim is that GAF Lab can assist in the testing and evaluation of a proposed GA. GAF Lab can ease the process of determining suitable GA parameters, monitoring diversity and convergence and investigating the population throughout the GA run.

Consumer Functions

GAF Lab can be used to run any GA that can be run using the GAF, however, before using GAF Lab it is important to understand the concept of 'Consumer Functions'.

When working with the Genetic Algorithm Framework (GAF) without using GAF Lab, the consumer/developer provides the Fitness and Terminate functions and passes these as delegates to the GAF. In addition an initial population is defined during initialisation of the GA. In order to use GAF Lab it is neccessary to implement these delegates and methods in a separate assembly so that these can be loaded into GAF Lab using the file menu. This assembly is referred to as the 'Consumer Functions' assembly. Creating a Consumer Functions assembly is a reasonably straightforward process and is described here. However, for this 'Getting Started' guide, the two sample Consumer Functions assemblies supplied with GAF Lab, will be used.

Binary F6 and Travelling Salesman Examples

Fileopen.png

The product includes two Consumer Functions assemblies.

  • GAF.ConsumerFunctions.BinaryF6.dll
  • GAF.ConsumerFunctions.TravellingSalesman.dll

Creating a Consumer Functions assembly is described here.

These assemblies contain the Fitness function, Terminate function, initial population and a couple of methods that provide a custom display at each generation. This assembly is loaded into GAF Lab using the File Menu.

Operators.png

Genetic Operator Configuration

All of the standard genetic operators are shown in Tabs on the right hand side of the applications main window. Settings can be made to the operators at any point before or after the GA has been started.

The meanings and effects of these parameters is described in the Genetic Operators section of the documentation.

Population Configuration

Leftpane.png

The population parameters, Re-Evaluate All, Linear Normalisation, and Evaluate in Parallel can all be configured using the left hand pane of the main applications window. The meanings and effects of these parameters is described in the Population section of the documentation.

Start, Stop, Pause, Step and Resume

Once the Consumer Functions have been loaded, the GA can be started and stopped from the left hand pane. In addition, the GA can be Paused, allowing the population to be inspected (see 'The Genotypic View'). The Step button steps the GA one generation at a time and the Resume button causes the GA to continue running normally.

Monitoring the GA

The GA can be monitored whilst it is running. The Monitor pane shows some basic statistics and graphical indications of convergence and diversity.

Where the chromosome in use is a binary string, hamming distance is the primary measure determining diversity and convergence. However, diversity and convergence based on Fitness is also shown (DiversityF and ConvergenceF) which works for all chromosome types.

As the the population converges the number of duplicate chromosomes can increase significantly, the panel shows the percentage and count of of duplicate chromosomes (based on value) that exist in the population.

The FitnesC9 indicator returns a value relating to the number of consecutive '9's that exist in the max fitness. For example a fitness of 0.999987654 would return a C9 fitness of 4\. This has been incuded as this measure has been used in some journal articles and text books.

The Distribution of Fitness display gives a visual representation of where the concentration of fitness values lie, within the fitness value range (0.0 - 1.0).

Gaflab1 large.png

The Genotypic View

This pane shows the population and is enabled when the GA is paused. This view will work for all types of chromosome including object based chromosomes.

Gaflab2 large.png

The Code Pane

This pane shows the code that would be required to be used with the GAF (i.e. without GAF Lab) in order to implement the currently configured GA. This pane is dynamic and changes as the various GA parameters are adjusted. Simply copy and paste this code into a code editor/IDE such as Visual Studio or Xamrin Studio to recreate the GAF Lab configured GA in code.

Gaflab3 large.png

The Output Pane

The output pane shows exception messages and any logging messages that may be presented from the the GAF or GAF Lab itself. In addition, customised messages defined within the Consumer Functions assembly will be displayed here at each generation and at the end of the run. See Creating a Consumer Functions Assembly for details.

Output.png

Creating a Consumer Functions Assembly

GAF Lab can be used to run any GA that can be run using the GAF, however, before using GAF Lab it is important to understand the concept of 'Consumer Functions'.

When working with the Genetic Algorithm Framework (GAF) without using GAF Lab, the consumer/developer provides the Fitness and Terminate functions and passes these as delegates to the GAF. In addition an initial population is defined during initialisation of the GA. In order to use GAF Lab it is neccessary to implement these delegates and methods in a separate assembly so that these can be loaded into GAF Lab using the file menu. The simplest way to do this is to create a new Class Library project in Xamarin Studio or Visual Studio and include a class that implements the IGafLabConsumerFunctions interface.

A class implemeting the IGafLabConsumerFunctions interface must implement the following methods.

  • Fitness and Termination functions: These are standard GAF Fitness and Terminate functions but must be called EvaluateFitness anf TerminateAlgorithm respectively.
  • CreatePopulation: This method should return an appropriate initial population.
  • CreateGenerationCompleteMessage: This method can be used to return useful information about the generation that has just been created. Any values returned here will be desplayed within the GAF Lab application. If this functionalty is not required, simply return _null_ or _string.Empty_.
  • CreateRunCompleteMessage: This method is similar to the CreateGenerationCompleteMessage method however, the method is called at the end of the GA run. If this functionalty is not required, simply return _null_ or _string.Empty_.

An example of such a class, taken from the Solving the Binary F6 Function, is shown below.

   using System;
   using GAF.Api;
   
   namespace GAF.ConsumerFunctions.BinaryF6
   {
       public class BinaryF6 : IGafLabConsumerFunctions
       {
           public double EvaluateFitness (Chromosome chromosome)
           {
               double fitnessValue = -1;
               if (chromosome != null) {
                   //this is a range constant that is used to keep the x/y range between -100 and +100
                   var rangeConst = 200 / (System.Math.Pow (2, chromosome.Count / 2) - 1);
   
                   //get x and y from the solution
                   var x1 = Convert.ToInt32 (chromosome.ToBinaryString (0, chromosome.Count / 2), 2);
                   var y1 = Convert.ToInt32 (chromosome.ToBinaryString (chromosome.Count / 2, chromosome.Count / 2), 2);
   
                   //Adjust range to -100 to +100
                   var x = (x1 * rangeConst) - 100;
                   var y = (y1 * rangeConst) - 100;
   
                   //using binary F6 for fitness.
                   var temp1 = System.Math.Sin (System.Math.Sqrt (x * x + y * y));
                   var temp2 = 1 + 0.001 * (x * x + y * y);
                   var result = 0.5 + (temp1 * temp1 - 0.5) / (temp2 * temp2);
   
                   fitnessValue = 1 - result;
               } else {
                   //chromosome is null
                   throw new ArgumentNullException ("chromosome", "The specified Chromosome is null.");
               }
   
               return fitnessValue;
           }
   
           public bool TerminateAlgorithm (Population population, int currentGeneration, long currentEvaluation)
           {
               return currentGeneration >= 350;
           }
   
           public Population CreatePopulation ()
           {
               //standard random binary chromosome (44bits) with a population of 100
               var population = new Population (100, 44, false, false, ParentSelectionMethod.StochasticUniversalSampling, true);
               return population;
           }
   
           public string CreateGenerationCompleteMessage (Population currentPopulation, int currentGeneration, long evaluations)
           {
               var message = string.Format ("Generation: {0}, Evaluations: {1}", 
                                 currentGeneration,
                                 evaluations);
               return message;
           }
   
           public string CreateRunCompleteMessage (Population currentPopulation, int currentGeneration, long evaluations)
           {
               var message = string.Format ("Generation: {0}, Evaluations: {1}", 
                   currentGeneration,
                   evaluations);
               return message;
           }
       }
   }