亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Jenetics 創建基因型時的限制

使用 Jenetics 創建基因型時的限制

牛魔王的故事 2023-11-10 16:12:59
我正在使用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 實現這一目標?
查看完整描述

2 回答

?
LEATH

TA貢獻1936條經驗 獲得超7個贊

這不是問題的唯一可能的編碼,因為每個問題都有其自己的特征。對于多背包問題,我會選擇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()。如果索引超出有效范圍,則跳過該項目。這種編碼將保證項目的明確劃分。


查看完整回答
反對 回復 2023-11-10
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

如果您想要一組不同的基因,則必須定義兩組不同的基因。


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())

        )

    );

}

通過這兩個整數集,您將保證獨特性。


查看完整回答
反對 回復 2023-11-10
  • 2 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號