def test_ExceptOnInvalidMaxIterParam(self):
        # Initialization
        class_obj = opt()
        lb = 0.0
        rb = 1.0
        r = 1.0
        method = class_obj.methods.Piyavsky
        max_iter = "1"
        class_obj.SetFunctionParameters(2, 3, 3, 5)
        # Action

        # Validation
        self.assertRaises(TypeError, class_obj.Minimize, lb, rb, r, method,
                          max_iter)
 def test_CorrectMinFuncValueBruteForse(self):
     # Initialization
     class_obj = opt()
     lb = 0.0
     rb = 8.0
     r = 2.0
     expected_func_value = -4.80579207
     method = class_obj.methods.BruteForse
     max_iter = 100
     class_obj.SetFunctionParameters(2, 3, 3, 5)
     # Action
     res = class_obj.Minimize(lb, rb, r, method, max_iter)
     # Validation
     self.assertAlmostEqual(res.minimum[1], expected_func_value)
 def test_CorrectMinFuncValueStrongin(self):
     # Initialization
     class_obj = opt()
     lb = 0.0
     rb = 8.0
     r = 2.0
     expected_func_value = -4.920616335
     method = class_obj.methods.Strongin
     max_iter = 800
     class_obj.SetFunctionParameters(2, 3, 3, 5)
     # Action
     res = class_obj.Minimize(lb, rb, r, method, max_iter=max_iter)
     # Validation
     self.assertAlmostEqual(res.minimum[1], expected_func_value)
Esempio n. 4
0
    def DisplaySolution(self):
        optimizer = opt()
        opt_params = params()

        opt_params.a = float(self.funcParamA.text())
        opt_params.b = float(self.funcParamB.text())
        opt_params.c = float(self.funcParamC.text())
        opt_params.d = float(self.funcParamD.text())
        opt_params.left_bound = float(self.funcParamLeftB.text())
        opt_params.right_bound = float(self.funcParamRightB.text())
        opt_params.max_iter = int(self.maxIterCount.text())
        opt_params.accuracy = float(self.accuracy.text())
        opt_params.r = float(self.r.text())

        if self.radioButtonBrutforce.isChecked():
            opt_params.method = optimizer.methods.BruteForse
        elif self.radioButtonPiyavsky.isChecked():
            opt_params.method = optimizer.methods.Piyavsky
        elif self.radioButtonStrongin.isChecked():
            opt_params.method = optimizer.methods.Strongin

        for param in dir(opt_params):
            if "__" not in param:
                print(param, "=", getattr(opt_params, param), type(getattr(opt_params, param)))

        optimizer.SetFunctionParameters(opt_params.a, opt_params.b, opt_params.c, opt_params.d)
        solution = optimizer.Minimize(opt_params.left_bound, opt_params.right_bound, opt_params.r, opt_params.method, max_iter=opt_params.max_iter, eps=opt_params.accuracy)

        points_multiplier = 100
        num_points = int(abs(opt_params.right_bound - opt_params.left_bound) * points_multiplier)
        step = abs(opt_params.right_bound - opt_params.left_bound)/num_points

        x = []
        for i in range(num_points):
            x.append(i * step + opt_params.left_bound)

        y = [optimizer.Func(point) for point in x]

        self.MplWidget.canvas.axes.plot(x, y)
        
        for point in solution.points:
            self.MplWidget.canvas.axes.plot(point[0], solution.minimum[1] - 0.1, color="green", marker="*")

        self.MplWidget.canvas.axes.plot(solution.minimum[0], solution.minimum[1], color="red", marker="o")
        self.solutionX.setText("x = " + str(round(solution.minimum[0], 7)))
        self.solutionY.setText("y = " + str(round(solution.minimum[1], 7)))
        self.solutionIterCount.setText("Количество итераций : " + str(solution.spent_iter))

        self.MplWidget.canvas.draw()
 def test_DoesNotRaiseOnNoneMaxIter(self):
     # Initialization
     class_obj = opt()
     lb = 0.0
     rb = 8.0
     r = 2.0
     method = class_obj.methods.Piyavsky
     max_iter = None
     raise_except = False
     class_obj.SetFunctionParameters(2, 3, 3, 5)
     # Action
     try:
         class_obj.Minimize(lb, rb, r, method, max_iter)
     except:
         raise_except = True
     # Validation
     self.assertEqual(False, raise_except)
Esempio n. 6
0
    def DisplaySolution(self):
        _translate = QtCore.QCoreApplication.translate
        self.labelErr.setText(_translate("MainWindow", ""))
        self.verticalLayout_3.addWidget(self.groupBox_3)
        self.SecondWindow = QtWidgets.QMainWindow()
        self.SecondWindow.setObjectName("SecondWindow")
        self.centralwidgetS = QtWidgets.QWidget(self.SecondWindow)
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidgetS)
        self.groupBox_2.setMaximumSize(QtCore.QSize(1280, 100))
        self.groupBox_2.setObjectName("groupBox_2")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.groupBox_2)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.solutionX = QtWidgets.QLabel(self.groupBox_2)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.solutionX.setFont(font)
        self.solutionX.setObjectName("solutionX")
        self.verticalLayout_2.addWidget(self.solutionX)
        self.solutionY = QtWidgets.QLabel(self.groupBox_2)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.solutionY.setFont(font)
        self.solutionY.setObjectName("solutionY")
        self.solutionIterCount = QtWidgets.QLabel(self.groupBox_2)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.solutionIterCount.setFont(font)
        self.solutionIterCount.setObjectName("solutionIterCount")
        self.finalFuncLabel = QtWidgets.QLabel(self.groupBox_2)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.finalFuncLabel.setFont(font)
        self.finalFuncLabel.setObjectName("finalFuncLabel")
        self.verticalLayout_secondary = QtWidgets.QVBoxLayout(self.centralwidgetS)
        self.verticalLayout_secondary.setObjectName("verticalLayout_secondary")
        self.MplWidget = MplWidget(self.centralwidget)
        self.MplWidget.setMinimumSize(QtCore.QSize(960, 300))
        self.MplWidget.setObjectName("MplWidget")
        self.groupBox_2.setTitle(_translate("SecondWindow", ""))
        self.solutionX.setText(_translate("SecondWindow", ""))
        self.solutionY.setText(_translate("SecondWindow", ""))
        self.solutionIterCount.setText(_translate("SecondWindow", ""))
        self.verticalLayout_secondary.addWidget(self.finalFuncLabel)
        self.verticalLayout_secondary.addWidget(self.MplWidget)
        self.verticalLayout_secondary.addWidget(self.solutionIterCount)
        self.verticalLayout_secondary.addWidget(self.solutionX)
        # self.verticalLayout_secondary.addWidget(self.solutionY)

        self.SecondWindow.setCentralWidget(self.centralwidgetS)
        optimizer = opt()
        opt_params = params()
        try:
            opt_params.a = float(self.funcParamA.text())
            opt_params.b = float(self.funcParamB.text())
            opt_params.c = float(self.funcParamC.text())
            opt_params.d = float(self.funcParamD.text())
            opt_params.left_bound = float(self.funcParamLeftB.text())
            opt_params.right_bound = float(self.funcParamRightB.text())
            opt_params.max_iter = int(self.maxIterCount.text())
            opt_params.r = float(self.r.text())

            if opt_params.left_bound >= opt_params.right_bound:
                raise ValueError()

            if self.MethodB.isChecked():
                opt_params.method = optimizer.methods.BruteForse
            elif self.MethodP.isChecked():
                opt_params.method = optimizer.methods.Piyavsky
            elif self.MethodS.isChecked():
                opt_params.method = optimizer.methods.Strongin

            for param in dir(opt_params):
                if "__" not in param:
                    print(param, "=", getattr(opt_params, param), type(getattr(opt_params, param)))

            optimizer.SetFunctionParameters(opt_params.a, opt_params.b, opt_params.c, opt_params.d)
            solution = optimizer.Minimize(opt_params.left_bound, opt_params.right_bound, opt_params.r,
                                          opt_params.method, max_iter=opt_params.max_iter)

            points_multiplier = 100
            num_points = int(abs(opt_params.right_bound - opt_params.left_bound) * points_multiplier)
            step = abs(opt_params.right_bound - opt_params.left_bound) / num_points

            x = []
            for i in range(num_points):
                x.append(i * step + opt_params.left_bound)

            y = [optimizer.Func(point) for point in x]

            self.MplWidget.canvas.axes.plot(x, y, color="black")

            for point in solution.points:
                self.MplWidget.canvas.axes.plot(point[0], solution.minimum[1] - 0.1, color="black", marker="*")

            self.MplWidget.canvas.axes.plot(solution.minimum[0], solution.minimum[1], color="red", marker="o")
            self.solutionX.setText(
                "(" + str(round(solution.minimum[0], 7)) + ", " + str(round(solution.minimum[1], 7)) + ")")
            self.solutionIterCount.setText("Пройдено " + str(solution.spent_iter) + " итераций")
            self.finalFuncLabel.setText("f(x) =  " + str(opt_params.a) +" * sin( " + str(opt_params.b) +" * x) + " + str(opt_params.c) +"  * cos( " + str(opt_params.d) +" * x)")
            self.SecondWindow.show()
            self.MplWidget.canvas.draw()
        except ValueError:
            self.labelErr.setText(_translate("MainWindow", "Неправильные параметры ввода"))
        except Exception:
            self.labelErr.setText(_translate("MainWindow", "Неожиданная ошибка ("))