Beispiel #1
0
        /// <exception cref="System.Exception"/>
        protected internal override void InitFromParameters()
        {
            base.InitFromParameters();
            _effort = 0;
            string cases      = GetParam("test-cases", true);
            string casesClass = GetParam("test-case-class", true);

            if (cases == null && casesClass == null)
            {
                throw new Exception("No acceptable test-case parameter.");
            }
            if (casesClass != null)
            {
                // Get test cases from the TestCasesClass.
                Type   iclass  = Type.GetType(casesClass);
                object iObject = System.Activator.CreateInstance(iclass);
                if (!(iObject is TestCaseGenerator))
                {
                    throw (new Exception("test-case-class must inherit from class TestCaseGenerator"));
                }
                TestCaseGenerator testCaseGenerator = (TestCaseGenerator)iObject;
                int numTestCases = testCaseGenerator.TestCaseCount();
                for (int i = 0; i < numTestCases; i++)
                {
                    ObjectPair testCase = testCaseGenerator.TestCase(i);
                    float      @in      = (float)testCase._first;
                    float      @out     = (float)testCase._second;
                    Print(";; Fitness case #" + i + " input: " + @in + " output: " + @out + "\n");
                    _testCases.Add(new GATestCase(@in, @out));
                }
            }
            else
            {
                // Get test cases from test-cases.
                Program caselist = new Program(cases);
                for (int i = 0; i < caselist.Size(); i++)
                {
                    Program p = (Program)caselist.DeepPeek(i);
                    if (p.Size() < 2)
                    {
                        throw new Exception("Not enough elements for fitness case \"" + p + "\"");
                    }
                    float @in  = System.Convert.ToSingle(p.DeepPeek(0).ToString());
                    float @out = System.Convert.ToSingle(p.DeepPeek(1).ToString());
                    Print(";; Fitness case #" + i + " input: " + @in + " output: " + @out + "\n");
                    _testCases.Add(new GATestCase(@in, @out));
                }
            }
            // Create and initialize predictors
            _predictorEffortPercent = GetFloatParam("PREDICTOR-effort-percent", true);
            _predictorGA            = PredictionGA.PredictionGAWithParameters(this, GetPredictorParameters(_parameters));
        }
Beispiel #2
0
        // Note: Oldest trainer has the lowest index; newest trainer has the highest
        // index.
        // The solution population and genetic algorithm.
        /// <summary>
        /// Customizes GA.GAWithParameters to allow the inclusion of the solution GA,
        /// which is required for the initialization of the prediction GA.
        /// </summary>
        /// <param name="ceFloatSymbolicRegression"/>
        /// <param name="getPredictorParameters"/>
        /// <returns/>
        /// <exception cref="System.Exception"></exception>
        public static PredictionGA PredictionGAWithParameters(PushGP inSolutionGA, Dictionary <string, string> inParams)
        {
            Type   cls      = Type.GetType(inParams["problem-class"]);
            object gaObject = System.Activator.CreateInstance(cls);

            if (!(gaObject is PredictionGA))
            {
                throw (new Exception("Predictor problem-class must inherit from" + " class PredictorGA"));
            }
            PredictionGA ga = (PredictionGA)gaObject;

            // Must set the solution GA before InitFromParameters, since the latter
            // uses _solutionGA while creating the predictor population.
            ga.SetSolutionGA(inSolutionGA);
            ga.SetParams(inParams);
            ga.InitFromParameters();
            return(ga);
        }