Module analyse_risque_floue
[hide private]
[frames] | no frames]

Source Code for Module analyse_risque_floue

  1  # -*- coding: iso-8859-1 -*- 
  2   
  3  # Cet exemple permet de démontrer l'utilisation de pyfuzzy pour la prise de décision. 
  4  # L'exemple utilisé est tiré d'un examen Median de l'UV SY10 
  5  # enseignée par Z. Zalila (Groupe de logique floue) de l'Université de Technologie de 
  6  # Compiègne.  (d'après une idée de A. Biannic & F. Linget, SY 10 A97) 
  7   
  8   
9 -def create_system():
10 """Create fuzzy system.""" 11 import fuzzy 12 import fuzzy.System 13 import fuzzy.InputVariable 14 import fuzzy.OutputVariable 15 import fuzzy.fuzzify.Plain 16 import fuzzy.defuzzify.COG 17 import fuzzy.defuzzify.Dict 18 import fuzzy.Adjective 19 import fuzzy.AdjectiveProxy 20 import fuzzy.Rule 21 import fuzzy.operator 22 import fuzzy.operator.Input 23 import fuzzy.operator.Compound 24 import fuzzy.norm 25 import fuzzy.norm.Min 26 import fuzzy.set 27 import fuzzy.set.Function 28 import fuzzy.set.Polygon 29 import fuzzy.set.Trapez 30 import fuzzy.set.Triangle 31 import fuzzy.set.SFunction 32 import fuzzy.set.ZFunction 33 import fuzzy.set.PiFunction 34 35 # create system object 36 system = fuzzy.System.System() 37 38 # definition of input variable 39 input_Nbre_habitants = fuzzy.InputVariable.InputVariable( 40 fuzzy.fuzzify.Plain.Plain(), 41 description="Taille aglomération", 42 min=0.0,max=10000.0, 43 unit="Milliers d'habitants" 44 ) 45 46 47 # add variable to system ########################################### 48 system.variables["input_Nbre_habitants"] = input_Nbre_habitants 49 50 # create fuzzy set 51 #in1_set = fuzzy.set.Trapez.Trapez(m1=1.,m2=20.,alpha=0., beta=20.) 52 in1_set = fuzzy.set.Polygon.Polygon([(1.,1.),(20.,1.),(40.,0.)]) 53 # make it to an adjective 54 in1 = fuzzy.Adjective.Adjective(in1_set) 55 # and add it to the input variable 56 input_Nbre_habitants.adjectives["Petite"] = in1 57 58 # create fuzzy set 59 in2_set = fuzzy.set.Triangle.Triangle(m=60.,alpha=40., beta=40.) 60 # make it to an adjective 61 in2 = fuzzy.Adjective.Adjective(in2_set) 62 # and add it to the input variable 63 input_Nbre_habitants.adjectives["Moyenne"] = in2 64 65 # create fuzzy set 66 #in3_set = fuzzy.set.Trapez.Trapez(m1=100., m2=10000. ,alpha=20., beta=0.) 67 in3_set = fuzzy.set.Polygon.Polygon([(80.,0.),(100.,1.),(120.,1.)]) 68 # make it to an adjective 69 in3 = fuzzy.Adjective.Adjective(in3_set) 70 # and add it to the input variable 71 input_Nbre_habitants.adjectives["Grande"] = in3 72 73 74 75 76 # definition of input variable 77 input_dist_frontiere = fuzzy.InputVariable.InputVariable( 78 fuzzy.fuzzify.Plain.Plain(), 79 description="Distance de la frontière", 80 min=0.0,max=1000.0, 81 unit="km" 82 ) 83 84 85 # add variable to system ########################################### 86 system.variables["input_dist_frontiere"] = input_dist_frontiere 87 88 # create fuzzy set 89 #in1_set = fuzzy.set.Trapez.Trapez(m1=0.001,m2=50.,alpha=0., beta=50.) 90 in1_set = fuzzy.set.Polygon.Polygon([(0.,1.),(50.,1.),(100.,0.)]) 91 # make it to an adjective 92 in1 = fuzzy.Adjective.Adjective(in1_set) 93 # and add it to the input variable 94 input_dist_frontiere.adjectives["Faible"] = in1 95 96 # create fuzzy set 97 in2_set = fuzzy.set.Triangle.Triangle(m=100.,alpha=50., beta=50.) 98 # make it to an adjective 99 in2 = fuzzy.Adjective.Adjective(in2_set) 100 # and add it to the input variable 101 input_dist_frontiere.adjectives["Moyenne"] = in2 102 103 # create fuzzy set 104 #in3_set = fuzzy.set.Trapez.Trapez(m1=200., m2=1000. ,alpha=75., beta=0.) 105 in3_set = fuzzy.set.Polygon.Polygon([(125.,0.),(200.,1.),(250.,1.)]) 106 # make it to an adjective 107 in3 = fuzzy.Adjective.Adjective(in3_set) 108 # and add it to the input variable 109 input_dist_frontiere.adjectives["Elevee"] = in3 110 111 112 # definition of output variable 113 # we want use later the center of gravity method 114 output_risque_sit_geo = fuzzy.OutputVariable.OutputVariable(defuzzify=fuzzy.defuzzify.Dict.Dict()) 115 # more properties could be (not implemented) 116 # - output as value or membership of adjectives in a dictionary 117 # (input could work this way too.) 118 119 # add to system 120 system.variables["output_risque_sit_geo"] = output_risque_sit_geo 121 122 out1_set = fuzzy.set.Function.Function() 123 out1 = fuzzy.Adjective.Adjective(out1_set) 124 output_risque_sit_geo.adjectives["Nul"] = out1 125 126 out2_set = fuzzy.set.Function.Function() 127 out2 = fuzzy.Adjective.Adjective(out2_set) # (out2_set) 128 output_risque_sit_geo.adjectives["Faible"] = out2 129 130 out3_set = fuzzy.set.Function.Function() 131 out3 = fuzzy.Adjective.Adjective(out3_set) # (out2_set) 132 output_risque_sit_geo.adjectives["Moyen"] = out3 133 134 out4_set = fuzzy.set.Function.Function() 135 out4 = fuzzy.Adjective.Adjective(out4_set) # (out2_set) 136 output_risque_sit_geo.adjectives["Eleve"] = out4 137 138 out5_set = fuzzy.set.Function.Function() 139 out5 = fuzzy.Adjective.Adjective(out5_set) # (out2_set) 140 output_risque_sit_geo.adjectives["Inacceptable"] = out5 141 142 143 # create a fuzzy rule 144 rule1 = fuzzy.Rule.Rule( 145 adjective=system.variables["output_risque_sit_geo"].adjectives["Nul"], 146 operator=fuzzy.operator.Compound.Compound( 147 fuzzy.norm.Min.Min(), 148 fuzzy.operator.Input.Input( 149 system.variables["input_Nbre_habitants"].adjectives["Petite"], 150 ), 151 fuzzy.operator.Input.Input( 152 system.variables["input_dist_frontiere"].adjectives["Elevee"], 153 ) 154 ) 155 ) 156 157 # create a fuzzy rule 158 rule2 = fuzzy.Rule.Rule( 159 adjective=system.variables["output_risque_sit_geo"].adjectives["Faible"], 160 operator=fuzzy.operator.Compound.Compound( 161 fuzzy.norm.Min.Min(), 162 fuzzy.operator.Input.Input( 163 system.variables["input_Nbre_habitants"].adjectives["Moyenne"], 164 ), 165 fuzzy.operator.Input.Input( 166 system.variables["input_dist_frontiere"].adjectives["Elevee"], 167 ) 168 ) 169 ) 170 171 # create a fuzzy rule 172 rule3 = fuzzy.Rule.Rule( 173 adjective=system.variables["output_risque_sit_geo"].adjectives["Eleve"], 174 operator=fuzzy.operator.Compound.Compound( 175 fuzzy.norm.Min.Min(), 176 fuzzy.operator.Input.Input( 177 system.variables["input_Nbre_habitants"].adjectives["Grande"], 178 ), 179 fuzzy.operator.Input.Input( 180 system.variables["input_dist_frontiere"].adjectives["Elevee"], 181 ) 182 ) 183 ) 184 185 # create a fuzzy rule 186 rule4 = fuzzy.Rule.Rule( 187 adjective=system.variables["output_risque_sit_geo"].adjectives["Faible"], 188 operator=fuzzy.operator.Compound.Compound( 189 fuzzy.norm.Min.Min(), 190 fuzzy.operator.Input.Input( 191 system.variables["input_Nbre_habitants"].adjectives["Petite"], 192 ), 193 fuzzy.operator.Input.Input( 194 system.variables["input_dist_frontiere"].adjectives["Moyenne"], 195 ) 196 ) 197 ) 198 199 # create a fuzzy rule 200 rule5 = fuzzy.Rule.Rule( 201 adjective=system.variables["output_risque_sit_geo"].adjectives["Moyen"], 202 operator=fuzzy.operator.Compound.Compound( 203 fuzzy.norm.Min.Min(), 204 fuzzy.operator.Input.Input( 205 system.variables["input_Nbre_habitants"].adjectives["Moyenne"], 206 ), 207 fuzzy.operator.Input.Input( 208 system.variables["input_dist_frontiere"].adjectives["Moyenne"], 209 ) 210 ) 211 ) 212 213 # create a fuzzy rule 214 rule6 = fuzzy.Rule.Rule( 215 adjective=system.variables["output_risque_sit_geo"].adjectives["Inacceptable"], 216 operator=fuzzy.operator.Compound.Compound( 217 fuzzy.norm.Min.Min(), 218 fuzzy.operator.Input.Input( 219 system.variables["input_Nbre_habitants"].adjectives["Grande"], 220 ), 221 fuzzy.operator.Input.Input( 222 system.variables["input_dist_frontiere"].adjectives["Moyenne"], 223 ) 224 ) 225 ) 226 227 # create a fuzzy rule 228 rule7 = fuzzy.Rule.Rule( 229 adjective=system.variables["output_risque_sit_geo"].adjectives["Eleve"], 230 operator=fuzzy.operator.Compound.Compound( 231 fuzzy.norm.Min.Min(), 232 fuzzy.operator.Input.Input( 233 system.variables["input_Nbre_habitants"].adjectives["Petite"], 234 ), 235 fuzzy.operator.Input.Input( 236 system.variables["input_dist_frontiere"].adjectives["Faible"], 237 ) 238 ) 239 ) 240 241 # create a fuzzy rule 242 rule8 = fuzzy.Rule.Rule( 243 adjective=system.variables["output_risque_sit_geo"].adjectives["Inacceptable"], 244 operator=fuzzy.operator.Compound.Compound( 245 fuzzy.norm.Min.Min(), 246 fuzzy.operator.Input.Input( 247 system.variables["input_Nbre_habitants"].adjectives["Moyenne"], 248 ), 249 fuzzy.operator.Input.Input( 250 system.variables["input_dist_frontiere"].adjectives["Faible"], 251 ) 252 ) 253 ) 254 255 # create a fuzzy rule 256 rule9 = fuzzy.Rule.Rule( 257 adjective=system.variables["output_risque_sit_geo"].adjectives["Inacceptable"], 258 operator=fuzzy.operator.Compound.Compound( 259 fuzzy.norm.Min.Min(), 260 fuzzy.operator.Input.Input( 261 system.variables["input_Nbre_habitants"].adjectives["Grande"], 262 ), 263 fuzzy.operator.Input.Input( 264 system.variables["input_dist_frontiere"].adjectives["Faible"], 265 ) 266 ) 267 ) 268 # add rules to system 269 system.rules["rule1"] = rule1 270 system.rules["rule2"] = rule2 271 system.rules["rule3"] = rule3 272 system.rules["rule4"] = rule4 273 system.rules["rule5"] = rule5 274 system.rules["rule6"] = rule6 275 system.rules["rule7"] = rule7 276 system.rules["rule8"] = rule8 277 system.rules["rule9"] = rule9 278 279 # system ready to use 280 return system
281