Dynamic Programming Method-01 backpack Problems

Source: Internet
Author: User

Concepts:

Optimization Problem: There are n input, and its solution is composed of a subset of the n input. This subset must meet certain conditions specified in advance. These conditions are called constraints, the solution that meets the constraints is the feasible solution of the problem. There may be more than one feasible solution meeting the constraints. to measure the advantages and disadvantages of these feasible solutions, some standards are given in advance. These standards are usually given in the form of functions. These standard functions are called objective functions, it makes the feasible solution of the objective function to obtain the extreme value an optimal solution. Such problems are called optimization problems.


Principle of bilinex:

For an optimization problem with n inputs, the process of solving the problem can be divided into several stages. The decision-making of each stage depends only on the status of the previous stage, the action taken by the decision changes the status and becomes the basis for the next stage. Thus, a decision sequence is generated in a changing state. The process of generating this decision sequence is called a multi-stage decision-making process.

There is a decision-making policy or goal in each stage. This strategy or goal is determined by the nature and characteristics of the problem. It is usually expressed in the form of a function and has a recursive relationship, it is called a dynamic planning function.

Multi-stage decision-making processes meet the principle of optimization: no matter the initial state and initial decision-making of the decision-making process, other decisions must form an optimal decision sequence relative to the current State produced by the initial decision-making.

If a problem satisfies the optimum principle, this problem is generally called an optimal substructure.

Three features

We know that dynamic planning is an effective way to solve the global optimal solution. Generally, the following two notable issues can be solved using dynamic planning algorithms:

It has an optimal sub-structure, that is, the optimal solution can be defined recursively.

It can be divided into several self-problems that overlap each other.

The optimal solution also contains the optimal solution of its subproblems.


4. Design Scheme

Dynamic Programming Method Design Scheme

Segmentation: the original problem is divided into several overlapping subproblems;

Analysis: analyze whether the problem meets the optimal sub-structure nature and find the dynamic programming function recurrence type;

Solution: uses recursive bottom-up computing to implement dynamic planning.


5 01 backpack Problems

0/1 in a backpack problem, if item I is put into a backpack or is not put into a backpack, set Xi to indicate that item I is loaded into a backpack, when Xi = 0, it indicates that item I is not loaded into the backpack. When Xi = 1, it indicates that item I is loaded into the backpack. According to the requirements of the problem, there are the following constraints and target functions:

Figure 1 Constraints


Figure 2 target functions

01 state transition equation f [I, j] = Max {f [I-1, j-Wi] + Pi (j> = Wi), f [I-1, j]}

F [I, j] indicates the maximum value that can be obtained by selecting a number of items in the first I-item and placing them in a backpack with a load-bearing j.

Pi indicates the value of item I.

Decision-making: Should I items be placed in a backpack to maximize the total value of items in the backpack?

Six Dynamic Planning Solutions

There are five items numbered a, B, c, d, and e. Their weights are 2, 2, 6, 5, and 4. Their values are 6, 3, 5, 4, and 6, respectively, now I will give you a 10-weight backpack. How can we make the items loaded in the backpack the greatest sum of value?

Name

Weight

Value

1

2

3

4

5

6

7

8

9

10

A

2

6

0

6

6

9

9

12

12

15

15

15

B

2

3

0

3

3

6

6

9

9

9

10

11

C

6

5

0

0

0

6

6

6

6

6

10

11

D

5

4

0

0

0

6

6

6

6

6

10

10

E

4

6

0

0

0

6

6

6

6

6

6

6

Table 10/1 Dynamic Programming Method for knapsack problems

Table 1 is generated from bottom to left to right.

E1 indicates the value of putting item e only when the backpack capacity is 1, because the value of capacity 1 <4 is 0; so when the backpack capacity is 4, the value of cell e4 is 6. Capacity continues to rise and can only be placed in e, so the value continues to be 6.

Line d can be referred to recursive f [I, j] = Max {f [I-1, j-Wi] + Pi (j> = Wi), f [I-1, j]} description

When I <= 5, the value of Row d is the same as that of Row e;

When I> = 6, for example, row 6th, select Max {f [] + 4, f []}, that is, Max {e1 + 4, e6} = Max {4, 6} = 6; similarly, d9 = Max {f [] + 4, f [} = 10;

The values of c, B, And a can be calculated by analogy, and the optimal path can be calculated based on the maximum value.


D1 indicates the value of placing items d and e when the backpack capacity is 1. Because the weight of e is 4> 1, and the weight of d is 5> 1, the value is 0. Until the contents of the row d table in column 5th (excluding d5) are the same as that in Row e,



7 program implementation C #

Code snippet 1 test function

class MainClass{public static void Main (string[] args){            Obj[] objs = new Obj[4];            objs[0] = new Obj() { Weight = 7, Price = 42 };            objs[1] = new Obj() { Weight = 3, Price = 12 };            objs[2] = new Obj() { Weight = 4, Price = 40 };            objs[3] = new Obj() { Weight = 5, Price = 25 };            Console.WriteLine ("Dynamicprogramming Model:");            dynamicprogramming d = new dynamicprogramming (objs, 10);int price = d.Execute ();d.Printing (price);            //Console.Read();}}

Code snippet 2 Implementation Functions


Public class dynamicprogramming {////// Store the recursive data of Dynamic Planning // m_V [I] [j] indicates the maximum value obtained by placing the first I items in a backpack with a capacity of j. // 0th rows as initialization, indicates the value when the item is not put; // The value of column 0th as initialization, indicating the value of the item when the capacity is 0 ;///Private int [,] m_V ;////// Number of items ///Private int m_n ;////// Backpack capacity ///Private int m_w; public Obj [] objs {get; set;} public dynamicprogramming (Obj [] objs, int w) {if (objs = null) {return ;} m_n = objs. length; this. objs = objs; m_w = w; m_V = new int [m_n + 1, m_w + 1]; for (int I = 0; I <m_w + 1; I ++) {m_V [0, I] = 0 ;}for (int I = 0; I <m_n + 1; I ++) {m_V [I, 0] = 0 ;}} public int Execute () {for (int r = 1; r <= m_n; r ++) {for (int w = 1; w <= m_w; w ++) {if (w <objs [r-1]. weweigh T) {m_V [r, w] = m_V [r-1, w];} else {int tmp = m_V [r-1, w-objs [r-1]. weight] + objs [r-1]. price; m_V [r, w] = m_V [r-1, w]> tmp? M_V [r-1, w]: tmp ;}}// calculate the int weight = m_w; for (int I = m_n; I> 0; I --) {if (m_V [I, weight]> m_V [I-1, weight]) {objs [I-1]. selected = true; weight-= objs [I-1]. weight ;}} return m_V [m_n, m_w];} public void Printing (int price) {Console. write ("<"); for (int I = 0; I <objs. length; I ++) {Console. write (objs [I]. selected? "1": "0");} Console. WriteLine (">, price:" + price );}}

Output result:

Note: The above programs are developed using Mono under FreeBSD.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.