public void TestA1() { initialiseA(); Boolean passed = true; int[] countNumbers; countNumbers = new int[150]; GeneticEngine testEngine = new GeneticEngine(APopulator, AEvaluator, AGeneticOperator, AFitnessThresholdTerminator, AOutputter, null); currentGeneration = testEngine.Generation; int temp = 0; //Check that 100 individuals were generated: if (currentGeneration.Count != 100) throw new Exception("Count not set to 100."); //Check that individuals exist with values 1-100 exactly once each. for (int i = 0; i < 100; i++) { temp = (((IntegerIndividual)(currentGeneration.Get(i)).Individual)).value; countNumbers[temp - 1]++; } for (int i = 0; i < 100; i++) { if (countNumbers[i] != 1) { passed = false; break; } } //if (!passed) throw new Exception("Individuals not populated from 1-100 exactly once each"); Assert.IsTrue(passed); //If no exceptions halt test: Console.WriteLine("Test A1 Successful"); }
/// <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); } }
public void TestA2() { initialiseA(); Boolean passed = true; int[] countNumber; countNumber = new int[198]; GeneticEngine testEngine = new GeneticEngine(APopulator, AEvaluator, AGeneticOperator, AFitnessThresholdTerminator, AOutputter, null); //testEngine.Step(); //testEngine.Step(); currentGeneration = testEngine.Generation; int temp = 0; //Check that individuals exist from 2 to 200. for (int i = 0; i < currentGeneration.Count; i++) { temp = (((IntegerIndividual)(currentGeneration.Get(i)).Individual)).value; countNumber[temp - 1]++; } for (int i = 0; i < currentGeneration.Count; i++) { if (countNumber[i] == 0) { passed = false; break; } } //if (!passed) throw new Exception("Individuals not generated from 2-200 correctly."); Assert.IsTrue(passed); //If no exceptions halt test, then successful: Console.WriteLine("Test A2 Successful"); }
//[TestMethod] public void TestA5() { initialiseA(); GeneticEngine testEngine = new GeneticEngine(APopulator, AEvaluator, AGeneticOperator, AFitnessThresholdTerminator, AOutputter, null); //testEngine.Reset(); testEngine.Run(); int[] countNumber; //Boolean passed = true; countNumber = new int[100]; currentGeneration = testEngine.Generation; int temp = 0; int min = 0; int max = 0; min = (((IntegerIndividual)(currentGeneration.Get(0)).Individual)).value; max = (((IntegerIndividual)(currentGeneration.Get(0)).Individual)).value; //Check that individuals exist from 101 to 200. for (int i = 0; i < currentGeneration.Count; i++) { temp = (((IntegerIndividual)(currentGeneration.Get(i)).Individual)).value; if (temp > max) max = temp; if (temp < min) min = temp; //countNumber[temp - 101]++; } //if (min != 101) passed = false; //if (max != 200) passed = false; Assert.AreEqual(max, 200); Assert.AreEqual(min, 101); //If we need to check ever single individual and make sure there is exactly 1 instance of every integer from 101-200. /* for (int i = 0; i < currentGeneration.Count; i++) { if (countNumber[i] == 0) { passed = false; break; } } */ //if (!passed) throw new Exception("Individuals not generated from 101-200 correctly."); //Assert.IsTrue(passed); //If no exceptions halt test, then successful: Console.WriteLine("Test A5 Successful"); }
public void TestA4() { initialiseA(); GeneticEngine testEngine = new GeneticEngine(APopulator, AEvaluator, AGeneticOperator, AFitnessThresholdTerminator, AOutputter, null); //testEngine.Reset(); testEngine.Repeat(5); currentGeneration = testEngine.Generation; //Assert.Equals Assert.AreEqual(5, AOutputter.numberGenerations); //Fails here, output expected is 99 but 0 is returned. The assertions after this pass though (101, 103,..) Assert.AreEqual(101, AOutputter.fitnesses[1]); Assert.AreEqual(103, AOutputter.fitnesses[2]); Assert.AreEqual(105, AOutputter.fitnesses[3]); Assert.AreEqual(107, AOutputter.fitnesses[4]); }
//[TestMethod] public void TestA3() { Boolean passed = true; initialiseA(); GeneticEngine testEngine = new GeneticEngine(APopulator, AEvaluator, AGeneticOperator, AFitnessThresholdTerminator, AOutputter, null); //testEngine.Reset(); if (testEngine.IsComplete) passed = false;//throw new Exception("Is Complete returns true when expected value is false."); testEngine.Step(); if (testEngine.IsComplete) passed = false;// throw new Exception("Is Complete returns true when expected value is false."); testEngine.Repeat(99); if (testEngine.IsComplete) passed = false;// throw new Exception("Is Complete returns true when expected value is false."); testEngine.Step(); if (!testEngine.IsComplete) passed = false;// throw new Exception("Is Complete returns false when expected value is true."); Assert.IsTrue(passed); //If no exceptions halt test, then successful: Console.WriteLine("Test A3 Successful"); }
static void run() { IPopulator populator = new Populator("map.xml"); IEvaluator evaluator = new Evaluator(null); IGeneticOperator mutator = new Mutator(null); ITerminator terminator = new FitnessThresholdTerminator(FitnessConverter.FromFloat(1.0f / 1024.0f)); IOutputter outputter = new RoadNetworkXmlOutputter(@"c:\roadnetworktest\index.xml"); GeneticEngine engine = new GeneticEngine(populator, evaluator, mutator, terminator, outputter); engine.Repeat(100); engine.FinishOutput(); }