使用Jenetics创建基因型时的约束

问题描述 投票:0回答:2

我正在使用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如何做到这一点?

java genetic-algorithm
2个回答
0
投票

这不是问题的唯一可能编码,因为每个问题都有其自身的特征。对于多背包问题,我选择IntegerChromosome而不是BitChromosomes

private static final ISeq<Integer> ITEMS = IntStream.rangeClosed(1, 10)
    .boxed()
    .collect(ISeq.toISeq());

public Codec<ISeq<List<Integer>>, IntegerGene> codec(final int knapsackCount) {
    return Codec.of(
        Genotype.of(IntegerChromosome.of(
            0, knapsackCount, ITEMS.length())
        ),
        gt -> {
            final ISeq<List<Integer>> knapsacks = IntStream.range(0, knapsackCount)
                .mapToObj(i -> new ArrayList<Integer>())
                .collect(ISeq.toISeq());

            for (int i = 0; i < ITEMS.length(); ++i) {
                final IntegerGene gene = gt.get(0, i);
                if (gene.intValue() < knapsackCount) {
                    knapsacks.get(gene.intValue()).add(ITEMS.get(i));
                }
            }

            return knapsacks;
        }
    );
}

上面给出的编解码器选择一个IntegerChromoses,其长度为背包项目的数量。它的基因范围将大于背包的数量。项目i将被放入具有IntegerChromosome.get(0, i).intValue()染色体索引的背包中。如果索引超出有效范围,则跳过该项目。这种编码将保证项目的明确划分。


0
投票

如果您想要一组不同的基因,则必须定义两个不同的基因组。

private static final ISeq<Integer> SET1 = IntStream.rangeClosed(1, 10)
    .boxed()
    .collect(ISeq.toISeq());

private static final ISeq<Integer> SET2 = IntStream.rangeClosed(11, 20)
    .boxed()
    .collect(ISeq.toISeq());


public Codec<ISeq<ISeq<Integer>>, BitGene> codec() {
    return Codec.of(
        Genotype.of(
            BitChromosome.of(SET1.length()),
            BitChromosome.of(SET2.length())
        ),
        gt -> ISeq.of(
            gt.getChromosome(0).as(BitChromosome.class).ones()
                .mapToObj(SET1)
                .collect(ISeq.toISeq()),
            gt.getChromosome(1).as(BitChromosome.class).ones()
                .mapToObj(SET2)
                .collect(ISeq.toISeq())
        )
    );
}

使用两个整数集,您将保证与众不同。

© www.soinside.com 2019 - 2024. All rights reserved.