/// <summary> /// Initialise a new instance of the GeneticEngine class with the supplied plug-ins and populate the initial generation. /// </summary> /// <param name="populator">The populator plug-in. Generates the initial population.</param> /// <param name="evaluator">The evaluator plug-in. Provides the fitness function.</param> /// <param name="geneticOperator">The genetic operator plug-in. Processes one generation to produce the individuals for the next.</param> /// <param name="terminator">The terminator plug-in. Provides the termination condition.</param> /// <param name="outputter">The outputter plug-in or null for no output. Outputs each generation.</param> /// <param name="generationFactory">The generation factory plug-in or null to use the default. Creates the generation container.</param> public GeneticEngine(IPopulator populator, IEvaluator evaluator, IGeneticOperator geneticOperator, ITerminator terminator, IOutputter outputter = null, IGenerationFactory generationFactory = null) { if (populator == null) { throw new GeneticEngineException("populator must not be null"); } if (evaluator == null) { throw new GeneticEngineException("pvaluator must not be null"); } if (geneticOperator == null) { throw new GeneticEngineException("geneticOperator must not be null"); } if (terminator == null) { throw new GeneticEngineException("terminator must not be null"); } this.populator = populator; this.evaluator = evaluator; this.geneticOperator = geneticOperator; this.terminator = terminator; this.outputter = outputter; this.generationFactory = generationFactory == null ? new AATreeGenerationFactory() : generationFactory; Setup(); }
/// <summary> ///Check if the plugins have been selected by the user, if not the revelant exceptions are thrown ///asking the user to make a choice. ///So, if the populator,evaluators, geneticOperator and terminator choices are not chosen, then ///error messages are displayed to alert the user, that the choices for these four plugins cannot be null. ///If the plugins are chosen, the engine object is created according to the user choices and it is initialised. ///Once the engine is initialised, the rest of the buttons on the interface are activated and the fitness values ///are displayed. /// </summary> private void initEngineButton_Click(object sender, EventArgs e) { if (!engineRunning) { SetEngineRunning(true); populator = null; evaluator = null; geneticOperator = null; terminator = null; outputter = null; generationFactory = null; string errorMsg = ""; if (cbPopulator.SelectedItem == null) errorMsg += "Populator must not be null\n"; else { string choice = getChoice(populators, cbPopulator); try { populator = (IPopulator)loader.GetInstance(choice, (object)tbMapFile.Text); } catch (GeneticEngineException exception) { MessageBox.Show(exception.Message); } } if (cbEvaluator.SelectedItem == null) errorMsg += "Evaluator must not be null\n"; else { string choice = getChoice(evaluators, cbEvaluator); try { evaluator = (IEvaluator)loader.GetInstance(choice, null); } catch (GeneticEngineException exception) { MessageBox.Show(exception.Message); } } if (cbGeneticOperator.SelectedItem == null) errorMsg += "Genetic Operator must not be null\n"; else { string choice = getChoice(geneticOperators, cbGeneticOperator); try { geneticOperator = (IGeneticOperator)loader.GetInstance(choice, null); } catch (GeneticEngineException exception) { MessageBox.Show(exception.Message); } } if (cbTerminator.SelectedItem == null) errorMsg += "Terminator must not be null\n"; else { string choice = getChoice(terminators, cbTerminator); if ((int)targetFitness.Value == 0) errorMsg += "Provide a target fitness value greater than 1 for the terminator plug-in\n"; else { try { terminator = (ITerminator)loader.GetInstance(choice, (object)(uint)targetFitness.Value); } catch (GeneticEngineException exception) { MessageBox.Show(exception.Message); } } } if (cbOutputter.SelectedItem != null) { string choice = getChoice(outputters, cbOutputter); if (choice != noOutputterString) { if (tbOutputFile.Text == "") { MessageBox.Show("Select an output file for the outputter\n"); } else { outputter = (IOutputter)loader.GetInstance(choice, (object)tbOutputFile.Text); } } } if (cbGenerationFactory.SelectedItem != null) { string choice = getChoice(generationFactories, cbGenerationFactory); if (choice != defaultGenerationFactoryString) { generationFactory = (IGenerationFactory)loader.GetInstance(choice, null); } } if (errorMsg != "") MessageBox.Show(errorMsg + "Please make sure you have selected a populator, evaluator, genetic operator and terminator and then try pressing the button again\n"); else { try { displayOutputter = new DisplayOutputter(this, outputter); engine = new GeneticEngine(populator, evaluator, geneticOperator, terminator, displayOutputter, generationFactory); hasInitialised = true; } catch (GeneticEngineException ex) { MessageBox.Show(ex.Message); } } SetEngineRunning(false); } }