private PrgPopulation Create() { EncogProgramContext context = new EncogProgramContext(); context.DefineVariable("x"); StandardExtensions.CreateAll(context); PrgPopulation pop = new PrgPopulation(context, 10); EncogProgram prg1 = new EncogProgram(context); EncogProgram prg2 = new EncogProgram(context); prg1.CompileExpression("x+1"); prg2.CompileExpression("(x+5)/2"); ISpecies defaultSpecies = pop.CreateSpecies(); defaultSpecies.Add(prg1); defaultSpecies.Add(prg2); return pop; }
/// <summary> /// Create a feed forward network. /// </summary> /// <param name="architecture">The architecture string to use.</param> /// <param name="input">The input count.</param> /// <param name="output">The output count.</param> /// <returns>The feedforward network.</returns> public IMLMethod Create(String architecture, int input, int output) { if (input <= 0) { throw new EncogError("Must have at least one input for EPL."); } if (output <= 0) { throw new EncogError("Must have at least one output for EPL."); } IDictionary<String, String> args = ArchitectureParse.ParseParams(architecture); var holder = new ParamsHolder(args); int populationSize = holder.GetInt( MLMethodFactory.PropertyPopulationSize, false, 1000); String variables = holder.GetString("vars", false, "x"); String funct = holder.GetString("funct", false, null); var context = new EncogProgramContext(); string[] tok = variables.Split(','); foreach (string v in tok) { context.DefineVariable(v); } if (String.Compare("numeric", funct, StringComparison.OrdinalIgnoreCase) == 0) { StandardExtensions.CreateNumericOperators(context); } var pop = new PrgPopulation(context, populationSize); if (context.Functions.Count > 0) { (new RampedHalfAndHalf(context, 2, 6)).Generate(new EncogRandom(), pop); } return pop; }
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> /// 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(); } }
/// <inheritdoc /> public Object Read(Stream istream) { var context = new EncogProgramContext(); var result = new PrgPopulation(context, 0); var reader = new EncogReadHelper(istream); EncogFileSection section; int count = 0; ISpecies lastSpecies = null; while ((section = reader.ReadNextSection()) != null) { if (section.SectionName.Equals("BASIC") && section.SubSectionName.Equals("PARAMS")) { IDictionary<string, string> prms = section.ParseParams(); EngineArray.PutAll(prms, result.Properties); } else if (section.SectionName.Equals("BASIC") && section.SubSectionName.Equals("EPL-POPULATION")) { foreach (string line in section.Lines) { IList<String> cols = EncogFileSection.SplitColumns(line); if (String.Compare(cols[0], "s", StringComparison.OrdinalIgnoreCase) == 0) { lastSpecies = new BasicSpecies { Age = int.Parse(cols[1]), BestScore = CSVFormat.EgFormat.Parse(cols[2]), Population = result, GensNoImprovement = int.Parse(cols[3]) }; result.Species.Add(lastSpecies); } else if (cols[0].Equals("p")) { double score; double adjustedScore; if (String.Compare(cols[1], "nan", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(cols[2], "nan", StringComparison.OrdinalIgnoreCase) == 0) { score = Double.NaN; adjustedScore = Double.NaN; } else { score = CSVFormat.EgFormat.Parse(cols[1]); adjustedScore = CSVFormat.EgFormat.Parse(cols[2]); } String code = cols[3]; var prg = new EncogProgram(context); prg.CompileEPL(code); prg.Score = score; prg.Species = lastSpecies; prg.AdjustedScore = adjustedScore; if (lastSpecies == null) { throw new EncogError( "Have not defined a species yet"); } lastSpecies.Add(prg); count++; } } } else if (section.SectionName.Equals("BASIC") && section.SubSectionName.Equals("EPL-OPCODES")) { foreach (String line in section.Lines) { IList<string> cols = EncogFileSection.SplitColumns(line); String name = cols[0]; int args = int.Parse(cols[1]); result.Context.Functions.AddExtension(name, args); } } else if (section.SectionName.Equals("BASIC") && section.SubSectionName.Equals("EPL-SYMBOLIC")) { bool first = true; foreach (string line in section.Lines) { if (!first) { IList<String> cols = EncogFileSection.SplitColumns(line); String name = cols[0]; String t = cols[1]; var vt = EPLValueType.Unknown; if (string.Compare(t, "f", true) == 0) { vt = EPLValueType.FloatingType; } else if (string.Compare(t, "b", true) == 0) { vt = EPLValueType.BooleanType; } else if (string.Compare(t, "i", true) == 0) { vt = EPLValueType.IntType; } else if (string.Compare(t, "s", true) == 0) { vt = EPLValueType.StringType; } else if (string.Compare(t, "e", true) == 0) { vt = EPLValueType.EnumType; } int enumType = int.Parse(cols[2]); int enumCount = int.Parse(cols[3]); var mapping = new VariableMapping( name, vt, enumType, enumCount); if (mapping.Name.Length > 0) { result.Context.DefineVariable(mapping); } else { result.Context.Result = mapping; } } else { first = false; } } } } result.PopulationSize = count; // set the best genome, should be the first genome in the first species if (result.Species.Count > 0) { ISpecies species = result.Species[0]; if (species.Members.Count > 0) { result.BestGenome = species.Members[0]; } // set the leaders foreach (ISpecies sp in result.Species) { if (sp.Members.Count > 0) { sp.Leader = sp.Members[0]; } } } return result; }
private void Validate(PrgPopulation pop) { IList<IGenome> list = pop.Flatten(); Assert.AreEqual(2, list.Count); EncogProgram prg1 = (EncogProgram)list[0]; EncogProgram prg2 = (EncogProgram)list[1]; RenderCommonExpression render = new RenderCommonExpression(); Assert.AreEqual("(x+1)", render.Render(prg1)); Assert.AreEqual("((x+5)/2)", render.Render(prg2)); }