猿问

使用 Jenetics 创建基因型时的限制

我正在使用Jenetics尝试多目标优化问题(MOP)。我创建的一个玩具问题是从给定的集合中选择两个子集,在每个子集都有限制的情况下最大化它们的总和。但是我想确保这两个子集是互斥的。创建两条染色体的基因型时如何设置此约束?

我用于解决玩具问题的套装是:

private static final ISeq<Integer> SET = ISeq.of( IntStream.rangeClosed( 1, 10 )
            .boxed()
            .collect( Collectors.toList() ) );

我的问题的签名是:

Problem<List<ISeq<Integer>>, BitGene, Vec<int[]>>

编解码器是:

@Override public Codec<List<ISeq<Integer>>, BitGene> codec() {

        Objects.requireNonNull( SET );

        final Genotype<BitGene> g =

                Genotype.of( BitChromosome.of( SET.length() ), BitChromosome.of( SET.length() ) );

        return Codec.of(

                g,

                gc -> gc.stream().map( z -> z.as( BitChromosome.class ).ones().mapToObj( SET )

                        .collect( ISeq.toISeq() ) ).collect( Collectors.toList() )

        );

    }

我为第一个子集指定了 9 个限制,为第二个子集指定了 4 个限制。


我预计两个染色体的初始群体具有互斥的基因,这样表型最终将产生不具有从 . 中复制的项目的个体SET。


我当前得到的示例输出是:


[[4,5], [4]]

但我希望两个人都有互斥的物品。如何通过 Jenetics 实现这一目标?


牛魔王的故事
浏览 116回答 2
2回答

LEATH

这不是问题的唯一可能的编码,因为每个问题都有其自己的特征。对于多背包问题,我会选择IntegerChromosome代替BitChromosomes。private static final ISeq<Integer> ITEMS = IntStream.rangeClosed(1, 10)&nbsp; &nbsp; .boxed()&nbsp; &nbsp; .collect(ISeq.toISeq());public Codec<ISeq<List<Integer>>, IntegerGene> codec(final int knapsackCount) {&nbsp; &nbsp; return Codec.of(&nbsp; &nbsp; &nbsp; &nbsp; Genotype.of(IntegerChromosome.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0, knapsackCount, ITEMS.length())&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; gt -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final ISeq<List<Integer>> knapsacks = IntStream.range(0, knapsackCount)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> new ArrayList<Integer>())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(ISeq.toISeq());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < ITEMS.length(); ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final IntegerGene gene = gt.get(0, i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (gene.intValue() < knapsackCount) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; knapsacks.get(gene.intValue()).add(ITEMS.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return knapsacks;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; );}上面给出的编解码器选择一个IntegerChromoses长度为背包物品数量的 。它的基因范围将比背包的数量还要大。物品i将被放入背包,其染色体索引为IntegerChromosome.get(0, i).intValue()。如果索引超出有效范围,则跳过该项目。这种编码将保证项目的明确划分。

繁花如伊

如果您想要一组不同的基因,则必须定义两组不同的基因。private static final ISeq<Integer> SET1 = IntStream.rangeClosed(1, 10)&nbsp; &nbsp; .boxed()&nbsp; &nbsp; .collect(ISeq.toISeq());private static final ISeq<Integer> SET2 = IntStream.rangeClosed(11, 20)&nbsp; &nbsp; .boxed()&nbsp; &nbsp; .collect(ISeq.toISeq());public Codec<ISeq<ISeq<Integer>>, BitGene> codec() {&nbsp; &nbsp; return Codec.of(&nbsp; &nbsp; &nbsp; &nbsp; Genotype.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitChromosome.of(SET1.length()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BitChromosome.of(SET2.length())&nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; gt -> ISeq.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gt.getChromosome(0).as(BitChromosome.class).ones()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(SET1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(ISeq.toISeq()),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gt.getChromosome(1).as(BitChromosome.class).ones()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(SET2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(ISeq.toISeq())&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; );}通过这两个整数集,您将保证独特性。
随时随地看视频慕课网APP

相关分类

Java
我要回答