import numpy as np
import skfuzzy as fuzz
import matplotlib.pyplot as plt
from skfuzzy import control as ctrl
temp = fuzz.control.Antecedent(np.arange(-30, 61, 1), 'podana')
temptarget = fuzz.control.Antecedent(np.arange(-30, 61, 1), 'zadana')
answer = fuzz.control.Consequent(np.arange(30, 86, 1), 'wymagana')
temp['vc_temp'] = fuzz.trapmf(temp.universe, [-30,-25,4,10])
temp['lc_temp'] = fuzz.trapmf(temp.universe, [6,9,17,20])
temp['mm_temp'] = fuzz.trapmf(temp.universe, [17,20,24,27])
temp['lh_temp'] = fuzz.trapmf(temp.universe, [25,27,33,36])
temp['vh_temp'] = fuzz.trapmf(temp.universe, [33,36,55,60])
temptarget['vc_temptarget'] = fuzz.trapmf(temptarget.universe, [-30,-25,4,10])
temptarget['lc_temptarget'] = fuzz.trapmf(temptarget.universe, [6,9,17,20])
temptarget['mm_temptarget'] = fuzz.trapmf(temptarget.universe, [17,20,24,27])
temptarget['lh_temptarget'] = fuzz.trapmf(temptarget.universe, [25,27,33,36])
temptarget['vh_temptarget'] = fuzz.trapmf(temptarget.universe, [33,36,55,60])
answer['lc_answer'] = fuzz.trimf(answer.universe, [30,35,45])
answer['nc_answer'] = fuzz.trimf(answer.universe, [40,53,60])
answer['hc_answer'] = fuzz.trimf(answer.universe, [55,80,85])
print('saving memberships')
rule1 = ctrl.Rule(temp['vc_temp']&temptarget['vc_temptarget'], answer['nc_answer'])
rule2 = ctrl.Rule(temp['vc_temp']&temptarget['lc_temptarget'], answer['hc_answer'])
rule3 = ctrl.Rule(temp['vc_temp']&temptarget['mm_temptarget'], answer['hc_answer'])
rule4 = ctrl.Rule(temp['vc_temp']&temptarget['lh_temptarget'], answer['hc_answer'])
rule5 = ctrl.Rule(temp['vc_temp']&temptarget['vh_temptarget'], answer['hc_answer'])
rule6 = ctrl.Rule(temp['lc_temp']&temptarget['vc_temptarget'], answer['lc_answer'])
rule7 = ctrl.Rule(temp['lc_temp']&temptarget['lc_temptarget'], answer['nc_answer'])
rule8 = ctrl.Rule(temp['lc_temp']&temptarget['mm_temptarget'], answer['hc_answer'])
rule9 = ctrl.Rule(temp['lc_temp']&temptarget['lh_temptarget'], answer['hc_answer'])
rule10 = ctrl.Rule(temp['lc_temp']&temptarget['vh_temptarget'], answer['hc_answer'])
rule11 = ctrl.Rule(temp['mm_temp']&temptarget['vc_temptarget'], answer['lc_answer'])
rule12 = ctrl.Rule(temp['mm_temp']&temptarget['lc_temptarget'], answer['lc_answer'])
rule13 = ctrl.Rule(temp['mm_temp']&temptarget['mm_temptarget'], answer['nc_answer'])
rule14 = ctrl.Rule(temp['mm_temp']&temptarget['lh_temptarget'], answer['hc_answer'])
rule15 = ctrl.Rule(temp['mm_temp']&temptarget['vh_temptarget'], answer['hc_answer'])
rule16 = ctrl.Rule(temp['lh_temp']&temptarget['vc_temptarget'], answer['lc_answer'])
rule17 = ctrl.Rule(temp['lh_temp']&temptarget['lc_temptarget'], answer['lc_answer'])
rule18 = ctrl.Rule(temp['lh_temp']&temptarget['mm_temptarget'], answer['lc_answer'])
rule19 = ctrl.Rule(temp['lh_temp']&temptarget['lh_temptarget'], answer['nc_answer'])
rule20 = ctrl.Rule(temp['lh_temp']&temptarget['vh_temptarget'], answer['hc_answer'])
rule21 = ctrl.Rule(temp['vh_temp']&temptarget['vc_temptarget'], answer['lc_answer'])
rule22 = ctrl.Rule(temp['vh_temp']&temptarget['lc_temptarget'], answer['lc_answer'])
rule23 = ctrl.Rule(temp['vh_temp']&temptarget['mm_temptarget'], answer['lc_answer'])
rule24 = ctrl.Rule(temp['vh_temp']&temptarget['lh_temptarget'], answer['lc_answer'])
rule25 = ctrl.Rule(temp['vh_temp']&temptarget['vh_temptarget'], answer['nc_answer'])
print('saving rules')
heating_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10, rule11, rule12, rule13, rule14, rule15, rule16, rule17, rule18, rule19, rule20, rule21, rule22, rule24, rule25])
heating = ctrl.ControlSystemSimulation(heating_ctrl)
print('saving rules2')
heating.input['temptarget']=30
print('saving rules3')
heating.compute()
print (heating.output['tip'])
answer.view(sim=heating)
print('generating output')