def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.selectionMethod = None self.problemController = None self.gp = Gnuplot.Gnuplot() self.setupConnections() self.realtimePlot = False self.plotDataBest = None self.plotDataAvg = None self.plotDataWorst = None
class MainWindow(QtGui.QWidget): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.selectionMethod = None self.problemController = None self.gp = Gnuplot.Gnuplot() self.setupConnections() self.realtimePlot = False self.plotDataBest = None self.plotDataAvg = None self.plotDataWorst = None def setupConnections(self): QtCore.QObject.connect( self.ui.selectionCombo, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(const QString)")), self.applySelectionMethod, ) QtCore.QObject.connect( self.ui.selectionOptionsButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.applySelectionMethod ) QtCore.QObject.connect( self.ui.problemCombo, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(const QString)")), self.applyProblem ) # Konsola QtCore.QObject.connect(self.ui.consoleBox, QtCore.SIGNAL(_fromUtf8("textChanged()")), self.autoScrollConsole) # Przyciski Rozpocznij i zatrzymaj QtCore.QObject.connect(self.ui.startButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.startClicked) QtCore.QObject.connect(self.ui.stopButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.stopClicked) # Wykres QtCore.QObject.connect( self.ui.realtimePlotCheckbox, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.toggleRealtimePlot ) QtCore.QObject.connect(self.ui.plotButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.plotPopulation) def setProblemConnections(self): QtCore.QObject.connect( self.ui.crossoverChanceSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setCrossoverChance, ) QtCore.QObject.connect( self.ui.mutationChanceSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setMutationChance, ) QtCore.QObject.connect( self.ui.populationSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setPopulationSize, ) QtCore.QObject.connect( self.ui.maxGenerationSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setGenerationStop, ) QtCore.QObject.connect( self.ui.geneMutation, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.problemController.setGeneMutation ) QtCore.QObject.connect( self.ui.bestToNewGeneration, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.problemController.setBestToNewGeneration, ) # Podlaczmy sygnaly dla komunikacji watek -> gui QtCore.QObject.connect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("consoleWrite(PyQt_PyObject)")), self.updateConsole, ) QtCore.QObject.connect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("plotData(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)")), self.updatePlotData, ) QtCore.QObject.connect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("problemEnd()")), self.stopClicked ) def destroyProblemConnections(self): QtCore.QObject.disconnect( self.ui.crossoverChanceSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setCrossoverChance, ) QtCore.QObject.disconnect( self.ui.mutationChanceSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setMutationChance, ) QtCore.QObject.disconnect( self.ui.populationSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setPopulationSize, ) QtCore.QObject.disconnect( self.ui.maxGenerationSpin, QtCore.SIGNAL(_fromUtf8("valueChanged(int)")), self.problemController.setGenerationStop, ) QtCore.QObject.disconnect( self.ui.geneMutation, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.problemController.setGeneMutation ) QtCore.QObject.disconnect( self.ui.bestToNewGeneration, QtCore.SIGNAL(_fromUtf8("toggled(bool)")), self.problemController.setBestToNewGeneration, ) # Komunikacja watek -> gui QtCore.QObject.disconnect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("consoleWrite(PyQt_PyObject)")), self.updateConsole, ) QtCore.QObject.disconnect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("plotData(PyQt_PyObject,PyQt_PyObject,PyQt_PyObject)")), self.updatePlotData, ) QtCore.QObject.disconnect( self.problemController.problemThread, QtCore.SIGNAL(_fromUtf8("problemEnd()")), self.stopClicked ) def passStartValues(self): self.problemController.setCrossoverChance(self.ui.crossoverChanceSpin.value()) self.problemController.setMutationChance(self.ui.mutationChanceSpin.value()) self.problemController.setPopulationSize(self.ui.populationSpin.value()) self.problemController.setGenerationStop(self.ui.maxGenerationSpin.value()) self.problemController.setGeneMutation(self.ui.geneMutation.isChecked()) self.problemController.setBestToNewGeneration(self.ui.bestToNewGeneration.isChecked()) def startClicked(self): if not self.ui.plotButton.isEnabled(): self.ui.plotButton.setEnabled(True) # Czyscimy dane do wykresu self.plotDataBest = [] self.plotDataAvg = [] self.plotDataWorst = [] self.gp = Gnuplot.Gnuplot() self.ui.startButton.setEnabled(False) self.ui.stopButton.setEnabled(True) self.ui.consoleBox.clear() # Inicjujemy watek i przekazujemy wartosci poczatkowe do problemu self.problemController.initSolvingThread() self.passStartValues() # Zadbajmy o polaczenia self.setProblemConnections() # Uruchamiamy watek do rozwiazywania problemu self.problemController.startSolvingThread() def stopClicked(self): self.ui.stopButton.setEnabled(False) self.ui.startButton.setEnabled(True) self.problemController.stopSolvingThread() # Zerwijmy polaczenia self.destroyProblemConnections() def applyProblem(self): if self.problemController is not None: QtCore.QObject.disconnect( self.ui.problemOptionsButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.problemController.showOptionsDialog, ) p = self.ui.problemCombo.currentIndex() # 0 - Ekstrama lokalne if p == 0: self.problemController = localExtremumController.LocalExtremumController() # 1 - problem plecakowy if p == 1: self.problemController = backpackController.backpackController() # 2 - miejsca zerowe if p == 2: self.problemController = zeroController.ZeroController() if self.selectionMethod is not None: self.problemController.selection = self.selectionMethod self.problemController.showOptionsDialog() # Odblokujmy panel z opcjami i przycisk opcji problemu self.ui.optionsBox.setEnabled(True) self.ui.problemOptionsButton.setEnabled(True) self.ui.selectionOptionsButton.setEnabled(False) QtCore.QObject.connect( self.ui.problemOptionsButton, QtCore.SIGNAL(_fromUtf8("clicked(bool)")), self.problemController.showOptionsDialog, ) def applySelectionMethod(self): text = self.ui.selectionCombo.currentText() if text == "Turniej": self.ui.selectionOptionsButton.setEnabled(True) [value, ok] = QtGui.QInputDialog.getInt( self, "Rozmiar grupy", "Podaj rozmiar grupy turniejowej", 4, 2, 50000, 1 ) if ok: self.selectionMethod = competition.CompetitionSelection(value) else: self.selectionMethod = competition.CompetitionSelection(4) if text == "Ruletka": self.ui.selectionOptionsButton.setEnabled(False) self.selectionMethod = roulette.RouletteSelection() self.problemController.selection = self.selectionMethod # Odblokuj przycisk Rozpocznij self.ui.startButton.setEnabled(True) def updateConsole(self, data): d = str(data) self.ui.consoleBox.appendPlainText(d) def autoScrollConsole(self): c = self.ui.consoleBox.textCursor() c.movePosition(QtGui.QTextCursor.End) self.ui.consoleBox.setTextCursor(c) def updatePlotData(self, best, avg, worst): self.plotDataBest.append(best) self.plotDataAvg.append(avg) self.plotDataWorst.append(worst) if self.realtimePlot: self.plotPopulation() def toggleRealtimePlot(self, value): self.realtimePlot = value def plotPopulation(self): self.gp.title(self.ui.problemCombo.currentText()) plotBest = Gnuplot.PlotItems.Data(self.plotDataBest, with_="lines", title="Najlepszy organizm") plotAvg = Gnuplot.PlotItems.Data(self.plotDataAvg, with_="lines", title="Srednia z populacji") plotWorst = Gnuplot.PlotItems.Data(self.plotDataWorst, with_="lines", title="Najgorszy organizm") self.gp.plot(plotBest, plotAvg, plotWorst)