Using Scikit-Fuzzy and Fuzzy Logic to build an Obesity Diagnosis System

This is a project on my last year at school, everything is free to use, no need to ask for permission. Although everything is free,  you can donate me some dollars Donate .

To understand this project, some knowledge about fuzzy logic and Python programming skill is required. Also, your pc or laptop must have Python 2.7, a Python editor (Sublime Text, PyCharm ….), and MatLab installed on it.
To install scikit-fuzzy, go here.
To install Python 2.7, go here.
Download .fis file Obesity.fis
I’ve downloaded MatLab using BitTorrent, but I think that updating the link here might violate copy right. So you can check out Google the link and download it yourself.

  • Implement on Matlab

To build a fuzzy control system:
step1 : create input and output variables
step2: create membership functions of each variable
step3: create rules set

First open MatLab, and type ‘fuzzy’: I’ve been using Matlab R2016a, with full packages installed.

screen-shot-2016-10-02-at-6-57-15-pm

A window pop up,  looks like this
screen-shot-2016-10-02-at-8-41-37-pm
Click file -> Import -> From File

screen-shot-2016-10-02-at-8-42-46-pm

We got

screen-shot-2016-10-02-at-8-44-49-pm

As you can see, the control system is mamdani, with three input variables : BMI, BF, WC and one output variable : Obesity Level. The system use max for aggregation , centroid for defuzzification. Those are all default values, we rarely change them.
Here are member functions of each input and output variables:
Note *: Usually we use trimf or trapmf as type of member functions. In this case, I use trimf for all member functions.

BMI: (Body Mass Index)

screen-shot-2016-10-02-at-8-47-06-pm

BF: (Body Fat)

screen-shot-2016-10-02-at-8-51-36-pm

WC : (Waist Circumference)

screen-shot-2016-10-02-at-8-52-44-pm

Obesity Level:

screen-shot-2016-10-02-at-8-53-29-pm

Rules set:

RULE 1: IF BMI = LOW AND BF = LOW AND WC =SMALL THEN TIP = HEALTHY.

RULE 2: IF BMI = LOW AND BF = LOW AND WC = MEDIUM THEN TIP = HEALTHY.

RULE 3: IF BMI = LOW AND BF = LOW AND WC = LARGE THEN TIP =HEALTHY.

RULE 4: IF BMI = LOW AND BF = NORMAL AND WC = SMALL THEN TIP =HEALTHY

RULE 5: IF BMI =LOW AND BF = NORMAL AND WC = MEDIUM THEN TIP =OVERWEIGHT

RULE 6: IF BMI = LOW AND BF = NORMAL AND WC =LARGE THEN TIP =OBESE

RULE 7: IF BMI = LOW AND BF =HIGH AND WC = SMALL THEN TIP = HEALTHY

RULE 8: IF BMI =LOW AND BF =HIGH AND WC = MEDIUM THEN TIP =OVERWEIGHT

RULE 9: IF BMI= LOW AND BF = HIGH AND WC = LARGE THEN TIP = OBESE

RULE 10: IF BMI = MODERATE AND BF =LOW AND WC = SMALL THEN TIP = HEALTHY

RULE 11: IF BMI = MODERATE AND BF = LOW AND WC = MEDIUM THEN TIP = HEALTHY

RULE 12: IF BMI = MODERATE AND BF = LOW AND WC = LARGE THEN TIP = OBESE

RULE 13: IF BMI=MODERATE AND BF=NORMAL AND WC = SMALL THEN TIP = OVERWEIGHT

RULE 14 IF BMI = MODERATE AND BF = NORMAL AND WC = MEDIUM THEN TIP = OVERWEIGHT RULE 15: IF BMI = MODERATE AND BF = NORMAL AND WC = LARGE THEN TIP = OBESE

RULE 16: IF BMI = MODERATE AND BF = HIGH AND WC = SMALL THEN TIP = OVERWEIGHT

RULE 17: IF BMI = MODERATE AND BF = HIGH AND WC = MEDIUM THEN TIP = OBESE

RULE 18: IF BMI = MODERATE AND BF = HIGH AND WC = LARGE THEN TIP = OBESE

RULE 19: IF BMI = HIGH AND BF = LOW AND WC = SMALL THEN TIP = HEALTHY

RULE 20: IF BMI = HIGH AND BF= LOW AND WC = MEDIUM THEN TIP = HEALTHY

RULE 21: IF BMI = HIGH AND BF = LOW AND WC = LARGE THEN TIP = OVERWEIGHT

RULE 22: IF BMI = HIGH AND BF = NORMAL AND WC = SMALL THEN TIP = OVERWEIGHT

RULE 23: IF BMI = HIGH AND BF = NORMAL AND WC = MEDIUM THEN TIP = OBESE

RULE 24: IF BMI = HIGH AND BF = NORMAL AND WC = LARGE THEN TIP = OBESE

RULE 25: IF BMI = HIGH AND BF = HIGH AND WC = SMALL THEN TIP = OVERWEIGHT

RULE 26: IF BMI = HIGH AND BF = HIGH AND WC = MEDIUM THEN TIP = OBESE

RULE 27: IF BMI = HIGH AND BF = HIGH AND WC = LARGE THEN TIP = OBESE

Edit -> Rules :
screen-shot-2016-10-02-at-8-57-55-pm
View->Rules
screen-shot-2016-10-02-at-8-59-04-pm

With input [BMI; BF; WC] = [17.5; 17.5; 60] , ObesityLevel = 31.9

 

  • Implement on Python (using scikit-fuzzy)
    As I explained above, there are three steps to build a fuzzy control system. But before we get started, let’s import skfuzzy module and skfuzzy.control and numpy as well.
import numpy as np
import skfuzzy as fuzz
import skfuzzy.control

Now let do step 1, create input and output variables

    def make_variables(self):
        """
            step 1: create input, output variables
        :return:
        """
        self.__bmi = skfuzzy.control.Antecedent(np.arange(self.__bmi_start, self.__bmi_stop), 'BMI')  # input variable bmi
        self.__bf = skfuzzy.control.Antecedent(np.arange(self.__bf_start, self.__bf_stop), 'BF')  # input variable bf
        self.__wc = skfuzzy.control.Antecedent(np.arange(self.__wc_start, self.__wc_stop), 'WC')  # input variable wc
        self.__obesity = skfuzzy.control.Consequent(np.arange(self.__o_start, self.__o_stop), 'OL')  # output variable OL = Obesity Level

Three input variables self.__bmi, self.__bf, self.__wc, and one output variable self.__obesity. BMI (kg/m^2) and BF (%) are in range 0-35, WC is from 0 to 120 (cm), Obesity is in range 0-100. We have ‘self’ because I use object-oriented programming to build this fuzzy control system, at the last part of this post, you will get all the code as well as some other instructions to build and run it.

Next, step 2, create membership functions

    def make_member_functions(self):
        """
            step 2: create member functions
        :return:
        """
        self.__bmi['low'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_low_start, self.__bmi_low_mid, self.__bmi_low_stop])
        self.__bmi['moderate'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_mod_start, self.__bmi_mod_mid, self.__bmi_mod_stop])
        self.__bmi['high'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_high_start, self.__bmi_high_mid, self.__bmi_high_stop])
        self.__bf['low'] = fuzz.trimf(self.__bf.universe, [self.__bf_low_start, self.__bf_low_mid, self.__bf_low_stop])
        self.__bf['normal'] = fuzz.trimf(self.__bf.universe, [self.__bf_nor_start, self.__bf_nor_mid, self.__bf_nor_stop])
        self.__bf['high'] = fuzz.trimf(self.__bf.universe, [self.__bf_high_start, self.__bf_high_mid, self.__bf_high_stop])
        self.__wc['small'] = fuzz.trimf(self.__wc.universe, [self.__wc_small_start, self.__wc_small_mid, self.__wc_small_stop])
        self.__wc['medium'] = fuzz.trimf(self.__wc.universe, [self.__wc_med_start, self.__wc_med_mid, self.__wc_med_stop])
        self.__wc['large'] = fuzz.trimf(self.__wc.universe, [self.__wc_large_start, self.__wc_large_mid, self.__wc_large_stop])
        self.__obesity['healthy'] = fuzz.trimf(self.__obesity.universe, [self.__o_healthy_start, self.__o_healthy_mid, self.__o_healthy_stop])
        self.__obesity['overweight'] = fuzz.trimf(self.__obesity.universe, [self.__o_over_start, self.__o_over_mid, self.__o_over_stop])
        self.__obesity['obese'] = fuzz.trimf(self.__obesity.universe, [self.__o_obese_start, self.__o_obese_mid, self.__o_obese_stop])

BMI has three membership functions: low, moderate, high
BF has three membership functions: low, normal, high
WC has three membership functions: small, medium, large
OL has three membership functions: healthy, overweight, obese
They’re all trimf type, so they need three values [start,mid,stop].

Now, we reach step 3, create rule set:
We got 27 rules in total:

    def make_rules(self):
        """
            step 3: create fuzzy rules
        :return:
        """
        rule1 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule2 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule3 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['large'], self.__obesity['healthy'])
        rule4 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['healthy'])
        rule5 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['overweight'])
        rule6 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule7 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['small'], self.__obesity['healthy'])
        rule8 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['overweight'])
        rule9 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        rule10 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule11 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule12 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['large'], self.__obesity['obese'])
        rule13 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['overweight'])
        rule14 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['overweight'])
        rule15 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule16 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['small'], self.__obesity['overweight'])
        rule17 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['obese'])
        rule18 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        rule19 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule20 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule21 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['large'], self.__obesity['overweight'])
        rule22 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['overweight'])
        rule23 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['obese'])
        rule24 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule25 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['small'], self.__obesity['overweight'])
        rule26 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['obese'])
        rule27 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        """
            step 4: create a control system
        """
        self.__rules = []
        for i in range(1, 28):
            self.__rules.append(eval("rule" + str(i)))
        self.__obesity_ctrl = skfuzzy.control.ControlSystem(self.__rules)
        self.__obesity_ctrl.view()

Example:
In speaking language:
RULE 1: IF BMI = LOW AND BF = LOW AND WC =SMALL THEN TIP = HEALTHY.
In Python

rule1 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])

Just imagine that the comma is equal with ‘then’ and & is equal with ‘and’
Now we got variables, membership functions, rules. What we need to do next is to create a fuzzy control system. The code in step 4 means: create an array of rules, store all 27 rules. And build the control system on top of those rules.

With all those stuff, we’re ready to take real input.

    def simulate(self, bmi_val, bf_val, wc_val):
        """
        :param bmi_val: body mass index value , int or float
        :param bf_val: body fat value, int or float
        :param wc_val: waist circuference value, int or float
        :return: res: a string store step by step instructions how the fuzzy controler infer
        res is stored in result.txt
        """
        obesity_ctrl_sil = skfuzzy.control.ControlSystemSimulation(self.__obesity_ctrl)
        obesity_ctrl_sil.input['BMI'] = bmi_val
        obesity_ctrl_sil.input['BF'] = bf_val
        obesity_ctrl_sil.input['WC'] = wc_val
        obesity_ctrl_sil.compute()
        self.res = obesity_ctrl_sil.print_state()
        self.output = obesity_ctrl_sil.output.items()[0][1]

        #def get_result():
        #    file = open("result.txt", "r")
        #    return file.read()
        #return get_result()
        print(obesity_ctrl_sil.output)
        print(self.res)

This method take three arguments, bmi, bf and wc. Create a control system simulation base on our control system, then pass into it three input values, and compute(). I have to change directly the code in controlsystem.py of skfuzzy.control to be able to get the step by step instructions of how the control system work. So bare with me and I’ll show you all.

Here are all the code of fuzzy.py.

import numpy as np
import skfuzzy as fuzz
import skfuzzy.control

from skfuzzy import *
class FuzzySystem:

    def __init__(self, bmi_start=0, bmi_stop=10, bf_start=0, bf_stop=10, wc_start=0, wc_stop=10, o_start=0, o_stop=10):
        """
        bmi : body mass index (kg / m^2)
        bf : body fat (%)
        wc : waist circumference (cm)
        o : obesity level
        """
        self.__bmi_start = bmi_start
        self.__bmi_stop = bmi_stop
        self.__bf_start = bf_start
        self.__bf_stop = bf_stop
        self.__wc_start = wc_start
        self.__wc_stop = wc_stop
        self.__o_start = o_start
        self.__o_stop = o_stop

    # these lines of code are pretty boring
    def set_bmi_low_start(self, start):
        self.__bmi_low_start = start

    def set_bmi_low_mid(self, mid):
        self.__bmi_low_mid = mid

    def set_bmi_low_stop(self, stop):
        self.__bmi_low_stop = stop

    def set_bmi_mod_start(self, start):
        self.__bmi_mod_start = start

    def set_bmi_mod_mid(self, mid):
        self.__bmi_mod_mid = mid

    def set_bmi_mod_stop(self, stop):
        self.__bmi_mod_stop = stop

    def set_bmi_high_start(self, start):
        self.__bmi_high_start = start

    def set_bmi_high_mid(self, mid):
        self.__bmi_high_mid = mid

    def set_bmi_high_stop(self, stop):
        self.__bmi_high_stop = stop

    def set_bmi_low(self, start, mid, stop):
        self.set_bmi_low_start(start)
        self.set_bmi_low_mid(mid)
        self.set_bmi_low_stop(stop)

    def set_bmi_mod(self, start, mid, stop):
        self.set_bmi_mod_start(start)
        self.set_bmi_mod_mid(mid)
        self.set_bmi_mod_stop(stop)

    def set_bmi_high(self, start, mid, stop):
        self.set_bmi_high_start(start)
        self.set_bmi_high_mid(mid)
        self.set_bmi_high_stop(stop)

    def set_bmi(self, low_start, low_mid, low_stop, mod_start, mod_mid, mod_stop, high_start, high_mid, high_stop):
        self.set_bmi_low(low_start, low_mid, low_stop)
        self.set_bmi_mod(mod_start, mod_mid, mod_stop)
        self.set_bmi_high(high_start, high_mid, high_stop)

    def set_bf_low_start(self, start):
        self.__bf_low_start = start

    def set_bf_low_mid(self, mid):
        self.__bf_low_mid = mid

    def set_bf_low_stop(self, stop):
        self.__bf_low_stop = stop

    def set_bf_nor_start(self, start):
        self.__bf_nor_start = start

    def set_bf_nor_mid(self, mid):
        self.__bf_nor_mid = mid

    def set_bf_nor_stop(self, stop):
        self.__bf_nor_stop = stop

    def set_bf_high_start(self, start):
        self.__bf_high_start = start

    def set_bf_high_mid(self, mid):
        self.__bf_high_mid = mid

    def set_bf_high_stop(self, stop):
        self.__bf_high_stop = stop

    def set_bf_low(self, start, mid, stop):
        self.set_bf_low_start(start)
        self.set_bf_low_mid(mid)
        self.set_bf_low_stop(stop)

    def set_bf_nor(self, start, mid, stop):
        self.set_bf_nor_start(start)
        self.set_bf_nor_mid(mid)
        self.set_bf_nor_stop(stop)

    def set_bf_high(self, start, mid, stop):
        self.set_bf_high_start(start)
        self.set_bf_high_mid(mid)
        self.set_bf_high_stop(stop)

    def set_bf(self, low_start, low_mid, low_stop, mod_start, mod_mid, mod_stop, high_start, high_mid, high_stop):
        self.set_bf_low(low_start, low_mid, low_stop)
        self.set_bf_nor(mod_start, mod_mid, mod_stop)
        self.set_bf_high(high_start, high_mid, high_stop)

    def set_wc_small_start(self, start):
        self.__wc_small_start = start

    def set_wc_small_mid(self, mid):
        self.__wc_small_mid = mid

    def set_wc_small_stop(self, stop):
        self.__wc_small_stop = stop

    def set_wc_med_start(self, start):
        self.__wc_med_start = start

    def set_wc_med_mid(self, mid):
        self.__wc_med_mid = mid

    def set_wc_med_stop(self, stop):
        self.__wc_med_stop = stop

    def set_wc_large_start(self, start):
        self.__wc_large_start = start

    def set_wc_large_mid(self, mid):
        self.__wc_large_mid = mid

    def set_wc_large_stop(self, stop):
        self.__wc_large_stop = stop

    def set_wc_small(self, start, mid, stop):
        self.set_wc_small_start(start)
        self.set_wc_small_mid(mid)
        self.set_wc_small_stop(stop)

    def set_wc_med(self, start, mid, stop):
        self.set_wc_med_start(start)
        self.set_wc_med_mid(mid)
        self.set_wc_med_stop(stop)

    def set_wc_large(self, start, mid, stop):
        self.set_wc_large_start(start)
        self.set_wc_large_mid(mid)
        self.set_wc_large_stop(stop)

    def set_wc(self, small_start, small_mid, small_stop, med_start, med_mid, med_stop, large_start, large_mid, large_stop):
        self.set_wc_small(small_start, small_mid, small_stop)
        self.set_wc_med(med_start, med_mid, med_stop)
        self.set_wc_large(large_start, large_mid, large_stop)

    def set_o_healthy_start(self, start):
        self.__o_healthy_start = start

    def set_o_healthy_mid(self, mid):
        self.__o_healthy_mid = mid

    def set_o_healthy_stop(self, stop):
        self.__o_healthy_stop = stop

    def set_o_over_start(self, start):
        self.__o_over_start = start

    def set_o_over_mid(self, mid):
        self.__o_over_mid = mid

    def set_o_over_stop(self, stop):
        self.__o_over_stop = stop

    def set_o_obese_start(self, start):
        self.__o_obese_start = start

    def set_o_obese_mid(self, mid):
        self.__o_obese_mid = mid

    def set_o_obese_stop(self, stop):
        self.__o_obese_stop = stop

    def set_o_healthy(self, start, mid , stop):
        self.set_o_healthy_start(start)
        self.set_o_healthy_mid(mid)
        self.set_o_healthy_stop(stop)

    def set_o_over(self, start, mid, stop):
        self.set_o_over_start(start)
        self.set_o_over_mid(mid)
        self.set_o_over_stop(stop)

    def set_o_obese(self, start , mid, stop):
        self.set_o_obese_start(start)
        self.set_o_obese_mid(mid)
        self.set_o_obese_stop(stop)

    def set_o(self, healthy_start, healthy_mid, healthy_stop, over_start, over_mid, over_stop, obese_start, obese_mid, obese_stop):
        self.set_o_healthy(healthy_start, healthy_mid, healthy_stop)
        self.set_o_over(over_start, over_mid, over_stop)
        self.set_o_obese(obese_start, obese_mid, obese_stop)

    def make_rules(self):
        """
            step 3: create fuzzy rules
        :return:
        """
        rule1 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule2 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule3 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['low'] & self.__wc['large'], self.__obesity['healthy'])
        rule4 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['healthy'])
        rule5 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['overweight'])
        rule6 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule7 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['small'], self.__obesity['healthy'])
        rule8 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['overweight'])
        rule9 = skfuzzy.control.Rule(self.__bmi['low'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        rule10 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule11 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule12 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['low'] & self.__wc['large'], self.__obesity['obese'])
        rule13 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['overweight'])
        rule14 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['overweight'])
        rule15 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule16 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['small'], self.__obesity['overweight'])
        rule17 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['obese'])
        rule18 = skfuzzy.control.Rule(self.__bmi['moderate'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        rule19 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['small'], self.__obesity['healthy'])
        rule20 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['medium'], self.__obesity['healthy'])
        rule21 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['low'] & self.__wc['large'], self.__obesity['overweight'])
        rule22 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['small'], self.__obesity['overweight'])
        rule23 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['medium'], self.__obesity['obese'])
        rule24 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['normal'] & self.__wc['large'], self.__obesity['obese'])
        rule25 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['small'], self.__obesity['overweight'])
        rule26 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['medium'], self.__obesity['obese'])
        rule27 = skfuzzy.control.Rule(self.__bmi['high'] & self.__bf['high'] & self.__wc['large'], self.__obesity['obese'])
        """
            step 4: create a control system
        """
        self.__rules = []
        for i in range(1, 28):
            self.__rules.append(eval("rule" + str(i)))
        self.__obesity_ctrl = skfuzzy.control.ControlSystem(self.__rules)
        self.__obesity_ctrl.view()

    def make_variables(self):
        """
            step 1: create input, output variables
        :return:
        """
        self.__bmi = skfuzzy.control.Antecedent(np.arange(self.__bmi_start, self.__bmi_stop), 'BMI')  # input variable bmi
        self.__bf = skfuzzy.control.Antecedent(np.arange(self.__bf_start, self.__bf_stop), 'BF')  # input variable bf
        self.__wc = skfuzzy.control.Antecedent(np.arange(self.__wc_start, self.__wc_stop), 'WC')  # input variable wc
        self.__obesity = skfuzzy.control.Consequent(np.arange(self.__o_start, self.__o_stop), 'OL')  # output variable OL = Obesity Level

    def make_member_functions(self):
        """
            step 2: create member functions
        :return:
        """
        self.__bmi['low'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_low_start, self.__bmi_low_mid, self.__bmi_low_stop])
        self.__bmi['moderate'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_mod_start, self.__bmi_mod_mid, self.__bmi_mod_stop])
        self.__bmi['high'] = fuzz.trimf(self.__bmi.universe, [self.__bmi_high_start, self.__bmi_high_mid, self.__bmi_high_stop])
        self.__bf['low'] = fuzz.trimf(self.__bf.universe, [self.__bf_low_start, self.__bf_low_mid, self.__bf_low_stop])
        self.__bf['normal'] = fuzz.trimf(self.__bf.universe, [self.__bf_nor_start, self.__bf_nor_mid, self.__bf_nor_stop])
        self.__bf['high'] = fuzz.trimf(self.__bf.universe, [self.__bf_high_start, self.__bf_high_mid, self.__bf_high_stop])
        self.__wc['small'] = fuzz.trimf(self.__wc.universe, [self.__wc_small_start, self.__wc_small_mid, self.__wc_small_stop])
        self.__wc['medium'] = fuzz.trimf(self.__wc.universe, [self.__wc_med_start, self.__wc_med_mid, self.__wc_med_stop])
        self.__wc['large'] = fuzz.trimf(self.__wc.universe, [self.__wc_large_start, self.__wc_large_mid, self.__wc_large_stop])
        self.__obesity['healthy'] = fuzz.trimf(self.__obesity.universe, [self.__o_healthy_start, self.__o_healthy_mid, self.__o_healthy_stop])
        self.__obesity['overweight'] = fuzz.trimf(self.__obesity.universe, [self.__o_over_start, self.__o_over_mid, self.__o_over_stop])
        self.__obesity['obese'] = fuzz.trimf(self.__obesity.universe, [self.__o_obese_start, self.__o_obese_mid, self.__o_obese_stop])

    def simulate(self, bmi_val, bf_val, wc_val):
        """
        :param bmi_val: body mass index value , int or float
        :param bf_val: body fat value, int or float
        :param wc_val: waist circuference value, int or float
        :return: res: a string store step by step instructions how the fuzzy controler infer
        res is stored in result.txt
        """
        obesity_ctrl_sil = skfuzzy.control.ControlSystemSimulation(self.__obesity_ctrl)
        obesity_ctrl_sil.input['BMI'] = bmi_val
        obesity_ctrl_sil.input['BF'] = bf_val
        obesity_ctrl_sil.input['WC'] = wc_val
        obesity_ctrl_sil.compute()
        self.res = obesity_ctrl_sil.print_state()
        self.output = obesity_ctrl_sil.output.items()[0][1]

        #def get_result():
        #    file = open("result.txt", "r")
        #    return file.read()
        #return get_result()
        print(obesity_ctrl_sil.output)
        print(self.res)

if __name__ == '__main__':
    fuzzy_system = FuzzySystem(0, 35, 0, 35, 0, 120, 0, 100)
    fuzzy_system.set_bmi(0, 15, 22, 15, 22, 29, 22, 29, 35)
    fuzzy_system.set_bf(0, 15, 22, 15, 22, 29, 22, 29, 35)
    fuzzy_system.set_wc(0, 30, 60, 30, 60, 90, 60, 90, 120)
    fuzzy_system.set_o(0, 20, 40, 30, 50, 70, 60, 80, 100)
    fuzzy_system.make_variables()
    fuzzy_system.make_member_functions()
    fuzzy_system.make_rules()

    fuzzy_system.simulate(20, 15, 90)

But this will not stop as a console-application.
Here is the GUI:

import Tkinter as tk
import tkMessageBox as messagebox
import matplotlib.pyplot as plt
from  Fuzzy import FuzzySystem

fuzzy = None
test = None
bmi_low_var, bmi_mod_var, bmi_high_var = [0, 15, 22], [15, 22, 29], [22, 29, 35]
bf_low_var, bf_nor_var, bf_high_var = [0, 15, 22], [15, 22, 29], [22, 29, 35]
wc_small_var, wc_med_var, wc_large_var = [0, 30, 60], [30, 60, 90], [60, 90, 120]
ol_healthy_var, ol_over_var, ol_obese_var = [0, 20, 40], [30, 50, 70], [60, 80, 100]
suggestions = [
    "Too thin, eat more and exercise to have a healthy body",
    "You look normal, but you can go to gym if you like",
    "Too fat, go to doctor to get a proper diet and exercise more "
    "if you don't want to die young."
]

class MainWindow(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.title("Fuzzy System") # set title of the window

        container = tk.Frame(self) # create the container contains all sub_windows
        container.pack(side="top", fill="both", expand=True) # pack it
        container.grid_rowconfigure(0, weight=1) # this is like 1x1 grid
        container.grid_columnconfigure(0, weight=1)

        self.frames = {} # store references to all sub_windows

        for Frame in (Fuzzy, Settings): # for each page class
            frame = Frame(container, self) # create page
            self.frames[Frame] = frame     # store into frames
            frame.grid(row=0, column=0, sticky='nsew') # pack it into container
        self.show_frame(Fuzzy) # set HomePage as default

        # create the menu bar
        menu_bar = tk.Menu(self)
        # create fuzzy menu
        fuzzy_menu = tk.Menu(menu_bar)
        fuzzy_menu.add_command(label='Fuzzy', compound='left', command=lambda : self.show_frame(Fuzzy))
        fuzzy_menu.add_separator()
        fuzzy_menu.add_command(label='Exit', compound='left', command=lambda : exit())
        menu_bar.add_cascade(label='Fuzzy', menu=fuzzy_menu)
        #create settings menu
        settings_menu = tk.Menu(menu_bar)
        settings_menu.add_command(label='Settings', compound='left', command=lambda : self.show_frame(Settings))
        menu_bar.add_cascade(label='Settings', menu=settings_menu)
        self.config(menu=menu_bar) # after you create it, you have to set it as menu_bar

        def exit():
            if messagebox.askokcancel('Quit?', 'Really Quit?'):
                self.destroy()
            return 'break'

    def show_frame(self, Frame):
        frame = self.frames[Frame] # get the reference to page
        frame.tkraise() # raise it to the top

class Fuzzy(tk.Frame):
    def __init__(self, parent, controller):
        global bmi_low_var, bmi_mod_var, bmi_high_var
        global bf_low_var, bf_nor_var, bf_high_var
        global wc_small_var, wc_med_var, wc_large_var
        global ol_healthy_var, ol_over_var, ol_obese_var
        global suggestions

        tk.Frame.__init__(self, parent)
        ###############################################
        input_frame = tk.LabelFrame(self, text="Input")
        input_frame.grid(row=0, column=0, sticky='wens', padx=10, pady=5)

        # BMI INPUTs
        self.button_question_mark_1 = tk.Button(input_frame, text='?', command=lambda : bmi_ask_handler())
        self.button_question_mark_1.grid(row=0, column=0, sticky='e', padx=(5,0))
        tk.Label(input_frame, text='Body Mass Index(kg/m^2)').grid(row=0, column=1, sticky='e', padx=10)
        self.bmi_slider_var = tk.DoubleVar()
        self.bmi_slider = tk.Scale(input_frame, variable=self.bmi_slider_var, orient=tk.HORIZONTAL, from_=0, to=35, resolution=0.1, command=lambda e: bmi_slider_handler())
        self.bmi_slider.grid(row=1, column=0, columnspan=2, sticky=tk.W + tk.E, padx=(5,0))
        self.bmi_entry_var = tk.StringVar()
        self.bmi_entry = tk.Entry(input_frame, width=6, textvariable=self.bmi_entry_var)
        self.bmi_entry.bind('<Key>', lambda e: bmi_entry_handler())
        self.bmi_entry.grid(row=1, column=2, sticky=tk.S, padx=(0,5))

        # BF INPUTs
        self.button_question_mark_2 = tk.Button(input_frame, text='?', command=lambda :bf_ask_handler())
        self.button_question_mark_2.grid(row=2, column=0, sticky='e', pady=(5,0))
        tk.Label(input_frame, text='Body Fat(%)').grid(row=2, column=1, sticky='w', padx=10,pady=(5,0))
        self.bf_slider_var = tk.DoubleVar()
        self.bf_slider = tk.Scale(input_frame, variable=self.bf_slider_var, orient=tk.HORIZONTAL, from_=0, to=35,resolution=0.1, command=lambda e: bf_slider_handler())
        self.bf_slider.grid(row=3, column=0, columnspan=2, sticky=tk.W + tk.E, padx=(5,0))
        self.bf_entry_var = tk.StringVar()
        self.bf_entry = tk.Entry(input_frame, width=6, textvariable=self.bf_entry_var)
        self.bf_entry.bind('<Key>', lambda e: bf_entry_handler())
        self.bf_entry.grid(row=3, column=2, sticky=tk.S, padx=(0,5))

        # WC INPUTs
        self.button_question_mark_3 = tk.Button(input_frame, text='?', command=lambda : wc_ask_handler())
        self.button_question_mark_3.grid(row=4, column=0, sticky='e', pady=(5, 0))
        tk.Label(input_frame, text='Waist Circumference(cm)').grid(row=4, column=1, sticky='w', padx=10, pady=(5, 0))
        self.wc_slider_var = tk.DoubleVar()
        self.wc_slider = tk.Scale(input_frame, variable=self.wc_slider_var, orient=tk.HORIZONTAL, from_=0, to=120,
                                  resolution=0.1, command=lambda e: wc_slider_handler())
        self.wc_slider.grid(row=5, column=0, columnspan=2, sticky=tk.W + tk.E, padx=(5,0))
        self.wc_entry_var = tk.StringVar()
        self.wc_entry = tk.Entry(input_frame, width=6, textvariable=self.wc_entry_var)
        self.wc_entry.bind('<Key>', lambda e: wc_entry_handler())
        self.wc_entry.grid(row=5, column=2, sticky=tk.S)

        start_button = tk.Button(input_frame, text='Start', command=lambda : start_handler())
        start_button.grid(row=6, column=1, sticky=tk.E+tk.W, padx=(0,5))

        ##################################################
        output_frame = tk.LabelFrame(self, text="Output")
        output_frame.grid(row=0, column=1, sticky='news', padx=10, pady=5)
        tk.Label(output_frame, text='Obesity Level:').grid(row=0, column=0, sticky='w', padx=(5,0))
        self.ol_entry_var = tk.StringVar()
        self.ol_entry = tk.Entry(output_frame, textvariable=self.ol_entry_var)
        self.ol_entry.grid(row=0, column=1, sticky='w')
        tk.Label(output_frame, text='Suggestion:').grid(row=1, column=0, sticky='w', padx=(5,0))
        temp = tk.Frame(output_frame, borderwidth=3, relief=tk.RAISED)
        temp.grid(row=2, column=0, columnspan=2, rowspan=2, padx=5)
        self.suggest_text = tk.Text(temp, cursor='plus', wrap=tk.WORD, width=47, height=10)
        self.suggest_text.grid(row=0, column=0, sticky='news')
        tk.Label(output_frame, text='Your should ask your doctor if obesity level >= 60!!').grid(row=5, column=0, columnspan=2, sticky='ne', padx=5)

        ##################################################
        process_frame = tk.LabelFrame(self, text="Process")
        process_frame.grid(row=1, column=0, columnspan=2, sticky='news', padx=10, pady=5)
        temp1 = tk.Frame(process_frame, borderwidth=3, relief=tk.RAISED)
        temp1.grid(row=0, column=0, padx=5, pady=5)
        self.process_text = tk.Text(temp1, cursor='plus', wrap=tk.WORD, width=93)
        self.process_text.grid(row=0, column=0, sticky='news')

        ###############--Handlers--###############
        def bmi_entry_handler(event=None):
            try:
                self.bmi_slider_var.set(float(self.bmi_entry_var.get()))
            except:
                pass

        def bmi_slider_handler(event=None):
            self.bmi_entry_var.set(str("%.2f" % (self.bmi_slider_var.get())))

        def bf_entry_handler(event=None):
            try:
                self.bf_slider_var.set(float(self.bf_entry_var.get()))
            except:
                pass

        def bf_slider_handler(event=None):
            self.bf_entry_var.set(str("%.2f" % (self.bf_slider_var.get())))

        def wc_entry_handler(event=None):
            try:
                self.wc_slider_var.set(float(self.wc_entry_var.get()))
            except:
                pass

        def wc_slider_handler(event=None):
            self.wc_entry_var.set(str("%.2f" % (self.wc_slider_var.get())))

        def start_handler():
            self.process_text.delete(1.0, tk.END)
            self.suggest_text.delete(1.0, tk.END)
            fuzzy = FuzzySystem(0, 35, 0, 35, 0, 120, 0, 100)
            fuzzy.set_bmi(bmi_low_var[0], bmi_low_var[1], bmi_low_var[2], bmi_mod_var[0], bmi_mod_var[1],bmi_mod_var[2],bmi_high_var[0], bmi_high_var[1], bmi_high_var[2])
            fuzzy.set_bf(bf_low_var[0], bf_low_var[1], bf_low_var[2], bf_nor_var[0], bf_nor_var[1], bf_nor_var[2], bf_high_var[0], bf_high_var[1], bf_high_var[2])
            fuzzy.set_wc(wc_small_var[0], wc_small_var[1], wc_small_var[2], wc_med_var[0], wc_med_var[1], wc_med_var[2], wc_large_var[0], wc_large_var[1], wc_large_var[2])
            fuzzy.set_o(ol_healthy_var[0], ol_healthy_var[1], ol_healthy_var[2], ol_over_var[0], ol_over_var[1], ol_over_var[2], ol_obese_var[0], ol_obese_var[1], ol_obese_var[2])
            fuzzy.make_variables()
            fuzzy.make_member_functions()
            fuzzy.make_rules()
            fuzzy.simulate(self.bmi_slider_var.get(), self.bf_slider_var.get(), self.wc_slider_var.get())
            self.process_text.insert(1.0, fuzzy.res)
            self.ol_entry_var.set(fuzzy.output)
            if 0.0 <= fuzzy.output <= 20.0:
                self.suggest_text.insert(1.0, suggestions[0])
            elif 20.0 < fuzzy.output <= 60.0:
                self.suggest_text.insert(1.0, suggestions[1])
            else:
                self.suggest_text.insert((1.0, suggestions[2]))

        def bmi_ask_handler():
            messagebox.showinfo("BMI", "Body mass index (BMI) is a measure of body fat based on height and weight that applies to adult men and women.\n"
                                       "BMI Categories: \n"
                                       "Underweight : < 18.5\n"
                                       "Normal weight : 18.5-24.9\n"
                                       "Overweight : 25-29.9\n"
                                       "Obesity : BMI of 30 or greater")
        def bf_ask_handler():
            messagebox.showinfo("BF", "The body fat percentage (BFP) of a human or other living being is the total mass of fat divided by total body mass; \n"
                                      "body fat includes essential body fat and storage body fat. \n"
                                      "Essential body fat is necessary to maintain life and reproductive functions.\n"
                                      "The percentage of essential fat is 2-5% in men, and 10-13% in women.\n")

        def wc_ask_handler():
            messagebox.showinfo("WC", "Waist circumference is a simple check to tell \n"
                                      "if you are carrying excess body fat around your middle.\n"
                                      "\nRegardless of your height or build, for most adults a "
                                      "waist measurement of greater than 94 cm for men and 80 cm "
                                      "for women is an indicator of the level of internal fat "
                                      "deposits which coat the heart, kidneys, liver, digestive "
                                      "organs and pancreas. This can increase the risk of heart "
                                      "disease and stroke.")

class Settings(tk.Frame):
    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)
        ####################################### variable - member functions frame
        var_func_frame = tk.LabelFrame(self, text='Variables - Member Functions')
        var_func_frame.grid(row=0, column=0, padx=10)
        # bmi member functions frame
        bmi_memfunc_frame = tk.LabelFrame(var_func_frame, text='Body Mass Index')
        tk.Label(bmi_memfunc_frame, text='LOW:').grid(row=0, column=0, sticky='e')
        self.bmi_low_var = tk.StringVar()
        self.bmi_low_var.set("[0, 15, 22]")
        bmi_low_entry = tk.Entry(bmi_memfunc_frame, textvariable=self.bmi_low_var, width=10)
        bmi_low_entry.grid(row=0, column=1, sticky='we')
        tk.Label(bmi_memfunc_frame, text='MOD:').grid(row=1, column=0, sticky='e')
        self.bmi_mod_var = tk.StringVar()
        self.bmi_mod_var.set('[15, 22, 29]')
        bmi_mod_entry = tk.Entry(bmi_memfunc_frame, textvariable=self.bmi_mod_var, width=10)
        bmi_mod_entry.grid(row=1,column=1)
        tk.Label(bmi_memfunc_frame, text='HIGH:').grid(row=2, column=0, sticky='e')
        self.bmi_high_var = tk.StringVar()
        self.bmi_high_var.set('[22, 29, 35]')
        bmi_high_entry = tk.Entry(bmi_memfunc_frame, textvariable=self.bmi_high_var, width=10)
        bmi_high_entry.grid(row=2, column=1)
        bmi_memfunc_frame.grid(row=0, column=0, padx=5, pady=5)
        plot_bmi_button = tk.Button(bmi_memfunc_frame, text='Plot', command=lambda :plot_bmi())
        plot_bmi_button.grid(row=3, column=0, columnspan=2)
        # bf member functions frame
        bf_memfunc_frame = tk.LabelFrame(var_func_frame, text='Body Fat')
        tk.Label(bf_memfunc_frame, text='LOW:').grid(row=0, column=0, sticky='e')
        self.bf_low_var = tk.StringVar()
        self.bf_low_var.set("[0, 15, 22]")
        bf_low_entry = tk.Entry(bf_memfunc_frame, textvariable=self.bf_low_var, width=10)
        bf_low_entry.grid(row=0, column=1, sticky='we')
        tk.Label(bf_memfunc_frame, text='NOR:').grid(row=1, column=0, sticky='e')
        self. bf_nor_var = tk.StringVar()
        self.bf_nor_var.set('[15, 22, 29]')
        bf_nor_entry = tk.Entry(bf_memfunc_frame, textvariable=self.bf_nor_var, width=10)
        bf_nor_entry.grid(row=1, column=1)
        tk.Label(bf_memfunc_frame, text='HIGH:').grid(row=2, column=0, sticky='e')
        self.bf_high_var = tk.StringVar()
        self.bf_high_var.set('[22, 29, 35]')
        bf_high_entry = tk.Entry(bf_memfunc_frame, textvariable=self.bf_high_var, width=10)
        bf_high_entry.grid(row=2, column=1)
        bf_memfunc_frame.grid(row=0, column=1, padx=5, pady=5)
        plot_bf_button = tk.Button(bf_memfunc_frame, text='Plot', command=lambda : plot_bf())
        plot_bf_button.grid(row=3, column=0, columnspan=2)

        # wc member functions frame
        wc_memfunc_frame = tk.LabelFrame(var_func_frame, text='Waist Circumference')
        tk.Label(wc_memfunc_frame, text='SMALL:').grid(row=0, column=0, sticky='e')
        self.wc_small_var = tk.StringVar()
        self.wc_small_var.set("[0, 30, 60]")
        wc_small_entry = tk.Entry(wc_memfunc_frame, textvariable=self.wc_small_var, width=10)
        wc_small_entry.grid(row=0, column=1, sticky='we')
        tk.Label(wc_memfunc_frame, text='MED:').grid(row=1, column=0, sticky='e')
        self.wc_med_var = tk.StringVar()
        self.wc_med_var.set('[30, 60, 90]')
        wc_med_entry = tk.Entry(wc_memfunc_frame, textvariable=self.wc_med_var, width=10)
        wc_med_entry.grid(row=1, column=1)
        tk.Label(wc_memfunc_frame, text='LARGE:').grid(row=2, column=0, sticky='e')
        self.wc_large_var = tk.StringVar()
        self.wc_large_var.set('[60, 90, 120]')
        wc_large_entry = tk.Entry(wc_memfunc_frame, textvariable=self.wc_large_var, width=10)
        wc_large_entry.grid(row=2, column=1)
        wc_memfunc_frame.grid(row=0, column=2, padx=5, pady=5)
        plot_wc_button = tk.Button(wc_memfunc_frame, text='Plot', command=lambda : plot_wc())
        plot_wc_button.grid(row=3, column=0, columnspan=2)

        # wc member functions frame
        ol_memfunc_frame = tk.LabelFrame(var_func_frame, text='Obesity Level')
        tk.Label(ol_memfunc_frame, text='HEALTHY:').grid(row=0, column=0, sticky='e')
        self.ol_healthy_var = tk.StringVar()
        self.ol_healthy_var.set("[0, 20, 40]")
        ol_healthy_entry = tk.Entry(ol_memfunc_frame, textvariable=self.ol_healthy_var, width=10)
        ol_healthy_entry.grid(row=0, column=1, sticky='we')
        tk.Label(ol_memfunc_frame, text='OVER:').grid(row=1, column=0, sticky='e')
        self.ol_over_var = tk.StringVar()
        self.ol_over_var.set('[30, 50, 70]')
        ol_over_entry = tk.Entry(ol_memfunc_frame, textvariable=self.ol_over_var, width=10)
        ol_over_entry.grid(row=1, column=1)
        tk.Label(ol_memfunc_frame, text='OBESE:').grid(row=2, column=0, sticky='e')
        self.ol_obese_var = tk.StringVar()
        self.ol_obese_var.set('[60, 80, 100]')
        ol_obese_entry = tk.Entry(ol_memfunc_frame, textvariable=self.ol_obese_var, width=10)
        ol_obese_entry.grid(row=2, column=1)
        ol_memfunc_frame.grid(row=0, column=3, padx=5, pady=5)
        plot_ol_button = tk.Button(ol_memfunc_frame, text='Plot', command=lambda : plot_ol())
        plot_ol_button.grid(row=3, column=0, columnspan=2)

        ######################################## rules frame
        rules_frame = tk.LabelFrame(self, text='Rules')
        rules_frame.grid(row=1, column=0, sticky='news', padx=5, pady=5)
        temp = tk.Frame(rules_frame, borderwidth=2, relief=tk.RAISED)
        temp.grid(row=0, column=0, padx=10, pady=5)
        rules_var = tk.StringVar()
        # load rules
        rules_text = tk.Text(temp, wrap='word', width=93,height=31, cursor='plus')
        rules_text.grid(row=0,column=0)
        with open("Rules.txt") as file:
            rules_text.insert(1.0, file.read())
        rules_text.configure(state='disabled')

        ####### event handlers
        def plot_ol():
            x_healthy = eval(self.ol_healthy_var.get())
            x_overweight = eval(self.ol_over_var.get())
            x_obese = eval(self.ol_obese_var.get())
            y = [0, 1, 0]
            plt.plot(x_healthy , y, color='blue', label='HEALTHY')
            plt.plot(x_overweight, y, color='green', label='OVERWEIGHT')
            plt.plot(x_obese, y, color='red', label='OBESE')
            plt.legend()
            plt.show()

        def plot_wc():
            x_small = eval(self.wc_small_var.get())
            x_medium = eval(self.wc_med_var.get())
            x_large = eval(self.wc_large_var.get())
            y = [0, 1, 0]
            plt.plot(x_small, y, color='blue', label='SMALL')
            plt.plot(x_medium, y, color='green', label='MEDIUM')
            plt.plot(x_large, y, color='red', label='LARGE')
            plt.legend()
            plt.show()

        def plot_bf():
            x_low = eval(self.bf_low_var.get())
            x_nor = eval(self.bf_nor_var.get())
            x_high = eval(self.bf_high_var.get())
            y = [0, 1, 0]
            plt.plot(x_low, y, color='blue', label='LOW')
            plt.plot(x_nor, y, color='green', label='NORMAL')
            plt.plot(x_high, y, color='red', label='HIGH')
            plt.legend()
            plt.show()

        def plot_bmi():
            x_low = eval(self.bmi_low_var.get())
            x_mod = eval(self.bmi_mod_var.get())
            x_high = eval(self.bmi_high_var.get())
            y = [0, 1, 0]
            plt.plot(x_low, y, color='blue', label='LOW')
            plt.plot(x_mod, y, color='green', label='MODERATE')
            plt.plot(x_high, y, color='red', label='HIGH')
            plt.legend()
            plt.show()

        def bmi_low_change_handler(event=None):
            global bmi_low_var
            try:
                arr = eval(self.bmi_low_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bmi_low_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bmi_low_var.set("[0, 15, 22]") # set back to default value
                bmi_low_var = [0, 15, 22]
        bmi_low_entry.bind("<Return>", bmi_low_change_handler)

        def bmi_mod_change_handler(event=None):
            global bmi_mod_var
            try:
                arr = eval(self.bmi_mod_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bmi_mod_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bmi_mod_var.set("[15, 22, 29]") # set back to default value
                bmi_mod_var = [15, 22, 29]

        bmi_mod_entry.bind('<Return>', bmi_mod_change_handler)

        def bmi_high_change_handler(event=None):
            global bmi_high_var
            try:
                arr = eval(self.bmi_high_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bmi_high_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bmi_high_var.set("[22, 29, 35]") # set back to default value
                bmi_high_var = [22, 29, 35]
        bmi_high_entry.bind('<Return>', bmi_high_change_handler)

        def bf_low_change_handler(event=None):
            global bf_low_var
            try:
                arr = eval(self.bf_low_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bf_low_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bf_low_var.set("[0, 15, 22]") # set back to default value
                bf_low_var = [0, 15, 22]
        bf_low_entry.bind("<Return>", bf_low_change_handler)

        def bf_nor_change_handler(event=None):
            global bf_nor_var
            try:
                arr = eval(self.bf_nor_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bf_low_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bf_nor_var.set("[15, 22, 29]") # set back to default value
                bf_low_var = [12, 22, 29]
        bf_nor_entry.bind('<Return>', bf_nor_change_handler)

        def bf_high_change_handler(event=None):
            global bf_high_var
            try:
                arr = eval(self.bf_high_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 35:
                        raise Exception("Values Out Of Range")
                else:
                    bf_high_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.bf_high_var.set("[22, 29, 35]") # set back to default value
                bf_high_var = [22, 29, 35]
        bf_high_entry.bind('<Return>', bf_high_change_handler)

        def wc_small_change_handler(event=None):
            global wc_small_var
            try:
                arr = eval(self.wc_small_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 120:
                        raise Exception("Values Out Of Range")
                else:
                    wc_small_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.wc_small_var.set("[0, 30, 60]") # set back to default value
                wc_small_var = [0, 30, 60]
        wc_small_entry.bind('<Return>', wc_small_change_handler)

        def wc_med_change_handler(event=None):
            global wc_med_var
            try:
                arr = eval(self.wc_med_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 120:
                        raise Exception("Values Out Of Range")
                else:
                    wc_med_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.wc_med_var.set("[30, 60, 90]") # set back to default value
                wc_med_var = [30, 60, 90]
        wc_med_entry.bind('<Return>', wc_med_change_handler)

        def wc_large_change_handler(event=None):
            global wc_large_var
            try:
                arr = eval(self.wc_large_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 120:
                        raise Exception("Values Out Of Range")
                else:
                    wc_large_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.wc_large_var.set("[60, 90, 120]") # set back to default value
                wc_large_var = [60, 90, 120]
        wc_large_entry.bind('<Return>', wc_large_change_handler)

        def ol_healthy_change_handler(event=None):
            global ol_healthy_var
            try:
                arr = eval(self.ol_healthy_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 100:
                        raise Exception("Values Out Of Range")
                else:
                    ol_healthy_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.ol_healthy_var.set("[0, 20, 40]") # set back to default value
                ol_healthy_var = [0, 20, 40]
        ol_healthy_entry.bind('<Return>', ol_healthy_change_handler)

        def ol_over_change_handler(event=None):
            global ol_over_var
            try:
                arr = eval(self.ol_over_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 100:
                        raise Exception("Values Out Of Range")
                else:
                    ol_over_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.ol_over_var.set("[20, 40, 60]") # set back to default value
                ol_over_var = [20, 40, 60]
        ol_over_entry.bind('<Return>', ol_over_change_handler)

        def ol_obese_change_handler(event=None):
            global ol_obese_var
            try:
                arr = eval(self.ol_obese_var.get())
                if len(arr) != 3:
                    raise Exception("Invalid Arguments")
                for item in arr:
                    temp = float(item)
                    if temp < 0 or temp > 100:
                        raise Exception("Values Out Of Range")
                else:
                    ol_obese_var = map(float, arr)
            except Exception as e:
                messagebox.showerror("Error", e.message) # display error message
                self.ol_obese_var.set("[60, 80, 100]") # set back to default value
                ol_obese_var = [60, 80, 100]
        ol_obese_entry.bind('<Return>', ol_obese_change_handler)

if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()

Now before you can run GUI.py, search for the folder of skfuzzy in folder Python 2.7/site-packages . And copy this code, paste over the content of controlsystem.py:

"""
controlsystem.py : Framework for the new fuzzy logic control system API.
"""
from __future__ import print_function, division

import numpy as np
import networkx as nx

from skfuzzy import interp_membership, interp_universe, defuzz
from .fuzzyvariable import FuzzyVariable
from .antecedent_consequent import Antecedent, Consequent
from .term import Term, WeightedTerm, TermAggregate
from .rule import Rule
from .visualization import ControlSystemVisualizer

try:
    from collections import OrderedDict
except ImportError:
    from .ordereddict import OrderedDict

class ControlSystem(object):
    """
    Base class to contain a Fuzzy Control System.

    Parameters
    ----------
    rules : Rule or iterable of Rules, optional
        If provided, the system is initialized and populated with a set of
        fuzzy Rules (see ``skfuzzy.control.Rule``). This is optional. If
        omitted the ControlSystem can be built interactively.
    """

    def __init__(self, rules=None):
        """
        Initialization method for the fuzzy ControlSystem object.
        """ + '\n'.join(ControlSystem.__doc__.split('\n')[1:])
        self.graph = nx.DiGraph()
        self._rule_generator = RuleOrderGenerator(self)

        # Construct a system from provided rules, if given
        if rules is not None:
            if hasattr(rules, '__iter__'):
                for rule in rules:
                    self.addrule(rule)
            else:
                try:
                    self.addrule(rules)
                except:
                    raise ValueError("Optional argument `rules` must be a "
                                     "FuzzyRule or iterable of FuzzyRules.")

    @property
    def rules(self):
        """
        Generator which yields Rules in the system in calculation order.
        """
        # We have to expose the rules in the order from antecedents to
        #  consequences.  For example if we have:
        #  Antecedent -> rule1 -> Intermediary -> rule2 -> Consequence
        #  if we expose rule1 before rule2, we won't calculate correctly
        return self._rule_generator

    @property
    def antecedents(self):
        """Generator which yields Antecedents in the system."""
        for node in self.graph.nodes():
            if isinstance(node, Antecedent):
                yield node

    @property
    def consequents(self):
        """Generator which yields Consequents in the system."""
        for node in self.graph.nodes():
            if isinstance(node, Consequent):
                yield node

    @property
    def fuzzy_variables(self):
        """
        Generator which yields fuzzy variables in the system.

        This includes Antecedents, Consequents, and Intermediaries.
        """
        for node in self.graph.nodes():
            if isinstance(node, FuzzyVariable):
                yield node

    def addrule(self, rule):
        """
        Add a new rule to the system.
        """
        if not isinstance(rule, Rule):
            raise ValueError("Input rule must be a Rule object!")

        # Ensure no label duplication
        labels = []
        for r in self.rules:
            if r.label in labels:
                raise ValueError("Input rule cannot have same label, '{0}', "
                                 "as any other rule.".format(r.label))
            labels.append(r.label)

        # Combine the two graphs, which may not be disjoint
        self.graph = nx.compose(self.graph, rule.graph)

    def view(self):
        """
        View a representation of the system NetworkX graph.
        """
        fig = ControlSystemVisualizer(self).view()
        fig.show()

class _InputAcceptor(object):
    """
    Set a single input value to an Antecedent in this ControlSystemSimulation.
    """

    def __init__(self, simulation):
        assert isinstance(simulation, ControlSystemSimulation)
        self.sim = simulation

    def __setitem__(self, key, value):
        # Find the antecedent we should set the input for
        matches = [n for n in self.sim.ctrl.graph.nodes()
                   if isinstance(n, Antecedent) and n.label == key]

        if len(matches) == 0:
            raise ValueError("Unexpected input: " + key)
        assert len(matches) == 1
        var = matches[0]

        if value > var.universe.max():
            if self.sim.clip_to_bounds:
                value = var.universe.max()
            else:
                raise ValueError("Input value out of bounds.  Max is %s" %
                                 max(var.universe))
        if value < var.universe.min():
            if self.sim.clip_to_bounds:
                value = var.universe.min()
            else:
                raise ValueError("Input value is out of bounds.  Min is %s" %
                                 min(var.universe))

        var.input['current'] = value
        self.sim._update_unique_id()
        self._update_to_current()

    def __repr__(self):
        """
        Print a convenient string representation of all current input data.
        """
        current_inputs = self._get_inputs()
        out = ""
        for key, val in current_inputs.items():
            out += "{0} : {1}\n".format(key, val)
        return out

    def _update_to_current(self):
        # Private method, used to store the current state of the system in a
        # cache, 'current', accessible before and after the unique_id changes.
        if self.sim.unique_id == 'current':
            return

        # Find all antecedents
        matches = [n for n in self.sim.ctrl.graph.nodes()
                   if isinstance(n, Antecedent)]

        for antecedent in matches:
            antecedent.input[self.sim] = antecedent.input['current']

    def _get_inputs(self):
        """
        Find and return all antecedent inputs available.
        """
        antecedents = [n for n in self.sim.ctrl.graph.nodes()
                       if isinstance(n, Antecedent)]

        inputs = OrderedDict()
        for antecedent in antecedents:
            try:
                inputs[antecedent.label] = antecedent.input['current']
            except AttributeError:
                # No system ID yet, because no assigned values
                inputs[antecedent.label] = None

        return inputs

class ControlSystemSimulation(object):
    """
    Calculate results from a ControlSystem.

    Parameters
    ----------
    control_system : ControlSystem
        A fuzzy ControlSystem object.
    clip_to_bounds : bool, optional
        Controls if input values should be clipped to the consequent universe
        range. Default is True.
    cache : bool, optional
        Controls if results should be stored for reference in fuzzy variable
        objects, allowing fast lookup for repeated runs of `.compute()`.
        Unless you are heavily memory constrained leave this `True` (default).
    flush_after_run : int, optional
        Clears cached results after this many repeated, unique simulations.
        The default of 1000 is appropriate for most hardware, but for small
        embedded systems this can be lowered as appropriate. Higher memory
        systems may see better performance with a higher limit.
    """

    def __init__(self, control_system, clip_to_bounds=True, cache=True,
                 flush_after_run=1000):
        """
        Initialize a new ControlSystemSimulation.
        """ + '\n'.join(ControlSystemSimulation.__doc__.split('\n')[1:])
        assert isinstance(control_system, ControlSystem)
        self.ctrl = control_system

        self.input = _InputAcceptor(self)
        self.output = OrderedDict()
        self.cache = cache
        self.unique_id = self._update_unique_id()

        self.clip_to_bounds = clip_to_bounds
        self._calculated = []

        self._run = 0
        self._flush_after_run = flush_after_run

    def _update_unique_id(self):
        """
        Unique hash of this control system including a specific set of inputs.

        Generated at runtime from the system state. Used as key to access data
        from `StatePerSimulation` objects, enabling multiple runs.
        """
        # The string to be hashed is the concatenation of:
        #  * the control system ID, which is independent of inputs
        #  * hash of the current input OrderedDict

        # Simple hashes and Python ids are fast and serve our purposes.
        self.unique_id = (str(id(self.ctrl)) +
                          str(hash(self._get_inputs().__repr__())))

    def _get_inputs(self):
        return self.input._get_inputs()

    def inputs(self, input_dict):
        """
        Convenience method to accept multiple inputs to antecedents.

        Parameters
        ----------
        input_dict : dict
            Contains key:value pairs where the key is the label for a
            connected Antecedent and the value is the input.
        """
        # Have to set this twice - first time to get the unique ID,
        #  second time to actually pass them into the correct ID.
        for label, value in input_dict.items():
            self.input[label] = value

    def compute(self):
        """
        Compute the fuzzy system.
        """
        self.input._update_to_current()

        # Shortcut with lookup if this calculation was done before
        if self.cache is not False and self.unique_id in self._calculated:
            for consequent in self.ctrl.consequents:
                self.output[consequent.label] = consequent.output[self]
            return

        # If we get here, cache is disabled OR the inputs are novel. Compute!

        # Check if any fuzzy variables lack input values and fuzzify inputs
        for antecedent in self.ctrl.antecedents:
            if antecedent.input[self] is None:
                raise ValueError("All antecedents must have input values!")
            CrispValueCalculator(antecedent, self).fuzz(antecedent.input[self])

        # Calculate rules, taking inputs and accumulating outputs
        first = True
        for rule in self.ctrl.rules:
            # Clear results of prior runs from Terms if needed.
            if first:
                for c in rule.consequent:
                    c.term.membership_value[self] = None
                    c.activation[self] = None
                first = False
            self.compute_rule(rule)

        # Collect the results and present them as a dict
        for consequent in self.ctrl.consequents:
            consequent.output[self] = \
                CrispValueCalculator(consequent, self).defuzz()
            self.output[consequent.label] = consequent.output[self]

        # Make note of this run so we can easily find it again
        if self.cache is not False:
            self._calculated.append(self.unique_id)
        else:
            # Reset StatePerSimulations
            self._reset_simulation()

        # Increment run number
        self._run += 1
        if self._run % self._flush_after_run == 0:
            self._reset_simulation()

    def compute_rule(self, rule):
        """
        Implement rule according to Mamdani inference.

        The three step method consists of::
         * Aggregation
         * Activation
         * Accumulation
        """
        # Step 1: Aggregation.  This finds the net accomplishment of the
        #  antecedent by AND-ing or OR-ing together all the membership values
        #  of the terms that make up the accomplishment condition.
        #  The process of actually aggregating everything is delegated to the
        #  TermAggregation class, but we can tell that class
        #  what aggregation style this rule mandates
        if isinstance(rule.antecedent, TermAggregate):
            rule.antecedent.agg_method = rule.aggregation_method
        rule.aggregate_firing[self] = rule.antecedent.membership_value[self]

        # Step 2: Activation.  The degree of membership of the consequence
        #  is determined by the degree of accomplishment of the antecedent,
        #  which is what we determined in step 1.  The only difference would
        #  be if the consequent has a weight, which we would apply now.
        for c in rule.consequent:
            assert isinstance(c, WeightedTerm)
            c.activation[self] = rule.aggregate_firing[self] * c.weight

        # Step 3: Accumulation.  Apply the activation to each consequent,
        #   accumulating multiple rule firings into a single membership value.
        #   The process of actual accumulation is delegated to the
        #   Term which uses its parent's accumulation method
        for c in rule.consequent:
            assert isinstance(c, WeightedTerm)
            term = c.term
            value = c.activation[self]

            # Find new membership value
            if term.membership_value[self] is None:
                term.membership_value[self] = value
            else:
                # Use the accumulation method of variable to determine
                #  how to to handle multiple cuts
                accu = term.parent.accumulation_method
                term.membership_value[self] = accu(value,
                                                   term.membership_value[self])

            term.cuts[self][rule.label] = term.membership_value[self]

    def _reset_simulation(self):
        """
        Clear temporary data from simulation objects.

        Called internally if cache=False (after every run) or after a certain
        number of runs if cache=True according to the `flush_after_run` kwarg.
        """
        def _clear_terms(fuzzy_var):
            for term in fuzzy_var.terms.values():
                term.membership_value.clear()
                term.cuts.clear()

        for rule in self.ctrl.rules:
            rule.aggregate_firing.clear()
            for c in rule.consequent:
                c.activation.clear()

        for consequent in self.ctrl.consequents:
            consequent.output.clear()
            _clear_terms(consequent)

        self._calculated = []
        self._run = 0

    def print_state(self):
        """
        Print info about the inner workings of a ControlSystemSimulation.
        """
        if self.ctrl.consequents.next().output[self] is None:
            raise ValueError("Call compute method first.")
        res = ""
        print("============="); res += "=============\n"
        print(" Antecedents "); res += " Antecedents \n"
        print("============="); res += "=============\n"
        for v in self.ctrl.antecedents:
            print("{0:<35} = {1}".format(v, v.input[self]));
            res += "{0:<35} = {1}\n".format(v, v.input[self])
            for term in v.terms.values():
                print("  - {0:<32}: {1}".format(term.label,
                                                term.membership_value[self]))
                res += "  - {0:<32}: {1}\n".format(term.label,
                                                term.membership_value[self])
        print(""); res += "\n"
        print("======="); res += "=======\n"
        print(" Rules "); res += " Rules \n"
        print("======="); res += "=======\n"
        rule_number = {}
        for rn, r in enumerate(self.ctrl.rules):
            assert isinstance(r, Rule)
            rule_number[r] = "RULE #%d" % rn
            print("RULE #%d:\n  %s\n" % (rn, r))
            res += "RULE #%d:\n  %s\n" % (rn, r)
            print("  Aggregation (IF-clause):")
            res += "  Aggregation (IF-clause):\n"
            for term in r.antecedent_terms:
                assert isinstance(term, Term)
                print("  - {0:<55}: {1}".format(term.full_label,
                                                term.membership_value[self]))
                res += "  - {0:<55}: {1}\n".format(term.full_label,                                                 term.membership_value[self])             print("    {0:>54} = {1}".format(r.antecedent,
                                             r.aggregate_firing[self]))
            res += "    {0:>54} = {1}\n".format(r.antecedent,
                                             r.aggregate_firing[self])

            print("  Activation (THEN-clause):")
            res += "  Activation (THEN-clause):\n"
            for c in r.consequent:
                assert isinstance(c, WeightedTerm)
                print("    {0:>54} : {1}".format(c,
                                                 c.activation[self]))
                res += "    {0:>54} : {1}\n".format(c,
                                                 c.activation[self])
            print(""); res += "\n"
        print(""); res += "\n"

        print("=============================="); res += "==============================\n"
        print(" Intermediaries and Conquests "); res += " Intermediaries and Conquests \n"
        print("=============================="); res += "==============================\n"
        for c in self.ctrl.consequents:
            print("{0:<36} = {1}".format(
                c, CrispValueCalculator(c, self).defuzz()))
            res += "{0:<36} = {1}\n".format(                 c, CrispValueCalculator(c, self).defuzz())             for term in c.terms.values():                 print("  %s:" % term.label); res += "  %s:\n" % term.label                 for cut_rule, cut_value in term.cuts[self].items():                     if cut_rule not in rule_number.keys():                         continue                     print("    {0:>32} : {1}".format(rule_number[cut_rule],
                                                     cut_value))
                    res += "    {0:>32} : {1}\n".format(rule_number[cut_rule],
                                                     cut_value)
                accu = "Accumulate using %s" % c.accumulation_method.func_name
                print("    {0:>32} : {1}".format(accu,
                                                 term.membership_value[self]))
                res += "    {0:>32} : {1}\n".format(accu,
                                                 term.membership_value[self])
            print(""); res += "\n"
        return res

class CrispValueCalculator(object):
    """
    Convert a calculated FuzzyVariable back into a crisp real number.

    Parameters
    ----------
    fuzzy_var : FuzzyVariable
        The fuzzy variable to be defuzzified.
    sim : ControlSystemSimulation
        The simulation which holds all necessary data for this calculation.
    """

    def __init__(self, fuzzy_var, sim):
        """
        Initialization method for CrispValueCalculator.
        """ + '\n'.join(CrispValueCalculator.__doc__.split('\n')[1:])
        assert isinstance(fuzzy_var, FuzzyVariable)
        assert isinstance(sim, ControlSystemSimulation)
        self.var = fuzzy_var
        self.sim = sim

    def defuzz(self):
        """Derive crisp value based on membership of adjective(s)."""
        ups_universe, output_mf, cut_mfs = self.find_memberships()

        if len(cut_mfs) == 0:
            raise ValueError("No terms have memberships.  Make sure you "
                             "have at least one rule connected to this "
                             "variable and have run the rules calculation.")

        try:
            return defuzz(ups_universe, output_mf,
                          self.var.defuzzify_method)
        except AssertionError:
            raise 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.")

    def fuzz(self, value):
        """
        Propagate crisp value down to adjectives by calculating membership.
        """
        if len(self.var.terms) == 0:
            raise ValueError("Set Term membership function(s) first")

        for label, term in self.var.terms.items():
            term.membership_value[self.sim] = \
                interp_membership(self.var.universe, term.mf, value)

    def find_memberships(self):
        # Check we have some adjectives
        if len(self.var.terms.keys()) == 0:
            raise ValueError("Set term membership function(s) first")

        '''
        First we have to upsample the universe of self.var in order to add the
        key points of the membership function based on the activation level
        for this consequent, using the interp_universe function, which
        interpolates the `xx` values in the universe such that its membership
        function value is the activation level.
        '''
        add_universe = set()
        for label, term in self.var.terms.items():
            cut = term.membership_value[self.sim]
            if cut is None:
                continue  # No membership defined for this adjective
            add_xx = interp_universe(self.var.universe, term.mf, cut)
            add_universe.update(add_xx)

        # We are only interested in points not in self.var.universe
        add_universe = add_universe - set(self.var.universe)

        # We want to sort the universe values and keep related their indices
        # to access to their mf values
        upsampled_universe = (
            list(zip(self.var.universe.tolist() + list(add_universe),
                     list(range(self.var.universe.size)) + [None] * len(add_universe))))

        upsampled_universe.sort(key=lambda element: element[0])
        upsampled_mf_indices = [element[1] for element in upsampled_universe]
        upsampled_universe = np.array([
            element[0] for element in upsampled_universe])

        # Initilize membership
        output_mf = np.zeros_like(upsampled_universe, dtype=np.float64)

        # Build output membership function
        term_mfs = {}
        for label, term in self.var.terms.items():
            cut = term.membership_value[self.sim]

            if cut is None:
                continue  # No membership defined for this adjective

            upsampled_mf = []
            for i in range(len(upsampled_mf_indices)):
                if upsampled_mf_indices[i] is not None:
                    upsampled_mf.append(term.mf[upsampled_mf_indices[i]])
                else:
                    upsampled_mf.append(
                        interp_membership(self.var.universe, term.mf,
                                          upsampled_universe[i]))

            term_mfs[label] = np.minimum(cut, upsampled_mf)
            np.maximum(output_mf, term_mfs[label], output_mf)

        return upsampled_universe, output_mf, term_mfs

class RuleOrderGenerator(object):
    """
    Generator to yield rules in the correct order for calculation.

    Parameters
    ----------
    control_system : ControlSystem
        Fuzzy control system object, instance of `skfuzzy.ControlSystem`.

    Returns
    -------
    out : Rule
        Fuzzy rules in computation order.
    """

    def __init__(self, control_system):
        """
        Generator to yield rules in the correct order for calculation.
        """ + '\n'.join(RuleOrderGenerator.__doc__.split('\n')[1:6])
        assert isinstance(control_system, ControlSystem)
        self.control_system = control_system
        self._cache = []
        self._cached_graph = None

    def __iter__(self):
        """
        Method to yield the fuzzy rules in order for computation.
        """
        # Determine if we can return the cached version or must calc new
        if self._cached_graph is not self.control_system.graph:
            # The controller is still using a different version of the graph
            #  than we created the rule order for.  Thus, make new cache
            self._init_state()
            self._cache = list(self._process_rules(self.all_rules[:]))
            self._cached_graph = self.control_system.graph

        for n, r in enumerate(self._cache):
            yield r
        else:
            n = 0

        if n == 0:
            pass
        else:
            assert n == len(self.all_rules) - 1, "Not all rules exposed"

    def _init_state(self):
        # This graph will represent what's been calculated so far.  We
        # initialize it to just the antecedents as they, by definition, already
        # have fuzzy values
        self.calced_graph = nx.DiGraph()
        for a in self.control_system.antecedents:
            self.calced_graph.add_star([a, ] + list(a.terms.values()))

        self.all_graph = self.control_system.graph

        self.all_rules = []
        for node in self.all_graph.nodes():
            if isinstance(node, Rule):
                self.all_rules.append(node)

    def _process_rules(self, rules):
        # Recursive funcion to process rules in the correct firing order
        len_rules = len(rules)
        skipped_rules = []
        while len(rules) > 0:
            rule = rules.pop(0)
            if self._can_calc_rule(rule):
                yield rule
                # Add rule to calced graph
                self.calced_graph = nx.compose(self.calced_graph, rule.graph)
            else:
                # We have not calculated the predecsors for this rule yet.
                #  Skip it for now
                skipped_rules.append(rule)

        if len(skipped_rules) == 0:
            # All done!
            raise StopIteration()
        else:
            if len(skipped_rules) == len_rules:
                # Avoid being caught in an infinite loop
                raise RuntimeError("Unable to resolve rule execution order. "
                                   "The most likely reason is two or more "
                                   "rules that depend on each other.\n"
                                   "Please check the rule graph for loops.")
            else:
                # Recurse across the skipped rules
                for r in self._process_rules(skipped_rules):
                    yield r

    def _can_calc_rule(self, rule):
        # Check that we've exposed all inputs to this rule by ensuring
        # the predecessor-degree of each predecessor node is the same
        # in both the calculation graph and overall graph
        for p in self.all_graph.predecessors_iter(rule):
            assert isinstance(p, Term)
            if p not in self.calced_graph:
                return False

            all_degree = len(self.all_graph.predecessors(p))
            calced_degree = len(self.calced_graph.predecessors(p))
            if all_degree != calced_degree:
                return False
        return True

Final result:

This might be the longest post I’ve ever made. If you need help, ask me in the comment section.
Cheers!

Advertisements