How do I sort the possibilities vector in 0/1 knapsack problem using the template min_max function I have written?

大城市里の小女人 提交于 2020-04-16 05:47:23

问题


SO already has multiple questions related to this topic. My question ain't 'bout the algorithm. It's 'bout sorting the final possibilities vector as per profit

I wrote the following piece of code to solve the 0/1 knapsack problem:

#include "algorithms.h"

struct Item {
    int index = 1;
    int profit = 1;
    int weight = 1;
    Item() = delete;
    explicit Item(int i, int _profit, int w) {
        index = i;
        profit = _profit;
        weight = w;
    }
    bool operator<(const Item& item) {
        return this->profit < item.profit;
    }
    bool operator<=(const Item& item) {
        return this->profit <= item.profit;
    }
    bool operator>(const Item& item) {
        return this->profit > item.profit;
    }
    bool operator>=(const Item& item) {
        return this->profit >= item.profit;
    }
    friend std::ostream& operator<<(std::ostream& out, const Item item) {
        out << item.index;
        return out;
    }
};

long weight(const std::vector<Item>& item_list, const std::vector<int>& item_switch) {
    long sum = 0;
    for (int i = 0; i < item_switch.size(); i++) {
        sum += item_switch[i] * item_list[i].weight;
    }
    return sum;
}

void increment(std::vector<int>& vec) {
    auto it_bit = vec.end();
    it_bit--;
    while (*it_bit == 1) {
        *it_bit = 0;
        if (it_bit == vec.begin()) {
            return;
        }
        it_bit--;
    }
    *it_bit = 1;
}

int main() {
    long M = 25;
    Item i1(1, 10, 9);
    Item i2(2, 12, 8);
    Item i3(3, 14, 12);
    Item i4(4, 16, 14);
    std::vector<Item> items = { i1,i2,i3,i4 };
    std::vector<int> enable(4,0);
    std::vector<std::vector<int>> possible;
    for (int i = 1; i <= 16; i++) {
        if (weight(items, enable) <= M) {
            possible.push_back(enable);
        }
        increment(enable);
    }
    for (const auto& vec : possible) {
        for (const auto& num : vec) {
            std::cout << num << " ";
        }
        std::endl(std::cout);
    }
    return 0;
}

As you can see, I've implemented a possibilities vector, which is essentially a vector denoting the presence of possibilities[index] numbered item by making that bit 1, i.e., 1010 means, objects 1 and 3 are present. I am not able to come up with a nice logic to find the possibility with the maximum probability by the min_max function I have written as template here. Please help.

The logic I used:

long profit(const std::vector<Item>& item_list, const std::vector<int>& item_switch) {
    long sum = 0;
    for (int i = 0; i < item_switch.size(); i++) {
        sum += item_switch[i] * item_list[i].profit;
    }
    return sum;
}

In main:

long pr = 0;
for (int i = 0; i < possible.size(); i++) {
    long temp = profit(items, possible[i]);
    if (temp > pr) {
        pr = temp;
    }
}

Also, the title of the question is open for suggestions 😅

来源:https://stackoverflow.com/questions/60917612/how-do-i-sort-the-possibilities-vector-in-0-1-knapsack-problem-using-the-templat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!