Postingan lainnya
Fuzzy logic implementation
permisi gan, saya baru belajar disini, saya mau mengimplementasikan fuzzy logic tapi applikasi tidak berjalan apabila saya memasukkan nilai. saya mengikuti contoh koding dari referensi saya namun masih stop working. apakah ada yabg bisa membantu? berikut saya sertakan potongan source code nya. terima kasih
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
from skfuzzy.control.visualization import FuzzyVariableVisualizer
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import QTime, QTimer, QDate, QDateTime, Qt
from time import strftime
import sys
from matplotlib.backends.backend_qt5 import NavigationToolbar2QT as NavigationToolbar
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from Desktop import Ui_DiagnosaTrafo
class Fuzzy(QDialog):
def __init__(self):
super(Fuzzy,self).__init__()
self.ui = Ui_DiagnosaTrafo()
self.ui.setupUi(self)
#set clock
timer = QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(1000)
self.ppmMax = 100
self.gasMax = 4
self.ppm_min = [0, 10, 20, 30]
self.ppm_max = [80, 85, 90, 100]
self.gas_min = [0, 0, 1, 1.5]
self.gas_max = [3, 3, 3.5, 4.]
self.ui.SET.toggled.connect(self.SET)
self.ui.SET.setCheckable(True)
self.ui.calculate.toggled.connect(self.calculate)
self.ui.calculate.setCheckable(True)
def showlcd(self):
now = QDate.currentDate()
#datetime = QDateTime.currentDateTime()
#time = QTime.currentTime()
thistime = now.toString(Qt.DefaultLocaleLongDate)
waktu = strftime('%H:%M:%S %p')
self.ui.dates.setText(thistime)
self.ui.clock.setText(waktu)
def SET(self):
try :
V_ppm_min = float (self.ui.line_ppm_min.text())
except:
V_ppm_min = 10.0
try:
V_ppm_max = float (self.ui.line_ppm_max.text())
except:
V_ppm_max = 100.0
try :
V_gas_min = float (self.ui.line_gas_min.text())
except:
V_gas_min = 1.0
try:
V_gas_max = float (self.ui.line_gas_max.text())
except:
V_gas_max = 4.0
try:
normal = float (self.ui.line_KNormal.text())
except:
normal = 25.0
try:
mulai_tinggi = float (self.ui.line_KMulai.text())
except:
mulai_tinggi = 50.0
try:
tingkat_tinggi = float (self.ui.line_KTingkat.text())
except:
tingkat_tinggi = 75.0
try:
sangat_tinggi = float (self.ui.line_KSangat.text())
except:
sangat_tinggi = 100.0
self.ppm_rendah = [0,0, V_ppm_min]
self.ppm_tinggi =[0,0,V_ppm_max]
self.gas_rendah = [0,0,V_gas_min]
self.gas_tinggi = [0,0,V_gas_max]
self.normal = [0,0,normal]
self.mulai_tinggi = [0,0, mulai_tinggi]
self.tingkat_tinggi = [0,0,tingkat_tinggi]
self.sangat_tinggi = [0,0,sangat_tinggi]
#give value to default setting
self.ui.lbl_ppm_min.setText('{}'.format(V_ppm_min))
self.ui.lbl_ppm_max.setText('{}'.format(V_ppm_max))
self.ui.lbl_gas_min.setText('{}'.format(V_gas_min))
self.ui.lbl_gas_max.setText('{}'.format(V_gas_max))
self.ui.lbl_kondisi_normal.setText('{}'.format(normal))
self.ui.lbl_kondisi_MTinggi.setText('{}'.format(mulai_tinggi))
self.ui.lbl_kondisi_TTinggi.setText('{}'.format(tingkat_tinggi))
self.ui.lbl_kondisi_STinggi.setText('{}'.format(sangat_tinggi))
self.ppm1 = V_ppm_max
self.gas1 = V_gas_max
self.kondisi = sangat_tinggi
def calculate (self):
# The universe of variables and membership functions
print("X=")
ppm = ctrl.Antecedent(np.arange(10, self.ppm1, 1), 'ppm')
gas = ctrl.Antecedent(np.arange(1, self.gas1, 1), 'gas')
kondisi = ctrl.Consequent(np.arange(0, self.kondisi, 1), 'kondisi')
#custom membership function can be built interactive with a familiar
ppm['rendah'] = fuzz.trimf(ppm.universe, self.ppm_rendah)
ppm['tinggi'] = fuzz.trimf(ppm.universe, self.ppm_tinggi)
gas['rendah'] = fuzz.trimf(gas.universe, self.gas_rendah)
gas['tingi'] = fuzz.trimf(gas.universe, self.gas_tinggi)
kondisi['normal'] = fuzz.trimf(kondisi.universe, self.normal)
kondisi['mulai tinggi'] = fuzz.trimf(kondisi.universe, self.mulai_tinggi)
kondisi['tingkat tinggi'] = fuzz.trimf(kondisi.universe, self.tingkat_tinggi)
kondisi['sangat tinggi'] = fuzz.trimf(kondisi.universe, self.sangat_tinggi)
print("X1= ")
"""
=============================
DECLARE THE RULES
=============================
"""
rule1 = ctrl.Rule(ppm['rendah'] & gas['rendah'], kondisi['normal'])
rule2 = ctrl.Rule(ppm['rendah'] & gas['tinggi'], kondisi['mulai tinggi'])
rule3 = ctrl.Rule(ppm['tinggi'] & gas['rendah'], kondisi['tingkat tinggi'])
rule4 = ctrl.Rule(ppm['tinggi'] & gas['tinggi'], kondisi['sangat tinggi'])
print("X2= ")
kondisi_ctrl = ctrl.ControlSystem(
[rule1, rule2, rule3, rule4 ])
"""
=============================
SIMULATION
=============================
Pass inputs to the ControlSystem using Antecedent labels with Pythonic API
Note: if you like passing many inputs all at once, use .inputs(dict_of_data)
"""
condition = ctrl.ControlSystemSimulation(kondisi_ctrl)
try:
x_ppm = float (self.ui.line_input_ppm.text())
x_gas = float (self.ui.line_input_gas.text())
condition.input['PPM'] = x_ppm
condition.input['GAS'] = x_gas
except:
condition.input['PPM'] = 0
condition.input['GAS'] = 0
x_ppm = 0
x_gas = 0
print(x_ppm)
print(x_gas)
condition.compute()
self.ui.layout_output.setText("{0:.2f} ".format(condition.output['kondisi']))
for i in reversed(range(self.ui.layout_1.count())):
self.ui.layout_1.itemAt(i).widget().setParent(None)
for i in reversed(range(self.ui.layout_2.count())):
self.ui.layout_2.itemAt(i).widget().setParent(None)
plt.rcParams['figure.constrained_layout.use'] = True
a1, b1 = FuzzyVariableVisualizer(ppm).view()
a2, b2 = FuzzyVariableVisualizer(gas).view()
a3, b3 = FuzzyVariableVisualizer(kondisi).view()
b1.axvline(x=x_ppm, linewidth=3, color = 'k')
b2.axvline(x=x_gas, linewidth=3, color = 'k')
self.ui.canvas1 = FigureCanvas(a1)
self.toolbar1 = NavigationToolbar(self.ui.canvas1, self)
self.ui.canvas2 = FigureCanvas(a2)
self.ui.toolbar2 = NavigationToolbar(self.ui.canvas2, self)
self.ui.canvas3 = FigureCanvas(a3)
self.toolbar3 = NavigationToolbar(self.ui.canvas3, self)
self.ui.layout_1.addWidget(self.ui.canvas1)
self.ui.layout_2.addWidget(self.ui.canvas2)
a4, b= FuzzyVariableVisualizer(kondisi).view(sim=condition)
self.ui.canvas4 = FigureCanvas(a4)
self.toolbar4 = NavigationToolbar(self.ui.canvas4, self)
self.ui.layout_3.addWidget(self.ui.canvas4)
self.ui.layout_3.addWidget(self.toolbar4)
kondisi = None
condition = None
if __name__ == "__main__":
app = QApplication(sys.argv)
Window = Fuzzy()
Window.setWindowTitle("Diagnosa Trafo")
Window.show()
sys.exit(app.exec_())
0