Пример #1
0
        /**
         * This is an ugly hack to simulate the "Sensible Initialization",
         * First we create a GPIndividual, then reverse-map it to GEIndividuals,
         * We do not need to call IntegerVectorSpecies.newIndividual() since it is overriden
         * by the GPSpecies.newIndividual();
         *
         * Moreover, as in the case for non-identical representations (i,e, GP-GE island
         * models etc,), the grammar rules, tree constraints, ERC's etc, are supposed to be
         * identical across all islands, so we are using the same "gpspecies" inside this class.
         *
         * However, the identicality of the GPTree particulars like grammar, constraints, ADFs,
         * ERC's may not be universally true.
         */
        public override Individual NewIndividual(IEvolutionState state, int thread)
        {
            GEIndividual gei = null;

            if (InitScheme != null && InitScheme.Equals("sensible"))
            {
                GPIndividual gpi = (GPIndividual)GPSpecies.NewIndividual(state, thread);
                gei = ReverseMap(state, gpi, thread);
            }
            else
            {
                gei         = (GEIndividual)base.NewIndividual(state, thread);
                gei.Species = this;
            }
            return(gei);
        }
Пример #2
0
        public override void Setup(IEvolutionState state, IParameter paramBase)
        {
            base.Setup(state, paramBase);

            var def = DefaultBase;

            var p = paramBase.Push(P_GPSPECIES);

            GPSpecies = (GPSpecies)(state.Parameters.GetInstanceForParameterEq(p, def.Push(P_GPSPECIES), typeof(GPSpecies)));
            GPSpecies.Setup(state, p);

            // check to make sure that our individual prototype is a GPIndividual
            if (!(I_Prototype is IntegerVectorIndividual))
            {
                state.Output.Fatal("The Individual class for the Species " + GetType().Name + " must be a subclass of ge.GEIndividual.", paramBase);
            }

            ERCBank = Hashtable.Synchronized(new Hashtable());

            // load the grammars, one per ADF tree
            var gpi         = (GPIndividual)(GPSpecies.I_Prototype);
            var trees       = gpi.Trees;
            var numGrammars = trees.Length;

            ParserPrototype = (GrammarParser)state.Parameters.GetInstanceForParameterEq(
                paramBase.Push(P_PARSER),
                def.Push(P_PARSER),
                typeof(GrammarParser));

            Grammar = new GrammarRuleNode[numGrammars];
            for (var i = 0; i < numGrammars; i++)
            {
                p   = paramBase.Push(P_FILE);
                def = DefaultBase;

                //var grammarFile = state.Parameters.GetFile(p, def.Push(P_FILE).Push("" + i));
                Stream grammarFile = state.Parameters.GetResource(p, def.Push(P_FILE).Push("" + i));

                if (grammarFile == null)
                {
                    state.Output.Fatal("Error retrieving grammar file(s): " + def + "." + P_FILE + "." + i + " is undefined.");
                }

                GPFunctionSet gpfs = trees[i].Constraints((GPInitializer)state.Initializer).FunctionSet;
                // now we need different parser object for each of the grammars,
                // why? see GrammarParser.java for details -- khaled
                GrammarParser[i] = (GrammarParser)ParserPrototype.Clone();
                StreamReader reader = new StreamReader(grammarFile);
                Grammar[i] = GrammarParser[i].ParseRules(state, reader, gpfs);

                // Enumerate the grammar tree -- khaled
                GrammarParser[i].EnumerateGrammarTree(Grammar[i]);
                // Generate the predictive parse table -- khaled
                GrammarParser[i].PopulatePredictiveParseTable(Grammar[i]);

                try
                {
                    reader.Close();
                }
                catch (IOException e)
                {
                    // do nothing
                }
            }
            // get the initialization scheme -- khaled
            InitScheme = state.Parameters.GetString(paramBase.Push(P_INITSCHEME), def.Push(P_INITSCHEME));
            if (InitScheme != null && InitScheme.Equals("sensible"))
            {
                state.Output.WarnOnce("Using a \"hacked\" version of \"sensible initialization\"");
            }
            else
            {
                state.Output.WarnOnce("Using default GE initialization scheme");
            }

            // setup the "passes" parameters
            int MAXIMUM_PASSES = 1024;

            Passes = state.Parameters.GetInt(paramBase.Push(P_PASSES), def.Push(P_PASSES), 1);
            if (Passes < 1 || Passes > MAXIMUM_PASSES)
            {
                state.Output.Fatal("Number of allowed passes must be >= 1 and <="
                                   + MAXIMUM_PASSES + ", likely small, such as <= 16.",
                                   paramBase.Push(P_PASSES), def.Push(P_PASSES));
            }
            int oldpasses = Passes;

            Passes = NextPowerOfTwo(Passes);
            if (oldpasses != Passes)
            {
                state.Output.Warning("Number of allowed passes must be a power of 2.  Bumping from "
                                     + oldpasses + " to " + Passes, paramBase.Push(P_PASSES), def.Push(P_PASSES));
            }
        }