public void describe( final EvolutionState state, final Individual ind, final int subpopulation, final int threadnum, final int log) { if (ca == null) ca = new CA(CA_WIDTH, NEIGHBORHOOD); int[] trial = new int[CA_WIDTH]; MajorityData input = (MajorityData) (this.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 String s = "Rule: "; for (int i = 0; i < rule.length; i++) s += rule[i]; state.output.println(s, log); double sum = 0; for (int 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); }
public void evaluate( final EvolutionState state, final Individual ind, final int subpopulation, final 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; } }