1
2
3 from simulation import Controller
4
6 import fuzzy.System
7 system = fuzzy.System.System(description=
8 """This fuzzy system is to control the inverted pendulum into an upright position as well as
9 at the position X=0.
10
11 It also is used to demonstrate some features of pyfuzzy.
12 This is the reason, it uses different fuzzy norm in normally
13 symmetrical rules.""")
14
15 from fuzzy.norm.AlgebraicProduct import AlgebraicProduct
16 from fuzzy.norm.AlgebraicSum import AlgebraicSum
17 from fuzzy.fuzzify.Plain import Plain
18 from fuzzy.defuzzify.COG import COG
19
20 INF = AlgebraicProduct()
21 ACC = AlgebraicSum()
22 COM = AlgebraicSum()
23 CER = AlgebraicProduct()
24 COG = COG(INF=INF,ACC=ACC,failsafe = 0., segment_size=0.5)
25
26 from fuzzy.InputVariable import InputVariable
27 from fuzzy.OutputVariable import OutputVariable
28 from fuzzy.Adjective import Adjective
29 from fuzzy.set.Polygon import Polygon
30
31 angle = InputVariable(fuzzify=Plain(),description='angle',min=0.,max=360.,unit='degrees')
32 system.variables['Phi'] = angle
33 angle.adjectives['up_more_right'] = Adjective(Polygon([(0.,0.),(30.,1.),(60.,0.)]))
34 angle.adjectives['up_right'] = Adjective(Polygon([(30.,0.),(60.,1.),(90.,0.)]))
35 angle.adjectives['up'] = Adjective(Polygon([(60.,0.),(90.,1.),(120.,0.)]))
36 angle.adjectives['up_left'] = Adjective(Polygon([(90.,0.),(120.,1.),(150.,0.)]))
37 angle.adjectives['up_more_left'] = Adjective(Polygon([(120.,0.),(150.,1.),(180.,0.)]))
38 angle.adjectives['down_more_left'] = Adjective(Polygon([(180.,0.),(210.,1.),(240.,0.)]))
39 angle.adjectives['down_left'] = Adjective(Polygon([(210.,0.),(240.,1.),(270.,0.)]))
40 angle.adjectives['down'] = Adjective(Polygon([(240.,0.),(270.,1.),(300.,0.)]))
41 angle.adjectives['down_right'] = Adjective(Polygon([(270.,0.),(300.,1.),(330.,0.)]))
42 angle.adjectives['down_more_right'] = Adjective(Polygon([(300.,0.),(330.,1.),(360.,0.)]))
43
44 angle_velocity = InputVariable(fuzzify=Plain(),description='angle velocity',min=-600.,max=600.,unit='degrees per second')
45 system.variables['dPhi_dT'] = angle_velocity
46 angle_velocity.adjectives['cw_fast'] = Adjective(Polygon([(-600.,1.),(-300.,0.)]))
47 angle_velocity.adjectives['cw_slow'] = Adjective(Polygon([(-600.,0.),(-300.,1.),(0.,0.)]))
48 angle_velocity.adjectives['stop'] = Adjective(Polygon([(-300.,0.),(0.,1.),(300.,0.)]))
49 angle_velocity.adjectives['ccw_slow'] = Adjective(Polygon([(0.,0.),(300.,1.),(600.,0.)]))
50 angle_velocity.adjectives['ccw_fast'] = Adjective(Polygon([(300.,0.),(600.,1.)]))
51
52 position = InputVariable(fuzzify=Plain(),description='position',min=-20.,max=20.,unit='meter')
53 system.variables['X'] = position
54 position.adjectives['left_far'] = Adjective(Polygon([(-20.,1.),(-10.,0.)]))
55 position.adjectives['left_near'] = Adjective(Polygon([(-20.,0.),(-5.,1.),(0.,0.)]))
56 position.adjectives['stop'] = Adjective(Polygon([(-5.,0.),(0.,1.),(5.,0.)]))
57 position.adjectives['right_near'] = Adjective(Polygon([(0.,0.),(5.,1.),(20.,0.)]))
58 position.adjectives['right_far'] = Adjective(Polygon([(10.,0.),(20.,1.)]))
59
60 velocity = InputVariable(fuzzify=Plain(),description='velocity',min=-10.,max=10.,unit='meter per second')
61 system.variables['dX_dT'] = velocity
62 velocity.adjectives['left_fast'] = Adjective(Polygon([(-10.,1.),(-5.,0.)]))
63 velocity.adjectives['left_slow'] = Adjective(Polygon([(-10.,0.),(-2.,1.),(0.,0.)]))
64 velocity.adjectives['stop'] = Adjective(Polygon([(-2.,0.),(0.,1.),(2.,0.)]))
65 velocity.adjectives['right_slow'] = Adjective(Polygon([(0.,0.),(2.,1.),(10.,0.)]))
66 velocity.adjectives['right_fast'] = Adjective(Polygon([(5.,0.),(10.,1.)]))
67
68 acceleration = OutputVariable(defuzzify=COG,description='acceleration',min=-50.,max=50.,unit='meter per second^2')
69 system.variables['a'] = acceleration
70 acceleration.adjectives['left_fast'] = a_left_fast = Adjective(Polygon([(-50.,0.),(-20.,1.),(-10.,0.)]),COM=COM)
71 acceleration.adjectives['left_slow'] = a_left_slow = Adjective(Polygon([(-20.,0.),(-10.,1.),(0.,0.)]),COM=COM)
72 acceleration.adjectives['stop'] = a_stop = Adjective(Polygon([(-10.,0.),(0.,1.),(10.,0.)]),COM=COM)
73 acceleration.adjectives['right_slow'] = a_right_slow = Adjective(Polygon([(0.,0.),(10.,1.),(20.,0.)]),COM=COM)
74 acceleration.adjectives['right_fast'] = a_right_fast = Adjective(Polygon([(10.,0.),(20.,1.),(50.,0.)]),COM=COM)
75
76 from fuzzy.Rule import Rule
77 from fuzzy.norm.Max import Max
78
79
80
81 from fuzzy.norm.EinsteinSum import EinsteinSum
82 from fuzzy.norm.DombiUnion import DombiUnion
83 from fuzzy.operator.Compound import Compound
84 from fuzzy.operator.Input import Input
85 from fuzzy.operator.Not import Not
86
87 system.rules['stop'] = Rule(
88 adjective=a_stop,
89
90 operator=Compound(
91 Max(),
92 Compound(
93 AlgebraicProduct(),
94 Input(system.variables["Phi"].adjectives["up"]),
95 Input(system.variables["dPhi_dT"].adjectives["stop"])
96 ),
97 Compound(
98 AlgebraicProduct(),
99 Input(system.variables["Phi"].adjectives["up_right"]),
100 Input(system.variables["dPhi_dT"].adjectives["ccw_slow"])
101 ),
102 Compound(
103 AlgebraicProduct(),
104 Input(system.variables["Phi"].adjectives["up_left"]),
105 Input(system.variables["dPhi_dT"].adjectives["cw_slow"])
106 )
107 ),
108 CER=CER
109 )
110
111 system.rules['tilts right'] = Rule(
112 adjective=a_right_slow,
113
114 operator=Compound(
115 AlgebraicProduct(),
116 Not(
117 Compound(
118 AlgebraicProduct(),
119 Compound(
120 AlgebraicSum(),
121 Input(system.variables["X"].adjectives["left_near"]),
122 Input(system.variables["X"].adjectives["left_far"])
123 ),
124 Compound(
125 EinsteinSum(),
126 Input(system.variables["dX_dT"].adjectives["left_slow"]),
127 Input(system.variables["dX_dT"].adjectives["left_fast"])
128 )
129 ),
130 ),
131 Input(system.variables["Phi"].adjectives["up_right"])
132 ),
133 CER=CER
134 )
135
136 system.rules['tilts left'] = Rule(
137 adjective=a_left_slow,
138
139 operator=Compound(
140 AlgebraicProduct(),
141 Not(
142 Compound(
143 AlgebraicProduct(),
144 Compound(
145 AlgebraicSum(),
146 Input(system.variables["X"].adjectives["right_near"]),
147 Input(system.variables["X"].adjectives["right_far"])
148 ),
149 Compound(
150 DombiUnion(0.25),
151 Input(system.variables["dX_dT"].adjectives["right_slow"]),
152 Input(system.variables["dX_dT"].adjectives["right_fast"])
153 )
154 ),
155 ),
156 Input(system.variables["Phi"].adjectives["up_left"])
157 ),
158 CER=CER
159 )
160
161 system.rules['far right'] = Rule(
162 adjective=a_right_fast,
163
164 operator=Input(system.variables["Phi"].adjectives["up_more_right"]),
165 CER=CER
166 )
167
168 system.rules['far left'] = Rule(
169 adjective=a_left_fast,
170
171 operator=Input(system.variables["Phi"].adjectives["up_more_left"]),
172 CER=CER
173 )
174
175 system.rules['accelerate cw if down'] = Rule(
176 adjective=a_right_slow,
177
178 operator=Compound(
179 AlgebraicProduct(),
180 Input(system.variables["Phi"].adjectives["down"]),
181 Compound(
182 AlgebraicProduct(),
183 Input(system.variables["dPhi_dT"].adjectives["cw_slow"]),
184 Input(system.variables["dPhi_dT"].adjectives["cw_slow"]),
185 )
186 ),
187 CER=CER
188 )
189
190 system.rules['accelerate ccw if down'] = Rule(
191 adjective=a_left_slow,
192
193 operator=Compound(
194 AlgebraicProduct(),
195 Input(system.variables["Phi"].adjectives["down"]),
196 Compound(
197 AlgebraicProduct(),
198 Input(system.variables["dPhi_dT"].adjectives["ccw_slow"]),
199 Input(system.variables["dPhi_dT"].adjectives["ccw_slow"]),
200 )
201 ),
202 CER=CER
203 )
204
205 return system
206
208 """Fuzzy controller."""
209
212
213 - def calculate(self,input={},output={'a':0.0}):
214 self.system.calculate(input,output)
215
216 self.system.variables['a'].defuzzify.failsafe = 0
217 return output
218
220 from fuzzy.doc.plot.gnuplot import doc
221 d = doc.Doc(directory)
222 d.createDoc(self.system)
223 d.overscan=0
224 d.create3DPlot(self.system,"Phi","dPhi_dT","a",{"X":0.,"dX_dT":0.})
225
227 import fuzzy.doc.structure.dot.dot
228 import subprocess
229 for name,rule in self.system.rules.items():
230 cmd = "dot -T png -o '%s/Rule %s.png'" % (directory,name)
231 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin
232 fuzzy.doc.structure.dot.dot.print_header(f,"XXX")
233 fuzzy.doc.structure.dot.dot.print_dot(rule,f,self.system,"")
234 fuzzy.doc.structure.dot.dot.print_footer(f)
235 cmd = "dot -T png -o '%s/System.png'" % directory
236 f = subprocess.Popen(cmd, shell=True, bufsize=32768, stdin=subprocess.PIPE).stdin
237 fuzzy.doc.structure.dot.dot.printDot(self.system,f)
238