我正在开发基于Jenetics框架(link to jenetics.io)的优化引擎。我有一个演化引擎定义,如下所示:
final Thread engineStream = new Thread(() -> {
final MinMax<EvolutionResult<DoubleGene, Double>> best = MinMax.of();
_scannerEngine.stream()
.limit(byPopulationConvergence(convergenceCriterion))
.limit(result -> !Thread.currentThread().isInterrupted())
.limit(maxGenerations)
.peek(best).forEach(evolutionResult -> {
waiting();
if (callback != null) {
callback.accept(evolutionResult, best.getMax());
}
final String generationCounter = String.valueOf(evolutionResult.getGeneration());
_callingInstance.doOnAlgorithmCallback(new String("****** finished generation: " + generationCounter + " ****"));
});
}
engineStream.start();
目前,我正在将一个演化周期(遍及整个种群的一个迭代)结束时通过以下方式向我的用户界面发送一条消息:
peek(best).forEach(
...
_callingInstance.doOnAlgorithmCallback(...)
)
我想做的是在执行完每个适应度步骤后,推动进化的当前状态(理想情况下是包含当前适应度平均值,最小值,最大值等的一些统计信息),以便用户可以看到优化过程的当前状态。 。
任何想法?
基于FranzWilhelmstötter的建议的最终实现:
final Thread engineStream = new Thread(() -> {
final MinMax<EvolutionResult<DoubleGene, Double>> best = MinMax.of();
try {
_scannerEngine.stream() //
// stop stream if fitness average across the population is smaller than % of best fitness
.limit(byFitnessConvergence(5, 20, convergence%)) //
.limit(interrupted -> !Thread.currentThread().isInterrupted()) //
.limit(_geneticScanParameters.getMaxGenerationsLimit()) // maximum number of generations
.peek(best) //
.peek(_statistics) //
.forEach(evolutionResult -> {
waiting();
if (callback != null) {
MessageLogger.logError(getClass(), best.getMax().toString());
if (_geneticScanParameters.getOptimizationStrategy() == Optimize.MAXIMUM) {
callback.accept(evolutionResult, best.getMax());
} else if (_geneticScanParameters.getOptimizationStrategy() == Optimize.MINIMUM) {
callback.accept(evolutionResult, best.getMin());
}
}
MessageLogger.log(getClass(), _statistics.toString());
final String generationCounter = String.valueOf(evolutionResult.getGeneration());
_callingInstance.doOnAlgorithmCallback(new String("****** finished generation: " + generationCounter + " ****"));
});
MessageLogger.log(getClass(), "\n" + _statistics.toString());
} catch (final Exception ex) {
MessageLogger.logError(getClass(), Thread.currentThread(), ex);
}
_callingInstance.doOnAlgorithmFinished();
});
engineStream.start();
_engineStream = engineStream;
请注意,我使用一种约定,其中“ _variable”是类属性。
最佳答案
为此,可以使用EvolutionStatistics类。
final Engine<DoubleGene, Double> engine = ...
final EvolutionStatistics<Double, DoubleMomentStatistics> statistics =
EvolutionStatistics.ofNumber();
final Phenotype<DoubleGene, Double> result = engine.stream()
.limit(bySteadyFitness(7))
.limit(100)
.peek(statistics)
.collect(toBestPhenotype());
System.println(statistics);
如果使用
peek
方法更新该类,则将获得一些其他统计信息。