-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.py
172 lines (135 loc) · 6.45 KB
/
interface.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
from PyQt5 import QtWidgets
import sys
from functions import *
from methods import *
import numpy as np
import qdarkstyle
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Optimisation Algorithm Demonstration")
self.init_UI()
def init_UI(self):
self.setFixedSize(1000, 700)
self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
self.vbox_left = QtWidgets.QVBoxLayout()
self.vbox_right = QtWidgets.QVBoxLayout()
self.function_features = QtWidgets.QHBoxLayout()
self.optim_features = QtWidgets.QHBoxLayout()
self.t_label = QtWidgets.QLabel("Choose a function:")
self.m_label = QtWidgets.QLabel("Choose optimisation method:")
self.c_label = QtWidgets.QLabel("Optimisation process:")
self.tol_label = QtWidgets.QLabel("Set method tolerance:")
self.stp_label = QtWidgets.QLabel("Choose starting point (comma separated):")
self.changing_panel = QtWidgets.QVBoxLayout()
self.d_label = QtWidgets.QLabel("Choose step length (Default: 1):")
self.int_label = QtWidgets.QLabel("Choose helper function interval (Default: [-5,5]):")
self.d_field = QtWidgets.QLineEdit()
self.int_field = QtWidgets.QLineEdit()
self.changing_panel.addWidget(self.d_label)
self.changing_panel.addWidget(self.d_field)
self.changing_panel.addWidget(self.int_label)
self.changing_panel.addWidget(self.int_field)
self.start_btn = QtWidgets.QPushButton("START")
self.plot_btn = QtWidgets.QPushButton("PLOT")
self.info1 = QtWidgets.QTextEdit()
self.info1.setReadOnly(True)
self.tol = QtWidgets.QLineEdit()
self.stp = QtWidgets.QLineEdit()
self.info2 = QtWidgets.QTextEdit()
self.info2.setReadOnly(True)
self.result_console = QtWidgets.QTextEdit()
self.result_console.setReadOnly(True)
self.screen_layout = QtWidgets.QHBoxLayout()
self.fun_box = QtWidgets.QComboBox()
for name in get_function_names():
self.fun_box.addItem(name)
self.get_fun_info()
self.fun_box.currentIndexChanged.connect(self.get_fun_info)
self.met_box = QtWidgets.QComboBox()
for name in get_method_names():
self.met_box.addItem(name)
self.get_met_info()
self.met_box.currentIndexChanged.connect(self.get_met_info)
self.function_features.addWidget(self.fun_box)
self.function_features.addWidget(self.plot_btn)
self.optim_features.addWidget(self.met_box)
self.optim_features.addWidget(self.start_btn)
self.vbox_left.addWidget(self.t_label)
self.vbox_left.addLayout(self.function_features)
self.vbox_left.addWidget(self.info1)
self.vbox_left.addSpacing(20)
self.vbox_left.addWidget(self.m_label)
self.vbox_left.addLayout(self.optim_features)
self.vbox_left.addWidget(self.info2)
self.vbox_left.addWidget(self.tol_label)
self.vbox_left.addWidget(self.tol)
self.vbox_left.addWidget(self.stp_label)
self.vbox_left.addWidget(self.stp)
self.vbox_left.addLayout(self.changing_panel)
self.vbox_right.addWidget(self.c_label)
self.vbox_right.addWidget(self.result_console)
self.screen_layout.addLayout(self.vbox_left)
self.screen_layout.addLayout(self.vbox_right)
self.setLayout(self.screen_layout)
self.plot_btn.clicked.connect(self.plot_function)
self.start_btn.clicked.connect(self.start_optim_process)
self.show()
def get_fun_info(self):
self.info1.setText(function_info[self.fun_box.currentText()])
def get_met_info(self):
self.info2.setText(methods_info[self.met_box.currentText()])
if self.met_box.currentText() == "Hooke-Jeeves":
self.int_field.setReadOnly(True)
self.d_field.setReadOnly(False)
else:
self.d_field.setReadOnly(True)
self.int_field.setReadOnly(False)
def start_optim_process(self):
try:
tolerance = float(self.tol.text().strip())
start_p = self.stp.text().strip().split(",")
for num in range(len(start_p)):
start_p[num] = float(start_p[num])
arr = np.array(start_p)
except ValueError:
QtWidgets.QMessageBox.critical(self, "Error", "Invalid params. Try again.")
return
if self.fun_box.currentText() == "Three Hump Camel" and len(start_p) > 2:
QtWidgets.QMessageBox.critical(self, "Error", "Unlike other 2 functions, THC can only be optimised in 3D.")
return
self.result_console.setText("")
self.result_console.append(
"Starting optimisation process with {} algorithm:\n".format(self.met_box.currentText()))
if self.met_box.currentText() == "Powell":
if self.int_field.text().strip() == "":
gr_int = [-5, 5]
else:
gr_int = self.int_field.text().strip().split(",")
try:
for num in range(2):
gr_int[num] = float(gr_int[num])
except ValueError:
QtWidgets.QMessageBox.critical(self, "Error", "Invalid params. Try again.")
return
x = methods[self.met_box.currentText()](functions[self.fun_box.currentText()], golden_ratio, arr, tolerance,
True, self, gr_int[0], gr_int[1])
else:
if self.d_field.text().strip() == "":
step = 1
else:
try:
step = float(self.d_field.text().strip())
except ValueError:
QtWidgets.QMessageBox.critical(self, "Error", "Invalid params. Try again.")
return
x = methods[self.met_box.currentText()](functions[self.fun_box.currentText()], arr, step, tolerance, True,
self)
result = "Final solution achieved in {} iterations.\nX: {}\nY= {}".format(x[2], x[0], x[1])
self.result_console.append(result)
def plot_function(self):
plot_function_3D(functions[self.fun_box.currentText()], -2.048, 2.048, "viridis", self.fun_box.currentText())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())