This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
224 lines (171 loc) · 9.25 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
import sys
from fractions import Fraction
from PySide6 import QtWidgets
from PySide6.QtWidgets import QApplication, QMainWindow, QFileDialog
from models.matrix_model import MatrixModel
from models.solve_model import SolveModel
from res.ui.py.answer_dialog import Ui_AnswerDialog
from res.ui.py.main_screen import Ui_MainWindow
from res.ui.py.info_dialog import Ui_InfoDialog
from res.ui.py.new_matrix_dialog import Ui_TaskInputDialog
from res.ui.py.instruction_dialog import Ui_InstructionDialog
from res.ui.py.solution_dialog import Ui_SolvingDialog
from symplex_logic import SymplexLogic
from task_config import TaskConfig, TypeOfFractions, SolutionType, ExtremumType
class LinearApp(QMainWindow):
def __init__(self):
super(LinearApp, self).__init__()
self.task_config = TaskConfig()
self.symplex_logic = SymplexLogic()
self.matrix_model, self.new_window, self.ui_window = None, None, None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Setup QSpinBox value
self.ui.input_count_limits.setValue(self.task_config.limits_count)
self.ui.input_count_variables.setValue(self.task_config.var_count)
# Меню бар
self.ui.action_info.triggered.connect(self, self.open_info_dialog)
self.ui.action_instruction.triggered.connect(self, self.open_instruction_dialog)
self.ui.action_load_from_file.triggered.connect(self, self.load_from_file)
# Кнопка далее
self.ui.btn_next.clicked.connect(self.open_input_task_matrix)
# Радио тип задачи
self.ui.radio_tot_min.setText(ExtremumType.MIN.value)
self.ui.radio_tot_max.setText(ExtremumType.MAX.value)
self.ui.radio_tot_min.toggled.connect(self.on_tot_radio_toggled)
# Радио тип дробей
self.ui.radio_tof_default.toggled.connect(self.on_tof_radio_toggled)
# Радио тип решения
self.ui.radio_st_auto.toggled.connect(self.on_st_radio_toggled)
def on_tot_radio_toggled(self):
if self.ui.radio_tot_min.isChecked():
self.task_config.set_extremum_type(ExtremumType.MIN)
elif self.ui.radio_tot_max.isChecked():
self.task_config.set_extremum_type(ExtremumType.MAX)
print(f"Тип задачи: {self.task_config.extremum_type.value}")
def on_tof_radio_toggled(self):
if self.ui.radio_tof_default.isChecked():
self.task_config.set_type_of_fractions(TypeOfFractions.COMMON)
elif self.ui.radio_tof_decimal.isChecked():
self.task_config.set_type_of_fractions(TypeOfFractions.DECIMAL)
print(f"Тип дробей: {self.task_config.type_of_fractions.value}")
def on_st_radio_toggled(self):
if self.ui.radio_st_auto.isChecked():
self.task_config.set_solution_type(SolutionType.AUTO)
else:
self.task_config.set_solution_type(SolutionType.MANUAL)
print(f"Тип решения: {self.task_config.solution_type.value}")
def open_input_task_matrix(self):
self.new_window = QtWidgets.QDialog()
self.ui_window = Ui_TaskInputDialog()
self.ui_window.setupUi(self.new_window)
self.new_window.show()
self.ui_window.btn_cancel.clicked.connect(self.new_window.close)
self.ui_window.btn_start_solving.clicked.connect(self.open_solve_dialog)
# Update TaksConfig
text_field_var_count = int(self.ui.input_count_variables.text())
text_field_limits_count = int(self.ui.input_count_limits.text())
if self.task_config.var_count != text_field_var_count or self.task_config.limits_count != text_field_limits_count:
self.task_config.set_var_count(int(self.ui.input_count_variables.text()))
self.task_config.set_limits_count(int(self.ui.input_count_limits.text()))
self.task_config.update_matrix_length()
# Create Matrix Model
self.matrix_model = MatrixModel(var_count=self.task_config.var_count, limits_count=self.task_config.limits_count, task_config=self.task_config)
self.ui_window.matrix_task_input.setModel(self.matrix_model)
def open_solve_dialog(self):
if self.task_config.solution_type == SolutionType.AUTO:
self.symplex_logic.solution_type = self.task_config.solution_type
self.symplex_logic.solve_task(task_matrix=self.task_config.input_matrix,
solution_type=self.task_config.solution_type,
extremum_type=self.task_config.extremum_type)
self.new_window.close()
self._open_result_dialog()
elif self.task_config.solution_type == SolutionType.MANUAL:
self.symplex_logic.solution_type = self.task_config.solution_type
self.new_window.close()
self.new_window = QtWidgets.QDialog()
self.ui_window = Ui_SolvingDialog()
self.ui_window.setupUi(self.new_window)
self.new_window.show()
self.solve_model = SolveModel(task_config=self.task_config, symplex_logic=self.symplex_logic)
self.ui_window.task_matrix.setModel(self.solve_model)
self.ui_window.task_matrix.clicked.connect(self._support_element_click)
self.ui_window.btn_step_back.clicked.connect(self._solve_dialog_back_step)
def _support_element_click(self, index):
selected_support_element = (index.row(), index.column())
if index.isValid():
if self.solve_model.check_table_element(index):
print(f"Выюран: {selected_support_element}")
self.solve_model.next_step(selected_support_element)
if self.solve_model.solving_is_ended:
self.ui_window.btn_step_back.setDisabled(True)
self._open_result_dialog()
def _solve_dialog_back_step(self):
if len(self.solve_model.steps) > 1:
self.solve_model.back_step()
else:
self.new_window.close()
def _open_result_dialog(self):
self.result_dialog = QtWidgets.QDialog()
self.ui_result_dialog = Ui_AnswerDialog()
self.ui_result_dialog.setupUi(self.result_dialog)
self.result_dialog.show()
self.ui_result_dialog.btn_answer_ok.clicked.connect(self.new_window.close)
self.ui_result_dialog.lbl_task_status.setText(self.symplex_logic.result.comment)
basis_output = "x* ( "
for el in self.symplex_logic.result.basis_result:
if self.task_config.type_of_fractions == TypeOfFractions.COMMON:
basis_output += str(el) + " "
else:
basis_output += str(round(float(el), 3)) + " "
basis_output += ")"
function_output = "f* = "
if self.task_config.type_of_fractions == TypeOfFractions.COMMON:
function_output += str(self.symplex_logic.result.function_result)
else:
function_output += str(round(float(self.symplex_logic.result.function_result), 3))
result_output = f"{basis_output}\n{function_output}"
self.ui_result_dialog.lbl_answer.setText(result_output)
self.ui_result_dialog.btn_answer_ok.clicked.connect(self._result_dialog_ok_click)
def _result_dialog_ok_click(self):
self.result_dialog.close()
self.new_window.close()
def open_info_dialog(self):
self.new_window = QtWidgets.QDialog()
self.ui_window = Ui_InfoDialog()
self.ui_window.setupUi(self.new_window)
self.new_window.show()
self.ui_window.btn_info_close.clicked.connect(self.new_window.close)
def open_instruction_dialog(self):
self.new_window = QtWidgets.QDialog()
self.ui_window = Ui_InstructionDialog()
self.ui_window.setupUi(self.new_window)
self.new_window.show()
self.ui_window.btn_instruction_close.clicked.connect(self.new_window.close)
def load_from_file(self):
file_path, _ = QFileDialog.getOpenFileName(self, "Выберите файл задачи", "res/tasks", "Text Files (*.txt)")
if file_path:
try:
print(f"Selected file: {file_path}")
file = open(file_path, "r")
file_data = file.readlines()
file.close()
new_task_matrix = []
for row_index in range(len(file_data) - 1):
new_row = [Fraction(i) for i in file_data[row_index].split(',')]
new_task_matrix.append(new_row)
self.task_config.set_type_of_fractions(TypeOfFractions.COMMON)
self.task_config.set_var_count(len(new_task_matrix[0]))
self.task_config.set_limits_count(len(new_task_matrix) - 2)
self.ui.input_count_variables.setValue(self.task_config.var_count)
self.ui.input_count_limits.setValue(self.task_config.limits_count)
self.task_config.set_extremum_type(str(file_data[-1]).upper())
self.task_config.input_matrix = new_task_matrix
print('Данные из файла успешно прочитаны!')
except:
print('Ошибка чтения файла!')
if __name__ == "__main__":
app = QApplication(sys.argv)
window = LinearApp()
window.show()
sys.exit(app.exec())