Artificial Intelligence

Download Report

Transcript Artificial Intelligence

AI – CS289
Fuzzy Logic - Labs
Fuzzy Logic – Lab 3
19th October 2006
Dr Bogdan L. Vrusias
[email protected]
AI – CS289
Fuzzy Logic - Labs
Exercise
•
Cart Pole Problem
From week 7 (week7_Fuzzy_Logic_Tutorial.ppt)

m
w
g
M
The problem is to balance an upright pole, with a mass m at its head and mass
M at its base. A weightless shaft connects these two masses. The base can be
moved on a horizontal axis. The task is to determine the force (F) necessary
to balance the pole. The calculation of the force F involves the measurement
of the angle θ and the angular velocity ω of the pole.
19th October 2006
Bogdan L. Vrusias © 2006
2
AI – CS289
Fuzzy Logic - Labs
Exercise: Cart Pole Problem

nb
nm
nb
ns
az
ns
pb
nm
w
19th October 2006
pm
pb
pm
pb
pm
ns
nm
ns
ps
az
nb
ns
az
ps
ps
ns
ps
pm
nm
pb
nb
nm
nb: negative big, nm: negative medium,
ps: positive small,
pm: positive medium,
IF
AND
THEN
ps
θ
w
F
is
is
is
pm
ps
ns: negative small az: approximately zero
pb: positive big
negative medium
approximately zero
negative medium
Bogdan L. Vrusias © 2006
3
AI – CS289
Fuzzy Logic - Labs
Exercise: Cart Pole Problem
The fuzzy sets for θ, and F are based on the linear equation
μ(x)=ax + b, and are defined based on the following table:
m(w)=1 if
w
w
w
m(w)=1 if
w=
m(w)=0 if
w
19th October 2006
Bogdan L. Vrusias © 2006
4
AI – CS289
Fuzzy Logic - Labs
Example – Getting Started
•
•
•
•
•
Start Matlab and the Fuzzy Inference System (FIS) editor (see
previous Lab how to launch Matlab).
Encode the rules and fuzzy sets into Matlab’s FIS.
Try to save the system to your home directory as cartpole.fis
Use the Rule Viewer to test your system with different inputs.
Follow the next slides to test your system using Matlab’s command
line.
19th October 2006
Bogdan L. Vrusias © 2006
5
AI – CS289
Fuzzy Logic - Labs
Example – Command line
• To evaluate the output of a fuzzy system for a given input, use the
function evalfis. For example, the following script evaluates tipper at
the input, [50 -5].
a = readfis(‘cartpole.fis');
evalfis([50 -5], a)
ans =
5.3…
• This function can also be used for multiple collections of inputs, since
different input vectors are represented in different parts of the input
structure.
evalfis([50 -5; 0 0], a)
ans =
5.3…
0.0…
19th October 2006
Bogdan L. Vrusias © 2006
6
AI – CS289
Fuzzy Logic - Labs
Example – Command line
•
Set some initial variables as:
b = 45; (that will be your angle)
c = 45; (that will be your angular velocity)
f = 7.5; (that will be the output force)
•
Write a loop statement to balance the pole between -5 and 5 degrees:
a = readfis(‘cartpole.fis');
while -5 > f || f > 5
f = evalfis([b c], a)
b = -b*90/100; %this reduces the angle by 10%
c = -c*90/100; %this reduces the velocity by 10%
end
•
This example assumes that the complicated formulae that calculates the new
angle and velocity of the pole is 10% less, in the opposite direction, each time
we apply the output force.
19th October 2006
Bogdan L. Vrusias © 2006
7