Discrete Optimization course notes (1) - knapsack problem

Table of contents

1. The knapsack problem

2. Greedy algorithm

3. Modeling

4. Dynamic programming

5. Branch and bound

6. Search strategy 

(1)Depth-first

(2)Best-First

(3)Limited Discrepancy Search (LDS)

7.Getting Started

8. Knapsack Programming Assignments


1. The knapsack problem

The Knapsack problem is an NP-complete problem for combinatorial optimization. The problem can be described as: Given a group of items, each item has its own weight and price, within the limited total weight, how do we choose to make the total price of the items the highest.

2. Greedy algorithm

 (1) Greedy Algorithms (Greedy Algorithms): Select one item at a time until it can no longer be selected. The selection method is as follows:

  • The higher the quantity, the better, choose the item with the smallest weight each time
  • The higher the value, the better, choose the item with the highest value each time
  • The higher the value ratio, the better, choose the item with the highest unit value each time

(2) Summary of greedy algorithm 

  1. For a problem, there are many possible greedy solutions: some are better than others; depends on the input
  2. Advantages: fast design and implementation; fast solution speed
  3. Problem: The quality of the solution cannot be guaranteed; the quality of the solution to different problems varies greatly; the assumption is that it is easy to find a feasible solution
  4. Methods superior to greedy solutions: constrained programming; local search; mixed integer programming

3. Modeling

(1) How to model

  •  Selecting decision variables: the outcomes we are interested in
  • Problem constraints that describe these variables: specify what the solution to the problem should look like
  • Describing the objective function: indicating the quality of the understanding

(2) The result is an optimized model

  • A descriptive form: tells you what rather than how
  • There are many forms of modeling

(3) The model of the knapsack problem is as follows: If the number of items is n, then there is a possibility of solution 2^{n}, and violent enumeration is not possible

4. Dynamic programming

(1) Dynamic Programming: A widely used optimization method, the basic principles are as follows

  • Divide into subproblems to solve
  • bottom-up calculation

Suppose there is an item I=\left \{ 1,2...n \right \}, O\left ( k,j \right )which means the optimal solution of the knapsack problem of item [1...j] with a capacity of k, we want to know O\left ( K,n \right ), the idea is as follows, assuming we know how to solve it O\left ( k,j-1 \right ), then we O\left ( k,j \right )only need to consider one more item j, if jThe weight of the item w_{j}is less than k, there are two cases: choose jor not choose j.

It can be realized with a small program. The following is a top-down method. How efficient is this method?

 Looking back at the code of the fibonacci number, fib(n-2) and fib(n-3) are required to calculate fib(n), and fib(n-2) requires fib(n-3) and fib(n-4), But fib(n-3) has already been calculated, so we calculate the same subproblem many times

The idea of ​​dynamic programming is to calculate the recursive equation from the bottom up, starting with one item, then two items, until all

(2) Case analysis: take the following question (left) as an example to illustrate the DP process through the table

  • Column 0: No items. O(k,0)=0
  • Column 1: Item 1 (value 5, weight 4). O(4,1)=5
  • Column 2: Item 1 ➕ Item 2 (value 6, weight 5). O(5,1)=5, O(5,2)=max(O(5,1),O(0,1)+6)=6; O(9,1)=5, O(9, 2)=max(O(9,1),O(4,1)+6)=11
  • Column 3: Item 1➕Item 2➕Item 3 (weight 3, weight 2). O(2,3)=max(O(2,2),3+O(0,2))=3;O(4,3)=max(O(4,2),3+O(1, 2))=5;O(5,3)=max(O(5,2),3+O(3,2))=6;O(6,3)=max(O(6,2), 3+O(4,2))=8;O(7,3)=max(O(7,2),3+O(5,2))=9;O(9,3)=max(O (9,2),3+O(7,2))=11
  • trace back: O(9,3)=11=O(9,2), item 3 is not selected, O(9,2)=5, item 2 is selected, O(9,1)=0, item 1 is selected 

5. Branch and bound

(1) For the knapsack problem, all possibilities can be found by violent search, which is the simplest and least efficient method

(2) Branch and Bound (Branch and Bound): two steps of iterative branch and bound

  • Branching: branching is to divide the problem into multiple sub-problems
  • Bounding: Bounding refers to finding an optimistic estimate of the optimal solution to a subproblem, including UB (upper bound) and LB (lower bound)

(3) Relaxation relaxation: find a method for optimistic estimation

As shown in the figure below, each branch updates the value, remaining capacity and optimistic estimate UB, such as x1=0, if item 1 is not selected, the optimal solution is UB=128-45=83, and the feasible solution found for the first time is 80, if other branches UB<80 can stop. Although some steps have been saved compared to brute force search, is there room for improvement? relax in other ways

Assuming that we can now divide the item into multiple small parts, that is, 0\leq x\leq 1according to the unit value ratio of the item, we can choose item 3 and item 1, and 1/4 of item 2, the optimal solution UB is updated to 92 instead of 128 , called linear relaxation.

 Why is it right to update UB this way? Write the model as the following form, the requirement sum y_{i}is the largest, \frac{\omega _{i}}{y_{i}}the larger y_{i}the smaller

 After updating UB and recalculating, it is found that after x1=0, UB=92-45=77, there is no need to perform subsequent branch operations, which saves more steps than the previous one. This is the meaning of Relaxation, which reduces the search space for problem solutions and improves search efficiency.

6. Search strategy 

 The search strategy used by the previous Branch Bound is depth-first. This section introduces three search methods: Depth-first, Best-First, Least-Discrepancy

(1)Depth-first

  • Select the node on the left
  • When to prune a new branch: it finds a new node worse than the found solution
  • Memory: Think about the exploration process, choose left, left, left until it can no longer continue, so the number of nodes that need to be saved in a search process is the depth of this branch, that is, the number of all items
  • Process: The sequence is marked with red numbers, 5 searches to the solution 80, 7 searches UB=77 and terminates directly

(2)Best-First

  • Choose the node with the best estimate
  • When to prune new branches: all the nodes are worse than a found solution
  •  Memory: Considering the worst case, the estimates on both sides of the optimal branch are the same every time, so the entire tree needs to be stored, which consumes a lot of memory
  • Process: The red numbers are marked in sequence, and the best estimate is 128, 128, 83, 83, 80 respectively

(3)Limited Discrepancy Search (LDS)

  •  Assuming that there is a heuristic rule that is branch left, if branch right represents a mistake, the solution space is searched with the number of mistakes increasing, that is to say, the heuristic rule is less and less trusted. The wave of LDS indicates the number of mistakes that can occur during a search. Take the figure below as an example, wave1 indicates 0 mistakes, all are always left, wave2 indicates 1 mistake, randomly select right once, and the rest are left. It can be seen that the second wave has already explored half of the space.

  •  Process: search according to the wave color in the figure below, and find the optimal solution 80 in wave2

  • Performance: Choice of time and space

7.Getting Started

How to solve an optimization problem? Use the simplest method such as heuristic rules first, and then improve the solution. For example, use constraint programming and mixed integer programming to improve the solution quality, and use domain search to improve the scalability of the solution. Sometimes these methods are also mixed. There is no general method for optimization problems, and various methods need to be tried to find the most suitable solution.

8. Knapsack Programming Assignments

The test scale is from 4-10000, requiring time and quality at the same time, and the final score is 60/60. Solve with DP and Gurobi respectively, see my github knapsack for the complete code

Guess you like

Origin blog.csdn.net/weixin_45526117/article/details/127208989