Ejemplo n.º 1
0
 def run(self):
     '''
         This function overloads the Thread class function,
         and is called when you run start() to start a new thread.
     '''
     alamoExe = self.dat.foqusSettings.alamo_path
     try:
         assert(os.path.isfile(alamoExe))
     except AssertionError:
         msg = "The ALAMO executable was not found. Check the ALAMO exec "\
               "path in settings and that ALAMO is installed."
         self.msgQueue.put(msg)
         raise AssertionError(msg)
     try:
         #Setup dictionaries to convert between foqus and ALAMO
         #variable names.  Also setup a list of input and output
         #variable names for ALAMO.  I'm going to keep the dicts
         #for now in case using the FOQUS variables names in ALAMO
         #doesn't work out for some reason.
         uq_file = self.options[
             "FOQUS Model (for UQ)"].value
         self.xList = self.input
         self.zList = self.output
         self.xi = {}
         self.zi = {}
         cn = self.graph.input.compoundNames(sort=True)
         for v in self.xList:
             self.xi[v] = cn.index(v)
         cn = self.graph.output.compoundNames(sort=False)
         for v in self.zList:
             self.zi[v] = cn.index(v)
         #Get options and show some information about settings
         adaptive = self.options['SAMPLER'].value
         alamoDir = self.alamoDir
         alamoDirFull = os.path.abspath(alamoDir)
         self.setupWorkingDir()
         adpexe = os.path.join(alamoDirFull, 'foqusALAMOClient.py')
         self.writeAlamoInputFile(adaptiveExe=adpexe)
         if self.checkNumVars():
             return
         alamoInput = self.options["Input File"].value
         alamoOutput = alamoInput.rsplit('.', 1)[0] + '.lst'
         self.msgQueue.put("------------------------------------")
         self.msgQueue.put("Starting ALAMO\n")
         self.msgQueue.put("Exec File Path:    " + alamoExe)
         self.msgQueue.put("Sub-directory:     " + alamoDir)
         self.msgQueue.put("Input File Name:   " + alamoInput)
         self.msgQueue.put("Output File Name:  " + alamoOutput)
         self.msgQueue.put("SAMPLER:           " + str(adaptive))
         self.msgQueue.put("UQ Driver File:    " + uq_file)
         self.msgQueue.put("------------------------------------")
         # If using adaptive sampleing open a network socket to listen
         if adaptive:
             listener = listen.foqusListener(self.dat)
             listener.setInputs(self.input)
             listener.setOutputs(self.output)
             listener.failValue = self.options["PRESET"].value
             address = listener.address
             listener.start()
         #Run Alamo
         process = subprocess.Popen([
             alamoExe,
             alamoInput],
             cwd=alamoDir,
             stdout=subprocess.PIPE,
             stderr=subprocess.STDOUT,
             stdin = None,
             creationflags=win32process.CREATE_NO_WINDOW)
         line = process.stdout.readline()
         while process.poll() == None or line != b'':
             if line == b'': time.sleep(0.2)
             if line != b'':
                 self.msgQueue.put(line.decode("utf-8").rstrip())
             line = process.stdout.readline()
             if self.stop.isSet():
                 self.msgQueue.put("**terminated by user**")
                 process.kill()
                 break
         if adaptive:
             # stop the listener
             conn = Client(address)
             conn.send(['quit'])
             conn.close()
         alamoOutput = os.path.join(alamoDir, alamoOutput)
         if self.options['FUNFORM'].value != "Fortran":
             self.msgQueue.put("MUST USE FORTRAN FORM to make UQ and flowsheet plugins")
             self.msgQueue.put("Skipping plugin creation")
             return
         res = SurrogateParser.parseAlamo(alamoOutput)
         self.result = res
         #self.msgQueue.put(str(res))
         self.writePlugin()
         SurrogateParser.writeAlamoDriver(
             self.result,
             uq_file,
             ii=self.ii,
             oi=self.oi,
             inputNames=self.xList2,
             outputNames=self.zList2)
         self.driverFile = uq_file
     except Exception:
         self.ex = sys.exc_info()
         logging.getLogger("foqus." + __name__).error(
                 "Exception in ALAMO Thread",
                 exc_info=sys.exc_info())
Ejemplo n.º 2
0
    def analyze(self):
        dir = os.getcwd()
        for f in os.listdir(dir):
            if re.search('psuadeEval.out', f):
                os.remove(os.path.join(dir, f))

        if self.run_button.text() == 'Run OUU': # Run OUU
            names, indices = self.input_table.getPrimaryVariables()
            if len(names) == 0:
                QMessageBox.information(self, 'No Primary Variables',
                                              'At least one input must be a primary variable!')
                return

            valid, error = self.input_table.checkValidInputs()
            if not valid:
                QMessageBox.information(self, 'Input Table Distributions',
                                              'Input table distributions are either not correct or not filled out completely! %s' % error)
                return

            if self.compressSamples_chk.isChecked() and not self.scenariosCalculated:
                QMessageBox.information(self, 'Compress Samples Not Calculated',
                                              'You have elected to compress samples for discrete random variables (Z3), but have not selected the sample size to use!')
                return

            M1 = len(self.input_table.getPrimaryVariables()[0])
            M2 = len(self.input_table.getRecourseVariables()[0])
            M3 = len(self.input_table.getUQDiscreteVariables()[0])
            M4 = len(self.input_table.getContinuousVariables()[0])

            Common.initFolder(OUU.dname)

            self.managePlots()
            self.clearPlots()
            self.manageBestValueTable()
            self.summary_group.setTitle('Best So Far')

            xtable = self.input_table.getTableValues()

            # get arguments for ouu()
            model = copy.deepcopy(self.model)
            inputNames = model.getInputNames()
            inputTypes = list(model.getInputTypes())
            defaultValues = model.getInputDefaults()
            for row in xtable:
                if row['type'] == 'Fixed':
                    modelIndex = inputNames.index(row['name'])
                    inputTypes[modelIndex] = Model.FIXED
                    defaultValues[modelIndex] = row['value']
            #print inputTypes
            #print defaultValues
            model.setInputTypes(inputTypes)
            model.setInputDefaults(defaultValues)
            data = SampleData(model)
            fname = 'ouuTemp.dat'
            data.writeToPsuade(fname)

            # Outputs
            numOutputs = self.outputs_table.rowCount()
            y = []
            self.useAsConstraint = [False] * numOutputs
            self.useAsDerivative = [False] * numOutputs
            for r in range(numOutputs):
                type = self.outputs_table.cellWidget(r,0).currentText()
                if type == ouuSetupFrame.ObjFuncText:
                    y.append(r + 1)
                elif type == ouuSetupFrame.ConstraintText:
                    self.useAsConstraint[r] = True
                elif type == ouuSetupFrame.DerivativeText:
                    self.useAsDerivative[r] = True

            if self.mean_radio.isChecked():
                phi = {'type':1}
            elif self.meanWithBeta_radio.isChecked():
                beta = self.betaDoubleSpin.value()
                phi = {'type':2, 'beta':beta}
            elif self.alpha_radio.isChecked():
                alpha = self.alphaDoubleSpin.value()
                phi = {'type':3, 'alpha':alpha}
            x3sample = None
            if M3 > 0:
                if self.compressSamples_chk.isChecked():
                    selectedSamples = int(self.scenarioSelect_combo.currentText())
                    sfile = self.scenarioFiles[selectedSamples][0]
                else:
                    sfile = 'z3Samples.smp'
                    success = self.writeTableToFile(self.z3_table, sfile, M3)
                    if not success:
                        return
                    if sfile.endswith('.csv'): # Convert .csv file to simple file
                        newFileName = OUU.dname + os.sep + os.path.basename(fname)[:-4] + '.smp'
                        inData = LocalExecutionModule.readDataFromCsvFile(sfile, askForNumInputs=False)
                        LocalExecutionModule.writeSimpleFile(newFileName, inData[0])
                        sfile = newFileName

                #print 'x3 file is', sfile
                x3sample = {'file':sfile}                           # x3sample file
                data,_, numInputs, _ = LocalExecutionModule.readDataFromSimpleFile(sfile, hasColumnNumbers=False)
                if numInputs != M3:
                    QMessageBox.critical(self, "Number of variables don't match",
                                              'The number of variables from the file (%d) does not match the number of Z3 discrete variables (%d).  You will not be able to perform analysis until this is corrected.' % (numInputs, M3))
                    return
            useRS = self.x4RSMethod_check.isChecked()
            x4sample = None
            if self.z4NewSample_radio.isChecked():
                method = self.x4SampleScheme_combo.currentText()
                method = SamplingMethods.getEnumValue(method)
                N = self.x4SampleSize_spin.value()
                if method in [SamplingMethods.LH,SamplingMethods.LPTAU]:
                    x4sample = {'method':method, 'nsamples':N}  # number of samples (range: [M1+1,1000])
                elif method == SamplingMethods.FACT:
                    x4sample = {'method':method, 'nlevels':N}   # number of levels (range: [3,100])
            else:
                sfile = 'z4Samples.smp'
                success = self.writeTableToFile(self.z4_table, sfile, M4)
                if not success:
                    return
                if len(sfile) == 0:
                    QMessageBox.critical(self, 'Missing file',
                               'Z4 sample file not specified!')
                    return

                if sfile.endswith('.csv'): # Convert .csv file to simple file
                    newFileName = OUU.dname + os.sep + os.path.basename(fname)[:-4] + '.smp'
                    inData = LocalExecutionModule.readDataFromCsvFile(sfile, askForNumInputs=False)
                    LocalExecutionModule.writeSimpleFile(newFileName, inData[0])
                    sfile = newFileName

                inData, outData, numInputs, numOutputs = LocalExecutionModule.readDataFromSimpleFile(sfile, hasColumnNumbers=False)
                numSamples = inData.shape[0]
                if numInputs != M4:
                    QMessageBox.critical(self, "Number of variables don't match",
                                              'The number of input variables from the file (%d) does not match the number of Z4 continuous variables (%d).  You will not be able to perform analysis until this is corrected.' % (numInputs, M4))
                    return
                if numSamples <= M4:
                    QMessageBox.critical(self, 'Not enough samples',
                               'Z4 sample file must have at least %d samples!' % (M4 + 1))
                    return

                x4sample = {'file': sfile}                      # x4sample file, must have at least M4+1 samples

                if useRS:
                    Nrs = self.z4SubsetSize_spin.value()     # add spinbox to get number of samples to generate RS
                    x4sample['nsamplesRS'] = Nrs               # TO DO: make sure spinbox has M4+1 as min and x4sample's sample size as max

            #  TODO: Get rid of usebobyqa option. Behavior should be as if usebobyqa is always false
            # TODO: Change GUI to display optimizer and optimizing with bobyqa
            # TODO: Get rid of primarySolver_combo completely
            useBobyqa = False
            method = self.primarySolver_combo.currentText()
            if method == "BOBYQA":
                pass
            elif method == "NEWUOA":
                pass
            if 'simulator' in self.secondarySolver_combo.currentText():
                useBobyqa = True  # use BOBYQA if driver is a simulator, not an optimizer

            self.run_button.setText('Stop')
            optDriver = None
            ensembleOptDriver = None
            if self.node_radio.isChecked():
                ensembleOptDriver = self.setupPSUADEClient()
                optDriver = ensembleOptDriver
                listener = listen.foqusListener(self.dat)
                variableNames = []
                fixedNames = []
                for row in xtable:
                    #print row
                    if row['type'] == 'Fixed':
                        fixedNames.append(row['name'])
                    else:
                        variableNames.append(row['name'])
                #print fixedNames, variableNames
                #print variableNames + fixedNames
                listener.inputNames = variableNames + fixedNames
                outputNames = self.model.getOutputNames()
                listener.outputNames = [outputNames[yItem-1] for yItem in y]
                listener.failValue = -111111
                self.listenerAddress = listener.address
                listener.start()

            # print M1, M2, M3, M4, useBobyqa
            self.OUUobj = OUU()
            try:
                results = self.OUUobj.ouu(fname,y,self.useAsConstraint,self.useAsDerivative,xtable,phi,
                                          x3sample=x3sample,x4sample=x4sample,useRS=useRS,useBobyqa=useBobyqa,
                                          optDriver = optDriver, ensOptDriver = ensembleOptDriver, plotSignal = self.plotSignal,
                                          endFunction=self.finishOUU)
            except:
                import traceback
                traceback.print_exc()
                if self.node_radio.isChecked():
                    # stop the listener
                    conn = Client(self.listenerAddress)
                    conn.send(['quit'])
                    conn.close()

                # enable run button
                self.run_button.setEnabled(True)
                return
        else: # Stop OUU
            self.OUUobj.stopOUU()
            self.run_button.setEnabled(False)
            self.freeze()