Exemple #1
0
        public override void Describe(
            IEvolutionState state,
            Individual ind,
            int subpopulation,
            int threadnum,
            int log)
        {
            if (_ca == null)
            {
                _ca = new CA(CA_WIDTH, NEIGHBORHOOD);
            }

            int[] trial = new int[CA_WIDTH];

            MajorityData input = (MajorityData)Input;

            // extract the rule
            ((GPIndividual)ind).Trees[0].Child.Eval(
                state, threadnum, input, Stack, (GPIndividual)ind, this);

            int[] rule = _ca.GetRule();
            for (int i = 0; i < 64; i++)
            {
                rule[i] = (int)((input.Data0 >> i) & 0x1);
            }
            for (int i = 64; i < 128; i++)
            {
                rule[i] = (int)((input.Data1 >> (i - 64)) & 0x1);
            }
            _ca.SetRule(rule); // for good measure though it doesn't matter

            // print rule
            var s = "Rule: ";

            foreach (int r in rule)
            {
                s += r;
            }
            state.Output.PrintLn(s, log);

            double sum = 0;

            for (var i = 0; i < NUM_TESTS; i++)
            {
                // set up and run the CA
                int result = MakeTrial(state, threadnum, trial, RANDOM) ? 1 : 0;
                _ca.SetVals(trial);
                _ca.Step(STEPS, true);

                // extract the fitness
                if (All(_ca.GetVals(), result))
                {
                    sum++;
                }
            }

            _density = (sum / NUM_TESTS);
            state.Output.PrintLn("Generalization Accuracy: " + _density, 1); // stderr
            state.Output.PrintLn("Generalization Accuracy: " + _density, log);
        }
Exemple #2
0
        public void Evaluate(IEvolutionState state,
                             Individual ind,
                             int subpopulation,
                             int threadnum)
        {
            if (_ca == null)
            {
                _ca = new CA(CA_WIDTH, NEIGHBORHOOD);
            }

            // we always reevaluate
            //if (!ind.evaluated)  // don't bother reevaluating
            {
                MajorityData input = (MajorityData)(this.Input);

                int sum = 0;

                // extract the rule
                ((GPIndividual)ind).Trees[0].Child.Eval(
                    state, threadnum, input, Stack, (GPIndividual)ind, this);

                int[] rule = _ca.GetRule();
                for (int i = 0; i < 64; i++)
                {
                    rule[i] = (int)(((input.Data0) >> i) & 0x1);
                }
                for (int i = 64; i < 128; i++)
                {
                    rule[i] = (int)(((input.Data1) >> (i - 64)) & 0x1);
                }
                _ca.SetRule(rule); // for good measure though it doesn't matter


                for (int i = 0; i < NUM_TRIALS; i++)
                {
                    // set up and run the CA
                    _ca.SetVals(_trials[i]);
                    _ca.Step(STEPS, true);

                    // extract the fitness
                    if (All(_ca.GetVals(), _majorities[i]))
                    {
                        sum++;
                    }
                }

                SimpleFitness f = (SimpleFitness)ind.Fitness;
                f.SetFitness(state, sum / (double)NUM_TRIALS, sum == NUM_TRIALS);
                ind.Evaluated = true;
            }
        }