Discussion:
fuzzy logic
(too old to reply)
Fadhil Mohammad
2016-11-02 11:35:52 UTC
Permalink
Hi. iam msc student, i have problem in my reseach, Where I need fuzzy logic code in matlab without using fuzzy logic toolbox, so, fuzzy logic file based on 2 parameters as input to fuzzy inference system (energy level, distance) and

everyone have 3 membership function

[energy level:- low . med. High]

[ distance:- close . adequate . far]

And the output be [fuzzy_cost]

And the rule of fuzzy as following:-



1. If (energy level is high)and(distance is close) then (fuzzy_cost is very low)

2. If (energy level is med)and(distance is close) then (fuzzy_cost is low)

3. If (energy level is low)and(distance is close) then (fuzzy_cost is rang low)

4. If (energy level is high)and(distance is adequate) then (fuzzy_cost is med low)

5. If (energy level is med)and(distance is adequate) then (fuzzy_cost is very low)

6. If (energy level is low)and(distance is adequate) then (fuzzy_cost is med high)

7. If (energy level is high)and(distance is far) then (fuzzy_cost is rang high)

8. If (energy level is med)and(distance is far) then (fuzzy_cost is high)

9. If (energy level is low)and(distance is far) then (fuzzy_cost is very high)

thank u for anybody who help me.

my email:- ***@yahoo.com
Walter Banks
2016-11-05 15:31:22 UTC
Permalink
Post by Fadhil Mohammad
Hi. iam msc student, i have problem in my reseach, Where I need fuzzy logic code in matlab without using fuzzy logic toolbox, so,
fuzzy logic file based on 2 parameters as input to fuzzy inference system (energy level, distance) and
everyone have 3 membership function
[energy level:- low . med. High]
[ distance:- close . adequate . far]
And the output be [fuzzy_cost]
And the rule of fuzzy as following:-
1. If (energy level is high)and(distance is close) then (fuzzy_cost is very low)
2. If (energy level is med)and(distance is close) then (fuzzy_cost is low)
3. If (energy level is low)and(distance is close) then (fuzzy_cost is rang low)
4. If (energy level is high)and(distance is adequate) then (fuzzy_cost is med low)
5. If (energy level is med)and(distance is adequate) then (fuzzy_cost is very low)
6. If (energy level is low)and(distance is adequate) then (fuzzy_cost is med high)
7. If (energy level is high)and(distance is far) then (fuzzy_cost is rang high)
8. If (energy level is med)and(distance is far) then (fuzzy_cost is high)
9. If (energy level is low)and(distance is far) then (fuzzy_cost is very high)
thank u for anybody who help me.
Fuzzy logic is about the manipulation and use of linguistic variables.
There is some material on our website that may give you an overview
of FuzzC an pre-processor to add manipulation and use of linguistic variables to C code.

http://bytecraft.com/Fuzzy_Logic

The fuzzy rules you outlined above with minor editing can be processed with FuzzC into a C program.

This page also has some examples and a small book that may be downloaded

========== Fuz code =======================

LINGUISTIC energy_level TYPE int MIN 0 MAX 150
{
MEMBER low { 0, 0, 40, 50 }
MEMBER med { 50, 70, 90 }
MEMBER high{ 70, 90, 150, 150 }
}

LINGUISTIC distance TYPE int MIN 0 MAX 100
{
MEMBER close { 0, 40, 50 }
MEMBER adequate { 30, 50, 60 }
MEMBER far{ 70, 90, 100, 100 }
}

CONSEQUENCE fuzzy_cost TYPE char MIN 30 MAX 240 DEFUZZ cg
{

MEMBER very_low { 30 }
MEMBER low { 60 }
MEMBER rang_low { 90 }
MEMBER med_low { 120 }
MEMBER med_high { 150 }
MEMBER rang_high { 180 }
MEMBER high { 210 }
MEMBER very_high { 240 }

}

FUZZY cost
{
IF energy_level IS high AND distance IS close THEN fuzzy_cost IS very_low
IF energy_level IS med AND distance IS close THEN fuzzy_cost IS_low
IF energy_level IS low AND distance IS close THEN fuzzy_cost IS rang_low
IF energy_level IS high AND distance IS adequate THEN fuzzy_cost IS med_low
IF energy_level IS med AND distance IS adequate THEN fuzzy_cost IS very_low
IF energy_level IS low AND distance IS adequate THEN fuzzy_cost IS med_high
IF energy_level IS high AND distance IS far THEN fuzzy_cost IS rang_high
IF energy_level IS med AND distance IS far THEN fuzzy_cost IS high
IF energy_level IS low AND distance IS far THEN fuzzy_cost IS very_high
}

void main (void)
{
while(1)
{

cost();
}
}



========== C coutput with defaults =============

#include "fuzzc.h"
char __IDOM[2];
/* LINGUISTIC energy_level TYPE int MIN 0 MAX 150 */
/* { */
int energy_level ;
/* MEMBER low { 0, 0, 40, 50 } */
/*

1-| ..........
| . .
| . .
| . .
0-| . ......................
----------------------------------
0 38 75 112 150

*/
char energy_level_low (int __CRISP)
{
{
if (__CRISP <= 40) return(255);
else
{
if (__CRISP <= 50)
return((( + 50 - __CRISP) * 25) + 2);
else
return(0);
}
}
}
/* MEMBER med { 50, 70, 90 } */
/*

1-| .
| . .
| . .
| . .
0-| ............ ..............
----------------------------------
0 38 75 112 150

*/
char energy_level_med (int __CRISP)
{
if (__CRISP < 50) return(0);
else
{
if (__CRISP <= 70) return(((__CRISP - 50) * 12) + 7);
else
{
{
if (__CRISP <= 90)
return((( + 90 - __CRISP) * 12) + 7);
else
return(0);
}
}
}
}
/* MEMBER high{ 70, 90, 150, 150 } */
/*

1-| ..............
| . .
| . .
| . .
0-| ................ .
----------------------------------
0 38 75 112 150

*/
char energy_level_high (int __CRISP)
{
if (__CRISP < 70) return(0);
else
{
if (__CRISP <= 90) return(((__CRISP - 70) * 12) + 7);
else
{
return(255);
}
}
}
/* } */
/*

Fuzzy Sets for energy_level

1-| .......... . ..............
| . . . . . .
| . . . . .
| . .. . . .
0-| ............ . ..............
----------------------------------
0 38 75 112 150


*/
/* LINGUISTIC distance TYPE int MIN 0 MAX 100 */
/* { */
int distance ;
/* MEMBER close { 0, 40, 50 } */
/*

1-| .
| . .
| . .
| . .
0-| . .................
----------------------------------
0 25 50 75 100

*/
char distance_close (int __CRISP)
{
{
if (__CRISP <= 40) return((__CRISP * 6) + 7);
else
{
{
if (__CRISP <= 50)
return((( + 50 - __CRISP) * 25) + 2);
else
return(0);
}
}
}
}
/* MEMBER adequate { 30, 50, 60 } */
/*

1-| .
| . .
| . .
| . .
0-| ........... ..............
----------------------------------
0 25 50 75 100

*/
char distance_adequate (int __CRISP)
{
if (__CRISP < 30) return(0);
else
{
if (__CRISP <= 50) return(((__CRISP - 30) * 12) + 7);
else
{
{
if (__CRISP <= 60)
return((( + 60 - __CRISP) * 25) + 2);
else
return(0);
}
}
}
}
/* MEMBER far{ 70, 90, 100, 100 } */
/*

1-| ....
| . .
| . .
| . .
0-| ....................... .
----------------------------------
0 25 50 75 100

*/
char distance_far (int __CRISP)
{
if (__CRISP < 70) return(0);
else
{
if (__CRISP <= 90) return(((__CRISP - 70) * 12) + 7);
else
{
return(255);
}
}
}
/* } */
/*

Fuzzy Sets for distance

1-| . . ....
| . .. . . .
| . . . . . .
| . . . . . .
0-| ........... . ..............
----------------------------------
0 25 50 75 100


*/
/* CONSEQUENCE fuzzy_cost TYPE char MIN 30 MAX 240 DEFUZZ cg */
/* { */
char fuzzy_cost ;
ERROR Defuzzification "cg.DFT" template not found
/* MEMBER very_low { 30 } */
/*

1-| .
| .
| .
| .
0-| *................................
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_very_low (char __DOM)
{
}
/* MEMBER low { 60 } */
/*

1-| .
| .
| .
| .
0-| .....*...........................
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_low (char __DOM)
{
}
/* MEMBER rang_low { 90 } */
/*

1-| .
| .
| .
| .
0-| .........*.......................
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_rang_low (char __DOM)
{
}
/* MEMBER med_low { 120 } */
/*

1-| .
| .
| .
| .
0-| ..............*..................
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_med_low (char __DOM)
{
}
/* MEMBER med_high { 150 } */
/*

1-| .
| .
| .
| .
0-| ..................*..............
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_med_high (char __DOM)
{
}
/* MEMBER rang_high { 180 } */
/*

1-| .
| .
| .
| .
0-| .......................*.........
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_rang_high (char __DOM)
{
}
/* MEMBER high { 210 } */
/*

1-| .
| .
| .
| .
0-| ...........................*.....
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_high (char __DOM)
{
}
/* MEMBER very_high { 240 } */
/*

1-| .
| .
| .
| .
0-| ................................*
----------------------------------
30 82 135 188 240


*/
void fuzzy_cost_very_high (char __DOM)
{
}
/* } */
/* FUZZY cost */
void cost (void)
{
/* { */
/* IF energy_level IS high AND distance IS close THEN fuzzy_cost IS very_low */
__IDOM[1] = energy_level_high(energy_level) ;
__IDOM[0] = distance_close(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_very_low( __IDOM[0] );
/* IF energy_level IS med AND distance IS close THEN fuzzy_cost IS_low */
__IDOM[1] = energy_level_med(energy_level) ;
__IDOM[0] = distance_close(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
__IDOM[0] = __IDOM[0];
fuzzy_cost_very_low( __IDOM[0] );
/* IF energy_level IS low AND distance IS close THEN fuzzy_cost IS rang_low */
__IDOM[1] = energy_level_low(energy_level) ;
__IDOM[0] = distance_close(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_rang_low( __IDOM[0] );
/* IF energy_level IS high AND distance IS adequate THEN fuzzy_cost IS med_low */
__IDOM[1] = energy_level_high(energy_level) ;
__IDOM[0] = distance_adequate(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_med_low( __IDOM[0] );
/* IF energy_level IS med AND distance IS adequate THEN fuzzy_cost IS very_low */
__IDOM[1] = energy_level_med(energy_level) ;
__IDOM[0] = distance_adequate(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_very_low( __IDOM[0] );
/* IF energy_level IS low AND distance IS adequate THEN fuzzy_cost IS med_high */
__IDOM[1] = energy_level_low(energy_level) ;
__IDOM[0] = distance_adequate(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_med_high( __IDOM[0] );
/* IF energy_level IS high AND distance IS far THEN fuzzy_cost IS rang_high */
__IDOM[1] = energy_level_high(energy_level) ;
__IDOM[0] = distance_far(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_rang_high( __IDOM[0] );
/* IF energy_level IS med AND distance IS far THEN fuzzy_cost IS high */
__IDOM[1] = energy_level_med(energy_level) ;
__IDOM[0] = distance_far(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_high( __IDOM[0] );
/* IF energy_level IS low AND distance IS far THEN fuzzy_cost IS very_high */
__IDOM[1] = energy_level_low(energy_level) ;
__IDOM[0] = distance_far(distance) ;
__IDOM[0] = F_AND(__IDOM[1],__IDOM[0]);
fuzzy_cost_very_high( __IDOM[0] );
/* } */
}
void main (void)
{
while(1)
{
cost();
}
}

=======================================

Walter..

Continue reading on narkive:
Loading...