@Test public void testProduce() { final LexicaseSelection instance = new LexicaseSelection(); instance.setup(state, BASE); state.population.subpops.get(0).individuals = getPopulation1(); final int N = 1000; int zeroCount = 0; int sixCount = 0; int otherCount = 0; for (int i = 0; i < N; i++) { final int result = instance.produce(0, state, 0); if (result == 0) zeroCount++; else if (result == 6) sixCount++; else otherCount++; } assertEquals(0, otherCount); final int expectedZeros = N/2; final int expectedSixes = N/2; final double chiSquared = (Math.pow(zeroCount - expectedZeros, 2)/expectedZeros + Math.pow(sixCount - expectedSixes, 2)/expectedSixes); assertFalse(chiSquared > 6.635); }
@Override public int produce(final int subpopulation, final EvolutionState state, final int thread) { assert(state != null); assert(subpopulation >= 0); assert(subpopulation < state.population.subpops.size()); assert(state.population.subpops.get(subpopulation) != null); assert(state.population.subpops.get(subpopulation).individuals.size() > 0); final ArrayList<Individual> pop = state.population.subpops.get(subpopulation).individuals; final ArrayList<Integer> candidates = new ArrayList<Integer>(); for (int i = 0; i < pop.size(); i++) candidates.add(i); assert(pop.get(candidates.get(0)).fitness.trials != null); final int numCases = pop.get(0).fitness.trials.size(); if (numCases == 0) state.output.fatal(String.format("Attempted to use %s on an individual with an empty list of trials.", this.getClass().getSimpleName())); final int[] caseOrder = new int[numCases]; for (int i = 0; i < numCases; i++) caseOrder[i] = i; shuffle(state, caseOrder); for (int i = 0; i < caseOrder.length; i++) { final int currentCase = caseOrder[i]; Fitness best = (Fitness) pop.get(candidates.get(0)).fitness.trials.get(currentCase); for (int j = 1; j < candidates.size(); j++) { assert(pop.get(candidates.get(j)).fitness.trials != null); assert(pop.get(candidates.get(j)).fitness.trials.size() == numCases); final Fitness caseFitness = (Fitness) pop.get(candidates.get(j)).fitness.trials.get(currentCase); if (caseFitness.betterThan(best)) best = caseFitness; } final Iterator<Integer> it = candidates.iterator(); while (it.hasNext()) { final Fitness caseFitness = (Fitness) pop.get(it.next()).fitness.trials.get(currentCase); if (caseFitness.compareTo(best) > 0) it.remove(); } if (candidates.size() == 1) { return candidates.get(0); } if (i == caseOrder.length - 1) { return candidates.get(state.random[0].nextInt(candidates.size())); } } throw new IllegalStateException(); }
LexicaseSelection extends SelectionMethod { @Override public int produce(final int subpopulation, final EvolutionState state, final int thread) { assert(state != null); assert(subpopulation >= 0); assert(subpopulation < state.population.subpops.size()); assert(state.population.subpops.get(subpopulation) != null); assert(state.population.subpops.get(subpopulation).individuals.size() > 0); final ArrayList<Individual> pop = state.population.subpops.get(subpopulation).individuals; final ArrayList<Integer> candidates = new ArrayList<Integer>(); for (int i = 0; i < pop.size(); i++) candidates.add(i); assert(pop.get(candidates.get(0)).fitness.trials != null); final int numCases = pop.get(0).fitness.trials.size(); if (numCases == 0) state.output.fatal(String.format("Attempted to use %s on an individual with an empty list of trials.", this.getClass().getSimpleName())); final int[] caseOrder = new int[numCases]; for (int i = 0; i < numCases; i++) caseOrder[i] = i; shuffle(state, caseOrder); for (int i = 0; i < caseOrder.length; i++) { final int currentCase = caseOrder[i]; Fitness best = (Fitness) pop.get(candidates.get(0)).fitness.trials.get(currentCase); for (int j = 1; j < candidates.size(); j++) { assert(pop.get(candidates.get(j)).fitness.trials != null); assert(pop.get(candidates.get(j)).fitness.trials.size() == numCases); final Fitness caseFitness = (Fitness) pop.get(candidates.get(j)).fitness.trials.get(currentCase); if (caseFitness.betterThan(best)) best = caseFitness; } final Iterator<Integer> it = candidates.iterator(); while (it.hasNext()) { final Fitness caseFitness = (Fitness) pop.get(it.next()).fitness.trials.get(currentCase); if (caseFitness.compareTo(best) > 0) it.remove(); } if (candidates.size() == 1) { return candidates.get(0); } if (i == caseOrder.length - 1) { return candidates.get(state.random[0].nextInt(candidates.size())); } } throw new IllegalStateException(); } }
LexicaseSelection extends SelectionMethod { @Override public int produce(final int subpopulation, final EvolutionState state, final int thread) { assert(state != null); assert(subpopulation >= 0); assert(subpopulation < state.population.subpops.size()); assert(state.population.subpops.get(subpopulation) != null); assert(state.population.subpops.get(subpopulation).individuals.size() > 0); final ArrayList<Individual> pop = state.population.subpops.get(subpopulation).individuals; final ArrayList<Integer> candidates = new ArrayList<Integer>(); for (int i = 0; i < pop.size(); i++) candidates.add(i); assert(pop.get(candidates.get(0)).fitness.trials != null); final int numCases = pop.get(0).fitness.trials.size(); if (numCases == 0) state.output.fatal(String.format("Attempted to use %s on an individual with an empty list of trials.", this.getClass().getSimpleName())); final int[] caseOrder = new int[numCases]; for (int i = 0; i < numCases; i++) caseOrder[i] = i; shuffle(state, caseOrder); for (int i = 0; i < caseOrder.length; i++) { final int currentCase = caseOrder[i]; Fitness best = (Fitness) pop.get(candidates.get(0)).fitness.trials.get(currentCase); for (int j = 1; j < candidates.size(); j++) { assert(pop.get(candidates.get(j)).fitness.trials != null); assert(pop.get(candidates.get(j)).fitness.trials.size() == numCases); final Fitness caseFitness = (Fitness) pop.get(candidates.get(j)).fitness.trials.get(currentCase); if (caseFitness.betterThan(best)) best = caseFitness; } final Iterator<Integer> it = candidates.iterator(); while (it.hasNext()) { final Fitness caseFitness = (Fitness) pop.get(it.next()).fitness.trials.get(currentCase); if (caseFitness.compareTo(best) > 0) it.remove(); } if (candidates.size() == 1) { return candidates.get(0); } if (i == caseOrder.length - 1) { return candidates.get(state.random[0].nextInt(candidates.size())); } } throw new IllegalStateException(); }  }
LexicaseSelection extends SelectionMethod { @Override public int produce(final int subpopulation, final EvolutionState state, final int thread) { assert(state != null); assert(subpopulation >= 0); assert(subpopulation < state.population.subpops.size()); assert(state.population.subpops.get(subpopulation) != null); assert(state.population.subpops.get(subpopulation).individuals.size() > 0); final ArrayList<Individual> pop = state.population.subpops.get(subpopulation).individuals; final ArrayList<Integer> candidates = new ArrayList<Integer>(); for (int i = 0; i < pop.size(); i++) candidates.add(i); assert(pop.get(candidates.get(0)).fitness.trials != null); final int numCases = pop.get(0).fitness.trials.size(); if (numCases == 0) state.output.fatal(String.format("Attempted to use %s on an individual with an empty list of trials.", this.getClass().getSimpleName())); final int[] caseOrder = new int[numCases]; for (int i = 0; i < numCases; i++) caseOrder[i] = i; shuffle(state, caseOrder); for (int i = 0; i < caseOrder.length; i++) { final int currentCase = caseOrder[i]; Fitness best = (Fitness) pop.get(candidates.get(0)).fitness.trials.get(currentCase); for (int j = 1; j < candidates.size(); j++) { assert(pop.get(candidates.get(j)).fitness.trials != null); assert(pop.get(candidates.get(j)).fitness.trials.size() == numCases); final Fitness caseFitness = (Fitness) pop.get(candidates.get(j)).fitness.trials.get(currentCase); if (caseFitness.betterThan(best)) best = caseFitness; } final Iterator<Integer> it = candidates.iterator(); while (it.hasNext()) { final Fitness caseFitness = (Fitness) pop.get(it.next()).fitness.trials.get(currentCase); if (caseFitness.compareTo(best) > 0) it.remove(); } if (candidates.size() == 1) { return candidates.get(0); } if (i == caseOrder.length - 1) { return candidates.get(state.random[0].nextInt(candidates.size())); } } throw new IllegalStateException(); }  @Override Parameter defaultBase(); @Override int produce(final int subpopulation, final EvolutionState state, final int thread); }
LexicaseSelection extends SelectionMethod { @Override public int produce(final int subpopulation, final EvolutionState state, final int thread) { assert(state != null); assert(subpopulation >= 0); assert(subpopulation < state.population.subpops.size()); assert(state.population.subpops.get(subpopulation) != null); assert(state.population.subpops.get(subpopulation).individuals.size() > 0); final ArrayList<Individual> pop = state.population.subpops.get(subpopulation).individuals; final ArrayList<Integer> candidates = new ArrayList<Integer>(); for (int i = 0; i < pop.size(); i++) candidates.add(i); assert(pop.get(candidates.get(0)).fitness.trials != null); final int numCases = pop.get(0).fitness.trials.size(); if (numCases == 0) state.output.fatal(String.format("Attempted to use %s on an individual with an empty list of trials.", this.getClass().getSimpleName())); final int[] caseOrder = new int[numCases]; for (int i = 0; i < numCases; i++) caseOrder[i] = i; shuffle(state, caseOrder); for (int i = 0; i < caseOrder.length; i++) { final int currentCase = caseOrder[i]; Fitness best = (Fitness) pop.get(candidates.get(0)).fitness.trials.get(currentCase); for (int j = 1; j < candidates.size(); j++) { assert(pop.get(candidates.get(j)).fitness.trials != null); assert(pop.get(candidates.get(j)).fitness.trials.size() == numCases); final Fitness caseFitness = (Fitness) pop.get(candidates.get(j)).fitness.trials.get(currentCase); if (caseFitness.betterThan(best)) best = caseFitness; } final Iterator<Integer> it = candidates.iterator(); while (it.hasNext()) { final Fitness caseFitness = (Fitness) pop.get(it.next()).fitness.trials.get(currentCase); if (caseFitness.compareTo(best) > 0) it.remove(); } if (candidates.size() == 1) { return candidates.get(0); } if (i == caseOrder.length - 1) { return candidates.get(state.random[0].nextInt(candidates.size())); } } throw new IllegalStateException(); }  @Override Parameter defaultBase(); @Override int produce(final int subpopulation, final EvolutionState state, final int thread); static final String P_LEXICASESELECT; }