Detailed description and Implementation of Dynamic Planning ideas, detailed examples of Dynamic Planning

Source: Internet
Author: User

Detailed description and Implementation of Dynamic Planning ideas, detailed examples of Dynamic Planning

This article uses two examples to describe the concept of dynamic planning algorithm design. It mainly refers to the introduction to algorithms in the Bible, and adds some understanding of it. It mainly includes some specific implementation processes, so I hope it will help you.

# _ * _ Coding: UTF-8 _*_

Import numpy as np

Def MemoizedCutRodAux (p, n, r, s ):

If r [n]> = 0:

Return r [n]

If n = 0:

Q = 0

C = 0

Else:

Q =-1

C =-1

For I in range (1, n + 1 ):

If q <(p [I] + MemoizedCutRodAux (p, n-I, r, s )):

Q = p [I] + MemoizedCutRodAux (p, n-I, r, s)

C = I

R [n] = q

S [n] = c

Return q

Def MemoizedCutRod (p, n ):

R =-np. ones (n + 1)

S =-np. ones (n + 1)

MemoizedCutRodAux (p, n, r, s)

Return r, s

If _ name __= = '_ main __':

P = np. array ([,])

R, s = MemoizedCutRod (p, 10)

Print r

Print s

 

Result output:

R = [0. 1. 5. 8. 10. 13. 17. 18. 22. 25. 30.]

S = [0. 1. 2. 3. 2. 6. 1. 2. 3. 10.]

Import numpy as np

Def BottomUpCutRod (p, n ):

R =-np. ones (n + 1)

S =-np. ones (n + 1)

R [0] = 0

S [0] = 0

Q =-1

For j in range (1, n + 1 ):

For I in range (1, j + 1 ):

If q <(p [I] + r [j-I]):

Q = p [I] + r [j-I]

S [j] = I

R [j] = q

Return r, s

If _ name __= = '_ main __':

P = np. array ([0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30])

R, s = BottomUpCutRod (p, 10)

Print r

Print s

★Step 3: Use the bottom-up iteration method to calculate the optimal value

Import numpy as np

Def MatrixChain (p ):

N = p. size-1

M = np. ones (n + 1, n + 1) * np. inf

S = np. zeros (n + 1, n + 1 ))

For I in range (n + 1 ):

M [I, I] = 0

For lenth in range (2, n + 1 ):

For I in range (1, n-lenth + 2 ):

J = I + lenth-1

For k in range (I, j ):

Q = m [I, k] + m [k + 1, j] + p [I-1] * p [k] * p [j]

If q <m [I, j]:

M [I, j] = q

S [I, j] = k

Return m, s

If _ name __= = '_ main __':

P = np. array ([50, 10, 40, 30, 5])

M, s = MatrixChain (p)

Print m

Print s

 

Result output:

 

M = [[0. inf]

[Inf 0. 20000. 27000. 10500.]

[Inf 0. 12000. 8000.]

[Inf 0. 6000.]

[Inf 0.]

S = [[0. 0. 0. 0. 0.]

[0. 0. 1. 1. 1.]

[0. 0. 0. 2. 2.]

[0. 0. 0. 0. 3.]

[0. 0. 0. 0. 0.]

 

 

Bytes ----------------------------------------------------------------------------------------------

This article is the author's original, where the code can be run through (Python), hope to help

 

Related Article

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.