private void setupOptimizer(int poolSize) { ep = new EvolutionaryProcess<>(threadCount, poolSize, seed); }
/** * Creates an evolutionary optimization framework with specified threadiness, * population size and initial state. * @param threadCount How many threads to use in parallelDo * @param populationSize How large a population to use * @param seed An initial population member */ public EvolutionaryProcess(int threadCount, int populationSize, State<T, U> seed) { this.populationSize = populationSize; setThreadCount(threadCount); initializePopulation(populationSize, seed); }
@Test public void testConverges() throws Exception { State<Foo, Double> s0 = new State<Foo, Double>(new double[5], 1); s0.setPayload(new Foo()); EvolutionaryProcess<Foo, Double> ep = new EvolutionaryProcess<Foo, Double>(10, 100, s0); State<Foo, Double> best = null; for (int i = 0; i < 20; i++) { best = ep.parallelDo(new EvolutionaryProcess.Function<Payload<Double>>() { @Override public double apply(Payload<Double> payload, double[] params) { int i = 1; double sum = 0; for (double x : params) { sum += i * (x - i) * (x - i); i++; } return -sum; } }); ep.mutatePopulation(3); System.out.printf("%10.3f %.3f\n", best.getValue(), best.getOmni()); } ep.close(); assertNotNull(best); assertEquals(0.0, best.getValue(), 0.02); }
private void trainWithBufferedExamples() { try { this.best = ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> z, double[] params) { ep.mutatePopulation(SURVIVORS); for (State<Wrapper, CrossFoldLearner> state : ep.getPopulation().subList(0, SURVIVORS)) { Wrapper.freeze(state);
@Override public void close() { trainWithBufferedExamples(); try { ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> payload, double[] params) { CrossFoldLearner learner = ((Wrapper) payload).getLearner(); learner.close(); return learner.logLikelihood(); } }); } catch (InterruptedException e) { log.warn("Ignoring exception", e); } catch (ExecutionException e) { throw new IllegalStateException(e); } finally { ep.close(); } }
ep = new EvolutionaryProcess<Wrapper, CrossFoldLearner>(); ep.readFields(in);
@Override public void readFields(DataInput input) throws IOException { setThreadCount(input.readInt()); int n = input.readInt(); population = new ArrayList<>(); for (int i = 0; i < n; i++) { State<T, U> state = (State<T, U>) PolymorphicWritable.read(input, State.class); population.add(state); } } }
@Override public void write(DataOutput out) throws IOException { out.writeInt(record); out.writeInt(cutoff); out.writeInt(minInterval); out.writeInt(maxInterval); out.writeInt(currentStep); out.writeInt(bufferSize); out.writeInt(buffer.size()); for (TrainingExample example : buffer) { example.write(out); } ep.write(out); best.write(out); out.writeInt(threadCount); out.writeInt(poolSize); seed.write(out); out.writeInt(numFeatures); out.writeBoolean(freezeSurvivors); }
private void trainWithBufferedExamples() { try { this.best = ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> z, double[] params) { ep.mutatePopulation(SURVIVORS); for (State<Wrapper, CrossFoldLearner> state : ep.getPopulation().subList(0, SURVIVORS)) { Wrapper.freeze(state);
@Override public void close() { trainWithBufferedExamples(); try { ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> payload, double[] params) { CrossFoldLearner learner = ((Wrapper) payload).getLearner(); learner.close(); return learner.logLikelihood(); } }); } catch (InterruptedException e) { log.warn("Ignoring exception", e); } catch (ExecutionException e) { throw new IllegalStateException(e); } finally { ep.close(); } }
ep = new EvolutionaryProcess<>(); ep.readFields(in);
@Override public void readFields(DataInput input) throws IOException { setThreadCount(input.readInt()); int n = input.readInt(); population = Lists.newArrayList(); for (int i = 0; i < n; i++) { State<T, U> state = (State<T, U>) PolymorphicWritable.read(input, State.class); population.add(state); } } }
@Override public void write(DataOutput out) throws IOException { out.writeInt(record); out.writeInt(cutoff); out.writeInt(minInterval); out.writeInt(maxInterval); out.writeInt(currentStep); out.writeInt(bufferSize); out.writeInt(buffer.size()); for (TrainingExample example : buffer) { example.write(out); } ep.write(out); best.write(out); out.writeInt(threadCount); out.writeInt(poolSize); seed.write(out); out.writeInt(numFeatures); out.writeBoolean(freezeSurvivors); }
private void trainWithBufferedExamples() { try { this.best = ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> z, double[] params) { ep.mutatePopulation(SURVIVORS); for (State<Wrapper, CrossFoldLearner> state : ep.getPopulation().subList(0, SURVIVORS)) { Wrapper.freeze(state);
@Override public void close() { trainWithBufferedExamples(); try { ep.parallelDo(new EvolutionaryProcess.Function<Payload<CrossFoldLearner>>() { @Override public double apply(Payload<CrossFoldLearner> payload, double[] params) { CrossFoldLearner learner = ((Wrapper) payload).getLearner(); learner.close(); return learner.logLikelihood(); } }); } catch (InterruptedException e) { log.warn("Ignoring exception", e); } catch (ExecutionException e) { throw new IllegalStateException(e); } finally { ep.close(); } }
ep = new EvolutionaryProcess<Wrapper, CrossFoldLearner>(); ep.readFields(in);
/** * Creates an evolutionary optimization framework with specified threadiness, * population size and initial state. * @param threadCount How many threads to use in parallelDo * @param populationSize How large a population to use * @param seed An initial population member */ public EvolutionaryProcess(int threadCount, int populationSize, State<T, U> seed) { this.populationSize = populationSize; setThreadCount(threadCount); initializePopulation(populationSize, seed); }
private void setupOptimizer(int poolSize) { ep = new EvolutionaryProcess<Wrapper, CrossFoldLearner>(threadCount, poolSize, seed); }
@Override public void readFields(DataInput input) throws IOException { setThreadCount(input.readInt()); int n = input.readInt(); population = Lists.newArrayList(); for (int i = 0; i < n; i++) { State<T, U> state = (State<T, U>) PolymorphicWritable.read(input, State.class); population.add(state); } } }
@Override public void write(DataOutput out) throws IOException { out.writeInt(record); out.writeInt(cutoff); out.writeInt(minInterval); out.writeInt(maxInterval); out.writeInt(currentStep); out.writeInt(bufferSize); out.writeInt(buffer.size()); for (TrainingExample example : buffer) { example.write(out); } ep.write(out); best.write(out); out.writeInt(threadCount); out.writeInt(poolSize); seed.write(out); out.writeInt(numFeatures); out.writeBoolean(freezeSurvivors); }