遗传算法:0-1背包的交叉

时间:2016-11-03 23:00:03

标签: algorithm knapsack-problem crossover

我正在遵循遗传算法的方法来解决背包问题here。我知道他们使用直接值编码方案而不是二进制表示。交叉功能如下:

def cxSet(ind1, ind2):
"""Apply a crossover operation on input sets. The first child is the
intersection of the two sets, the second child is the difference of the
two sets.
"""
temp = set(ind1)                # Used in order to keep type
ind1 &= ind2                    # Intersection (inplace)
ind2 ^= temp                    # Symmetric Difference (inplace)
return ind1, ind2

如果我将Knapsack问题的染色体编码为二进制表示,则inersection将是AND操作。设定差异的分析操作是什么?

另外,我只是想知道这种类型的交叉背后的原理是什么,以及这种类型的交叉优于其他常用的交叉技术,如单点交叉或两点交叉。

1 个答案:

答案 0 :(得分:1)

解决此类问题的最简单方法是举一个小例子:

   .flatMap(new Func1<News, Observable<News>>() {
        @Override
        public Observable<News> call(News news) {
            return apiService.getAuthor(news.getId())
                    .doOnNext(new Action1<Author>() {
                        @Override
                        public void call(Author author) {

                            if (!author.getName().equals("null")) {
                                news.setAuthorName(author.getName());
                            } else {
                                news.setAuthorName("Unknown");
                            }
                        }
                    })
                    .observeOn(Schedulers.io())
                    .map(new Func1<Author, News>() {
                        @Override
                        public News call(Author author) {
                            return news;
                        }
                    })
                    .subscribeOn(Schedulers.newThread());
        }
    })

看看这个,我们提出了以下按位运算符:

  1. intersect:s1 AND s2(通常为s1 = {1, 2, 3, 4} => 11110000 s2 = {3, 4, 5, 6} => 00111100 s1 intersect s2 = {3, 4} => 00110000 s1 difference s2 = {1, 2, 5, 6} => 11001100

  2. 差异:s1 XOR s2(通常是&

相关问题