Beispiel #1
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;
            }
        }
Beispiel #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
            {
                int sum = 0;

                bool[] genome = ((BitVectorIndividual)ind).genome;

                // extract the rule
                int[] rule = _ca.GetRule();
                for (int i = 0; i < 128; i++)
                {
                    rule[i] = (genome[i] ? 1 : 0);
                }
                _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, false);
                ind.Evaluated = true;
            }
        }