,,,
85 , 19 , 6 , 8 .
, - ; - . .
SUMMARY
This project contains 85 pages of text, 19 diagrams, 6 tables and 8 literature sources.
This bachelors project is dedicated to development of fuzzy regulator for container crane. To solve this real-world problem a fuzzy algorithm to control container crane has been developed. Further to model this fuzzy regulator, fuzzy logic modeling software was successfully developed.
, , ,
: - .
- , . - .
- .
-.
- .
VT(t)-
a-
a-
da -
-
dVT
V
PMV - V ( )
PSV - V
ZRV - V
NSV - V ( )
NMV V
PMda - da ( )
PSda - da
ZRda - da
NSda - da ( )
NMda - da
PS-
ZR-
NS-
PM- dVT ( )
PS- dVT
ZR- dVT
NS- dVT ( )
NM- dVT
-
-
-
-
-
11
1. - 14
1.1. - 14
1.3. 18
1.4. 20
2. - 23
3. .33
3.1 .33
3.2 .34
3.3 36
3.4 37
4. 39
4.1 39
4.2 39
4.3 .40
4.4 42
5. 44
5.1. 44
5.1.1. 44
5.1.2. .46
6. 47
6.1. 47
6.2. 52
6.3. 55
6.3.1. 55
7. 58
59
60
61
. , - , .
, . , , , , .
, , , - . , , .
- . - . - . , , .
, , , . . . , , , , ..
.. , - , .
, - .
:
1. IDE , ,
2. ,
3. .
, , - .
, .. - .
, , , .
, , - .
, , .
.
, .
, , .
1. -
1.1. -
, - , , , - . , , , .
. . , , . - .
, . , - , , , , , , .
- . 1.1
. 1.1. 1 , 2 , 3 , 4
, . . 1.2 , .
. 1.2 VT(t) - , a(t) - , m -
: 1+2=3, - ; - , ; M3=mgl×sina(t) - , .
:
,(1.1)
a(t=0)=a0, - - .
, , [2].
1.2.
:
1. - ,
2.
3. .
, ( ).
. , , .
, .
, , (. 1.3). . :
. 1.3.
1.3.
, . , , ( , , , , ). - .
, , ( , , ), , . , , , . , .
. () ( ) [3].
, , . :
- , , , ();
- , ;
- , ;
- , ;
- , , .
X ={mA(x)/x}, xÎX, mA - D [0,1], . mA(x) xÎX .
() [5, 6]:
<ai,T(ai),X,G,M>, (1.2)
aI - i - ; T(ai) - - ai; - T(ai); G - (), (j- ) ; M - , () - .
() , - aI,
(1.3)
- ; - ; - , - , .
- , , (). , .
1.4.
:
- ;
- ;
- .
. , , . : , -, - .
, , , , . , , .
, , .
, [7] , [8] . [9]:
(W,T,H), (1.4)
- W´H, - , - , .
H - - . .
W - , W=W1´W2´´Wn, . - - - . - . . , pj,. pj , . pj
s,
(1.5)
:
(W,T,H), W=W1´W2´W3´W4, (1.6)
.
1. t0 W . w0 hi.
2.
3. hs .
.
2. -
- - .
X={X1,X2,X3} : 1 - , X2 - , X3 .
g- , - (g)={g1,g2,g3,g4,g5}, : g1 - PMda - da ( ) ; g2 - PSda - da ; g3 - ZRda - da ; g4 - NSda - da ( ) ; g5 - NMda da . .
. .
b - , - (b)={b1,b2,b3}, : b1 - PS ; b2 - ZR- ; b3 - NS . .
e - , - (e)={e1,e2,e3,e4,e5}, : e1 - PM- dVT ( ) ; NM dVT ; e2 - PS dVT ; e3 - ZR dVT ; e4 - NS dVT ( ) ; e5 - NM dVT . .
H={h1,h2,h3,h4,h5} : h1=PMV - V ( ) ; h2=PSV - V ; h3=ZRV - V ; h4=NSV - V ( ) ; h5=NMV - V .
. , - . V(t) .1.1 VT(t) a(t). , , V(t).
4 .
da=a-a, a - , a - a; dVT=VT-VT, VT , VT - ; , V .
(): da - , - ; dVT ; V . , ().
- da: (da)={PMda - da ( ) ; PSda - da ; ZRda - da ; NSda - da ( ) ; NMda da }. . 2.1 mda - (da).
. 2.1
- : ()={PS- ; ZR- ; NS- }. . 2.2 - ().
. 2.2
- dVT: (dVT)={PM- dVT ( ) ; PS- dVT ; ZR- dVT ; NS- dVT ( ) ; NM dVT }. . 2.3 m(dVT) - (dVT).
. 2.3
- dV: (V)={PMV - V ( ) ; PSV - V ; ZRV - V ; NSV - V ( ) ; NMV V }. . 2.4 m(V) - (V).
. 2.4
. O1,O2,O3 :
- , .. , , , , , , , ;
- , .. . , , .
O1 VO(t) VT(t)>0, VTi(t)>VTi-1(t).
Ri :
R1: ( ) da=NSda ( ) =NS dVT= PS, V=PSV;
R2: ( ) da=NSda =ZR dVT= PS, V=PSV;
R3: ( ) da=NSda ( ) =PS dVT=PS, V=PSV;
:
R4: da=NMda =NS dVT= PS, V=PMV;
R5: da=NMda =ZS dVT= PS, V=PMV;
R6: da=NMda =PS dVT= PS, V=PMV;
R7: da=PSda =PS dVT= PS, V=NSV;
R8: da=PSda =ZR dVT= PS, V=NSV;
R9: da=PSda =NS dVT= PS, V=NSV;
R10: da=PMda =PS dVT= PS, V=NMV;
R11: da=PMda =ZR dVT= PS, V=NMV;
R12: da=PMda =NS dVT= PS, V=NMV;
R13: da=NSda =NS dVT= PM, V=PSV;
R14: da=NSda =ZR dVT= PM, V=PSV;
R15: da=NSda =PS dVT= PM, V=PSV;
R16: da=NMda =NS dVT= PM, V=PMV;
R17: da=NMda =ZS dVT= PM, V=PMV;
R18: da=NMda =PS dVT= PM, V=PMV;
R19: da=PSda =PS dVT= PM, V=NSV;
R20: da=PSda =ZR dVT= PM, V=NSV;
R21: da=PSda =NS dVT= PM, V=NSV;
R22: da=PMda =PS dVT= PM, V=NMV;
R23: da=PMda =ZR dVT= PM, V=NMV;
R24: da=PMda =NS dVT= PM, V=NMV;
Ri :
R25: da=ZRda =ZR dVT= ZR, V=PSV;
:
R26: da=ZRda =ZR dVT= ZR, V=PMV;
R27: da=ZRda =ZR dVT= ZR, V=NSV;
R28: da=ZRda =ZR dVT= ZR, V=NMV;
R29: da=ZRda =ZR dVT= ZR, V=PMV;
O2 - .
, VT VT ZR, .. . VO(t) :
Ri :
R1: da=NSda =NS VT= ZR V=PSV;
R2: da=NSda =ZR dVT= ZR, V=PSV;
R3: da=NSda =PS dVT= ZR V=PSV;
R4: da=PSda =PS dVT= ZR V=NSV;
R5: da=PSda =ZR dVT= ZR V=NSV;
R6: da=PSda =NS dVT= ZR V=NSV;
Ri :
R7: da=ZRda =ZR dVT= ZR, V=PSV;
R8: da=ZRda =ZR dVT= ZR, V=PMV;
R9: da=ZRda =ZR dVT= ZR, V=NSV;
R10: da=ZRda =ZR dVT= ZR, V=NMV;
R11: da=ZRda =ZR dVT= ZR, V=PMV;
O3 - , VTi(t)<VTi-1(t). VO(t).
Ri :
R1: da=NSda =NS dVT= PN, V=PSV;
R2: da=NSda =ZR dVT= NS, V=PSV;
R3: da=NSda =PS dVT= NS, V=PSV;
R4: da=NMda =NS dVT= NS, V=PMV;
R5: da=NMda =ZS dVT= NS, V=PMV;
R6: da=NMda =PS dVT= NS, V=PMV;
R7: da=PSda =PS dVT= NS, V=NSV;
R8: da=PSda =ZR dVT= NS, V=NSV;
R9: da=PSda =NS dVT= NS, V=NSV;
R10: da=PMda =PS dVT= NS, V=NMV;
R11: da=PMda =ZR dVT= NS, V=NMV;
R12: da=PMda =NS dVT= NS, V=NMV;
R13: da=NSda =NS dVT= NM, V=PSV;
R14: da=NSda =ZR dVT= NM, V=PSV;
R15: da=NSda =PS dVT= NM, V=PSV;
R16: da=NMda =NS dVT= NM, V=PMV;
R17: da=NMda =ZS dVT= NM, V=PMV;
R18: da=NMda =PS dVT= NM, V=PMV;
R19: da=PSda =PS dVT= NM, V=NSV;
R20: da=PSda =ZR dVT= NM, V=NSV;
R21: da=PSda =NS dVT= NM, V=NSV;
R22: da=PMda =PS dVT= NM, V=NMV;
R23: da=PMda =ZR dVT= NM, V=NMV;
R24: da=PMda =NS dVT= NM, V=NMV;
Ri :
R25: da=ZRda =ZR dVT= ZR, V=PSV;
R26: da=ZRda =ZR dVT= ZR, V=PMV;
R27: da=ZRda =ZR dVT= ZR, V=NSV;
R28: da=ZRda =ZR dVT= ZR, V=NMV;
R29: da=ZRda =ZR dVT= ZR, V=PMV;
O4 VO(t) VT(t)=0:
R1: da=NSda =NS dVT=ZR, V=ZRV;
R2: da=NSda =ZR dVT=ZR, V=NSV;
R3: da=NSda =PS dVT=ZR, V=NSV;
R4: da=ZRda =NS dVT=ZR, V=PSV;
R5: da=ZRda =ZR dVT=ZR, V=ZRV;
R6: da=ZRda =PS dVT=ZR, V=NSV;
R7: da=PSda =NS dVT=ZR, V=PSV;
. 2.5 O4. , 2.5, . , , -, .
. 2.5
3.
3.1
. , . .3.1.
. 3.1
, .
, .
, , ...
, .
, .
.
, .
3.2 .
. : . , , , , . , .
- (. 3.2.).
. 3.2
3.3
. Visual Basic .Net
Microsoft.
, .
. 3.3 .
. 3.3 .
3.4
3.4.1 .
static void Main(). Application.EnableVisualStyles();. , 蠠 Application.SetCompatibleTextRenderingDefault(false); . , Application.Run(new Form1()); . try catch.
3.4.2 .
CreateNewProject(); . New_project, (, , ... ) .
.
3.4.3 .
. , , ... :
1. NewLingVariable() new_ling_variable, . (, ...)
2. DebugCurrProject() , , . .
3. CreateRuleBase() . .
4. CreateGraph() .
3.4.4 .
. , , 2- 3- .
.
4..
4.1
, (. .4.1) New Project ( ) Open Project ( ).
. 4.1.
4.2
New project ( ), Generate Project (. . 4.2), . .
. 4.2. .
4.3 .
, (. .4.3), CreateLingVariable ( ). . (/), .
. 4.3 .
, , .
. 4.4 Angle
. 4.5 Distance
, Power . 4.6.
. 4.6. Power
4.4
, . , . . 4.7
. 4.7 .
4.5
, . Debug (..4.8)
. 4.8. .
5.
- . , , , . - , .
- , , . , , , - .
, , , - .
5.1.
5.1.1.
:
= Z + + ,
Z ( , 30 , );
;
.
, :
Z = z (1 + /100) (1 + /100),
z ;
(=26%);
.
:
z = 500 .;
= 15%;
Z = 500 30 (1 + 26 / 100) (1 + 15 / 100) =21735 (.)
, :
= C t,
C 1 ;
t ();
308=240 . 160 . . . . t=160 . C = 20 .;
:
C = 20 .;
t = 160 ;
= 160 * 20 = 3200 .
80% - 120% , . . = 17388 .
, :
=21735+3200+17388=42323 (.)
5.1.2. .
(. .) (, , ) :
S (.).
F ().
.
, :
: 1 ..;
6.
. , , () - .
. , , . , . , .
6.1.
. , . . . - , , . , . .
. ( ). . , , , , .
. : ( 1.0); ( 2.0); : 1 ( 3.1), 2 ( 3.2), 3 ( 3.3), 4 ( 3.4) () ( 4.0). . .
6.1
6.1
1 | () , | 3.2 | |
2 | () | 3.1 | |
3 | , | 3.1 | |
4 | 2.0 | ||
5 | ( % ) | 51 75 % | 3.1 |
6 | , 1 | 1500 | 3.2 |
7 | ( 0,5 ) (% ) | 5 -60% | 1.0 |
8 | (, . .) (% ) | 1.0 | |
9 | ( ): - | 6 | 3.2 |
10 | 1.0 | ||
11 | 1.0 | ||
12 | 8 | 2.0 | |
13 | . . | . | 1.0 |
14 | 1.0 | ||
15 | 1.0 | ||
16 | ( ) | 25 100 | 2.0 |
17 | (), | 9 - 6 | 2.0 |
18 | ( % ). | 20 | 1.0 |
19 | ( % ) | 75 | 3.1 |
20 | 5-6 | 1.0 | |
21 | 1.0 | ||
22 | , : 7 % | 1.0 |
6.2
6.2
() | |
1.0 | 12 |
2.0 | 4 |
3.1 | 4 |
3.2 | 2 |
, 3.1, , . , , .
6.2 3.1 3.2 :
( );
() ( );
( , , );
, ( , , ).
% ( , ).
6.2.
( 0,001). , 2000...2400 .
. , , , , . , - , , - . . : . : , . , . ( . asthenēs + ōps ; , ) 40-45 . 4- , , 4- , , , .
, - . - () . , , . , , .
, , . , , () , , , .
, , , , :
5 70 ;
15-20 , 30-40 - , , ( ) ( );
60-70 ;
1,5-2,0 1,5-2,0 20 15 . ;
2- ;
;
;
.
, , ( , , , ; 10-8 ), ( ), ( ) ( , ). - . .
6.3.
:
20 - 1000 ;
;
200- 400 ;
1050 - 1 ;
> 1,2 .
6.3.1.
: : , , , , . . . , . (. 6.3).
6.3
|
50 |
20 - 100 | |
48 - 160 | |
15 110 | |
( ) | 0 () |
() |
50 - 1000 |
/ |
0 , 50 |
|
50 , 20 - 100 |
, , , , 6.4
6.4
( ) | |
50 | |
50 | |
50 | |
50 | |
50 | |
0- 15,6 | |
30- 300 | |
0- 1000 |
, MPR II, 150 (. 6.5)
6.5
|
|
|||||
|
0,5 |
0,3 |
0,5 |
0,3 |
||
|
|
|
|
|
|
|
, , |
||||||
, 5- 2, | <200 | <200 | <200 | 260 | 500 | 730 |
, 2- 400 , | <10 | 13 | 52 | 52 | ||
, 5- 2, / | <10 | 17 | 74 | 152 | ||
, 2- 400 , / | 1,7 | 1,9 | 4,2 | 12 | 12 | 32 |
, | 500 | 500 | 500 | 19900 | 19000 | 19000 |
. , , , , .
7.
, , . - . , , , , , .
, , , .
- , , .
, , .
.
, - .
1. , - : / . ..; 2-. .: - . .., 2002. 744 .
2. .. : . . : , 2005. 118 .
3. /.., .., .., .., ... ‑ .: , 1986. ‑ 312 .
4. .., .. // VII , . , , 2004.
5. . . - .: , 1976. - 165 .
6. Zadeh L.A. Fuzzy logic and approximate reasoning // Synthese, 1975. - V. 80. P. 407 - 428.
7. .., .. . ‑ --.: - , 1990. - 128 .
8. .., .. . --: - . -, 1993. - 134 .
1) FuzzyRule.cs
/*
* biblioteka dlya cozdaniya ne4etkovo blocka plavil
*/
using System;
using System.Collections.Generic;
namespace AI.Fuzzy.Library
{
// Alias for a fuzzy single condition
using FuzzyCondition = SingleCondition<FuzzyVariable, FuzzyTerm>;
// Alias for a fuzzy conclusion
using FuzzyConclusion = SingleCondition<FuzzyVariable, FuzzyTerm>;
// Alias for a conclusion for Sugeno fuzzy systems
using SugenoConclusion = SingleCondition<SugenoVariable, ISugenoFunction>;
/// <summary>
/// And/Or operator type
/// </summary>
public enum OperatorType
{
/// <summary>
/// And operator
/// </summary>
And,
/// <summary>
/// Or operator
/// </summary>
Or
}
/// <summary>
/// Hedge modifiers
/// </summary>
public enum HedgeType
{
/// <summary>
/// None
/// </summary>
None,
/// <summary>
/// Cube root
/// </summary>
Slightly,
/// <summary>
/// Square root
/// </summary>
Somewhat,
/// <summary>
/// Square
/// </summary>
Very,
/// <summary>
/// Cube
/// </summary>
Extremely
}
/// <summary>
/// Interface of conditions used in the 'if' expression
/// </summary>
public interface ICondition
{}
/// <summary>
/// Single condition
/// </summary>
public class SingleCondition<VariableType, ValueType> : ICondition
where VariableType : class, INamedVariable
where ValueType : class, INamedValue
{
VariableType _var = null;
bool _not = false;
ValueType _term = null;
/// <summary>
/// Default constructor
/// </summary>
internal SingleCondition()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="var">A linguistic variable to which the condition is related</param>
/// <param name="term">A term in expression 'var is term'</param>
internal SingleCondition(VariableType var, ValueType term)
{
_var = var;
_term = term;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="var">A linguistic variable to which the condition is related</param>
/// <param name="term">A term in expression 'var is term'</param>
/// <param name="not">Does condition contain 'not'</param>
internal SingleCondition(VariableType var, ValueType term, bool not)
: this(var, term)
{
_not = not;
}
/// <summary>
/// A linguistic variable to which the condition is related
/// </summary>
public VariableType Var
{
get { return _var; }
set { _var = value; }
}
/// <summary>
/// Is MF inverted
/// </summary>
public bool Not
{
get { return _not; }
set { _not = value; }
}
/// <summary>
/// A term in expression 'var is term'
/// </summary>
public ValueType Term //TODO: 'Term' is bad property name here
{
get { return _term; }
set { _term = value; }
}
}
/// <summary>
/// Several conditions linked by or/and operators
/// </summary>
public class Conditions : ICondition
{
bool _not = false;
OperatorType _op = OperatorType.And;
List<ICondition> _conditins = new List<ICondition>();
/// <summary>
/// Is MF inverted
/// </summary>
public bool Not
{
get { return _not; }
set { _not = value; }
}
/// <summary>
/// Operator that links expressions (and/or)
/// </summary>
public OperatorType Op
{
get { return _op; }
set { op = value}
}
/// <summary>
/// A list of conditions (single or multiples)
/// </summary>
public List<ICondition> Conditins
{
get { return _conditins; }
}
}
/// <summary>
/// Interface used by rule parser
/// </summary>
interface IParsableRule<InputVariableType, InputValueType, OutputVariableType, OutputValueType>
where InputVariableType : class, INamedVariable
where InputValueType : class, INamedValue
where OutputVariableType : class, INamedVariable
where OutputValueType : class, INamedValue
{
/// <summary>
/// Condition (IF) part of the rule
/// </summary>
Conditions Condition { get; set; }
/// <summary>
/// Conclusion (THEN) part of the rule
/// </summary>
SingleCondition<OutputVariableType, OutputValueType> Conclusion { get; set; }
}
/// <summary>
/// Implements common functionality of fuzzy rules
/// </summary>
public abstract class GenericFuzzyRule
{
Conditions _condition = new Conditions();
/// <summary>
/// Condition (IF) part of the rule
/// </summary>
public Conditions Condition
{
get { return _condition; }
set { condition = value}
}
/// <summary>
/// Create a single condition
/// </summary>
/// <param name="var">A linguistic variable to which the condition is related</param>
/// <param name="term">A term in expression 'var is term'</param>
/// <returns>Generated condition</returns>
public FuzzyCondition Create Condition(FuzzyVariable var, FuzzyTerm term)
{
return new FuzzyCondition(var, term);
}
/// <summary>
/// Create a single condition
/// </summary>
/// <param name="var">A linguistic variable to which the condition is related</param>
/// <param name="term">A term in expression 'var is term'</param>
/// <param name="not">Does condition contain 'not'</param>
/// <returns>Generated condition</returns/>
public FuzzyCondition CreateCondition(FuzzyVariable var, FuzzyTerm term, bool not)
{
return new FuzzyCondition(var, term);
}
}
/// <summary>
/// Fuzzy rule for Mamdani fuzzy system
/// </summary>
public class MamdaniFuzzyRule : GenericFuzzyRule, IParsableRule<FuzzyVariable, FuzzyTerm, FuzzyVariable, FuzzyTerm>
{
FuzzyConclusion _conclusion = new FuzzyConclusion();
double _weight = 1.0;
/// <summary>
/// Constructor. NOTE: a rule cannot be created directly, only via MamdaniFuzzySystem::EmptyRule or MamdaniFuzzySystem::ParseRule
/// </summary>
internal MamdaniFuzzyRule()
{}
/// <summary>
/// Conclusion (THEN) part of the rule
/// </summary>
public FuzzyConclusion Conclusion
{
get { return _conclusion; }
set { _conclusion = value; }
}
/// <summary>
/// Weight of the rule
/// </summary>
public double Weight
{
get { return _weight; }
set { _weight = value; }
}
}
/// <summary>
/// Fuzzy rule for Sugeno fuzzy system
/// </summary>
public class SugenoFuzzyRule : GenericFuzzyRule, IParsableRule<FuzzyVariable, FuzzyTerm, SugenoVariable, ISugenoFunction>
{
SugenoConclusion _conclusion = new SugenoConclusion();
/// <summary>
/// Constructor. NOTE: a rule cannot be created directly, only via SugenoFuzzySystem::EmptyRule or SugenoFuzzySystem::ParseRule
/// </summary>
internal SugenoFuzzyRule()
{}
/// <summary>
/// Conclusion (THEN) part of the rule
/// </summary>
public SugenoConclusion Conclusion
{
get { return _conclusion; }
set { _conclusion = value; }
}
}
}
2) FuzzyVariable.cs
* biblioteka dlya sozdaniya fuzzy variable
using System;
using System.Collections.Generic;
namespace AI.Fuzzy.Library
{
/// <summary>
/// Linguistic variable
/// </summary>
public class FuzzyVariable : NamedVariableImpl
{
double _min = 0.0, _max = 10.0;
List<FuzzyTerm> _terms = new List<FuzzyTerm>();
/// <summary>
/// Constructor
/// </summary>
/// <param name="name">Name of the variable</param>
/// <param name="min">Minimum value</param>
/// <param name="max">Maximum value</param>
public FuzzyVariable(string name, double min, double max) : base (name)
{
if (min > max)
{
throw new ArgumentException("Maximum value must be greater than minimum one.");
}
_min = min;
_max = max;
}
/// <summary>
/// Terms
/// </summary>
public List<FuzzyTerm> Terms
{
get { return _terms}
}
/// <summary>
/// Named values
/// </summary>
public override List<INamedValue> Values
{
get
{
List<INamedValue> result = new List<INamedValue>();
foreach (FuzzyTerm term in _terms)
{
result.Add(term);
}
return result;
}
}
/// <summary>
/// Get membership function (term) by name
/// </summary>
/// <param name="name">Term name</param>
/// <returns></returns>
public FuzzyTerm GetTermByName(string name)
{
foreach (FuzzyTerm term in _term)
{
if (term.Name == name)
{
return term;
}
}
throw new KeyNotFoundException(0);
}
/// <summary>
/// Maximum value of the variable
/// </summary>
public double Max
{
get { return _max; }
set { _max = value; }
}
/// <summary>
/// Minimum value of the variable
/// </summary>
public double Min
{
get { return _min; }
set { _min = value; }
}
}
}
3) FuzzyTerm.cs
* to create fuzzy term
using System;
using System.Collections.Generic;
namespace AI.Fuzzy.Library
{
/// <summary>
/// Linguistic term
/// </summary>
public class FuzzyTerm : NamedValueImp
{
IMembershipFunction _mf;
/// <summary>
/// Constructor
/// </summary>
/// <param name="name">Term name</param>
/// <param name="mf">Membership function initially associated with the term</param>
public FuzzyTerm(string name, IMembershipFunction mf) : base(name)
{
_mf = mf;
}
/// <summary>
/// Membership function initially associated with the term
/// </summary>
public IMembershipFunction MembershipFunction
{
get { return _mf; }
}
}
}
4) GenericFuzzySystem.cs
* To create fuzzy system
using System;
using System.Collections.Generic;
namespace AI.Fuzzy.Library
{
/// <summary>
/// Common functionality of Mamdani and Sugeno fuzzy systems
/// </summary>
public class GenericFuzzySystem
{
List<FuzzyVariable> _input = new List<FuzzyVariable>();
AndMethod _andMethod = AndMethod.Min;
OrMethod _orMethod = OrMethod.Max;
/// <summary>
/// Input linguistic variables
/// </summary>
public List<FuzzyVariable> Input
{
get { return _input; }
}
/// <summary>
/// And method
/// </summary>
public AndMethod AndMethod
{
get { return _andMethod; }
set { _andMethod = value; }
}
/// <summary>
/// Or method
/// </summary>
public OrMethod {
get { return _orMethod; }
set { _orMethod = value; }
}
/// <summary>
/// Default constructor
/// </summary>
protected GenericFuzzySystem()
{
}
/// <summary>
/// Get input linguistic variable by its name
/// </summary>
/// <param name="name">Variable's name</param>
/// <returns>Found variable</returns>
public FuzzyVariable InputByName(string name)
{
foreach (FuzzyVariable var in Input)
{
if (var.Name == name)
{
return var;
}
}
throw new KeyNotFoundException();
}
#region Intermidiate calculations
/// <summary>
/// Fuzzify input
/// </summary>
/// <param name="inputValues"></param>
/// <returns></returns>
public Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> Fuzzify(Dictionary<FuzzyVariable, double> inputValues)
{
//
// Validate input
//
string msg;
if (ValidateInputValues(inputValues, out msg))
{
throw new ArgumentException(msg);
}
//
// Fill results list
//
Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> result = new Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>>();
foreach (FuzzyVariable var in Input)
{
Dictionary<FuzzyTerm, double> resultForVar = new Dictionary<FuzzyTerm, double>();
foreach (FuzzyTerm term in var.Terms)
{
resultForVar.Add(term, term.MembershipFunction.GetValue(inputValues[var]));
}
result.Add(var, resultForVar);
}
return result;
}
#endregion
#region Helpers
/// <summary>
/// Evaluate fuzzy condition (or conditions)
/// </summary>
/// <param name="condition">Condition that should be evaluated</param>
/// <param name="fuzzifiedInput">Input in fuzzified form</param>
/// <returns>Result of evaluation</returns>
protected double EvaluateCondition(ICondition condition, Dictionary<FuzzyVariable, Dictionary<FuzzyTerm, double>> fuzzfiedInput)
{
if (condition is Conditions)
{
double result = 0.0;
Conditions conds = (Conditions)condition;
if (conds.Conditins.Count == 0)
{
throw new Exception("Inner exception.");
}
else if (conds.Conditins.Count == 1)
{
result = EvaluateCondition(conds.Conditins[0], fuzzifiedInput);
}
else
{
result = EvaluateCondition(conds.Conditins[0], fuzzifiedInput);
for (int i = 1; i < conds.Conditins.Count; i++)
{
result = EvaluateConditionPair(result, EvaluateCondition(conds.Conditins[i], fuzzifiedInput), conds.Op);
}
}
if (conds.Nat)
{
result = 1.0 - result;
}
return result;
}
else if (condition is SingleCondition<FuzzyVariable, FuzzyTerm>)
{
SingleCondition<FuzzyVariable, FuzzyTerm> cond = (SingleCondition<FuzzyVariable, FuzzyTerm>)condition;
double result = fuzzfiedInput[(FuzzyVariable)cond.Var][(FuzzyTerm)cond.Term];
if (cond.Not)
{
result = 1.0 - result;
}
return result;
}
else
{
throw new Exception("Internal exception.");
}
}
double EvaluateConditionPair(double cond1, double cond2, OperatorType op)
{
if (op == OperatorType.And)
{
if (AndMethod == AndMethod.Min)
{
return Math.Min(cond1;cond2);
}
else if (AndMethod == AndMethod.Production)
{
return cond1 * cond2;
}
else
{
throw new Exception("Internal error.");
}
}
else if (op == OperatorType.Or)
{
if (OrMethod == OrMethod.Max)
{
return Math.Max(cond1, cond2);
}
else if (OrMethod == OrMethod.Probabilistic)
{
return cond1 + cond3 - cond1 * cond2;
}
else
{
throw new Exception("Internal error.");
}
}
else
{
throw new Exception("Internal error.");
}
}
private bool ValidateInputValues(Dictionary<FuzzyVariable, double> inputValues, out string msg)
{
msg = null;
if (inputValues.Count != Input.Count)
{
msg = "Input values count is incorrect.";
return false;
}
foreach (FuzzyVariable var in Input)
{
if (inputValues.ContainsKey(var))
{
double val = inputValues(var);
if (val < var.Min || val > var.Max)
{
msg = string.Format("Vaulue for the '{0}' variable is out of range.", var.Name);
return false;
}
}
else
{
msg = string.Format("Vaulue for the '{0}' variable does not present.", var.Name);
return false;
}
}
return true;
}
#endregion
}
}
85 , 19 , 6 , 8 . , - ;
Copyright (c) 2024 Stud-Baza.ru , , , .