Search code examples
pythonfuzzy-logicskfuzzy

How to generate custom membership function Scikit Fuzzy module?


I'm developing a fuzzy system for predicting the increase in price of flight according to the distance of journey and number of active users. Such that, more the number of users higher would be the price.

For that I've defined the Antecents (Inputs) & Consequent (Outputs) :

distance = ctrl.Antecedent(np.arange(1, 20000, 1), 'distance')
users = ctrl.Antecedent(np.arange(0, 50, 1), 'users')
price = ctrl.Consequent(np.arange(0, 10000, 1), 'price')

I can auto-generate membership functions by :

distance.automf(3)
users.automf(3)
price.automf(5)

But, I would like to make custom membership functions like this :

distance['low'] = fuzz.trimf(distance.universe, [50, 1000, 2000])
distance['medium'] = fuzz.trimf(distance.universe, [2000, 3000, 5000])
distance['high'] = fuzz.trimf(distance.universe, [5000, 10000, 20000])

And similarly for others as well.

But on computing, I'm getting :

ValueError: Crisp output cannot be calculated, likely because the system is too sparse. Check to make sure this set of input values will activate at least one connected Term in each Antecedent via the current set of Rules.

I suppose, this error is due to my wrong choice of values in my custom membership functions.

Due to lack of sufficient examples in official documentation, I am not able find the root cause or to understand the proper methodology of choice of x,y,z values in the membership functions.

where, x, y, z are :

distance['low'] = fuzz.trimf(distance.universe, [x, y, z])
.
.
users['low'] = fuzz.trimf(users.universe, [x, y, z])
. 
.

What am I missing here ?


Solution

  • You defined distance using np.arange(1, 20000, 1) but you created the fuzzified distance starting from 50 in distance['low'] = fuzz.trimf(distance.universe, [50, 1000, 2000]). So i believe you can solve the error by simply doing this:

    distance['low'] = fuzz.trimf(distance.universe, [1, 1000, 2000])