forked from kiyan-rezaee/Single-Layer-Perceptron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
311 lines (278 loc) · 12.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import sys
import subprocess
try:
from PyQt5 import QtCore, QtGui, QtWidgets
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", 'PyQt5'])
finally:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
try:
import pyqtgraph as pg
except ImportError:
subprocess.check_call(
[sys.executable, "-m", "pip", "install", 'pyqtgraph'])
finally:
import pyqtgraph as pg
w1, w2, alpha = 0.0, 0.0, 0.0
OPR = ""
class Windows:
def __init__(self):
self.main = Ui_MainWindow()
self.func = Ui_func()
def show_main(self):
self.main.setupUi(self.main.window)
self.main.window.show()
def switch_window(self):
self.main.window.close()
self.func.setupUi(self.func.window)
self.func.window.show()
def switch_back(self):
self.func.window.close()
self.main.setupUi(self.main.window)
self.main.window.show()
class Ui_MainWindow(object):
def __init__(self):
self.window = QtWidgets.QMainWindow()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(850, 335)
MainWindow.setMinimumSize(QtCore.QSize(850, 335))
MainWindow.setMaximumSize(QtCore.QSize(850, 335))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 0, 831, 51))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 30, 831, 271))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(10, 0, 10, 0)
self.gridLayout.setHorizontalSpacing(40)
self.gridLayout.setVerticalSpacing(30)
self.gridLayout.setObjectName("gridLayout")
self.NOR = QtWidgets.QPushButton(self.gridLayoutWidget)
self.NOR.setObjectName("NOR")
self.gridLayout.addWidget(self.NOR, 1, 0, 1, 1)
self.AND = QtWidgets.QPushButton(self.gridLayoutWidget)
self.AND.setObjectName("AND")
self.gridLayout.addWidget(self.AND, 0, 1, 1, 1)
self.OR = QtWidgets.QPushButton(self.gridLayoutWidget)
self.OR.setObjectName("OR")
self.gridLayout.addWidget(self.OR, 0, 0, 1, 1)
self.XOR = QtWidgets.QPushButton(self.gridLayoutWidget)
self.XOR.setObjectName("XOR")
self.gridLayout.addWidget(self.XOR, 0, 2, 1, 1)
self.NAND = QtWidgets.QPushButton(self.gridLayoutWidget)
self.NAND.setObjectName("NAND")
self.gridLayout.addWidget(self.NAND, 1, 1, 1, 1)
self.XNOR = QtWidgets.QPushButton(self.gridLayoutWidget)
self.XNOR.setObjectName("XNOR")
self.gridLayout.addWidget(self.XNOR, 1, 2, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 850, 28))
self.label_2 = QLabel(self.centralwidget)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(10, 243, 831, 81))
self.label_2.setAlignment(Qt.AlignCenter)
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.NOR.clicked.connect(lambda: self.switchWindow('NOR'))
self.XNOR.clicked.connect(lambda: self.switchWindow('XNOR'))
self.OR.clicked.connect(lambda: self.switchWindow('OR'))
self.AND.clicked.connect(lambda: self.switchWindow('AND'))
self.NAND.clicked.connect(lambda: self.switchWindow('NAND'))
self.XOR.clicked.connect(lambda: self.switchWindow('XOR'))
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Gate Networks"))
self.label.setText(
_translate("MainWindow", "WELCOME TO GATE NETWORKS!!!"))
self.NOR.setText(_translate("MainWindow", "NOR"))
self.AND.setText(_translate("MainWindow", "AND"))
self.OR.setText(_translate("MainWindow", "OR"))
self.XOR.setText(_translate("MainWindow", "XOR"))
self.NAND.setText(_translate("MainWindow", "NAND"))
self.XNOR.setText(_translate("MainWindow", "XNOR"))
self.label_2.setText(QCoreApplication.translate(
"MainWindow", u"Ali Dadashzadeh - Kiyan Rezaei - Vahid Aghilzadeh", None))
def switchWindow(self, text):
global OPR
OPR = text
Page.switch_window()
class Ui_func(object):
def __init__(self):
self.window = QtWidgets.QMainWindow()
def setupUi(self, func):
func.setObjectName("func")
func.resize(850, 554)
func.setMinimumSize(QtCore.QSize(850, 554))
func.setMaximumSize(QtCore.QSize(850, 554))
font = QtGui.QFont()
font.setFamily("Arial")
font.setPointSize(14)
func.setFont(font)
self.centralwidget = QtWidgets.QWidget(func)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 0, 831, 51))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 50, 341, 451))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.grid = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.grid.setContentsMargins(10, 0, 10, 5)
self.grid.setHorizontalSpacing(17)
self.grid.setVerticalSpacing(39)
self.grid.setObjectName("grid")
self.alpha = QtWidgets.QSlider(self.gridLayoutWidget)
self.alpha.setOrientation(QtCore.Qt.Horizontal)
self.alpha.setObjectName("alpha")
self.grid.addWidget(self.alpha, 1, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_4.setObjectName("label_4")
self.grid.addWidget(self.label_4, 4, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_2.setObjectName("label_2")
self.grid.addWidget(self.label_2, 0, 0, 1, 1)
self.w1 = QtWidgets.QSlider(self.gridLayoutWidget)
self.w1.setOrientation(QtCore.Qt.Horizontal)
self.w1.setObjectName("w1")
self.grid.addWidget(self.w1, 3, 0, 1, 1)
self.label_3 = QtWidgets.QLabel(self.gridLayoutWidget)
self.label_3.setObjectName("label_3")
self.grid.addWidget(self.label_3, 2, 0, 1, 1)
self.w2 = QtWidgets.QSlider(self.gridLayoutWidget)
self.w2.setOrientation(QtCore.Qt.Horizontal)
self.w2.setObjectName("w2")
self.grid.addWidget(self.w2, 5, 0, 1, 1)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setSpacing(17)
self.horizontalLayout.setObjectName("horizontalLayout")
self.back = QtWidgets.QPushButton(self.gridLayoutWidget)
self.back.setObjectName("back")
self.horizontalLayout.addWidget(self.back)
self.submit = QtWidgets.QPushButton(self.gridLayoutWidget)
self.submit.setObjectName("submit")
self.horizontalLayout.addWidget(self.submit)
self.grid.addLayout(self.horizontalLayout, 6, 0, 1, 1)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(370, 50, 20, 441))
self.line.setFrameShape(QtWidgets.QFrame.VLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
func.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(func)
self.menubar.setGeometry(QtCore.QRect(0, 0, 850, 28))
self.menubar.setObjectName("menubar")
func.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(func)
self.statusbar.setObjectName("statusbar")
func.setStatusBar(self.statusbar)
self.graphicsView = pg.PlotWidget(self.centralwidget)
self.graphicsView.setObjectName(u"graphicsView")
self.graphicsView.setGeometry(QRect(400, 50, 431, 441))
self.retranslateUi(func)
QtCore.QMetaObject.connectSlotsByName(func)
self.alpha.setRange(1, 100)
self.w1.setRange(-100, 100)
self.w2.setRange(-100, 100)
self.alpha.valueChanged.connect(lambda: self.updateLabel())
self.w1.valueChanged.connect(lambda: self.updateLabel())
self.w2.valueChanged.connect(lambda: self.updateLabel())
self.back.clicked.connect(lambda: self.switchWindow())
self.submit.clicked.connect(lambda: self.plot())
def retranslateUi(self, func):
global w1, w2, alpha
_translate = QtCore.QCoreApplication.translate
func.setWindowTitle(_translate("func", "Gate Networks"))
self.label.setText(_translate("func", "Enter Variables"))
self.label_4.setText(_translate("func", f"W2: {w2}"))
self.label_2.setText(_translate("func", f"Alpha: {alpha}"))
self.label_3.setText(_translate("func", f"W1: {w1}"))
self.back.setText(_translate("func", "Back"))
self.submit.setText(_translate("func", "Submit"))
def updateLabel(self):
global w1, w2, alpha
w2 = self.w2.value() / 100
w1 = self.w1.value() / 100
alpha = self.alpha.value() / 100
_translate = QtCore.QCoreApplication.translate
self.label_4.setText(_translate("func", f"W2: {w2}"))
self.label_2.setText(_translate("func", f"Alpha: {alpha}"))
self.label_3.setText(_translate("func", f"W1: {w1}"))
def switchWindow(self):
Page.switch_back()
def plot(self):
global OPR, w1, w2, alpha
self.graphicsView.clear()
def AND(x, y):
return int(x and y)
def NAND(x, y):
return int(not (x and y))
def OR(x, y):
return int(x or y)
def NOR(x, y):
return int(not (x or y))
def XOR(x, y):
return int(x ^ y)
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
def XNOR(x, y):
return int(not (x ^ y))
counter = 0
b = 0
for i in inputs:
if eval(OPR)(i[0], i[1]):
self.graphicsView.plot([i[0]], [i[1]],
pen=None,
symbolBrush=(255, 255, 255),
symbol='o')
else:
self.graphicsView.plot([i[0]], [i[1]],
pen=None,
symbolBrush=(0, 0, 0),
symbol='o')
for i in range(50):
if i % 4 == 0:
counter = 0
x, y = inputs[i % 4]
f = 1 if (w1 * x + w2 * y) + b > 0 else 0
E = eval(OPR)(x, y) - f
if E != 0:
w1 = w1 + alpha * x * E
w2 = w2 + alpha * y * E
counter = 0
b = b + alpha * E
else:
counter += 1
if counter == 4:
break
p1 = self.graphicsView.plot([x for x in [-0.2, 1.2]],
[(-w1 * x - b) / w2
for x in [-0.2, 1.2]],
pen=(100 + 3 * i, 0, 0))
if counter == 4:
p1 = self.graphicsView.plot([x for x in [-0.2, 1.2]],
[(-w1 * x - b) / w2
for x in [-0.2, 1.2]],
pen=(0, 200, 0))
# print(f'{i} : {x}, {y} ,{w1}, {w2}, {f}, {E}, {counter}')
self.graphicsView.mapToScene(10, 10, 10, 10)
app = QtWidgets.QApplication(sys.argv)
Page = Windows()
Page.show_main()
sys.exit(app.exec_())