def __init__(self, dat, parent=None): ''' ''' super(surrogateFrame, self).__init__(parent=parent) self.mainWin = parent self.setupUi(self) self.dat = dat self.blockapply = False self.tools = sorted(list(dat.surrogateMethods.plugins.keys()), key=lambda s: s.lower()) self.toolSelectBox.clear() self.toolSelectBox.addItems(self.tools) self.createOptionsTables() self.toolSelectBox.currentIndexChanged.connect(self.selectTool) self.toolBox.setCurrentIndex(1) self.dataBrowser = dataBrowserFrame(dat, self.toolBox.currentWidget()) self.dataBrowser.editFiltersButton.clicked.connect(self.updateFilters) self.toolBox.currentWidget().layout().addWidget(self.dataBrowser) self.runButton.clicked.connect(self.run) self.inputCols = gh.colIndexes(self.inputTable) self.outputCols = gh.colIndexes(self.outputTable) self.refreshContents() self.timer = QtCore.QTimer(self) self.timer.timeout.connect(self.updateStatus) self.updateDelay = 500 self.runButton.setEnabled(True) self.stopButton.setEnabled(False) self.stopButton.clicked.connect(self.stop) self.addAllInButton.clicked.connect(self.selectAllInputs) self.addAllOutButton.clicked.connect(self.selectAllOutputs) self.delAllInButton.clicked.connect(self.selectNoInputs) self.delAllOutButton.clicked.connect(self.selectNoOutputs) self.ivGeneralButton1.clicked.connect(self.ivGeneralButton1Click) self.ivGeneralButton2.clicked.connect(self.ivGeneralButton2Click) self.ovGeneralButton1.clicked.connect(self.ovGeneralButton1Click) self.ovGeneralButton2.clicked.connect(self.ovGeneralButton2Click) self.addSamplesButton.clicked.connect(self.addSamples) self.prevTool = None try: self.selectTool(0) except: pass self.toolBox.setCurrentIndex(0)
def updateSampleVarsTable(self): prob = self.dat.optProblem table = self.sampleTable table.setColumnCount(0) vs = gh.addColumns(table, prob.vs, s=True) table.setRowCount(prob.numSamples()) ci = gh.colIndexes(table) for name in ci: for row in range(prob.numSamples()): if name in prob.samp: gh.setCellJSON(table, row, ci[name], prob.samp[name][row]) else: gh.setCellJSON(table, row, ci[name], float('nan')) table.resizeColumnsToContents()
def refreshContents(self): ''' Update the contents of the surrogate for to reflect the current state of the foqus session. ''' self.ivGeneralButton1.hide() self.ivGeneralButton2.hide() self.ovGeneralButton1.hide() self.ovGeneralButton2.hide() self.dataBrowser.refreshContents() inputNames = self.dat.flowsheet.input.compoundNames() self.dat.flowsheet.markConnectedInputs() delList = [] for i in range(len(inputNames)): if self.dat.flowsheet.input.get(inputNames[i]).con == True: delList.append(inputNames[i]) for n in delList: inputNames.remove(n) outputNames = self.dat.flowsheet.output.compoundNames() if self.dat.surrogateCurrent is not None: i = self.toolSelectBox.findText(self.dat.surrogateCurrent) if i >= 0: self.blockapply = True self.toolSelectBox.setCurrentIndex(i) self.blockapply = False tool = self.toolSelectBox.currentText() if tool == '': return pg = self.dat.surrogateMethods.plugins[tool].\ surrogateMethod(self.dat) pg.loadFromSession() pg.updateOptions() for i, btn in enumerate(pg.inputVarButtons): # this is because I am beeing lazy there are two # preexisting buttons if i == 0: self.ivGeneralButton1.setText(btn[0]) self.ivGeneralButton1.show() elif i == 1: self.ivGeneralButton2.setText(btn[0]) self.ivGeneralButton2.show() for i, btn in enumerate(pg.outputVarButtons): if i == 0: self.ovGeneralButton1.setText(btn[0]) self.ovGeneralButton1.show() elif i == 1: self.ovGeneralButton2.setText(btn[0]) self.ovGeneralButton2.show() for i in range(self.optTable[tool].rowCount()): oname = gh.getCellText(self.optTable[tool], i, 0) if oname in pg.options: if pg.options[oname].dtype == bool: gh.setCellChecked(self.optTable[tool], i, 1, pg.options[oname].value) elif len(pg.options[oname].validValues) > 0: gh.cellPulldownSetItemsJSON(self.optTable[tool], i, 1, pg.options[oname].validValues) gh.cellPulldownSetJSON(self.optTable[tool], i, 1, pg.options[oname].value) else: gh.setCellJSON(self.optTable[tool], i, 1, pg.options[oname].value) self.inputTable.setRowCount(0) self.inputTable.setColumnCount(3 + len(pg.inputCols)) for i, item in enumerate(pg.inputCols): gh.setColHeaderIntem(self.inputTable, i + 3, item[0]) self.outputTable.setRowCount(0) self.outputTable.setColumnCount(1 + len(pg.outputCols)) for i, item in enumerate(pg.outputCols): gh.setColHeaderIntem(self.outputTable, i + 1, item[0]) self.inputTable.setRowCount(len(inputNames)) self.outputTable.setRowCount(len(outputNames)) self.inputCols = gh.colIndexes(self.inputTable) self.outputCols = gh.colIndexes(self.outputTable) for i in range(len(inputNames)): var = self.dat.flowsheet.input.get(inputNames[i]) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Name'], text=inputNames[i], check=inputNames[i] in pg.input, editable=False) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Max'], text=var.max, jsonEnc=True) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Min'], text=var.min, jsonEnc=True) for item in pg.inputCols: val = pg.getInputVarOption(item[0], inputNames[i]) gh.setTableItem(self.inputTable, row=i, col=self.inputCols[item[0]], text=val, jsonEnc=True) for i in range(len(outputNames)): var = self.dat.flowsheet.output.get(outputNames[i]) gh.setTableItem(self.outputTable, row=i, col=self.outputCols['Name'], text=outputNames[i], check=outputNames[i] in pg.output, editable=False) for item in pg.outputCols: val = pg.getOutputVarOption(item[0], outputNames[i]) gh.setTableItem(self.outputTable, row=i, col=self.outputCols[item[0]], text=val, jsonEnc=True) self.inputTable.resizeColumnsToContents() self.outputTable.resizeColumnsToContents()
def applyChanges(self): ''' Use information stored in this form to update the optimization options ''' # # Delete optimization settings and rebuild them from the form if self.running: return prob = self.dat.optProblem gr = self.dat.flowsheet prob.solver = self.solverBox.currentText() # Get solver options s = self.lastSolver prob.solverOptions[s] = {} opts = self.methods[s].opt().options for row in range(self.solverOptionTable.rowCount()): settingName = self.solverOptionTable.item(row, 0).text() if opts[settingName].dtype == bool: setting = gh.isChecked(self.solverOptionTable, row, 1) elif len(opts[settingName].validValues)>0: setting = \ gh.cellPulldownJSON(self.solverOptionTable,row,1) else: setting = gh.getCellJSON(self.solverOptionTable, row, 1) prob.solverOptions[s][settingName] = setting # Get objective functions prob.obj = [] table = self.fTable for row in range(self.fTable.rowCount()): pc = gh.getCellText(table, row, self.ofCols["Expression"]) ps = gh.getCellJSON(table, row, self.ofCols["Penalty Scale"]) fv = gh.getCellJSON( table, row, self.ofCols["Value for Failure"]) prob.obj.append( optimObj(pc, ps, fv) ) # Get constraints prob.g = [] table = self.gTable for row in range(self.gTable.rowCount()): pc = gh.getCellText(table, row, self.icCols["Expression"]) ps = gh.getCellJSON( table, row, self.icCols["Penalty Factor"]) pf = gh.getCellText(table, row, self.icCols["Form"] ) prob.g.append( optimInEq(pc, ps, pf) ) # Get decision variables prob.v = [] prob.vs = [] table = self.varForm for row in range(self.varForm.rowCount()): name = gh.getCellText(table, row, self.vtCols["Variable"]) gr.x[name].scaling = gh.getCellText( table, row, self.vtCols["Scale"]) gr.x[name].min = gh.getCellJSON( table, row, self.vtCols["Min"]) gr.x[name].max = gh.getCellJSON( table, row, self.vtCols["Max"]) gr.x[name].value = gh.getCellJSON( table, row, self.vtCols["Value"]) vt = gh.cellPulldownValue(table, row, self.vtCols["Type"]) if vt == "Decision": prob.v.append(name) gr.x[name].optVar=True elif vt == "Sample": prob.vs.append(name) else: gr.x[name].optVar=False # Get sample information table = self.sampleTable ci = gh.colIndexes(table) n = prob.numSamples() for row in range(table.rowCount()): if row < n: #re-set sample data for name in ci: if name in prob.samp: prob.samp[name][row] = \ gh.getCellJSON(table, row, ci[name]) else: prob.addSampleVar(name) prob.samp[name][row] = \ gh.getCellJSON(table, row, ci[name]) else: s = ci.copy() for name in s: s[name] = gh.getCellJSON(table, row, ci[name]) prob.addSample(s) # Get objective type if self.objTypeCombo.currentIndex() == 0: prob.objtype = prob.OBJ_TYPE_EVAL elif self.objTypeCombo.currentIndex() == 1: prob.objtype = prob.OBJ_TYPE_CUST # Get custom code prob.custpy = self.customCodeEdit.toPlainText() self.updateSampleVarsTable()