public void Eval(String start, String expect) { EncogProgramContext context = new EncogProgramContext(); StandardExtensions.CreateNumericOperators(context); PrgPopulation pop = new PrgPopulation(context, 1); ICalculateScore score = new ZeroEvalScoreFunction(); TrainEA genetic = new TrainEA(pop, score); genetic.ValidationMode = true; genetic.CODEC = new PrgCODEC(); genetic.AddOperation(0.95, new SubtreeCrossover()); genetic.AddOperation(0.05, new SubtreeMutation(context, 4)); genetic.AddScoreAdjuster(new ComplexityAdjustedScore()); genetic.Rules.AddRewriteRule(new RewriteConstants()); genetic.Rules.AddRewriteRule(new RewriteAlgebraic()); EncogProgram expression = new EncogProgram(context); expression.CompileExpression(start); RenderCommonExpression render = new RenderCommonExpression(); genetic.Rules.Rewrite(expression); Assert.AreEqual(expect, render.Render(expression)); }
/// <summary> /// Create an EPL GA trainer. /// </summary> /// <param name="method">The method to use.</param> /// <param name="training">The training data to use.</param> /// <param name="argsStr">The arguments to use.</param> /// <returns>The newly created trainer.</returns> public IMLTrain Create(IMLMethod method, IMLDataSet training, String argsStr) { PrgPopulation pop = (PrgPopulation)method; ICalculateScore score = new TrainingSetScore(training); TrainEA train = new TrainEA(pop, score); train.Rules.AddRewriteRule(new RewriteConstants()); train.Rules.AddRewriteRule(new RewriteAlgebraic()); train.CODEC = new PrgCODEC(); train.AddOperation(0.8, new SubtreeCrossover()); train.AddOperation(0.1, new SubtreeMutation(pop.Context, 4)); train.AddOperation(0.1, new ConstMutation(pop.Context, 0.5, 1.0)); train.AddScoreAdjuster(new ComplexityAdjustedScore()); train.Speciation = new PrgSpeciation(); return(train); }
/// <summary> /// Program entry point. /// </summary> /// <param name="app">Holds arguments and other info.</param> public void Execute(IExampleInterface app) { IMLDataSet trainingData = GenerationUtil.GenerateSingleDataRange( (x) => (3 * Math.Pow(x, 2) + (12 * x) + 4) , 0, 100, 1); EncogProgramContext context = new EncogProgramContext(); context.DefineVariable("x"); StandardExtensions.CreateNumericOperators(context); PrgPopulation pop = new PrgPopulation(context, 1000); MultiObjectiveFitness score = new MultiObjectiveFitness(); score.AddObjective(1.0, new TrainingSetScore(trainingData)); TrainEA genetic = new TrainEA(pop, score); genetic.ValidationMode = true; genetic.CODEC = new PrgCODEC(); genetic.AddOperation(0.5, new SubtreeCrossover()); genetic.AddOperation(0.25, new ConstMutation(context, 0.5, 1.0)); genetic.AddOperation(0.25, new SubtreeMutation(context, 4)); genetic.AddScoreAdjuster(new ComplexityAdjustedScore(10, 20, 10, 20.0)); genetic.Rules.AddRewriteRule(new RewriteConstants()); genetic.Rules.AddRewriteRule(new RewriteAlgebraic()); genetic.Speciation = new PrgSpeciation(); (new RampedHalfAndHalf(context, 1, 6)).Generate(new EncogRandom(), pop); genetic.ShouldIgnoreExceptions = false; EncogProgram best = null; genetic.ThreadCount = 1; try { for (int i = 0; i < 1000; i++) { genetic.Iteration(); best = (EncogProgram)genetic.BestGenome; Console.Out.WriteLine(genetic.IterationNumber + ", Error: " + best.Score + ",Best Genome Size:" + best.Size + ",Species Count:" + pop.Species.Count + ",best: " + best.DumpAsCommonExpression()); } //EncogUtility.evaluate(best, trainingData); Console.Out.WriteLine("Final score:" + best.Score + ", effective score:" + best.AdjustedScore); Console.Out.WriteLine(best.DumpAsCommonExpression()); //pop.dumpMembers(Integer.MAX_VALUE); //pop.dumpMembers(10); } catch (Exception t) { Console.Out.WriteLine(t.ToString()); } finally { genetic.FinishTraining(); EncogFramework.Instance.Shutdown(); } }