forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTrapezoidalFunction.cs
198 lines (187 loc) · 8.7 KB
/
TrapezoidalFunction.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
// 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 AForge;
/// <summary>
/// Membership function in the shape of a trapezoid. Can be a half trapzoid if the left or the right side is missing.
/// </summary>
///
/// <remarks><para>Since the <see cref="PiecewiseLinearFunction"/> can represent any piece wise linear
/// function, it can represent trapezoids too. But as trapezoids are largely used in the creation of
/// Linguistic Variables, this class simplifies the creation of them. </para>
///
/// <para>Sample usage:</para>
/// <code>
/// // creating a typical triangular fuzzy set /\
/// TrapezoidalFunction function1 = new TrapezoidalFunction( 10, 20, 30 );
/// // creating a right fuzzy set, the rigth side of the set is fuzzy but the left is opened
/// TrapezoidalFunction function2 = new TrapezoidalFunction( 10, 20, 30, TrapezoidalFunction.EdgeType.Right );
/// </code>
///
/// </remarks>
public class TrapezoidalFunction : PiecewiseLinearFunction
{
/// <summary>
/// Enumeration used to create trapezoidal membership functions with half trapezoids.
/// </summary>
///
/// <remarks><para>If the value is Left, the trapezoid has the left edge, but right
/// is open (/--). If the value is Right, the trapezoid has the right edge, but left
/// is open (--\).</para></remarks>
///
public enum EdgeType
{
/// <summary>
/// The fuzzy side of the trapezoid is at the left side.
/// </summary>
Left,
/// <summary>
/// The fuzzy side of the trapezoid is at the right side.
/// </summary>
Right
};
/// <summary>
/// A private constructor used only to reuse code inside of this default constructor.
/// </summary>
///
/// <param name="size">Size of points vector to create. This size depends of the shape of the
/// trapezoid.</param>
///
private TrapezoidalFunction( int size )
{
points = new Point[size];
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With four points the shape is known as flat fuzzy number or fuzzy interval (/--\).
/// </summary>
///
/// <param name="m1">X value where the degree of membership starts to raise.</param>
/// <param name="m2">X value where the degree of membership reaches the maximum value.</param>
/// <param name="m3">X value where the degree of membership starts to fall.</param>
/// <param name="m4">X value where the degree of membership reaches the minimum value.</param>
/// <param name="max">The maximum value that the membership will reach, [0, 1].</param>
/// <param name="min">The minimum value that the membership will reach, [0, 1].</param>
///
public TrapezoidalFunction( float m1, float m2, float m3, float m4, float max, float min )
: this( 4 )
{
points[0] = new Point( m1, min );
points[1] = new Point( m2, max );
points[2] = new Point( m3, max );
points[3] = new Point( m4, min );
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With four points the shape is known as flat fuzzy number or fuzzy interval (/--\).
/// </summary>
///
/// <param name="m1">X value where the degree of membership starts to raise.</param>
/// <param name="m2">X value where the degree of membership reaches the maximum value.</param>
/// <param name="m3">X value where the degree of membership starts to fall.</param>
/// <param name="m4">X value where the degree of membership reaches the minimum value.</param>
///
/// <remarks>
/// <para>Maximum membership value is set to <b>1.0</b> and the minimum is set to <b>0.0</b>.</para>
/// </remarks>
///
public TrapezoidalFunction( float m1, float m2, float m3, float m4 )
: this( m1, m2, m3, m4, 1.0f, 0.0f )
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With three points the shape is known as triangular fuzzy number or just fuzzy number (/\).
/// </summary>
///
/// <param name="m1">X value where the degree of membership starts to raise.</param>
/// <param name="m2">X value where the degree of membership reaches the maximum value and starts to fall.</param>
/// <param name="m3">X value where the degree of membership reaches the minimum value.</param>
/// <param name="max">The maximum value that the membership will reach, [0, 1].</param>
/// <param name="min">The minimum value that the membership will reach, [0, 1].</param>
///
public TrapezoidalFunction( float m1, float m2, float m3, float max, float min )
: this( 3 )
{
points[0] = new Point( m1, min );
points[1] = new Point( m2, max );
points[2] = new Point( m3, min );
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With three points the shape is known as triangular fuzzy number or just fuzzy number (/\).
/// </summary>
///
/// <param name="m1">X value where the degree of membership starts to raise.</param>
/// <param name="m2">X value where the degree of membership reaches the maximum value and starts to fall.</param>
/// <param name="m3">X value where the degree of membership reaches the minimum value.</param>
///
/// <remarks>
/// <para>Maximum membership value is set to <b>1.0</b> and the minimum is set to <b>0.0</b>.</para>
/// </remarks>
///
public TrapezoidalFunction( float m1, float m2, float m3 )
: this( m1, m2, m3, 1.0f, 0.0f )
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With two points and an edge this shape can be a left fuzzy number (/) or a right fuzzy number (\).
/// </summary>
///
/// <param name="m1">Edge = Left: X value where the degree of membership starts to raise.
/// Edge = Right: X value where the function starts, with maximum degree of membership. </param>
/// <param name="m2">Edge = Left: X value where the degree of membership reaches the maximum.
/// Edge = Right: X value where the degree of membership reaches minimum value. </param>
/// <param name="max">The maximum value that the membership will reach, [0, 1].</param>
/// <param name="min">The minimum value that the membership will reach, [0, 1].</param>
/// <param name="edge">Trapezoid's <see cref="EdgeType"/>.</param>
///
public TrapezoidalFunction( float m1, float m2, float max, float min, EdgeType edge )
: this( 2 )
{
if ( edge == EdgeType.Left )
{
points[0] = new Point( m1, min );
points[1] = new Point( m2, max );
}
else
{
points[0] = new Point( m1, max );
points[1] = new Point( m2, min );
}
}
/// <summary>
/// Initializes a new instance of the <see cref="TrapezoidalFunction"/> class.
///
/// With three points and an edge this shape can be a left fuzzy number (/--) or a right fuzzy number (--\).
/// </summary>
///
/// <param name="m1">Edge = Left: X value where the degree of membership starts to raise.
/// Edge = Right: X value where the function starts, with maximum degree of membership. </param>
/// <param name="m2">Edge = Left: X value where the degree of membership reaches the maximum.
/// Edge = Right: X value where the degree of membership reaches minimum value. </param>
/// <param name="edge">Trapezoid's <see cref="EdgeType"/>.</param>
///
/// <remarks>
/// <para>Maximum membership value is set to <b>1.0</b> and the minimum is set to <b>0.0</b>.</para>
/// </remarks>
///
public TrapezoidalFunction( float m1, float m2, EdgeType edge )
: this( m1, m2, 1.0f, 0.0f, edge )
{
}
}
}