简体   繁体   English

Jenetics ,如何使用 GA 找到集合的子集

[英]Jenetics , how to find subset of set using GA

I am trying to find best subset of set.我试图找到集合的最佳子集。 Imagine that we need to find subset of objects.想象一下,我们需要找到对象的子集。 We have got some fitness function for this subset.我们已经得到了这个子集的一些适应度函数。 So at the beginning we should make a population of subsets and then using GA we should try to find the best subset.所以一开始我们应该做一个子集的总体,然后使用GA我们应该尝试找到最好的子集。

I would like to use Jenetics.io but I do not know how to use it in this case.我想使用 Jenetics.io,但我不知道在这种情况下如何使用它。 Problem for me is that chromosomes are much different data structure than subset.对我来说问题是染色体与子集的数据结构大不相同。

I would like to have a function( population, fitness function) which makes all needed job.我想要一个函数(人口,适应度函数)来完成所有需要的工作。

I tried to understand how Jenetics exactly works.我试图了解 Jenetics 究竟是如何运作的。 Maybe I am wrong but I think there is no way to make it works the way I want.也许我错了,但我认为没有办法让它按照我想要的方式工作。

Please give me advice , maybe there is option to use Jenetics in this case?请给我建议,也许在这种情况下可以选择使用 Jenetics?

There is a sub-set example in the Jenetics library. Jenetics库中有一个子集示例。 Essentially, it has the following form:本质上,它具有以下形式:

class SubsetExample
    implements Problem<ISeq<MyObject>, EnumGene<MyObject>, Double>
{
    // Define your basic set here.
    private final ISeq<MyObject> basicSet = ISeq.empty();
    private final int subSetSize = 5;

    @Override
    public Function<ISeq<MyObject>, Double> fitness() {
        return subSet -> {
            assert(subset.size() == subSetSize);
            double fitness = 0;
            for (MyObject obj : subSet) {
                // Do some fitness calculation
            }

            return fitness;
        };
    }

    @Override
    public Codec<ISeq<MyObject>, EnumGene<MyObject>> codec() {
        return codecs.ofSubSet(basicSet, subSetSize);
    }

    public static void main(final String[] args) {
        final SubsetExample problem = new SubsetExample()

        final Engine<EnumGene<MyObject>, Double> engine = Engine.builder(problem)
            .minimizing()
            .maximalPhenotypeAge(5)
            .alterers(
                new PartiallyMatchedCrossover<>(0.4),
                new Mutator<>(0.3))
            .build();

        final Phenotype<EnumGene<MyObject>, Double> result = engine.stream()
            .limit(limit.bySteadyFitness(55))
            .collect(EvolutionResult.toBestPhenotype());

        System.out.print(result);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM