Jenetics:我如何在每一步获取进化结果(或统计数据)?

时间:2018-03-22 11:49:48

标签: java frameworks genetic-algorithm genetic-programming

我正在研究基于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();

目前,我正在推动一个进化周期的结束(通过人口一次迭代)向我的UI发送消息

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”是类属性。

1 个答案:

答案 0 :(得分:2)

您可以使用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方法更新课程,则会获得一些额外的统计信息。