我正在开发基于 Jenetics 框架的优化引擎(指向 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();
目前,我在一个进化周期(通过人口的一次迭代)结束时向我的 UI 推送一条消息
peek(best).forEach(
...
_callingInstance.doOnAlgorithmCallback(...)
)
我想做的是在执行每个适应度步骤后推送进化的当前状态(最好是一些包含当前适应度平均值、最小值、最大值等的统计数据) ,以便用户可以看到优化过程的当前状态.
任何想法?
基于 Franz Wilhelmstö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”是类属性的约定。