From: Robert Baruch <[log in to unmask]> To: [log in to unmask] Subject: Re: Two problems with GP coevolve, subpops > 1 Date: Wed, 23 Aug 2006 20:30:39 +0000 Content-Type: Multipart/mixed; boundary="NextPart_Webmail_9m3u9jl4l_11916_1156442977_2" MIME-Version: 1.0 --NextPart_Webmail_9m3u9jl4l_11916_1156442977_2 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0644; name=RandomOpponentEvaluator.java Content-Disposition: attachment; filename=RandomOpponentEvaluator.java package mille; import ec.*; import ec.util.*; import ec.coevolve.*; public class RandomOpponentEvaluator extends Evaluator { public static final String size = "group-size"; public int groupSize; public void setup(final EvolutionState state, final Parameter base) { super.setup(state, base); groupSize = state.parameters.getInt(base.push(size), null, 1); if (groupSize < 1) { state.output.fatal("Incorrect value for parameter", base.push(size)); } } public boolean runComplete(final EvolutionState state) { return false; } public void randomizeOrder(final EvolutionState state, final Individual[] individuals) { // copy the inds into a new array, then dump them randomly into the // subpopulation again Individual[] queue = new Individual[individuals.length]; int len = queue.length; System.arraycopy(individuals, 0, queue, 0, len); for (int x = len; x > 0; x--) { int i = state.random[0].nextInt(x); individuals[x - 1] = queue[i]; // get rid of queue[i] by swapping the highest guy there and then // decreasing the highest value :-) queue[i] = queue[x - 1]; } } public void evaluatePopulation(final EvolutionState state) { int numinds[] = new int[state.evalthreads]; int from[] = new int[state.evalthreads]; GroupedProblemForm prob = (GroupedProblemForm) (p_problem.clone()); prob.preprocessPopulation(state, state.population); for (int subpop = 0; subpop < state.population.subpops.length; subpop++) { for (int y = 0; y < state.evalthreads; y++) { // figure numinds if (y < state.evalthreads - 1) // not last one numinds[y] = state.population.subpops[subpop].individuals.length / state.evalthreads; else numinds[y] = state.population.subpops[subpop].individuals.length / state.evalthreads + (state.population.subpops[subpop].individuals.length - (state.population.subpops[subpop].individuals.length / state.evalthreads) * state.evalthreads); // figure from from[y] = (state.population.subpops[subpop].individuals.length / state.evalthreads) * y; } randomizeOrder(state, state.population.subpops[subpop].individuals); evalNRandomOneWay(state, subpop, from, numinds, state.population.subpops[subpop].individuals, prob); } prob.postprocessPopulation(state, state.population); } public void evalNRandomOneWay(final EvolutionState state, int subpop, int[] from, int[] numinds, final Individual[] individuals, final GroupedProblemForm prob) { if (state.evalthreads == 1) evalNRandomOneWayPopChunk(state, subpop, from[0], numinds[0], 0, individuals, prob); else { Thread[] t = new Thread[state.evalthreads]; // start up the threads for (int y = 0; y < state.evalthreads; y++) { CompetitiveEvaluatorThread r = new NRandomOneWayCompetitiveEvaluatorThread(); r.threadnum = y; r.subpop = subpop; r.numinds = numinds[y]; r.from = from[y]; r.me = this; r.state = state; r.p = prob; r.inds = individuals; t[y] = new Thread(r); t[y].start(); } // gather the threads for (int y = 0; y < state.evalthreads; y++)try { t[y].join(); } catch (InterruptedException e) { state.output.fatal( "Whoa! The main evaluation thread got interrupted! Dying..."); } } } public void evalNRandomOneWayPopChunk(final EvolutionState state, int subpop, int from, int numinds, int threadnum, final Individual[] individuals, final GroupedProblemForm prob) { Individual[] queue = new Individual[individuals.length]; int len = queue.length; System.arraycopy(individuals, 0, queue, 0, len); Individual[] competition = new Individual[2]; boolean[] updates = new boolean[2]; updates[0] = true; updates[1] = false; int upperBound = from + numinds; for (int x = from; x < upperBound; x++) { competition[0] = individuals[x]; for (int y = 0; y < groupSize; y++) { int opponent_subpop = state.random[0].nextInt(state.population.subpops.length - 1); if (opponent_subpop >= subpop) opponent_subpop ++; int opponent = state.random[0].nextInt(state.population.subpops[opponent_subpop]. individuals.length); competition[1] = state.population.subpops[opponent_subpop].individuals[opponent]; prob.evaluate(state, competition, updates, false, 0); } } } abstract class CompetitiveEvaluatorThread implements Runnable { public int numinds; public int subpop; public int from; public RandomOpponentEvaluator me; public EvolutionState state; public int threadnum; public GroupedProblemForm p; public Individual[] inds; } class NRandomOneWayCompetitiveEvaluatorThread extends CompetitiveEvaluatorThread { public synchronized void run() { me.evalNRandomOneWayPopChunk(state, subpop, from, numinds, threadnum, inds, p); } } } --NextPart_Webmail_9m3u9jl4l_11916_1156442977_2--