ParallelFunctionEvaluator implements FunctionEvaluator { public synchronized void evaluate(Population[] populations, Function function) { this.barrier.reset(); int totalIndividuals = PopulationUtils.sumSizes(populations); int individualsPerThread = totalIndividuals / numProcessors; PositionDescriptor from = new PositionDescriptor(0, 0); PositionDescriptor toExclusive; if (individualsPerThread > 0){ int currentIndividualExclusive = individualsPerThread; for (int i = 0; i < numProcessors - 1; i++){ toExclusive = getIndexForIndividual(populations, currentIndividualExclusive); new Thread(new EvaluatorThread(this.barrier, function, populations, from, toExclusive)).start(); from = getIndexForIndividual(populations, currentIndividualExclusive); currentIndividualExclusive += individualsPerThread; } } toExclusive = new PositionDescriptor(populations.length - 1, populations[populations.length-1].getIndividuals().length); new EvaluatorThread(this.barrier, function, populations, from, toExclusive).run(); } ParallelFunctionEvaluator();  ParallelFunctionEvaluator(int numThreads); synchronized void evaluate(Population[] populations, Function function);  }
@Test public void testEvaluate1() throws Exception { DeJongParabolaFunction function = new DeJongParabolaFunction(); function.setDimension(2); function.setOffset(0.0); function.setInverted(false); SimpleIndividual ind1 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, 2.0})); SimpleIndividual ind2 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-2.0, 2.0})); SimpleIndividual ind3 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-3.0, -2.0})); SimpleIndividual ind4 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, -3.0})); ind1.setValue(0.0); ind2.setValue(0.0); ind3.setValue(0.0); ind4.setValue(0.0); Individual[] individuals = new Individual[]{ind1, ind2, ind3, ind4}; Population[] populations = new Population[]{new SimplePopulation(individuals)}; ParallelFunctionEvaluator evaluator = new ParallelFunctionEvaluator(1); evaluator.evaluate(populations, function); assertEquals(5.0, ind1.getValue(), 0.0); assertEquals(8.0, ind2.getValue(), 0.0); assertEquals(13.0, ind3.getValue(), 0.0); assertEquals(10.0, ind4.getValue(), 0.0); }
@Test public void testEvaluate2() throws Exception { DeJongParabolaFunction function = new DeJongParabolaFunction(); function.setDimension(2); function.setOffset(0.0); function.setInverted(false); SimpleIndividual ind1 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, 2.0})); SimpleIndividual ind2 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-2.0, 2.0})); SimpleIndividual ind3 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-3.0, -2.0})); SimpleIndividual ind4 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, -3.0})); ind1.setValue(0.0); ind2.setValue(0.0); ind3.setValue(0.0); ind4.setValue(0.0); Individual[] individuals = new Individual[]{ind1, ind2, ind3, ind4}; Population[] populations = new Population[]{new SimplePopulation(individuals)}; ParallelFunctionEvaluator evaluator = new ParallelFunctionEvaluator(2); evaluator.evaluate(populations, function); assertEquals(5.0, ind1.getValue(), 0.0); assertEquals(8.0, ind2.getValue(), 0.0); assertEquals(13.0, ind3.getValue(), 0.0); assertEquals(10.0, ind4.getValue(), 0.0); }
@Test public void testEvaluate3() throws Exception { DeJongParabolaFunction function = new DeJongParabolaFunction(); Function functionWrapper = new FunctionWrapper(function, 6); function.setDimension(2); function.setOffset(0.0); function.setInverted(false); SimpleIndividual ind1 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, 2.0})); SimpleIndividual ind2 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-2.0, 2.0})); SimpleIndividual ind3 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{-3.0, -2.0})); SimpleIndividual ind4 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{1.0, -3.0})); SimpleIndividual ind5 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{2.0, -1.0})); SimpleIndividual ind6 = new SimpleIndividual(1, 0.0, new SimplePhenotypeRepresentation(new double[]{4.0, 3.0})); ind1.setValue(0.0); ind2.setValue(0.0); ind3.setValue(0.0); ind4.setValue(0.0); ind5.setValue(0.0); ind6.setValue(0.0); Individual[] individuals1 = new Individual[]{ind1, ind2}; Individual[] individuals2 = new Individual[]{ind3}; Individual[] individuals3 = new Individual[]{ind4, ind5, ind6}; Population[] populations = new Population[]{new SimplePopulation(individuals1), new SimplePopulation(individuals2), new SimplePopulation(individuals3)}; ParallelFunctionEvaluator evaluator = new ParallelFunctionEvaluator(5); evaluator.evaluate(populations, functionWrapper); assertEquals(5.0, ind1.getValue(), 0.0); assertEquals(8.0, ind2.getValue(), 0.0); assertEquals(13.0, ind3.getValue(), 0.0); assertEquals(10.0, ind4.getValue(), 0.0); assertEquals(5.0, ind5.getValue(), 0.0); assertEquals(25.0, ind6.getValue(), 0.0); }