forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathFuzzyOutput.cs
208 lines (193 loc) · 6.99 KB
/
FuzzyOutput.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// AForge Fuzzy Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Fuzzy
{
using System;
using System.Collections.Generic;
/// <summary>
/// The class represents the output of a Fuzzy Inference System.
/// </summary>
///
/// <remarks><para>The class keeps set of rule's output - pairs with the output fuzzy label
/// and the rule's firing strength.
/// </para>
///
///
/// <para>Sample usage:</para>
/// <code>
/// // linguistic labels (fuzzy sets) that compose the distances
/// FuzzySet fsNear = new FuzzySet( "Near",
/// new TrapezoidalFunction( 15, 50, TrapezoidalFunction.EdgeType.Right ) );
/// FuzzySet fsMedium = new FuzzySet( "Medium",
/// new TrapezoidalFunction( 15, 50, 60, 100 ) );
/// FuzzySet fsFar = new FuzzySet( "Far",
/// new TrapezoidalFunction( 60, 100, TrapezoidalFunction.EdgeType.Left ) );
///
/// // front distance (input)
/// LinguisticVariable lvFront = new LinguisticVariable( "FrontalDistance", 0, 120 );
/// lvFront.AddLabel( fsNear );
/// lvFront.AddLabel( fsMedium );
/// lvFront.AddLabel( fsFar );
///
/// // linguistic labels (fuzzy sets) that compose the angle
/// FuzzySet fsZero = new FuzzySet( "Zero",
/// new TrapezoidalFunction( -10, 5, 5, 10 ) );
/// FuzzySet fsLP = new FuzzySet( "LittlePositive",
/// new TrapezoidalFunction( 5, 10, 20, 25 ) );
/// FuzzySet fsP = new FuzzySet( "Positive",
/// new TrapezoidalFunction( 20, 25, 35, 40 ) );
/// FuzzySet fsVP = new FuzzySet( "VeryPositive",
/// new TrapezoidalFunction( 35, 40, TrapezoidalFunction.EdgeType.Left ) );
///
/// // angle
/// LinguisticVariable lvAngle = new LinguisticVariable( "Angle", -10, 50 );
/// lvAngle.AddLabel( fsZero );
/// lvAngle.AddLabel( fsLP );
/// lvAngle.AddLabel( fsP );
/// lvAngle.AddLabel( fsVP );
///
/// // the database
/// Database fuzzyDB = new Database( );
/// fuzzyDB.AddVariable( lvFront );
/// fuzzyDB.AddVariable( lvAngle );
///
/// // creating the inference system
/// InferenceSystem IS = new InferenceSystem( fuzzyDB, new CentroidDefuzzifier( 1000 ) );
///
/// // going straight
/// IS.NewRule( "Rule 1", "IF FrontalDistance IS Far THEN Angle IS Zero" );
/// // turning left
/// IS.NewRule( "Rule 2", "IF FrontalDistance IS Near THEN Angle IS Positive" );
///
/// ...
/// // inference section
///
/// // setting inputs
/// IS.SetInput( "FrontalDistance", 20 );
///
/// // getting outputs
/// try
/// {
/// FuzzyOutput fuzzyOutput = IS.ExecuteInference ( "Angle" );
///
/// // showing the fuzzy output
/// foreach ( FuzzyOutput.OutputConstraint oc in fuzzyOutput.OutputList )
/// {
/// Console.WriteLine( oc.Label + " - " + oc.FiringStrength.ToString( ) );
/// }
/// }
/// catch ( Exception )
/// {
/// ...
/// }
/// </code>
/// </remarks>
///
public class FuzzyOutput
{
/// <summary>
/// Inner class to store the pair fuzzy label / firing strength of
/// a fuzzy output.
/// </summary>
public class OutputConstraint
{
// The label of a fuzzy output
private string label;
// The firing strength of a fuzzy rule, to be applied to the label
private float firingStrength;
/// <summary>
/// Initializes a new instance of the <see cref="OutputConstraint"/> class.
/// </summary>
///
/// <param name="label">A string representing the output label of a <see cref="Rule"/>.</param>
/// <param name="firingStrength">The firing strength of a <see cref="Rule"/>, to be applied to its output label.</param>
///
internal OutputConstraint( string label, float firingStrength )
{
this.label = label;
this.firingStrength = firingStrength;
}
/// <summary>
/// The <see cref="FuzzySet"/> representing the output label of a <see cref="Rule"/>.
/// </summary>
///
public string Label
{
get { return label; }
}
/// <summary>
/// The firing strength of a <see cref="Rule"/>, to be applied to its output label.
/// </summary>
///
public float FiringStrength
{
get { return firingStrength; }
}
}
// the linguistic variables repository
private List<OutputConstraint> outputList;
// the output linguistic variable
private LinguisticVariable outputVar;
/// <summary>
/// A list with <see cref="OutputConstraint"/> of a Fuzzy Inference System's output.
/// </summary>
///
public List<OutputConstraint> OutputList
{
get
{
return outputList;
}
}
/// <summary>
/// Gets the <see cref="LinguisticVariable"/> acting as a Fuzzy Inference System Output.
/// </summary>
///
public LinguisticVariable OutputVariable
{
get { return outputVar; }
}
/// <summary>
/// Initializes a new instance of the <see cref="FuzzyOutput"/> class.
/// </summary>
///
/// <param name="outputVar">A <see cref="LinguisticVariable"/> representing a Fuzzy Inference System's output.</param>
///
internal FuzzyOutput( LinguisticVariable outputVar )
{
// instance of the constraints list
this.outputList = new List<OutputConstraint>( 20 );
// output linguistic variable
this.outputVar = outputVar;
}
/// <summary>
/// Adds an output to the Fuzzy Output.
/// </summary>
///
/// <param name="labelName">The name of a label representing a fuzzy rule's output.</param>
/// <param name="firingStrength">The firing strength [0..1] of a fuzzy rule.</param>
///
/// <exception cref="KeyNotFoundException">The label indicated was not found in the linguistic variable.</exception>
///
internal void AddOutput( string labelName, float firingStrength )
{
// check if the label exists in the linguistic variable
this.outputVar.GetLabel( labelName );
// adding label
this.outputList.Add( new OutputConstraint( labelName, firingStrength ) );
}
/// <summary>
/// Removes all the linguistic variables of the database.
/// </summary>
///
internal void ClearOutput( )
{
this.outputList.Clear( );
}
}
}