Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming (II)

Similar presentations


Presentation on theme: "Dynamic Programming (II)"— Presentation transcript:

1 Dynamic Programming (II)
2012/11/27

2 15.1 Assembly-line scheduling
An automobile chassis enters each assembly line, has parts added to it at a number of stations, and a finished auto exits at the end of the line. Each assembly line has n stations, numbered j = 1, 2,...,n. We denote the jth station on line i ( where i is 1 or 2) by Si,j. The jth station on line 1 (S1,j) performs the same function as the jth station on line 2 (S2,j). Chapter 15

3 The stations were built at different times and with different technologies, however, so that the time required at each station varies, even between stations at the same position on the two different lines. We denote the assembly time required at station Si,j by ai,j. As the coming figure shows, a chassis enters station 1 of one of the assembly lines, and it progresses from each station to the next. There is also an entry time ei for the chassis to enter assembly line i and an exit time xi for the completed auto to exit assembly line i. Chapter 15

4 a manufacturing problem to find the fast way through a factory
Chapter 15

5 Normally, once a chassis enters an assembly line, it passes through that line only. The time to go from one station to the next within the same assembly line is negligible. Occasionally a special rush order comes in, and the customer wants the automobile to be manufactured as quickly as possible. For the rush orders, the chassis still passes through the n stations in order, but the factory manager may switch the partially-completed auto from one assembly line to the other after any station. Chapter 15

6 The time to transfer a chassis away from assembly line i after having gone through station Sij is ti,j, where i = 1, 2 and j = 1, 2, ..., n-1 (since after the nth station, assembly is complete). The problem is to determine which stations to choose from line 1 and which to choose from line 2 in order to minimize the total time through the factory for one auto. Chapter 15

7 An instance of the assembly-line problem with costs
Chapter 15

8 Step1 The structure of the fastest way through the factory
the fast way through station S1,j is either the fastest way through Station S1,j-1 and then directly through station S1,j, or the fastest way through station S2,j-1, a transfer from line 2 to line 1, and then through station S1,j. Using symmetric reasoning, the fastest way through station S2,j is either the fastest way through station S2,j-1 and then directly through Station S2,j, or the fastest way through station S1,j-1, a transfer from line 1 to line 2, and then through Station S2,j. Chapter 15

9 Step 2: A recursive solution
Chapter 15

10 step 3: computing the fastest times
Let ri(j)be the number of references made to fi[j] in a recursive algorithm. r1(n)=r2(n)=1 r1(j) = r2(j)=r1(j+1)+r2(j+1) The total number of references to all fi[j] values is (2n). We can do much better if we compute the fi[j] values in different order from the recursive way. Observe that for j  2, each value of fi[j] depends only on the values of f1[j-1] and f2[j-1]. Chapter 15

11 FASTEST-WAY procedure
FASTEST-WAY(a, t, e, x, n) 1 f1[1]  e1 + a1,1 2 f2[1]  e2 + a2,1 3 for j  2 to n 4 do if f1[j-1] + a1,j  f2[j-1] + t2,j-1 +a1,j then f1[j]  f1[j-1] + a1,j l1[j]  1 else f1[j]  f2[j-1] + t2,j-1 +a1,j 8 l1[j]  2 if f2[j-1] + a2, j  f1[j-1] + t1,j-1 +a2,j Chapter 15

12 12 else f2[j]  f1[j – 1] + t1,j-1 + a2,j 13 l2[j]  1
then f2[j]  f2[j – 1] + a2,j l2[j]  2 else f2[j]  f1[j – 1] + t1,j-1 + a2,j l2[j]  1 14 if f1[n] + x1  f2[n] + x2 15 then f* = f1[n] + x1 l* = 1 17 else f* = f2[n] + x2 l* = 2 Chapter 15

13 step 4: constructing the fastest way through the factory
PRINT-STATIONS(l, n) 1 i  l* 2 print “line” i “,station” n 3 for j  n downto 2 4 do i  li[j] print “line” i “,station” j – 1 output line 1, station 6 line 2, station 5 line 2, station 4 line 1, station 3 line 2, station 2 line 1, station 1 Chapter 15

14 Optimal Binary search trees
cost:2.75 optimal!! cost:2.80

15 expected cost the expected cost of a search in T is

16 For a given set of probabilities, our goal is to construct a binary search tree whose expected search is smallest. We call such a tree an optimal binary search tree.

17 Step 1: The structure of an optimal binary search tree
Consider any subtree of a binary search tree. It must contain keys in a contiguous range ki, ...,kj, for some 1  i  j  n. In addition, a subtree that contains keys ki, ..., kj must also have as its leaves the dummy keys di-1, ..., dj. If an optimal binary search tree T has a subtree T' containing keys ki, ..., kj, then this subtree T' must be optimal as well for the subproblem with keys ki, ..., kj and dummy keys di-1, ..., dj.

18 Step 2: A recursive solution
j j = + w ( i , j ) å p å q w[i, j] = w[i, j – 1] + pj+qj l l l = i l = i - 1 If the optimal tree containing ki,…,kj is rooted at kr, then e[i, j]=pr+(e[i, r-1]+w(i, r-1))+(e[r+1, j]+w(r+1, j)) ì = q if j i - 1, = i - 1 e [ i , j ] í - + + + min { e [ i , r 1 ] e [ r 1 , j ] w ( i , j )} if i j . î i r j

19 Step 3:computing the expected search cost of an optimal binary search tree
OPTIMAL-BST(p,q,n) 1 for i  1 to n + 1 2 e[i, i – 1]  qi-1 3 w[i, i – 1]  qi-1 4 for l  1 to n 5 for i  1 to n – l + 1 j  i + l – 1 e[i, j]   w[i, j]  w[i, j – 1] + pj+qj

20 9 for r  i to j t  e[i, r –1]+e[r +1, j]+w[i, j] if t < e[i, j] then e[i, j]  t root[i, j]  r 14 return e and root the OPTIMAL-BST procedure takes (n3)

21 The table e[i,j], w[i,j], and root[i,j] computed by OPTIMAL-BST on the key distribution.

22 0/1 knapsack problem n items, with weights w1, w2, , wn
values (profits) v1, v2, , vn the capacity W to maximize subject to  W xi = 0 or 1, 1in (We let T={i| xi=1}S={1,…,n})

23 Brute force solution The 0/1 knapsack problem is an optimization problem. We may try all 2n possible subsets of S to solve it. Question: Any solution better than the brute-force? Answer: Yes, the dynamic programming (DP)! We trade space for time.

24 Table (Array) We construct an array V[0..n, 0..M].
The entry V[i, w], 1in, and 0wW, will store the maximum (combined) value of any subset of items 1,2,…,i of the (combined) weight at most w. If we can compute all the entries of this array, then the array entry V[n, W] will contain the maximum value of any subset of items 1,2,…,n that can fit into the knapsack of capacity W.

25 Recursive relation Initial settings: V[0, w]=0 for 0wW
V[i, w]=max(V[i-1, w], vi+V[i-1, w-wi]) for 0in and 0wW if wiw. Otherwise, V[i, w]=V[i-1, w].

26

27

28

29

30

31

32 0/1 knapsack problem in multistage representation
n objects , weight W1, W2, ,Wn profit P1, P2, ,Pn capacity M maximize subject to  M xi = 0 or 1, 1in E.g.

33 0/1 knapsack problem in multistage representation
Multi-stage graph:

34 The resource allocation problem
Given m resources, n projects with profit p(i, j) : i resources are allocated to project j. The goal is to maximize the total profit. E.g. i: # of resources 1 2 3 1 2 8 9 2 5 6 7 Project j 3 4 4 4 4 2 4 5

35 Multistage graph (i, j) : the state where i resources are allocated to projects 1, 2, …, j E.g. node H=(3, 2) : 3 resources are been allocated to projects 1, 2. The Resource Allocation Problem Described as a Multi-Stage Graph.

36 Multistage graph Find the longest path from S to T :
S C H L T , 8+5=13 2 resources allocated to project 1 1 resource allocated to project 2 0 resource allocated to projects 3, 4

37 Comment If a problem can be described by a multistage graph then it can be solved by dynamic programming.

38 Q&A


Download ppt "Dynamic Programming (II)"

Similar presentations


Ads by Google