0% found this document useful (0 votes)
6 views

Chapter 6 - Algorithms Part II

python

Uploaded by

Duc Vuong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 6 - Algorithms Part II

python

Uploaded by

Duc Vuong Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

EE3491 – KỸ THUẬT LẬP TRÌNH

PROGRAMMING TECHNIQUES

CHAPTER 6
Algorithms – Part II
Võ Duy Thành
Department of Automation Engineering
Control Technique and Innovation Lab. for Electric Vehicles
School of Electrical and Electronic Engineering
Hanoi University of Science and Technology
[email protected] | [email protected]
Outline

1. Brute Force Algorithm


2. Greedy Algorithm
3. Dynamic Programming
4. 0/1 Knapsack Problem

2
1. Brute Force Algorithm

Example:
• Smithsonian
Museums in
Washington DC
• In 2018, we
stayed here for
1 month
• We want to visit
as many as
possible events
• We can arrange
some days in a
week

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 3


1. Brute Force Algorithm

• Here are our free days


• Each day has some events Week 1
Mon
Week 1
Thu
• Events can be repeated and
we don’t want to attend Week 2 Week 2 Week 2 Week 2 Week 2
events twice Mon Tue Fri Sat Sun

⇒ What is the best


combination? Week 3 Week 3 Week 3 Week 3
Mon Wed Thu Sat

Week 4 Week 4 Week 4


Tue Wed Fri

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 4


1. Brute Force Algorithm

• Brute Force Algorithm will check all combinations and see what is the
best.
Week 1 Week 1
Mon Thu
2x5 = 10 combinations
Week 2 Week 2 Week 2 Week 2 Week 2
Mon Tue Fri Sat Sun

Week 3 Week 3 Week 3 Week 3 10x4 = 40 combinations


Mon Wed Thu Sat

Week 4 Week 4 Week 4 40x3 = 120 combinations


Tue Wed Fri

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 5


1. Brute Force Algorithm

• Brute Force Algorithm Complexity


Assume the available days Re-arrange the available days

Week 1 Week 1 Week 1 Week 1


Mon Thu Mon Thu

Week 2 Week 2 Week 4 Week 4 Week 4


Mon Tue Tue Wed Fri

Week 3 Week 3 Week 3 Week 3 Week 3 Week 3 - Brute force is for


Mon Wed Mon Wed Thu Sat solving optimization
problems
Week 4 Week 4 Week 2 Week 2 Week 2 Week 2 Week 2 - Easy to implement by
Tue Wed Mon Tue Fri Sat Sun trying all possible
solutions
Complexity: 𝑂(2𝑛) Complexity = 2x3x4x5: 𝑂(𝑛!)
- But, complexity…

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 6


1. Brute Force Algorithm

Exercise: Use Brute force algorithm to solve the following problem


• Problem statement:
Given a system as:
𝑦1 = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐
𝑦2 = 𝑒𝑥 + 𝑓
Find the optimal value 𝑥 ∗ :
𝑥 ∗ = argmin{𝛼𝑦1 + 𝛽𝑦2 }
with respect to:
𝑥𝑚𝑖𝑛 ≤ 𝑥 ≤ 𝑥𝑚𝑎𝑥
𝑥∈ℤ

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 7


2. Greedy Algorithm

• Greedy algorithm solves problems by selecting the best option available


at the moment. In other words: “Tùy cơ ứng biến”.
• Cannot guarantee the overall optimal result.
• Greedy algorithm can be applied to problems that have properties:
• Greedy Choice Property: the optimal choice at each step does not consider the
choice of previous steps
• Optimal Structure: optimality of sub-problems contributes to the optimal overall
solution to the whole problem.
• Advantages:
• Easier to describe and implement,
• Real-time realization (e.g. for optimal control)

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 8


2. Greedy Algorithm

• Drawback of Greedy algorithm


• Find the way with highest sum of 20
weighted values from root to leaf
2 3 Total = 24
Greedy algorithm
20 6 10 1

2 3

6 10 1 20

2 3 Total = 32
Optimal Solution
6 10 1

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 9


2. Greedy Algorithm

Example: when should I charge my laptop?


• My laptop battery lasts 3 hours, and needs 30 minutes for full-charged
• I have my full-day seminar but forgot to bring the charger. Must come
back office and charge it there.
• What’s the best solution for me?

Chargeeerrrrr….

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 10


2. Greedy Algorithm
8AM 8:00 – 9:30: Breakfast meeting
9AM Use in 2.5 hours
10:00 – 10:30: Students meeting
10AM Charge
11AM 11:00 – 12:00: Industrial meeting
12PM Use in 3 hours
1PM 12:30 – 14:00: Lunch meeting

2PM Charge
14:30 – 15:00: TA meeting
3PM Use in 2 hours
15:30 – 16:30: Lab. Seminar
4PM Charge
5PM 17:00 – 18:00: Students meeting
6PM Use in 2.5 hours
7PM 18:30 – 20:00: Gala Dinner

8PM After 20:00: Home


EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 11
2. Greedy Algorithm

• Example: traffic in Hanoi – people use greedy algorithm

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 12


2. Greedy Algorithm

Application in control theory: Greedy Optimal Control


Given a system:
𝑥𝑘+1 = 𝑓 𝑥𝑘 + 𝑔 𝑥𝑘 𝑢𝑘
where 𝑥𝑘 ≔ 𝑥(𝑘) ∈ ℝ𝑛 and 𝑢𝑘 ≔ 𝑢 𝑘 ∈ ℝ𝑚
Assume that 𝑓𝑘 ≔ 𝑓 𝑥𝑘 : ℝ𝑛 → ℝ𝑛 , 𝑔𝑘 ≔ 𝑔(𝑥𝑘 ): ℝ𝑛 → ℝ𝑚
Cost function:
𝐽𝑘 = 𝑢𝑘𝑇 𝐺𝑢𝑘 + 𝑥𝑘+1
𝑇
𝐻𝑥𝑘+1
Find optimal value of control signal 𝑢𝑘∗
𝑢𝑘∗ = argmin{𝐽𝑘 }
For Greedy Optimal Control, the control law:
∗ 𝑇 −1 𝑇
𝑢𝑘 = − 𝐺 + 𝑔𝑘 𝐻𝑔𝑘 𝑔𝑘 𝐻𝑓𝑘

Eugen Lavresky, “Greedy Optimal Control,” in proc. American Control Conference, 2000.
EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 13
2. Greedy Algorithm

• Example: Use Greedy optimal control to 𝑎𝑘 𝑣𝑘+1


achieve optimal response of a linear 𝑣𝑘+1 = 𝑣𝑘 + 𝑇𝑎𝑘
system given by:
𝑣𝑘+1 = 𝑣𝑘 + 𝑇𝑎𝑘 𝐿
Following Eugene et.al., the control
law can be written (let 𝐺 = 1)
∗ 𝐻𝑇
𝑎𝑘 = − 2
𝑣𝑘 = 𝐿𝑣𝑘
1 + 𝐻𝑇
Write a program to simulate the
control system with parameters:
𝐻 = 1000, 𝑇 = 0.001, 𝑥0 = 10

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 14


3. Dynamic Programming

• Dynamic Programming (Quy hoạch động) is an


efficient method to solve problems that exhibit
characteristics of overlapping subproblems and
optimal structure.
• Optimal structure: a global optimal solution can be found
by combining optimal solutions to local subproblems
• Overlapping subproblems: an optimal solution involves
solving the same problem multiple times.
• Principle of Optimality
𝐽𝐴𝐶 C
If A→C→B is optimal path and
Richard Bellman
𝐽𝐴𝐶 is optimal cost of AC, then CB is the A 𝐽𝐶𝐵 B
(26/8/1920-19/3/1984) optimal path and 𝐽𝐴𝐵 = 𝐽𝐴𝐶 + 𝐽𝐶𝐵
An optimal policy has the property that whatever the previous state and decision (i.e., control), the remaining
decisions must constitute an optimal policy with regard to the state resulting from the previous decision – D.E.K
EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 15
3. Dynamic Programming

Example E • Flight routing system. Starting


5 (6) 4 from city A to city B, through 5
C H states: 𝑘 = 0 to 𝑘 = 𝑁 = 4.
2 4 3 (2) 2 • Backward solution
(9)
F • 𝑘 = 𝑘𝑓 = 𝑁 = 4: destination point
A B • 𝑘 = 3: two decision
(11) 2 (5) 5 (0) • H→B(2); I→B(3)
4 D • 𝑘 = 2: 4 decision
I 3
• E→H(2+4=6); G→I(6+3=9)
(7) (3) • F→H(3+2=5)<F→I(5+3=8) ⇒ F(5)
3 • 𝑘 = 1: similarly ⇒ C(9), D(7)
G 6
• 𝑘 = 0: starting point
(9) ⇒ The optimal route is either A, C, F, H, B or A, D, F, H, B
Do the same procedure yourself for the Forward solution
EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 16
3. Dynamic Programming

• Recursive Fibonacci example revisit.


def Fibo(n):
if n == 0 or n ==1:
return 1
else:
return Fibo(n-2) + Fibo(n-1)
• Try Fibo(120) and wait for the result

Fibo(120) = 8.670.007.398.507.948.658.051.921
If each recursive call takes 1ns → it will take about 250.000 years to finish

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 17


3. Dynamic Programming

• Tree of calls for Fibo(6) Fibo(6)

Fibo(5) Fibo(4)

Fibo(4) Fibo(3) Fibo(3) Fibo(2)

Fibo(3) Fibo(2) Fibo(2) Fibo(1) Fibo(2) Fibo(1) Fibo(1) Fibo(0)

Fibo(2) Fibo(1) Fibo(1) Fibo(0) Fibo(1) Fibo(0) Fibo(1) Fibo(0)

Fibo(1) Fibo(0)

Function Fibo(6) Fibo(5) Fibo(4) Fibo(3) Fibo(2) Fibo(1) Fibo(0)


No. of Execution 1 1 2 3 5 8 5

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 18


3. Dynamic Programming

• To reduce calculation:
• Record the result by the first call Memorization is the key idea
• Check if the result is stored when being called behind dynamic programming
def fastFibo(n, memo = {}):
if n == 0 or n == 1:
return 1
try:
return memo[n]
except KeyError:
result = fastFibo(n-1, memo) + fastFibo(n-2, memo)
memo[n] = result
return result

⇒ Rewrite the code using if…else instead of try…except

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 19


3. Dynamic Programming

Introductory example: Optimal Energy Management of a Dual-motor


Electric Vehicle using Dynamic Programming

Real-time solution for the same


studied object in Chapter 3

The optimal solution is obtained using Matlab-based DPM toolbox,


written by O. Sundstrom and L. Guzzella
H. -L. T. Nguyen, S. Nguyen-Van, T. X. Hoang, T. Vo-Duy, B. -H. Nguyễn and M. C. Ta, "Optimal Energy Management of a Dual-motor Electric Vehicle
using Dynamic Programming," 2021 IEEE Vehicle Power and Propulsion Conference (VPPC), Gijon, Spain, 2021.

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 20


4. 0/1 Knapsack Problem

• The 0/1 Knapsack Problem:


• is a type of optimization problem, which has two parts:
• An objective function that is to be minimized or maximized, e.g., energy consumption of a
Hybrid Electric Vehicle (HEV).
• A set of constraints that must not be violated, e.g., SoC of battery and maximum torque of
electric motors.
• can be solved by Greedy Algorithm (for real-time applications) or Dynamic
Programming/Brute Force (for offline benchmark), and of course other methods
• can be found in many real-world applications
• Electrical: Home energy management, Power allocation management
• Production: Solve production planning problem, Appliance scheduling optimization for
demand response
• IT: Sensor selection in distributed multiple-radar, 5G mobile edge computing, Network
selection for mobile nodes
• and many other applications

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 21


4. 0/1 Knapsack Problem

The 0/1 Knapsack Problem: It’s not easy being a burglar


Value Weight Value/Weight
Clock 175 5 35
Painting 90 4.5 20
Radio 20 2 10
Vase 50 1 50
Book 10 0.5 20
Image is created by Bing Creator
Computer 200 10 20
• The house contains some valuable things with appropriate weight
• The burglar’s knapsack can hold at most 10 kg The constraint
• The burglar has to decide what to steal and what to leave behind
What’s a poor burglar’s “best” steal?
EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 22
4. 0/1 Knapsack Problem

What is “best”? → The Cost Function


• Highest value?
⇒ Takes the computer and leaves
everything behind ⇒ 200USD
• Least heavy?
Value Weight Value/Weight
⇒ Takes, in order, the book, vase, Clock 175 5 35
radio and painting ⇒ 170USD Painting 90 4.5 20
Radio 20 2 10
• Highest value-to-weight ratio? Vase 50 1 50
⇒ Takes the vase and the clock, Book 10 0.5 20
and only the book ⇒ 235USD Computer 200 10 20
Python code to solve the 0/1 Knapsack problem, using Brute Force, Greedy and Dynamic Programming can be found in:
John V. Guttag, Introduction to Computation and Programming Using Python: with application to understanding data,
Massachusetts Institute of Technology, 2016

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 23


4. 0/1 Knapsack Problem

• Knapsack Problem generalized formulation:


• 0/1 Knapsack Problem
maximize σ𝑛𝑖=1 𝑣𝑖 𝑥𝑖
subject to σ𝑛𝑖=1 𝑤𝑖 𝑥𝑖 ≤ 𝑊 and 𝑥𝑖 ∈ 0,1 .
• Bounded Knapsack Problem
maximize σ𝑛𝑖=1 𝑣𝑖 𝑥𝑖
subject to σ𝑛𝑖=1 𝑤𝑖 𝑥𝑖 ≤ 𝑊 and 𝑥𝑖 ∈ 0,1,2,3, . . , 𝑐 .
• Unbounded Knapsack Problem
maximize σ𝑛𝑖=1 𝑣𝑖 𝑥𝑖
subject to σ𝑛𝑖=1 𝑤𝑖 𝑥𝑖 ≤ 𝑊 and 𝑥𝑖 ∈ ℤ, 𝑥𝑖 ≥ 0.
where, 𝑥𝑖 , 𝑣𝑖 , 𝑤𝑖 are the number, value, weight of item 𝑖, 𝑊 the maximum weight
capacity, 𝑛 number of all items, 𝑐 a certain constant value.

EE3491 - Programming Techniques | Control and Automation Engineering – SEEE, HUST 24


End of
Chapter 6

25

You might also like