Question

In: Computer Science

create a genetic algorithm for knapsack problem in c++

create a genetic algorithm for knapsack problem in c++

Solutions

Expert Solution

Knapsack problem can be understand by following problem:

A thief is robbing a store and can carry a maximal weight of W into his knapsack. There are n items and weight of ith item is wi and the profit of selecting this item is pi. What items should the thief take?

Hence solution is which to take and what to leave. To solve we choose only those item of max value or profit.

To consider all subsets of items, there can be two cases for every item:

I. Include item as optimal as solution part

II. Don't consider as solution part..

Therefore, the maximum value / profit that can be obtained from n items is max of following two values.
a) Maximum value / result  by n-1 items and W weight (excluding nth item).
b) Value of nth item plus maximum value obtained by n-1 items and W minus weight of the nth item (including nth item).

If weight of nth item is greater than W, then the nth item cannot be included and case 1 is the only possibility.

for Value[] = { 60, 100, 120 }
and profit [] = { 10, 20, 30 }

Using 0/1 Knapsack solution approach,
solution leads to following view with optimal solution
weight : 10 ==> Value : 60
Weight : 20 ==> Value : 100
Weight : 30 ==> Value: 120
Weight: 20 & 10 ==> Value ( 120)
and similarly finally,
Weight: 30 & 20 will give 120 + 100 = 220 which is max profitable value.


In this way, problem is solved.

C++ implementation is following:

int KNAP_SACK(int W, int wt[], int val[], int n)

{

if (n == 0 || W == 0)      return 0; // This is base case of recursive approach

  

// Else select the max giving value recursively, until base is not returned

if (wt[n-1] > W)   return KNAP_SACK(W, wt, val, n-1);

else return max( val[n-1] + KNAP_SACK(W-wt[n-1], wt, val, n-1),

KNAP_SACK(W, wt, val, n-1) );

}


Related Solutions

how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of items, let call them 1,2,3,4,...,n. There are exactly c_i copies of item i, and each such copy has value v_i and weight w_i. As before, the knapsack capacity is W, and the other constraint is that you can only take at most c_i copies of item i ( since no more are available). Show how to compute the optimal value that can be achieved...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with...
Fractional Knapsack Problem Algorithm which best describes the tightest range of the number of items with only fractional inclusion (i.e. not entirely included or excluded) in the knapsack? (Let n denote the number of items for possible inclusion.) A) At least 0 items and at most n items B) At least 1 items and at most n items C) Exactly n items D) At least 0 items and at most n-1 items E) At least 1 items and at most...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items,...
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items, their values and weights. The maximum weight capacity of the knapsack is 113. What could be the maximum value of the knapsack and the most valuable set of items that fit in the knapsack? Item Weight Value 1 32 727 2 40 763 3 44 60 4 20 606 5 1 45 6 29 370 7 3 414 8 13 880 9 6 133...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6,...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6, 4, 3, 7, 1. Find which items will exactly fill the knapsack using dynamic programming solution introduced in class. Show all your work. use I/O knapsack
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. **The arguments to the knapsack function are target weight and the array index where the remaining items start. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT