private GeneticAlgorithm CreateCFGGPSample() { GeneticAlgorithm ga = new GeneticAlgorithm(); #region Problem Configuration CFGPythonProblem cfgPythonProblem = new CFGPythonProblem(); cfgPythonProblem.Name = "Smallest Problem"; cfgPythonProblem.Description = "Smallest (described in: http://dl.acm.org/citation.cfm?id=2754769)"; var provider = new BenchmarkSuiteListInstanceProvider(); var instance = provider.GetDataDescriptors().Single(x => x.Name.Equals("Smallest")); CFGData data = (CFGData)provider.LoadData(instance); data.Input = File.ReadAllLines(@"Data\Smallest-Input.txt"); data.Output = File.ReadAllLines(@"Data\Smallest-Output.txt"); cfgPythonProblem.Load(data); // configure remaining problem parameters cfgPythonProblem.MaximumSymbolicExpressionTreeLengthParameter.Value.Value = 250; cfgPythonProblem.MaximumSymbolicExpressionTreeDepthParameter.Value.Value = 100; #endregion #region Algorithm Configuration ga.Problem = cfgPythonProblem; ga.Name = "Genetic Programming - CFG Python"; ga.Description = "A standard genetic programming algorithm to solve a cfg python problem"; SamplesUtils.ConfigureGeneticAlgorithmParameters <LexicaseSelector, SubtreeCrossover, MultiSymbolicExpressionTreeManipulator>( ga, 100, 1, 10, 0.05, 5); var mutator = (MultiSymbolicExpressionTreeManipulator)ga.Mutator; mutator.Operators.SetItemCheckedState(mutator.Operators .OfType <FullTreeShaker>() .Single(), false); mutator.Operators.SetItemCheckedState(mutator.Operators .OfType <OnePointShaker>() .Single(), false); #endregion return(ga); }
public void RunCFGGPSampleTestSolutionFound() { var ga = CreateCFGGPSample(); ga.SetSeedRandomly.Value = false; ga.Seed.Value = 461206446; ((CFGPythonProblem)ga.Problem).TimeoutParameter.Value.Value = 10000; SamplesUtils.RunAlgorithm(ga); Assert.AreEqual(0, SamplesUtils.GetDoubleResult(ga, "BestQuality")); Assert.AreEqual(13.09, SamplesUtils.GetDoubleResult(ga, "CurrentAverageQuality")); Assert.AreEqual(99, SamplesUtils.GetDoubleResult(ga, "CurrentWorstQuality")); Assert.AreEqual(1090, SamplesUtils.GetIntResult(ga, "EvaluatedSolutions")); Assert.AreEqual(7, SamplesUtils.GetIntResult(ga, "Best training solution generation")); var bestTrainingSolution = (CFGPythonSolution)ga.Results["Best training solution"].Value; Assert.AreEqual(0, ((DoubleValue)bestTrainingSolution["Training Quality"].Value).Value); Assert.AreEqual(0, ((DoubleValue)bestTrainingSolution["Test Quality"].Value).Value); Assert.AreEqual(17, ((IntValue)bestTrainingSolution["Model Depth"].Value).Value); Assert.AreEqual(62, ((IntValue)bestTrainingSolution["Model Length"].Value).Value); Assert.AreEqual(1.0, ((PercentValue)bestTrainingSolution["Training Solved Cases Percentage"].Value).Value); Assert.AreEqual(1.0, ((PercentValue)bestTrainingSolution["Test Solved Cases Percentage"].Value).Value); var exceptionTable = (DataTable)ga.Results["Exception frequencies"].Value; Assert.AreEqual(1, exceptionTable.Rows.Count); Assert.IsTrue(exceptionTable.Rows.ContainsKey("No Exception")); Assert.IsTrue(exceptionTable.Rows["No Exception"].Values.All(x => x == 1)); }
public void DisposePythonProcesses() { var ga = CreateCFGGPSample(); ga.SetSeedRandomly.Value = false; ga.Seed.Value = 461206446; var before = Process.GetProcessesByName("python").Length; SamplesUtils.RunAlgorithm(ga); var after = Process.GetProcessesByName("python").Length; Assert.AreEqual(before + Environment.ProcessorCount * 2, after); (ga.Problem as CFGPythonProblem).Dispose(); Thread.Sleep(1000); //give it a second var afterDispose = Process.GetProcessesByName("python").Length; Assert.AreEqual(before, afterDispose); }