def startEdit(self, tabIndex: int) -> None: self.editingTabIndex = tabIndex rect: QRect = self.tabRect(tabIndex) topMargin = 3 leftMargin = 6 lineEdit = QLineEdit(self) lineEdit.setAlignment(Qt.AlignCenter) lineEdit.move(rect.left() + leftMargin, rect.top() + topMargin) lineEdit.resize(rect.width() - 2 * leftMargin, rect.height() - 2 * topMargin) lineEdit.setText(self.tabText(tabIndex)) lineEdit.selectAll() lineEdit.setFocus() lineEdit.show() lineEdit.editingFinished.connect(self.finishEdit) self.lineEdit = lineEdit
class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() self.timer.start(10, self) def initUI(self): ml = QVBoxLayout(self) ul = QHBoxLayout(self) dl = QHBoxLayout(self) ml.addLayout(ul) ml.addLayout(dl) col = QColor(0, 0, 0) self.colorText = 'Color Nmae: {}' self.btn = QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.btn2 = QPushButton('Color', self) self.btn2.move(20, 40) self.btn2.clicked.connect(self.showColorDialoge) self.frm = QFrame(self) self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) self.frm.setGeometry(130, 50, 100, 100) self.le = QLineEdit(self) self.le.move(130, 22) self.label = QLabel(self.colorText.format(None), self) self.label.setGeometry(20, 80, 100, 30) self.timer = QBasicTimer() self.pb = QProgressBar(self) self.step = 0 self.pb.setGeometry(20, 170, 300, 20) # calendar self.timeLable = QLabel('time is: ', self) # self.timeLable.setGeometry(20, 120, 100, 30) cal = QCalendarWidget(self) cal.clicked[QDate].connect(self.showDate) ul.addWidget(self.timeLable) dl.addWidget(cal) self.setLayout(ml) self.setGeometry(300, 300, 550, 550) self.setWindowTitle('Dialog') self.show() def showDialog(self): # 通过下面的语句来实现QInputDialog的显示 text, ok = QInputDialog.getText(self, 'Input Dialog', 'Input some thing') if ok: self.le.setText(text) def showColorDialoge(self): col = QColorDialog.getColor() # 获取colorDialog的颜色 if col.isValid(): self.label.setText(self.colorText.format(col.name())) self.frm.setStyleSheet("QWidget { background-color: %s}" % col.name()) def timerEvent(self, e): if self.step >= 100: self.timer.stop() else: self.step += 1 self.pb.setValue(self.step) def showDate(self, date): d = date.toString(Qt.ISODate) self.timeLable.setText(d)
class UI(QWidget): def __init__(self): self.__problem = Problem() self.__controller = Controller(self.__problem) super().__init__() #self.validation() self.initUI() def initUI(self): self.l0 = QLabel(self) self.l0.setText("1.EA;2.HC;3.PSO") self.l0.move(0, 210) self.le0 = QLineEdit(self) self.le0.move(150, 210) self.l1 = QLabel(self) self.l1.setText("Probability of mutation") self.l1.move(0, 0) self.le1 = QLineEdit(self) self.le1.move(150, 0) # self.l2 = QLabel(self) self.l2.setText("Population size") self.l2.move(0, 30) self.le2 = QLineEdit(self) self.le2.move(150, 30) # self.l3 = QLabel(self) self.l3.setText("Number of iterations") self.l3.move(0, 60) self.le3 = QLineEdit(self) self.le3.move(150, 60) # self.l4 = QLabel(self) self.l4.setText("Number of neighbours") self.l4.move(0, 90) self.le4 = QLineEdit(self) self.le4.move(150, 90) # self.l5 = QLabel(self) self.l5.setText("w") self.l5.move(0, 120) self.le5 = QLineEdit(self) self.le5.move(150, 120) # self.l6 = QLabel(self) self.l6.setText("c1") self.l6.move(0, 150) self.le6 = QLineEdit(self) self.le6.move(150, 150) # self.l7 = QLabel(self) self.l7.setText("c2") self.l7.move(0, 180) self.le7 = QLineEdit(self) self.le7.move(150, 180) self.qtable = QTableWidget(self) self.qtable.move(200, 300) self.qtable.setGeometry(200, 300, 600, 600) self.setWindowTitle('Input dialog') self.btn = QPushButton('Take arguments', self) self.btn.move(200, 250) self.btn.clicked.connect(self.showDialog) self.show() def showDialog(self): if isinstance(self.le1.text(), str) and self.le1.text() != '': pM = float(self.le1.text()) else: pM = 0 if isinstance(self.le2.text(), str) and self.le2.text() != '': dimPopulation = int(self.le2.text()) else: dimPopulation = 20 if isinstance(self.le3.text(), str) and self.le3.text() != '': noIteratii = int(self.le3.text()) else: noIteratii = 1000 if isinstance(self.le4.text(), str) and self.le4.text() != '': sizeOfNeighborhood = int(self.le4.text()) else: sizeOfNeighborhood = 2 if isinstance(self.le5.text(), str) and self.le5.text() != '': w = float(self.le5.text()) else: w = 1 if isinstance(self.le6.text(), str) and self.le6.text() != '': c1 = float(self.le6.text()) else: c1 = 1 if isinstance(self.le7.text(), str) and self.le7.text() != '': c2 = float(self.le7.text()) else: c2 = 2.5 if isinstance(self.le0.text(), str) and self.le0.text() != '': a = self.le0.text() else: a = 0 self.run(a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2) def printMatrix(self, array): self.qtable.setColumnCount(len(array[0])) # rows and columns of table self.qtable.setRowCount(len(array[0])) for row in range(len( array[0])): # add items from array to QTableWidget for column in range(len(array[0])): item = (array[0][row][column], array[1][row][column] ) # each item is a QTableWidgetItem self.qtable.setItem(row, column, QTableWidgetItem(str(item))) self.qtable.show() def case1(self, a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2): dimIndividual = 4 P = self.__problem.population(dimPopulation, dimIndividual, 0, 0) for i in range(noIteratii): P = self.__controller.iteration(P, pM, 0, 0) # print the best individual graded = [(self.__problem.fitness(x), x) for x in P] graded = sorted(graded) result = graded[0] fitnessOptim = result[0] individualOptim = result[1] self.printMatrix(individualOptim) def case2(self, a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2): dimIndividual = 4 ind = self.__problem.individual(dimIndividual, 0, 0) res = self.__controller.hillClimb(ind) self.printMatrix(res) def case3(self, a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2): noParticles = dimPopulation # individual size dimParticle = 4 # the boundries of the search interval vmin = -100 vmax = -10 # specific parameters for PSO w = 1.0 c1 = 1. c2 = 2.5 # sizeOfNeighborhood = 2 P = self.__problem.populationForParticles(noParticles, dimParticle, vmin, vmax) # we establish the particles' neighbors neighborhoods = self.__problem.selectNeighbors(P, sizeOfNeighborhood) for i in range(noIteratii): P = self.__controller.iterationForParticles( P, neighborhoods, c1, c2, w / (i + 1)) # print the best individual best = 0 for i in range(1, len(P)): if (P[i].fitness < P[best].fitness): best = i fitnessOptim = P[best].fitness individualOptim = P[best].pozition self.printMatrix(individualOptim) def run(self, a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2): #ui dimIndividual = 4 vmin = 0 vmax = 0 if a == "1": self.case1(a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2) if a == "2": self.case2(a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2) if a == "3": self.case3(a, pM, dimPopulation, noIteratii, sizeOfNeighborhood, w, c1, c2) def validation(self): fitnessOptimForEA = [] for i in range(30): dimIndividual = 4 dimPopulation = 40 noIteratii = 1000 pM = 0.01 P = self.__problem.population(dimPopulation, dimIndividual, 0, 0) for i in range(noIteratii): P = self.__controller.iteration(P, pM, 0, 0) graded = [(self.__problem.fitness(x), x) for x in P] graded = sorted(graded) result = graded[0] fitnessOptimForEA.append(result[0]) fitnessOptimForHC = [] for i in range(30): dimIndividual = 4 ind = self.__problem.individual(dimIndividual, 0, 0) res = self.__controller.hillClimb(ind) fitnessOptimForHC.append(self.__problem.fitness(res)) fitnessOptimForPSO = [] for i in range(3): noIteratii = 1000 noParticles = 40 dimParticle = 4 # the boundries of the search interval vmin = -100 vmax = -10 # specific parameters for PSO w = 1.0 c1 = 1. c2 = 2.5 sizeOfNeighborhood = 2 P = self.__problem.populationForParticles(noParticles, dimParticle, vmin, vmax) neighborhoods = self.__problem.selectNeighbors( P, sizeOfNeighborhood) for i in range(noIteratii): P = self.__controller.iterationForParticles( P, neighborhoods, c1, c2, w / (i + 1)) best = 0 for i in range(1, len(P)): if (P[i].fitness < P[best].fitness): best = i fitnessOptim = P[best].fitness fitnessOptimForPSO.append(fitnessOptim) plt.plot(fitnessOptimForEA) # plotting by columns np1 = np.array(fitnessOptimForEA) std1 = np.std(np1) m1 = np.mean(np1) plt.xlabel("standard deviation= " + str(std1) + ";mean= " + str(m1) + " Trials") plt.ylabel("EA algorithm" + " Fitness") plt.show() plt.plot(fitnessOptimForHC) # plotting by columns np2 = np.array(fitnessOptimForHC) std2 = np.std(np2) m2 = np.mean(np2) plt.xlabel("standard deviation= " + str(std2) + ";mean= " + str(m2) + " Trials") plt.ylabel("HC algorithm" + " Fitness") plt.show() plt.plot(fitnessOptimForPSO) # plotting by columns np3 = np.array(fitnessOptimForPSO) std3 = np.std(np3) m3 = np.mean(np3) plt.xlabel("standard deviation= " + str(std3) + ";mean= " + str(m3) + " Trials") plt.ylabel("PSO algorithm" + " Fitness") plt.show()
class ZusatzFensterKerndaten(QWidget): def __init__(self, nummer, text): super().__init__() self.initMe(nummer, text) def initMe(self, nummer, text): self.l1 = QLabel(self) self.l1.setText('Inhalt der eingelesenen Zelle') self.l1.move(20, 5) self.nummer = nummer self.setGeometry(400, 300, 500, 700) self.zelle = QPlainTextEdit(self) self.zelle.setGeometry(0, 40, 500, 250) self.zelle.setPlainText(text) self.zelle.setReadOnly(True) self.l2 = QLabel(self) self.l2.setText( """Bitte geben Sie hier den Wert ein nach dem in der Zelle gesucht werden soll. Bsp. Wollen Sie einen Lastpunkt auslesen, welcher mit 5000 rpm angegeben ist, geben Sie 5000 ein. Achtung: keine Einheiten mit angeben. Nur Zahlen!""") self.l2.move(10, 330) self.eing = QLineEdit(self) self.eing.move(10, 410) p = QPushButton('Prüfen', self) p.clicked.connect(self.pruefen) p.move(180, 409) self.l3 = QLabel(self) self.l3.setText('vorangehende Zeichenkette') self.l3.move(10, 460) self.suchstring = QLineEdit(self) self.suchstring.move(180, 459) self.suchstring.setDisabled(True) self.l5 = QLabel(self) self.l5.setStyleSheet("background-color: yellow") self.l5.setText( "Prüfen Sie die vorrangehende Zeichenkette.\nSollte diese nicht stimmen, können Sie selbst eine angeben und erneut prüfen.\nAchtung: Leerzeichen nicht vergessen " ) self.l5.move(10, 490) self.l5.setVisible(False) self.l4 = QLabel(self) self.l4.setText('gefundener Eintrag') self.l4.move(10, 540) self.gefundener_string = QLineEdit(self) self.gefundener_string.move(180, 539) self.gefundener_string.setReadOnly(True) frage = QPushButton(self) frage.setIcon(QIcon("bilder_vorlagenersteller\\FrageIcon.png")) frage.move(450, 10) frage.clicked.connect(self.Hilfe) self.weiter = QPushButton('Weiter', self) self.weiter.move(420, 650) self.weiter.setDisabled(True) self.weiter.clicked.connect(self.weiter_gehts) def suchstring_finden(self): startindex = self.zelle.toPlainText().find(self.eing.text()) if startindex == 0: suchstring = '##Anfang###' elif startindex == -1: suchstring = 'ungültige Eingabe' else: suchstring = '' for i in range(0, 11): suchstring = self.zelle.toPlainText()[startindex - i] + suchstring if (startindex - i) == 0: break return suchstring[:-1] def pruefen(self): suchstring = self.suchstring.text() if suchstring == '': suchstring = self.suchstring_finden() print(suchstring) self.suchstring.setDisabled(False) self.l5.setVisible(True) self.weiter.setDisabled(False) self.suchstring.setText(suchstring) startindex = self.zelle.toPlainText().find(suchstring) + len( suchstring) ende = startindex + len(self.eing.text()) self.gefundener_string.setText( self.zelle.toPlainText()[startindex:ende]) def weiter_gehts(self): w.findChild(QLabel, self.nummer).setVisible(True) w.findChild(QLineEdit, 'suchstr' + self.nummer).setVisible(True) w.findChild(QLineEdit, 'suchstr' + self.nummer).setText(self.suchstring.text()) self.close() def Hilfe(self): self.h = HilfeFenster( "bilder_vorlagenersteller\\erweitertes_einlesen.png") self.h.show()