The real code to do
this is listed below:
/* ********************************* */
/* FUZZY KNOWLEDGE BASE */
/* ********************************* */
mem_light(5,10,15,low). % Assumed membership
mem_light(10,15,20,high). % values ( 0,1,0)
mem_power(12,14,16,low).
mem_power(14,16,18,high).
/* ********************************* */
/* FUZZY KNOWLEDGE RULES */
/* ********************************* */
light_power(low,high). % Defined for the linguistic
light_power(high,low). % variables power and light
% with the terms low and high
/* *********************************************** */
find_membership_light_low(U,Memb,low)
:- |
|
mem_light(A,B,C,low), |
|
|
A<= U, |
|
|
U <= B, |
|
|
Memb = (U-A)/(B-A),!. |
find_membership_light_low(U,Memb,low)
:- |
|
mem_light(A,B,C,low), |
|
|
A<= U, |
|
|
U <= C, |
|
|
Memb = (C-U)/(C-B),!. |
find_membership_light_low(U,Memb,low)
:- |
|
mem_light(A,_,_,low), |
|
|
U < A, |
|
|
Memb = 0,!. |
find_membership_light_low(U,Memb,low)
:- |
|
mem_light(A,B,C,low), |
|
|
U > C, |
|
|
Memb = 0,!. |
find_membership_light_low(_,Member,low)
:- Member=0,!. % |
|
|
find_membership_light_high(U,Memb,high)
:- |
|
mem_light(A,B,C,high), |
|
|
A<= U, |
|
|
U <= B, |
|
|
Memb = (U-A)/(B-A),!. |
find_membership_light_high(U,Memb,high)
:- |
|
mem_light(A,B,C,high), |
|
|
A<= U, |
|
|
U <= C, |
|
|
Memb = (C-U)/(C-B),!. |
find_membership_light_high(U,Memb,high)
:- |
|
mem_light(A,B,C,low), |
|
|
U < A, |
|
|
Memb = 0,!. |
find_membership_light_high(U,Memb,high)
:- |
|
mem_light(A,B,C,low), |
|
|
U > C, |
|
|
Memb = 0,!. |
find_membership_light_high(_,Member,high):-Member=0,!. |
check_data( Membership1,
Membership2) :- |
|
Membership1=0, |
|
Membership2=0, |
|
Write("Not enough data
in the program."),nl,exit. |
check_data( _, _). % this
is for normal cases for the |
% |
|
goal to succeed as it will
fail in the previous clause. |
/* *********************************** */
/* DEFUZZIFICATION MODULE */
/**************************************/
defuzz(U,Ans):- find_membership_light_low(U,Memb,low), |
|
find_membership_light_high(U,Memb1,high), |
|
|
light_power(low,X), |
|
|
light_power(high,X1), |
|
|
mem_power(A,B,C,X), |
|
|
mem_power(A1,B1,C1,X1), |
|
|
check_data(Memb,Memb1), |
|
|
Ans = ( ( ( (A+C)/2 ) * Memb
) + |
|
|
( ( (A1+C1)/2 ) * Memb1 )
)/ (Memb + Memb1). |
The fuzzy engine can be triggered by calling
the defuzz
predicate. U being the input light density and
'Ans' being the answer in voltage. |
|
We derive the output value using the weighted
average method.
|
Z* = x . l + y . k / x + y |
|
|
|
Z*= (0.8 x 16) + (0.2 x 14) / (0.8
+ 0.2)
= 12.8 + 2.8 / 1 = 15.6 |
|
|
|
So the output value is 15.6 |
Implementation of this Fuzzy System in Visual Prolog
To make our understanding of the Fuzzy Program
easy
we will first look at how the individual fuzzy structures
are mapped to Visual Prolog structures.
The following listing illustrates how Fuzzy sets
are
implemented:
|
mem_light(5,10,15,low).
mem_light(10,15,20,high).
mem_voltage(12,14,16,low).
mem_voltage(14,16,18,high). |
The fuzzy sets for light and voltage are defined
as Visual
Prolog facts. The fuzzy set for the term, 'low' is
indicated by
mem_light(5,10,15,low).
|
|
The left and the right most values
have an assumed membership function of 0 and the middle
value has 1.
The fuzzy knowledge base or the fuzzy rules are
again
defined as Visual Prolog clauses.
light_voltage(low,high).
light_voltage(high,low).
The first clause means that if the light is 'low'
the value for
voltage will be 'high'. The code for this example is
in the box above.
References
Yan, J., Ryan M., and Power, J., "Using Fuzzy
Logic",
Prentice Hall, Hertfordshire, UK, 1994.
Ross, T. J., "Fuzzy Logic with Engineering
Applications",
McGraw Hill, New York, 199
Alroy Mascrenghe is currently reading
for his Masters (specialising in e-commerce) awarded by Charles Sturt
University, Australia (through affiliate programme). His research
interests include Object Oriented Method-ology, Software Process Improvement,
Knowledge Representation, AI and E-commerce. He has done research
on Knowledge Representaion and AI techniques for his BCS dissertation
- with work published in IEEE. He can be contacted at alroy@sltnet.lk.
PDC can be reache at www.pdc.dk
or sales@pdc.dk.
|
|