コード例 #1
0
        /// <summary>
        ///     Construct the ramped half-and-half generator.
        /// </summary>
        /// <param name="theContext">The context.</param>
        /// <param name="theMinDepth">The minimum depth.</param>
        /// <param name="theMaxDepth">The maximum depth.</param>
        public RampedHalfAndHalf(EncogProgramContext theContext,
                                 int theMinDepth, int theMaxDepth)
            : base(theContext, theMaxDepth)
        {
            _minDepth = theMinDepth;

            _fullGenerator = new PrgFullGenerator(theContext, theMaxDepth);
            _growGenerator = new PrgGrowGenerator(theContext, theMaxDepth);
        }
コード例 #2
0
        /// <summary>
        ///     Construct the generator.
        /// </summary>
        /// <param name="theContext">The context that is to be used for generation.</param>
        /// <param name="theMaxDepth">The maximum depth to generate to.</param>
        protected AbstractPrgGenerator(EncogProgramContext theContext,
                                    int theMaxDepth)
        {
            if (theContext.Functions.Count == 0)
            {
                throw new EncogError("There are no opcodes defined");
            }

            _context = theContext;
            _maxDepth = theMaxDepth;
            _hasEnum = _context.HasEnum;
            Score = new ZeroEvalScoreFunction();
            MinConst = -10;
            MaxConst = 10;
            RandomFactory = EncogFramework.Instance.RandomFactory.FactorFactory();
            MaxGenerationErrors = 500;
        }
コード例 #3
0
 /// <summary>
 ///     Add the opcodes for numeric operations to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 /// <param name="protectedDiv">Should protected division be used.</param>
 public static void CreateNumericOperators(
     EncogProgramContext context, bool protectedDiv)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_VAR_SUPPORT);
     factory.AddExtension(EXTENSION_CONST_SUPPORT);
     factory.AddExtension(EXTENSION_NEG);
     factory.AddExtension(EXTENSION_ADD);
     factory.AddExtension(EXTENSION_SUB);
     factory.AddExtension(EXTENSION_MUL);
     if (protectedDiv)
     {
         factory.AddExtension(EXTENSION_PDIV);
     }
     else
     {
         factory.AddExtension(EXTENSION_DIV);
     }
     factory.AddExtension(EXTENSION_POWER);
 }
コード例 #4
0
 /// <summary>
 ///     Add the opcodes for numeric operations to a context, do not use protected
 ///     division.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateNumericOperators(EncogProgramContext context)
 {
     CreateNumericOperators(context, false);
 }
コード例 #5
0
 /// <summary>
 ///     Add the opcodes for type conversion operations to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateConversionFunctions(
     EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_CINT);
     factory.AddExtension(EXTENSION_CFLOAT);
     factory.AddExtension(EXTENSION_CSTR);
     factory.AddExtension(EXTENSION_CBOOL);
 }
コード例 #6
0
 /// <summary>
 ///     Construct the full generator.
 /// </summary>
 /// <param name="theContext">The context.</param>
 /// <param name="theMaxDepth">The max depth.</param>
 public PrgFullGenerator(EncogProgramContext theContext,
                         int theMaxDepth)
     : base(theContext, theMaxDepth)
 {
 }
コード例 #7
0
        /// <inheritdoc />
        public bool IsPossibleReturnType(EncogProgramContext context, EPLValueType rtn)
        {
            if (_delIsPossibleReturnType == null)
            {
                return _returnValue.PossibleTypes.Contains(rtn);
            }
            if (!_returnValue.PossibleTypes.Contains(rtn))
            {
                return false;
            }

            return _delIsPossibleReturnType(context, rtn);
        }
コード例 #8
0
 /// <summary>
 ///     Construct a const mutator.
 /// </summary>
 /// <param name="theContext">The program context.</param>
 /// <param name="theFrequency">The frequency of mutation.</param>
 /// <param name="theSigma">The sigma to use for mutation.</param>
 public ConstMutation(EncogProgramContext theContext,
                      double theFrequency, double theSigma)
 {
     _frequency = theFrequency;
     _sigma = theSigma;
 }
コード例 #9
0
 /// <summary>
 ///     Construct a factory.
 /// </summary>
 /// <param name="theContext">The context to use.</param>
 public PrgGenomeFactory(EncogProgramContext theContext)
 {
     _context = theContext;
 }
コード例 #10
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();
            }
        }
コード例 #11
0
 /// <summary>
 ///     Construct the population.
 /// </summary>
 /// <param name="theContext">The context.</param>
 /// <param name="thePopulationSize">The population size.</param>
 public PrgPopulation(EncogProgramContext theContext,
                      int thePopulationSize)
     : base(thePopulationSize, new PrgGenomeFactory(theContext))
 {
     _context = theContext;
 }
コード例 #12
0
 /// <summary>
 ///     Construct the population.
 /// </summary>
 /// <param name="theContext">The context.</param>
 /// <param name="thePopulationSize">The population size.</param>
 public PrgPopulation(EncogProgramContext theContext,
                      int thePopulationSize)
     : base(thePopulationSize, new PrgGenomeFactory(theContext))
 {
     _context = theContext;
 }
コード例 #13
0
 /// <summary>
 ///     Construct the subtree mutation object.
 /// </summary>
 /// <param name="theContext">The program context.</param>
 /// <param name="theMaxDepth">The maximum depth.</param>
 public SubtreeMutation(EncogProgramContext theContext,
                        int theMaxDepth)
 {
     Generator = new PrgGrowGenerator(theContext, theMaxDepth);
     _maxDepth = theMaxDepth;
 }
コード例 #14
0
 /// <summary>
 ///     Construct a factory.
 /// </summary>
 /// <param name="theContext">The context to use.</param>
 public PrgGenomeFactory(EncogProgramContext theContext)
 {
     _context = theContext;
 }
コード例 #15
0
 /// <summary>
 ///     Add the opcodes for string operations to a context.
 /// </summary>
 /// <param name="context">
 ///     <The context to add the opcodes to./ param>
 public static void CreateStringFunctions(EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_LENGTH);
     factory.AddExtension(EXTENSION_FORMAT);
     factory.AddExtension(EXTENSION_LEFT);
     factory.AddExtension(EXTENSION_RIGHT);
 }
コード例 #16
0
 /// <summary>
 ///     Add the opcodes for numeric operations to a context, do not use protected
 ///     division.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateNumericOperators(EncogProgramContext context)
 {
     CreateNumericOperators(context, false);
 }
コード例 #17
0
 /// <summary>
 ///     Add the opcodes for trig functions operations to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateTrigFunctions(EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_ACOS);
     factory.AddExtension(EXTENSION_ASIN);
     factory.AddExtension(EXTENSION_ATAN);
     factory.AddExtension(EXTENSION_ATAN2);
     factory.AddExtension(EXTENSION_COS);
     factory.AddExtension(EXTENSION_COSH);
     factory.AddExtension(EXTENSION_SIN);
     factory.AddExtension(EXTENSION_SINH);
     factory.AddExtension(EXTENSION_TAN);
     factory.AddExtension(EXTENSION_TANH);
 }
コード例 #18
0
 /// <summary>
 ///     Add all known opcodes to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateAll(EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     foreach (IProgramExtensionTemplate temp in EncogOpcodeRegistry.Instance
                                                                   .FindAllOpcodes())
     {
         factory.AddExtension(temp);
     }
 }
コード例 #19
0
 /// <summary>
 ///     Construct a const mutator.
 /// </summary>
 /// <param name="theContext">The program context.</param>
 /// <param name="theFrequency">The frequency of mutation.</param>
 /// <param name="theSigma">The sigma to use for mutation.</param>
 public ConstMutation(EncogProgramContext theContext,
                      double theFrequency, double theSigma)
 {
     _frequency = theFrequency;
     _sigma     = theSigma;
 }
コード例 #20
0
 /// <summary>
 ///     Add the opcodes for basic operations to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateBasicFunctions(EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_ABS);
     factory.AddExtension(EXTENSION_CEIL);
     factory.AddExtension(EXTENSION_EXP);
     factory.AddExtension(EXTENSION_FLOOR);
     factory.AddExtension(EXTENSION_LOG);
     factory.AddExtension(EXTENSION_LOG10);
     factory.AddExtension(EXTENSION_MAX);
     factory.AddExtension(EXTENSION_MIN);
     factory.AddExtension(EXTENSION_POWFN);
     factory.AddExtension(EXTENSION_RANDOM);
     factory.AddExtension(EXTENSION_ROUND);
     factory.AddExtension(EXTENSION_SQRT);
     factory.AddExtension(EXTENSION_CLAMP);
 }
コード例 #21
0
 /// <summary>
 ///     Construct the grow generator.
 /// </summary>
 /// <param name="theContext">The program context.</param>
 /// <param name="theMaxDepth">The max depth.</param>
 public PrgGrowGenerator(EncogProgramContext theContext,
                         int theMaxDepth)
     : base(theContext, theMaxDepth)
 {
 }
コード例 #22
0
 /// <summary>
 ///     Add the opcodes for boolean operations to a context.
 /// </summary>
 /// <param name="context">The context to add the opcodes to.</param>
 public static void CreateBooleanOperators(EncogProgramContext context)
 {
     FunctionFactory factory = context.Functions;
     factory.AddExtension(EXTENSION_AND);
     factory.AddExtension(EXTENSION_OR);
     factory.AddExtension(EXTENSION_EQUAL);
     factory.AddExtension(EXTENSION_LT);
     factory.AddExtension(EXTENSION_GT);
     factory.AddExtension(EXTENSION_LTE);
     factory.AddExtension(EXTENSION_GTE);
     factory.AddExtension(EXTENSION_IFF);
     factory.AddExtension(EXTENSION_NOT_EQUAL);
     factory.AddExtension(EXTENSION_NOT);
 }
コード例 #23
0
        /// <summary>
        ///     Find all opcodes that match the search criteria.
        /// </summary>
        /// <param name="types">The return types to consider.</param>
        /// <param name="context">The program context.</param>
        /// <param name="includeTerminal">True, to include the terminals.</param>
        /// <param name="includeFunction">True, to include the functions.</param>
        /// <returns>The opcodes found.</returns>
        public IList<IProgramExtensionTemplate> FindOpcodes(
            IList<EPLValueType> types, EncogProgramContext context,
            bool includeTerminal, bool includeFunction)
        {
            IList<IProgramExtensionTemplate> result = new List<IProgramExtensionTemplate>();

            foreach (IProgramExtensionTemplate temp in _opcodes)
            {
                foreach (EPLValueType rtn in types)
                {
                    // it is a possible return type, but given our variables, is it
                    // possible
                    if (temp.IsPossibleReturnType(context, rtn))
                    {
                        if (temp.ChildNodeCount == 0 && includeTerminal)
                        {
                            result.Add(temp);
                        }
                        else if (includeFunction)
                        {
                            result.Add(temp);
                        }
                    }
                }
            }

            return result;
        }
コード例 #24
0
 /// <summary>
 ///     Construct the subtree mutation object.
 /// </summary>
 /// <param name="theContext">The program context.</param>
 /// <param name="theMaxDepth">The maximum depth.</param>
 public SubtreeMutation(EncogProgramContext theContext,
                        int theMaxDepth)
 {
     Generator = new PrgGrowGenerator(theContext, theMaxDepth);
     _maxDepth = theMaxDepth;
 }