예제 #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 TestCloneVar()
        {
            EncogProgramContext context = new EncogProgramContext();
            context.LoadAllFunctions();
            context.DefineVariable("x");
            RenderCommonExpression render = new RenderCommonExpression();

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

            Assert.AreEqual("((x*2)*3)", render.Render(prg1));
            Assert.AreEqual("((x*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));
        }
예제 #4
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));
        }
예제 #5
0
        public void TestCloneVar()
        {
            EncogProgramContext context = new EncogProgramContext();

            context.LoadAllFunctions();
            context.DefineVariable("x");
            RenderCommonExpression render = new RenderCommonExpression();

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

            Assert.AreEqual("((x*2)*3)", render.Render(prg1));
            Assert.AreEqual("((x*2)*3)", 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;
        }
예제 #8
0
        /// <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();
            }
        }