public void TestRenderFunction()
 {
     EncogProgram expression = new EncogProgram("(sin(x)+cos(x))/2");
     RenderCommonExpression render = new RenderCommonExpression();
     String result = render.Render(expression);
     Assert.AreEqual("((sin(x)+cos(x))/2)", result);
 }
 public void TestRenderFunction()
 {
     EncogProgram expression = new EncogProgram("(sin(x)+cos(x))/2");
     RenderEPL render = new RenderEPL();
     String result = render.Render(expression);
     Assert.AreEqual("[#var:0:0][sin:1][#var:0:0][cos:1][+:2][#const:0:2][/:2]", result);
 }
 public void TestKnownConst()
 {
     EncogProgram expression = new EncogProgram("x*2*PI");
     RenderCommonExpression render = new RenderCommonExpression();
     String result = render.Render(expression);
     Assert.AreEqual("((x*2)*PI)", result);
 }
 public void TestRenderComplex()
 {
     EncogProgram expression = new EncogProgram("((a+25)^3/25)-((a*3)^4/250)");
     RenderRPN render = new RenderRPN();
     String result = render.Render(expression);
     Assert.AreEqual("a 25 [+] 3 [^] 25 [/] a 3 [*] 4 [^] 250 [/] [-]", result);
 }
 public void TestRenderFunction()
 {
     EncogProgram expression = new EncogProgram("(sin(x)+cos(x))/2");
     RenderRPN render = new RenderRPN();
     String result = render.Render(expression);
     Assert.AreEqual("x [sin] x [cos] [+] 2 [/]", result);
 }
 public void TestRenderBasic()
 {
     EncogProgram expression = new EncogProgram("(2+6)");
     RenderRPN render = new RenderRPN();
     String result = render.Render(expression);
     Assert.AreEqual("2 6 [+]", result);
 }
 public void TestRenderComplex()
 {
     EncogProgram expression = new EncogProgram("((a+25)^3/25)-((a*3)^4/250)");
     RenderCommonExpression render = new RenderCommonExpression();
     String result = render.Render(expression);
     Assert.AreEqual("((((a+25)^3)/25)-(((a*3)^4)/250))", result);
 }
 public void TestRenderBasic()
 {
     EncogProgram expression = new EncogProgram("(2+6)");
     RenderEPL render = new RenderEPL();
     String result = render.Render(expression);
     Assert.AreEqual("[#const:0:2][#const:0:6][+:2]", result);
 }
 public void TestRenderComplex()
 {
     EncogProgram expression = new EncogProgram("((a+25)^3/25)-((a*3)^4/250)");
     RenderEPL render = new RenderEPL();
     String result = render.Render(expression);
     Assert.AreEqual("[#var:0:0][#const:0:25][+:2][#const:0:3][^:2][#const:0:25][/:2][#var:0:0][#const:0:3][*:2][#const:0:4][^:2][#const:0:250][/:2][-:2]", result);
 }
 public void TestKnownConst()
 {
     EncogProgram expression = new EncogProgram("x*2*PI");
     RenderRPN render = new RenderRPN();
     String result = render.Render(expression);
     Assert.AreEqual("x 2 [*] PI [*]", result);
 }
Exemple #11
0
        /// <summary>
        ///     Create a new program, using this context.
        /// </summary>
        /// <param name="expression">The common expression to compile.</param>
        /// <returns>The resulting program.</returns>
        public EncogProgram CreateProgram(String expression)
        {
            var result = new EncogProgram(this);

            result.CompileExpression(expression);
            return(result);
        }
Exemple #12
0
        /// <summary>
        ///     Clone a branch of the program from the specified node.
        /// </summary>
        /// <param name="targetProgram">The program that this branch will be "grafted" into.</param>
        /// <param name="sourceBranch">The branch to clone, from the source program.</param>
        /// <returns>The cloned branch.</returns>
        public ProgramNode CloneBranch(EncogProgram targetProgram,
                                       ProgramNode sourceBranch)
        {
            if (sourceBranch == null)
            {
                throw new EncogError("Can't clone null branch.");
            }

            String name = sourceBranch.Name;

            // create any subnodes
            var args = new ProgramNode[sourceBranch.ChildNodes.Count];

            for (int i = 0; i < args.Length; i++)
            {
                args[i] = CloneBranch(targetProgram, (ProgramNode)sourceBranch
                                      .ChildNodes[i]);
            }

            ProgramNode result = targetProgram.Context.Functions
                                 .FactorProgramNode(name, targetProgram, args);

            // now copy the expression data for the node
            for (int i = 0; i < sourceBranch.Data.Length; i++)
            {
                result.Data[i] = new ExpressionValue(sourceBranch.Data[i]);
            }

            // return the new node
            return(result);
        }
 public void TestAssignment3()
 {
     EncogProgram expression = new EncogProgram("v1+v2+v3");
     expression.Variables.SetVariable("v1", 1);
     expression.Variables.SetVariable("v2", 2);
     expression.Variables.SetVariable("v3", 3);
     Assert.AreEqual(6, expression.Evaluate().ToFloatValue(), EncogFramework.DefaultDoubleEqual);
 }
Exemple #14
0
        /// <summary>
        ///     Clone an entire program, keep the same context.
        /// </summary>
        /// <param name="sourceProgram">The source program.</param>
        /// <returns>The cloned program.</returns>
        public EncogProgram CloneProgram(EncogProgram sourceProgram)
        {
            ProgramNode rootNode = sourceProgram.RootNode;
            var         result   = new EncogProgram(this);

            result.RootNode = CloneBranch(result, rootNode);
            return(result);
        }
 public void TestAssignment2()
 {
     EncogProgram expression = new EncogProgram("cccc*(aa+bbb)");
     expression.Variables.SetVariable("aa", 1);
     expression.Variables.SetVariable("bbb", 2);
     expression.Variables.SetVariable("cccc", 3);
     Assert.AreEqual(9, expression.Evaluate().ToFloatValue(), EncogFramework.DefaultDoubleEqual);
 }
 public void TestCrossoverOperation()
 {
     RenderCommonExpression render = new RenderCommonExpression();
     EncogProgram prg = new EncogProgram("1+2");
     EncogProgram prg2 = new EncogProgram("4+5");
     ProgramNode node = prg.FindNode(2);
     prg.ReplaceNode(node, prg2.RootNode);
     Assert.AreEqual("(1+(4+5))", render.Render(prg));
 }
Exemple #17
0
        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 TestSize()
        {
            EncogProgram expression = new EncogProgram("1");
            Assert.AreEqual(1, expression.RootNode.Count);

            expression = new EncogProgram("1+1");
            Assert.AreEqual(3, expression.RootNode.Count);

            expression = new EncogProgram("1+1+1");
            Assert.AreEqual(5, expression.RootNode.Count);

            expression = new EncogProgram("(sin(x)+cos(x))/2");
            Assert.AreEqual(7, expression.RootNode.Count);
        }
Exemple #19
0
        /// <summary>
        ///     Construct the program node.
        /// </summary>
        /// <param name="theOwner">The owner of the node.</param>
        /// <param name="theTemplate">The opcode that this node is based on.</param>
        /// <param name="theArgs">The child nodes to this node.</param>
        public ProgramNode(EncogProgram theOwner,
                           IProgramExtensionTemplate theTemplate,
                           ITreeNode[] theArgs)
        {
            _owner    = theOwner;
            _data     = new ExpressionValue[theTemplate.DataSize];
            _template = theTemplate;
            AddChildNodes(theArgs);

            for (int i = 0; i < _data.Length; i++)
            {
                _data[i] = new ExpressionValue((long)0);
            }
        }
        /// <summary>
        ///     Construct the program node.
        /// </summary>
        /// <param name="theOwner">The owner of the node.</param>
        /// <param name="theTemplate">The opcode that this node is based on.</param>
        /// <param name="theArgs">The child nodes to this node.</param>
        public ProgramNode(EncogProgram theOwner,
                           IProgramExtensionTemplate theTemplate,
                           ITreeNode[] theArgs)
        {
            _owner = theOwner;
            _data = new ExpressionValue[theTemplate.DataSize];
            _template = theTemplate;
            AddChildNodes(theArgs);

            for (int i = 0; i < _data.Length; i++)
            {
                _data[i] = new ExpressionValue((long) 0);
            }
        }
Exemple #21
0
        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));
        }
        public void TestSize()
        {
            EncogProgram expression = new EncogProgram("1");

            Assert.AreEqual(1, expression.RootNode.Count);

            expression = new EncogProgram("1+1");
            Assert.AreEqual(3, expression.RootNode.Count);

            expression = new EncogProgram("1+1+1");
            Assert.AreEqual(5, expression.RootNode.Count);

            expression = new EncogProgram("(sin(x)+cos(x))/2");
            Assert.AreEqual(7, expression.RootNode.Count);
        }
        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;
        }
        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>
 ///     Parse the specified program, or expression, and return the result. No
 ///     variables can be defined for this as a default context is used. The
 ///     result is returned as a boolean.
 /// </summary>
 /// <param name="str">The program expression.</param>
 /// <returns>The value the expression was evaluated to.</returns>
 public static bool ParseBoolean(String str)
 {
     var holder = new EncogProgram(str);
     return holder.Evaluate().ToBooleanValue();
 }
 /// <summary>
 ///     Create a new program, using this context.
 /// </summary>
 /// <param name="expression">The common expression to compile.</param>
 /// <returns>The resulting program.</returns>
 public EncogProgram CreateProgram(String expression)
 {
     var result = new EncogProgram(this);
     result.CompileExpression(expression);
     return result;
 }
 /// <summary>
 ///     Clone an entire program, keep the same context.
 /// </summary>
 /// <param name="sourceProgram">The source program.</param>
 /// <returns>The cloned program.</returns>
 public EncogProgram CloneProgram(EncogProgram sourceProgram)
 {
     ProgramNode rootNode = sourceProgram.RootNode;
     var result = new EncogProgram(this);
     result.RootNode = CloneBranch(result, rootNode);
     return result;
 }
        /// <summary>
        ///     Clone a branch of the program from the specified node.
        /// </summary>
        /// <param name="targetProgram">The program that this branch will be "grafted" into.</param>
        /// <param name="sourceBranch">The branch to clone, from the source program.</param>
        /// <returns>The cloned branch.</returns>
        public ProgramNode CloneBranch(EncogProgram targetProgram,
                                       ProgramNode sourceBranch)
        {
            if (sourceBranch == null)
            {
                throw new EncogError("Can't clone null branch.");
            }

            String name = sourceBranch.Name;

            // create any subnodes
            var args = new ProgramNode[sourceBranch.ChildNodes.Count];
            for (int i = 0; i < args.Length; i++)
            {
                args[i] = CloneBranch(targetProgram, (ProgramNode) sourceBranch
                                                                       .ChildNodes[i]);
            }

            ProgramNode result = targetProgram.Context.Functions
                                              .FactorProgramNode(name, targetProgram, args);

            // now copy the expression data for the node
            for (int i = 0; i < sourceBranch.Data.Length; i++)
            {
                result.Data[i] = new ExpressionValue(sourceBranch.Data[i]);
            }

            // return the new node
            return result;
        }
 public String Render(EncogProgram theProgram)
 {
     this.program = theProgram;
     return RenderNode(this.program.RootNode);
 }
 public void TestNegAssignment()
 {
     EncogProgram expression = new EncogProgram("-a");
     expression.Variables.SetVariable("a", 5);
     Assert.AreEqual(-5, expression.Evaluate().ToFloatValue(), EncogFramework.DefaultDoubleEqual);
 }
 public void testVarComplex()
 {
     EncogProgram expression = new EncogProgram("(x^((1+((x^-8)-(4^x)))^(((-7/2)-(0--5.8))/x)))");
     expression.Variables.SetVariable("x", 10);
     Assert.IsTrue(Double.IsNaN(expression.Evaluate().ToFloatValue()));
 }
        /// <summary>
        ///     Analyze the neural network.
        /// </summary>
        /// <param name="inputFile">The input file.</param>
        /// <param name="headers">True, if there are headers.</param>
        /// <param name="format">The format of the CSV file.</param>
        public void Analyze(FileInfo inputFile, bool headers, CSVFormat format)
        {
            InputFilename = inputFile;
            ExpectInputHeaders = headers;
            Format = format;

            Analyzed = true;

            PerformBasicCounts();

            expressionFields.Clear();
            extension = new ProcessExtension(Format);
            extension.register(programContext.Functions);

            foreach (ProcessField field in analyst.Script.Process.Fields)
            {
                var prg = new EncogProgram(programContext, programVariables);
                prg.SetExtraData(ProcessExtension.EXTENSION_DATA_NAME, extension);
                prg.CompileExpression(field.Command);
                expressionFields.Add(prg);
            }
        }
 public ParseCommonExpression(EncogProgram theHolder)
 {
     this.holder = theHolder;
 }
        /// <summary>
        ///     Parse the specified program, or expression, and return the result. No
        ///     variables can be defined for this as a default context is used. The
        ///     result is returned as a boolean.
        /// </summary>
        /// <param name="str">The program expression.</param>
        /// <returns>The value the expression was evaluated to.</returns>
        public static bool ParseBoolean(String str)
        {
            var holder = new EncogProgram(str);

            return(holder.Evaluate().ToBooleanValue());
        }
        /// <summary>
        ///     Parse the specified program, or expression, and return the result. No
        ///     variables can be defined for this as a default context is used. The
        ///     result is returned as a boolean.
        /// </summary>
        /// <param name="str">The program expression.</param>
        /// <returns>The value the expression was evaluated to.</returns>
        public static ExpressionValue ParseExpression(String str)
        {
            var holder = new EncogProgram(str);

            return(holder.Evaluate());
        }
Exemple #36
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>
        ///     Parse the specified program, or expression, and return the result. No
        ///     variables can be defined for this as a default context is used. The
        ///     result is returned as a float.
        /// </summary>
        /// <param name="str">The program expression value.</param>
        /// <returns>The value the expression was evaluated to.</returns>
        public static double ParseFloat(String str)
        {
            var holder = new EncogProgram(str);

            return(holder.Evaluate().ToFloatValue());
        }
 /// <summary>
 ///     Parse the specified program, or expression, and return the result. No
 ///     variables can be defined for this as a default context is used. The
 ///     result is returned as a float.
 /// </summary>
 /// <param name="str">The program expression value.</param>
 /// <returns>The value the expression was evaluated to.</returns>
 public static double ParseFloat(String str)
 {
     var holder = new EncogProgram(str);
     return holder.Evaluate().ToFloatValue();
 }
 /// <summary>
 ///     Parse the specified program, or expression, and return the result. No
 ///     variables can be defined for this as a default context is used. The
 ///     result is returned as a boolean.
 /// </summary>
 /// <param name="str">The program expression.</param>
 /// <returns>The value the expression was evaluated to.</returns>
 public static ExpressionValue ParseExpression(String str)
 {
     var holder = new EncogProgram(str);
     return holder.Evaluate();
 }
        /// <summary>
        ///     Parse the specified program, or expression, and return the result. No
        ///     variables can be defined for this as a default context is used. The
        ///     result is returned as a string.
        /// </summary>
        /// <param name="str">The program expression value.</param>
        /// <returns>The value the expression was evaluated to.</returns>
        public static String ParseString(String str)
        {
            var holder = new EncogProgram(str);

            return(holder.Evaluate().ToStringValue());
        }
 /// <summary>
 ///     Parse the specified program, or expression, and return the result. No
 ///     variables can be defined for this as a default context is used. The
 ///     result is returned as a string.
 /// </summary>
 /// <param name="str">The program expression value.</param>
 /// <returns>The value the expression was evaluated to.</returns>
 public static String ParseString(String str)
 {
     var holder = new EncogProgram(str);
     return holder.Evaluate().ToStringValue();
 }
 public ParseEPL(EncogProgram theHolder)
 {
     this.holder = theHolder;
 }