VIP Call Girls Service Mehdipatnam Hyderabad Call +91-8250192130
Ece478 12es_final_report
1. ECE478
FINAL REPORT
Applying Fuzzy Logic
to Control a Submarine
CLASS 12ES
GROUP MEMBERS:
Đinh Tuấn Anh
Nguyễn Thanh Sang
Đoàn Công Danh
Tạ Đình Sang
2. Table of Contents
I. Fuzzy logic and fuzzy controller.............................................................................................3
1. Fuzzy Logic...........................................................................................................................3
2. Working-Process..................................................................................................................4
3. Fuzzy Logic Algorithm.........................................................................................................5
I. Fuzzy Logic Algorithm ofControlling a Submarine...............................................................6
1. Linguistic Variables .............................................................................................................6
2. Membership functions.........................................................................................................7
a. Popular Membership Functions:.........................................................................................7
b. Membership Functions in Cotrolling a Submarine ..............................................................8
3. Fuzzy Rules.........................................................................................................................10
4. Convert the output data to non-fuzzy values (defuzzification) ......................................13
1. main function........................................................................................................................15
2.double triangular....................................................................................................................16
3.Assigning rules and returns the angles two blades rotate..........................................................18
4. max-min method...................................................................................................................30
5. Result..................................................................................................................................32
6. Conclusion..........................................................................................................................36
Figures
Figure 1: A Fuzzy Logic System (FLS)................................................................................................4
Figure 2:Examples of Popular Membership Functions.......................................................................7
Figure 3: Triangular Membership Function.......................................................................................9
Figure 4: Depth Membership Function..............................................................................................9
Figure 5: Left-Right Obstacle Membership Function..........................................................................9
Figure 6: Velocity Membership Function.........................................................................................10
Figure 7.Blade1-Blade2 Membership Function................................................................................10
Figure 8: Weighted Average Method .....................................................Error! Bookmark not defined.
3. I. Fuzzy logic and fuzzy controller
1. Fuzzy Logic
Fuzzy logic is an approach to computing based on "degrees of truth" rather than
the usual "true or false" (1 or 0) Boolean logic on which the modern computer is
based.
The idea of fuzzy logic was first advanced by Dr. Lotfi Zadeh of the University of
California at Berkeley in the 1960s. Dr. Zadeh was working on the problem of
computer understanding of natural language. Natural language (like most other
activities in life and indeed the universe) is not easily translated into the absolute
terms of 0 and 1. (Whether everything is ultimately describable in binary terms is a
philosophical question worth pursuing, but in practice much data we might want to
feed a computer is in some state in between and so, frequently, are the results of
computing.) It may help to see fuzzy logic as the way reasoning really works and
binary or Boolean logic is simply a special case of it.
Fuzzy logic includes 0 and 1 as extreme cases of truth (or "the state of matters" or
"fact") but also includes the various states of truth in between so that, for example,
the result of a comparison between two things could be not "tall" or "short" but ".38
of tallness."
Fuzzy logic seems closer to the way our brains work. We aggregate data and form
a number of partial truths which we aggregate further into higher truths which in
turn, when certain thresholds are exceeded, cause certain further results such as
motor reaction. A similar kind of process is used in neural networks, expert
systems and other artificial intelligence applications. Fuzzy logic is essential to the
development of human-like capabilities for AI, sometimes referred to as artificial
general intelligence: the representation of generalized human cognitive abilities in
software so that, faced with an unfamiliar task, the AI system could find a solution.
4. 2. Working-Process
Figure 1: A Fuzzy Logic System (FLS)
Firstly, a crisp set of input data are gathered and converted to a fuzzy set using
fuzzy linguistic variables, fuzzy linguistic terms and membership functions. This
step is known as fuzzification. Afterwards, an inference is made based on a set of
rules. Lastly, the resulting fuzzy output is mapped to a crisp output using the
membership functions, in the defuzzification step.
Fuzzy sets
A fuzzy set is represented by a membership function defined on the universe of
discourse. The universe of discourse is the space where the fuzzy variables are
defined. The membership function gives the grade, or degree, of membership within
the set, of any element of the universe of discourse. The membership function maps
the elements of the universe onto numerical values in the interval [0, 1]. A
membership function value of zero implies that the corresponding element is
definitely not an element of the fuzzy set, while a value of unity means that the
element fully belongs to the set. A grade of membership in between corresponds to
the fuzzy membership to set. In crisp set theory, if someone is taller than 1.8 meters,
we can state that such person belongs to the “set of tall people”. However, such
5. sharp change from 1.7999 meters of a “short person” to 1.8001 meters of a “tall
person” is against the common sense.
Fuzzifcation
Fuzzification is the process of decomposing a system input and/or output into
one or more fuzzy sets. Many types of curves can be used, but triangular or
trapezoidal shaped membership functions are the most common because they are
easier to represent in embedded controllers. Fig 1 shows a system of fuzzy sets for
an input with trapezoidal and triangular membership functions. Each fuzzy set
spans a region of input (or output) value graphed with the membership. Any
particular input is interpreted from this fuzzy set and a degree of membership is
interpreted. The membership functions should overlap to allow smooth mapping of
the system. The process of fuzzification allows the system inputs and outputs to be
expressed in linguistic terms so that rules can be applied in a simple manner to
express a complex system.
Defuzzification
After fuzzy reasoning we have a linguistic output variable which needs to be
translated into a crisp value. The objective is to derive a single crisp numeric value
that best represents the inferred fuzzy values of the linguistic output variable.
Defuzzification is such inverse transformation which maps the output from the
fuzzy domain back into the crisp domain. Some defuzzification methods tend to
produce an integral output considering all the elements of the resulting fuzzy set
with the corresponding weights. Other methods take into account just the elements
corresponding to the maximum points of the resulting membership functions.
3. Fuzzy Logic Algorithm
1. Define the linguistic variables and terms (initialization)
2. Construct the membership functions (initialization)
3. Construct the rule base (initialization)
4. Convert crisp input data to fuzzy values using the membership functions
(fuzzification)
5. Evaluate the rules in the rule base (inference)
6. 6. Combine the results of each rule (inference)
7. Convert the output data to non-fuzzy values (defuzzification)
I. Fuzzy Logic Algorithm of Controlling a Submarine
1. Linguistic Variables
Linguistic variables are the input or output variables of the system whose values
are words or sentences from a natural language, instead of numerical values. A
linguistic variable is generally decomposed into a set of linguistic terms.
Fuzzy sets for depth:
Depth = {Low, Medium, High}
Fuzzy sets for left obstacles:
Left_Obstacles = {Close, Near, Far}
Fuzzy sets for right obstacles:
Right_Obstacles = {Close, Near, Far}
Fuzzy sets for velocity:
Velocity = {Slow, Medium, Fast}
Fuzzy sets for blade 1:
Blade1 = {Lowlow, Low, Zero, High, Highhigh};
Fuzzy sets for blade 2:
Blade2 = {Lowlow, Low, Zero, High, Highhigh};
7. 2. Membershipfunctions
Membership functions are used in the fuzzification and defuzzification steps of a
FLS, to map the non-fuzzy input values to fuzzy linguistic terms and vice versa. A
membership function is used to quantify a linguistic term.
a. Popular Membership Functions:
Figure 2:Examples of Popular Membership Functions
The simplest membership functions are formed using straight lines. Of these, the
simplest is the triangular membership function, and it has the function name trimf.
This function is nothing more than a collection of three points forming a triangle.
The trapezoidal membership function, trapmf, has a flat top and really is just a
truncated triangle curve. These straight line membership functions have the
advantage of simplicity.
Two membership functions are built on the Gaussian distribution curve: a simple
Gaussian curve and a two-sided composite of two different Gaussian curves. The
two functions aregaussmf and gauss2mf.
The generalized bell membership function is specified by three parameters and
has the function name gbellmf. The bell membership function has one more
8. parameter than the Gaussian membership function, so it can approach a non-fuzzy
set if the free parameter is tuned. Because of their smoothness and concise notation,
Gaussian and bell membership functions are popular methods for specifying fuzzy
sets. Both of these curves have the advantage of being smooth and nonzero at all
points.
Although the Gaussian membership functions and bell membership functions
achieve smoothness, they are unable to specify asymmetric membership functions,
which are important in certain applications. Next, you define
the sigmoidal membership function, which is either open left or right. Asymmetric
and closed (i.e. not open to the left or right) membership functions can be
synthesized using two sigmoidal functions, so in addition to the basic sigmf, you also
have the difference between two sigmoidal functions, dsigmf, and the product of two
sigmoidal functions psigmf.
Polynomial based curves account for several of the membership functions in the
toolbox. Three related membership functions are the Z, S, and Pi curves, all named
because of their shape. The function zmf is the asymmetrical polynomial curve open
to the left, smf is the mirror-image function that opens to the right, and pimf is zero
on both extremes with a rise in the middle.
There is a very wide selection to choose from when you're selecting a
membership function. You can also create your own membership functions with the
toolbox. However, if a list based on expanded membership functions seems too
complicated, just remember that you could probably get along very well with just
one or two types of membership functions, for example the triangle and trapezoid
functions. The selection is wide for those who want to explore the possibilities, but
expansive membership functions are not necessary for good fuzzy inference
systems. Finally, remember that more details are available on all these functions in
the reference section.
b. Membership Functionsin Cotrollinga Submarine
10. Figure 6: Velocity Membership Function
Figure 7.Blade1-Blade2 Membership Function
3. Fuzzy Rules
In a FLS, a rule base is constructed to control the output variable. A fuzzy rule is a
simple IF-THEN rule with a condition and a conclusion.
Let:
L1: depth is low
L2: left obstacle is close
L3: right obstacle is close
L4: velocity is slow
11. M1: depth is medium
M2: left obstacle is near
M3: right obstacle is near
M4: velocity is medium
H1: depth is high
H2: left obstacle is far
H3: right obstacle is far
H4: velocity is fast
Z/Z or Z/L … stands for blade1/blade2.
Then, we have the rule table below.
Table 1. Rule Table of Fuzzy Logic
L1 L2 L1 M2 L1 H2 M1 L2 M1 M2 M1 H2 H1 L2 H1 M2 H1 H2
L3 L4 Z/Z L/H L/H Z/Z L/H L/H Z/Z L/L L/L
L3 M4 Z/Z L/H LL/H Z/Z L/H LL/H Z/Z L/L LL/L
L3 H4 Z/Z LL/H LL/H Z/Z LL/H LL/H Z/Z LL/L LL/L
M3 L4 H/H Z/H L/HH H/H Z/H L/H H/L Z/L L/LL
M3 M4 H/H Z/H L/HH H/H Z/H L/HH H/L Z/L L/LL
M3 H4 HH/H Z/HH L/HH HH/H Z/H L/HH HH/L H/LL L/LL
H3 L4 HH/H Z/HH Z/HH HH/H H/HH Z/HH HH/L HH/LL Z/LL
12. H3 M4 HH/H Z/HH Z/HH HH/H HH/H Z/HH HH/L H/L Z/LL
H3 H4 HH/H Z/HH Z/HH HH/H H/H Z/HH HH/L H/L Z/LL
According to table 1, we have 81 rules for two output blade1 and blade2
(1) if (Depth(Low) and Left_Obstacle(Far) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero
(2) if (Depth(Low) and Left_Obstacle(Near) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is High
(3) if (Depth(Low) and Left_Obstacle(Close) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is High
(4) if (Depth(Medium) and Left_Obstacle(Far) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero
(5) if (Depth(Medium) and Left_Obstacle(Near) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is High
(6) if (Depth(Medium) and Left_Obstacle(Close) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is High
(7) if (Depth(High) and Left_Obstacle(Far) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero
13. (8) if (Depth(High) and Left_Obstacle(Near) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is Low
(9) if (Depth(High) and Left_Obstacle(Close) and Right_Obstacle(Far) and
Velocity(Slow)) then Blade1 is Low and Blade2 is Low
…
In the C codes we discuss in next section, we will have full of Rules.
4. Convert the output data to non-fuzzy values (defuzzification)
After the inference step, the overall result is a fuzzy value. This result should be
defuzzified to obtain a final crisp output. This is the purpose of the defuzzifier
component of a FLS. Defuzzification is performed according to the membership
function of the output variable.
In this project, we choose Weighted Average Method because this method is valid
for symmetrical output membership functions.
14. The follwing codes based on C programming language run on CodeBlocks
platform.
#include <stdio.h>
#include <stdlib.h>
void assignRule_Blade (double L1, double L2, double L3, double L4,
double M1, double M2, double M3, double M4, double H1, double H2,
double H3, double H4, double blade1, double blade2);
double triangular (double low, double mid, double high, double x);
void Depth (double x, double a[]);
void Left_Obstacles (double x, double a[]);
void Right_Obstacles (double x, double a[]);
void Velocity (double x, double a[]);
double min4 (double x, double y, double m, double n);
/* The C sub-functions:
void assignRule_Blade returns values of two blades (1 and 2)
double triangular returns the probability of number x in the triangle.
15. void Depth builds Depth membership function based on triangular function.
void Left_Obstacles builds Left_Obstacles membership function based on
triangular function.
void Right_Obstacles builds Right_Obstacles membership function based on
triangular function.
void Velocity builds Velocity membership function based on triangular
function.
double min4 returns the minimum value of four given numbers.
*/
1. main function
int main()
{
double D[3], Le[3], Ri[3], V[3];
double L1, L2, L3, L4, M1, M2, M3, M4, H1, H2, H3, H4;
Depth(52.4,D);
Left_Obstacles (3.91,Le);
Right_Obstacles(3.91,Ri);
Velocity(10.2,V);
L1 = D[0];
M1 = D[1];
H1 = D[2];
L2 = Le[2];
M2 = Le[1];
32. 5. Result
The following figures show the results of simulating fuzzy logic on Matlab (above)
and implementing C programming on CodeBlocks (below).
33.
34.
35.
36. After testing many times, we get other results recorded in table 2
Table2. Theinputparametersandresults comparison
Depth
Left
Obstacle
Right
Obstacle
Velocity
Matlab’s
results
Blade1/Blade2
CodeBlock’s
results
Blade1/Blade2
75 5 5 75 90/62 92/64.55
80 6 9 18 13/53 15/50
10 6 9 18 17/129 15/130
29.89 8.97 5.3 6.17 129/133 130/130
25.62 3.42 5.32 17.17 68/150 68.8/151.264
15.8452 8.472 2.32 7.127 159/135 162/130
90 9.472 9.32 9.127 90/90 92/90
7 9 9.32 9.13 90/90 92/90
35 4.6 9.5 9.7 53/127 52/130
6. Conclusion
There are some little differences between Matlab and CodeBlocks. This comes
from the difference of processing input and defuzzification method.
If time permits, we think we can improve the C codes to enhance the quality of
results efficiently.