Beispiel #1
0
        public void TestDepth()
        {
            EncogProgramContext context = new EncogProgramContext();
            context.DefineVariable("x");

            StandardExtensions.CreateAll(context);

            PrgGrowGenerator rnd = new PrgGrowGenerator(context, 2);
            EncogProgram prg = (EncogProgram)rnd.Generate(new EncogRandom());
            RenderCommonExpression render = new RenderCommonExpression();
        }
        public void TestSimpleClone()
        {
            EncogProgramContext context = new EncogProgramContext();
            context.LoadAllFunctions();
            RenderCommonExpression render = new RenderCommonExpression();

            EncogProgram prg1 = context.CreateProgram("1*2*3");
            EncogProgram prg2 = context.CloneProgram(prg1);

            Assert.AreEqual("((1*2)*3)", render.Render(prg1));
            Assert.AreEqual("((1*2)*3)", render.Render(prg2));
        }
        public void TestCloneComplex()
        {
            EncogProgramContext context = new EncogProgramContext();
            context.LoadAllFunctions();
            context.DefineVariable("a");
            RenderCommonExpression render = new RenderCommonExpression();

            EncogProgram prg1 = context.CreateProgram("((a+25)^3/25)-((a*3)^4/250)");
            EncogProgram prg2 = context.CloneProgram(prg1);

            Assert.AreEqual("((((a+25)^3)/25)-(((a*3)^4)/250))", render.Render(prg1));
            Assert.AreEqual("((((a+25)^3)/25)-(((a*3)^4)/250))", render.Render(prg2));
        }
        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>
        ///     Construct an Encog program using the specified context and variable
        ///     holder.
        /// </summary>
        /// <param name="theContext">The context.</param>
        /// <param name="theVariables">The variable holder.</param>
        public EncogProgram(EncogProgramContext theContext,
                            EncogProgramVariables theVariables)
        {
            _context = theContext;
            _variables = theVariables;

            // define variables
            foreach (VariableMapping v in _context.DefinedVariables)
            {
                _variables.DefineVariable(v);
            }
        }
 /// <summary>
 ///     Construct the Encog program with the specified context, but create a new
 ///     variable holder.
 /// </summary>
 /// <param name="theContext">The context.</param>
 public EncogProgram(EncogProgramContext theContext)
     : this(theContext, new EncogProgramVariables())
 {
 }
        /// <summary>
        ///     Generate a EPL population method.
        /// </summary>
        /// <param name="inputColumns">The input column count.</param>
        /// <param name="outputColumns">The output column count.</param>
        private void generateEPL(int inputColumns,
                                 int outputColumns)
        {
            _script.Properties.SetProperty(
                ScriptProperties.MlConfigType,
                MLMethodFactory.TypeEPL);
            String vars = "";

            if (inputColumns > 26)
            {
                throw new EncogError("More than 26 input variables is not supported for EPL.");
            }
            else if (inputColumns <= 3)
            {
                var temp = new StringBuilder();
                for (int i = 0; i < inputColumns; i++)
                {
                    if (temp.Length > 0)
                    {
                        temp.Append(',');
                    }
                    temp.Append((char) ('x' + i));
                }
                vars = temp.ToString();
            }
            else
            {
                var temp = new StringBuilder();
                for (int i = 0; i < inputColumns; i++)
                {
                    if (temp.Length > 0)
                    {
                        temp.Append(',');
                    }
                    temp.Append((char) ('a' + i));
                }
                vars = temp.ToString();
            }

            _script.Properties.SetProperty(
                ScriptProperties.MlConfigArchitecture,
                "cycles=" + NEATPopulation.DefaultCycles + ",vars=\"" + vars + "\"");

            _script.Properties.SetProperty(ScriptProperties.MlTrainType,
                                           MLTrainFactory.TypeEPLGA);
            _script.Properties.SetProperty(
                ScriptProperties.MlTrainTargetError, MaxError);

            // add in the opcodes
            var context = new EncogProgramContext();

            if (Goal == AnalystGoal.Regression)
            {
                StandardExtensions.CreateNumericOperators(context);
            }
            else
            {
                StandardExtensions.CreateNumericOperators(context);
                StandardExtensions.CreateBooleanOperators(context);
            }
            foreach (IProgramExtensionTemplate temp in context.Functions.OpCodes)
            {
                _script.Opcodes.Add(new ScriptOpcode(temp));
            }
        }
Beispiel #10
0
        /// <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);
        }
        /// <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();
            }
        }
Beispiel #12
0
 /// <summary>
 ///     Construct the Encog program with the specified context, but create a new
 ///     variable holder.
 /// </summary>
 /// <param name="theContext">The context.</param>
 public EncogProgram(EncogProgramContext theContext)
     : this(theContext, new EncogProgramVariables())
 {
 }
        /// <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;
        }