K-optimization algorithm

K-optimization algorithm

ps: Personal understanding, please correct me if there are errors (recently a college homework involves this, is it not available online, hehehe)

definition

Insert picture description here
This is the definition == to
help understand, don’t talk nonsense and go directly to the example

Example 1

Use the 1-optimization method to solve the following 0/1 knapsack problem, given that: n = 8, w = [16, 20, 4, 15, 25, 10, 5, 8], p = [100, 200, 50, 90 , 175, 50, 20, 60], c = 70.

Solution: Greedy strategy: Check the items in a non-decreasing order of value density. If the remaining capacity can accommodate the item under investigation, load it; otherwise, consider the next item.
The process of solving with the greedy method: the
benefit density is [6.25, 10, 12.5, 6, 7, 5, 4, 7.5 ], and the order of the items obtained after sorting is [3, 2, 8, 5, 1, 4, 6, 7 ], the corresponding weight is [4, 20, 8, 25, 16, 15, 10, 5 ].
When k=0, the calculation result is:
after loading items 3, 2, 8, and 5, the remaining capacity of the backpack is 13, and the weight of items 1 and 4 are all over 13. The remaining capacity is 3, considering the item 7, its weight is greater than the remaining capacity, so the answer x = (0, 1, 1, 0, 1, 1, 0, 1 ), the benefit value obtained is 535.
When k=1, the calculation result is:
first put items 3, 2, 8, and 5, the benefit value obtained is still 535, and the greedy solution is the same as above;
first put item 1 to get the benefit value 520, and the greedy solution is (1, 1 ,1,1,0,0,1,1); put the item 4 first, the benefit value obtained is 520, and the greedy solution is (1,1,1,1,0,0,1,1);
put the item first 6. Get the benefit value of 535, and the greedy solution is (0,1,1,0,1,1,0,1); put item 7 first, get the benefit value of 505, and the greedy solution is (0,1,1, 0, 1, 1, 1). So the solution obtained by the k optimization method is (0, 1, 1, 0, 1, 1, 0, 1 ), and the benefit value is 535.

This should be regarded as a reference answer, written a bit abstract ==
1- There are fewer optimizations, let’s take a look at 2-Optimization (my idea)

Example 2

Question reference example 1, using 2-optimization algorithm
ps: very energetic, exhaustive. . . I am lazy. . .
The following shows the number of 2-优化代码this example uses Oh.


// An highlighted block
#include <iostream>

using namespace std;
int w[8]={
    
    16,20,4,15,25,10,5,8};
int p[8]={
    
    100,200,50,90,175,50,20,60};
int r[8]={
    
    2,1,7,4,0,3,5,6};//rank 值
int sumw,sump;
int main()
{
    
    
    for(int i=0;i<=6;i++){
    
    
        for(int j=i+1;j<=7;j++){
    
    
                int vis[8] = {
    
    0};
                cout << "a" << r[i]+1 << " " << "a" << r[j]+1 << " ";
                sumw = w[r[i]]+w[r[j]];
                sump = p[r[i]]+p[r[j]];
                vis[r[i]]=vis[r[j]]=1;
                for(int k = 0;k <= 7; k++){
    
    
                    if(!vis[r[k]]){
    
    
                    if((sumw+w[r[k]])<=70){
    
    sumw+=w[r[k]];sump+=p[r[k]];vis[r[k]]=1;cout << "a" << r[k]+1 << " ";}
                    }
                }
                cout << "    " << sump << endl;
        }
    }

    return 0;
}

Operation result
Insert picture description here
== water, water, still the same sentence, if you understand it incorrectly, please point it out to the scumbag, thank you for checking it~~~

Guess you like

Origin blog.csdn.net/weixin_45653643/article/details/109752625