我正在用杰尼提斯进行多目标优化问题(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]]但我希望两个人都有相互排斥的项目。如何才能用杰尼蒂来实现这一点呢?
发布于 2019-10-18 17:51:08
这并不是问题的唯一可能编码,因为每个问题都有自己的特点。对于多背包问题,我会选择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。它的基因范围将大于背包数量。第一项将被放入带有IntegerChromosome.get(0, i).intValue()染色体指数的背包。如果索引超出有效范围,则跳过该项。这种编码将保证项目的明确划分。
发布于 2019-10-18 14:32:22
如果你想要一组不同的基因,你必须定义两组不同的基因。
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())
)
);
}有了这两个整数集,您就可以保证其独特性。
https://stackoverflow.com/questions/58452013
复制相似问题