示例#1
0
文件: OUU.py 项目: elbashandy/FOQUS
    def ouu(self,
            fname,
            y,
            outputsAsConstraint,
            outputsAsDerivative,
            xtable,
            phi,
            x3sample=None,
            x4sample=None,
            useRS=False,
            useBobyqa=True,
            driver=None,
            optDriver=None,
            auxDriver=None,
            ensOptDriver=None,
            plotSignal=None,
            endFunction=None):

        # Function to execute after inference has finished.
        # Function would enable button again and such things.
        self.endFunction = endFunction

        # read data, assumes data already have fixed variables written to file
        data = LocalExecutionModule.readSampleFromPsuadeFile(fname)
        if optDriver == None and ensOptDriver == None and \
               data.getOptDriverName() == None and \
               data.getEnsembleOptDriverName() == None:
            Common.showError('Model file does not have any drivers set!', \
                             showDeveloperHelpMessage = False)
            self.hadError = True
            if endFunction is not None:
                endFunction()
            return
        if driver != None:
            data.setDriverName(driver)
        if optDriver != None:
            data.setOptDriverName(optDriver)
        if auxDriver != None:
            data.setAuxDriverName(auxDriver)
        if ensOptDriver != None:
            data.setEnsembleOptDriverName(ensOptDriver)
        else:
            ensOptDriver = data.getEnsembleOptDriverName()

        # Remove file that tells OUU to stop
        if os.path.exists(OUU.stopFile):
            os.remove(OUU.stopFile)

        # process input table
        dname = OUU.dname
        deleteFiles = True
        if x3sample is not None:
            deleteFiles = not x3sample['file'].startswith(dname)
        #Common.initFolder(dname, deleteFiles = deleteFiles)
        if platform.system() == 'Windows':
            import win32api
            dname = win32api.GetShortPathName(dname)
        fnameOUU = Common.getLocalFileName(dname, fname, '.ouudat')
        p = RSAnalyzer.parsePrior(data, xtable)
        if p is not None:
            inputLB = p['inputLB']
            inputUB = p['inputUB']
            dist = p['dist']

        init_input = []
        vartypes = []
        for e in xtable:
            t = e['type']
            if t == u'Opt: Primary Continuous (Z1)':
                vartypes.append(1)
            elif t == u'Opt: Primary Continuous (Z1c)':
                vartypes.append(1)
            elif t == u'Opt: Primary Discrete (Z1d)':
                vartypes.append(1)
            elif t == u'Opt: Recourse (Z2)':
                vartypes.append(2)
            elif t == u'UQ: Discrete (Z3)':
                vartypes.append(3)
            elif t == u'UQ: Continuous (Z4)':
                vartypes.append(4)
            if t != u'Fixed':
                init_input.append(e['value'])
        M1 = vartypes.count(1)
        M2 = vartypes.count(2)
        M3 = vartypes.count(3)
        M4 = vartypes.count(4)

        # check arguments
        if M1 < 1:
            error = 'OUU: In function ouu(), number of Z1 (design opt) '
            error = error + 'must be at least 1.'
        if M3 > 0:
            if x3sample == None:
                error = 'OUU: In function ouu(), "x3sample" is undefined.'
                Common.showError(error)
                return None

        if M4 > 0:
            if x4sample == None:
                error = 'OUU: In function ouu(), "x4sample" is undefined.'
                Common.showError(error)
                return None
            loadcs = 'file' in x4sample
            if loadcs:
                N = 0  # number of samples in x4sample['file']

                ### TO DO for Jeremy: check sample size in GUI
                with open(x4sample['file']) as f:
                    header = f.readline()
                    header = header.split()
                    N = int(header[0])

                Nmin = M4 + 1  # minimum number of samples
                if N < Nmin:
                    error = 'OUU: In function ouu(), "x4sample file" requires '
                    error = error + 'at least %d samples.' % Nmin
                    Common.showError(error)
                    return None
                if useRS:
                    Nrs = 'nsamplesRS' in x4sample
                    if not Nrs:
                        error = 'OUU: In function ouu(), "x4sample nsamplesRS" is '
                        error = error + 'required for setting up response surface.'
                        Common.showError(error)
                        return None
                    Nrs = x4sample['nsamplesRS']
                    Nrs = min(max(Nrs, Nmin),
                              N)  ### TO DO for Jeremy: check in GUI

        # TO DO: remove randSeed
        ouuFile = OUU.writeOUUdata(fnameOUU,
                                   y,
                                   outputsAsConstraint,
                                   outputsAsDerivative,
                                   data,
                                   xtable,
                                   randSeed=41491431,
                                   inputLowerBounds=inputLB,
                                   inputUpperBounds=inputUB,
                                   inputPDF=dist,
                                   useEnsOptDriver=(ensOptDriver != None),
                                   init_input=init_input)
        if (ouuFile == None):
            return None

        # write script
        f = OUU.writescript(vartypes,
                            fnameOUU,
                            outputsAsConstraint,
                            phi,
                            x3sample,
                            x4sample,
                            useRS,
                            useBobyqa,
                            useEnsOptDriver=(ensOptDriver != None))

        # delete previous history file
        if os.path.exists(OUU.hfile):
            os.remove(OUU.hfile)

        self.textDialog = Common.textDialog()
        self.thread = psuadeThread(self, f, self.finishOUU, self.textDialog,
                                   plotSignal)
        self.thread.start()
示例#2
0
文件: OUU.py 项目: elbashandy/FOQUS
    def writescript(vartypes,
                    fnameOUU,
                    outputsAsConstraint,
                    phi=None,
                    x3sample=None,
                    x4sample=None,
                    useRS=False,
                    useBobyqa=False,
                    useEnsOptDriver=None):

        M1 = vartypes.count(1)
        M2 = vartypes.count(2)
        M3 = vartypes.count(3)
        M4 = vartypes.count(4)
        nInputs = M1 + M2 + M3 + M4

        # write script
        #f = open('ouu.in','w')
        f = tempfile.SpooledTemporaryFile()
        if platform.system() == 'Windows':
            import win32api
            fnameOUU = win32api.GetShortPathName(fnameOUU)
        f.write('run %s\n' % fnameOUU)
        f.write('y\n')  # ready to proceed
        # ... partition variables
        f.write('%d\n' % M1)  # number of design opt variables
        if M1 == nInputs:
            f.write('quit\n')
            f.seek(0)
            return f

        # ________ M1 < nInputs ________
        f.write('%d\n' % M2)  # number of operating opt variables
        if M1 + M2 == nInputs:
            for i in xrange(nInputs):
                f.write('%d\n' % vartypes[i])
            if useBobyqa:
                f.write('n\n')  # use BOBYQA means 'no' to use own driver
            else:
                f.write('y\n')  # use own driver as optimizer
            f.write('quit\n')
            f.seek(0)
            return f

        # ________ M1+M2 < nInputs ________
        f.write('%d\n' % M3)  # number of discrete UQ variables
        for i in xrange(nInputs):
            f.write('%d\n' % vartypes[i])

        # ... set objective function w.r.t. to uncertainty
        ftype = phi['type']
        f.write('%d\n' % ftype)  # optimization objective w.r.t. UQ variables
        if ftype == 2:
            beta = max(0, phi['beta'])  # beta >= 0
            f.write('%f\n' % beta)
        elif ftype == 3:
            alpha = phi['alpha']
            alpha = min(max(alpha, 0.5), 1.0)  # 0.05 <= alpha <= 1.0
            f.write('%f\n' % alpha)

        if outputsAsConstraint.count(True) > 0:
            f.write('1\n')

        # ... get sample for discrete UQ variables
        # The file format should be:
        # line 1: <nSamples> <nInputs>
        # line 2: <sample 1 input 1> <input 2> ... <probability>
        # line 3: <sample 2 input 1> <input 2> ... <probability>
        if M3 > 0:
            if platform.system() == 'Windows':
                import win32api
                x3sample['file'] = win32api.GetShortPathName(x3sample['file'])

            f.write('%s\n' %
                    x3sample['file'])  # sample file for discrete UQ variables

        # ... get sample for continuous UQ variables
        # The file format should be:
        # line 1: <nSamples> <nInputs>
        # line 2: <sample 1 input 1> <input 2> ...
        # line 3: <sample 2 input 1> <input 2> ...
        #                .....
        # line N: <sample N input 1> <input 2> ...
        if M4 > 0:
            loadcs = 'file' in x4sample
            if loadcs:
                if platform.system() == 'Windows':
                    import win32api
                    x4sample['file'] = win32api.GetShortPathName(
                        x4sample['file'])
                f.write('1\n')  # load samples for continuous UQ vars
                f.write('%s\n' %
                        x4sample['file'])  # sample file for continuous UQ vars
            else:
                f.write('2\n')  # generate samples for continuous UQ variables
            # ... apply response surface
            if useRS:
                f.write('y\n')  # use response surface
                if loadcs:
                    Nrs = x4sample['nsamplesRS']
                    f.write(
                        '%d\n' % Nrs
                    )  # number of points to build RS (range: [M4+1,N] where N=samplesize)
                    randsample = True  # set to False to pass in subsample based on convex hull analysis
                    # set to True to use psuade's built-in random sampler
                    if randsample:
                        f.write('2\n')  # 2 to generate random sample
                    else:
                        f.write('1\n')  # 1 to upload subsample file
                        x, y = RSAnalyzer.readRSsample(x4sample['file'])
                        xsub = OUU.subsample(x, Nrs)
                        dname = OUU.dname
                        if platform.system() == 'Windows':
                            import win32api
                            dname = win32api.GetShortPathName(dname)
                        x4subsample = Common.getLocalFileName(
                            dname, x4sample['file'], '.subsample')
                        RSAnalyzer.writeRSsample(x4subsample, xsub)
                        f.write(
                            '%s\n' % x4subsample
                        )  # subsample file containing subset of original points
            else:
                f.write('n\n')  # do not use response surface
            # ... # create samples for continuous UQ variables
            if not loadcs:
                Nmin = M4 + 1
                if x4sample['method'] == SamplingMethods.LH:
                    f.write('1\n')  # sampling scheme: Latin Hypercube
                    nSamples = x4sample['nsamples']
                    nSamples = min(max(nSamples, Nmin), 1000)
                    f.write('%d\n' %
                            nSamples)  # number of samples (range: [M4+1,1000])
                elif x4sample['method'] == SamplingMethods.FACT:
                    f.write('2\n')  # sampling scheme: Factorial
                    nlevels = x4sample['nlevels']
                    nlevels = min(max(nlevels, 3), 100)
                    f.write('%d\n' % nlevels
                            )  # number of levels per variable (range: [3,100])
                elif x4sample['method'] == SamplingMethods.LPTAU:
                    f.write('3\n')  # sampling scheme: Quasi Monte Carlo
                    nSamples = x4sample['nsamples']
                    nSamples = min(max(nSamples, Nmin), 1000)
                    f.write('%d\n' %
                            nSamples)  # number of samples (range: [M4+1,1000])

        # ... choose optimizer
        if M2 > 0:
            #if useBobyqa:
            #    f.write('n\n')    # use BOBYQA
            #else:
            #    f.write('y\n')    # use own driver as optimizer
            f.write('y\n')  # use own driver as optimizer

        # ... choose ensemble optimization driver
        if M3 + M4 > 0:  #and not useBobyqa:
            if useEnsOptDriver:
                f.write('y\n')  # use ensemble driver
            else:
                f.write('n\n')

        # ... choose mode to run simulations for computing statistics
        if M3 + M4 > 0:
            f.write('n\n')  # do not use asynchronous mode (not tested)

        f.write('quit\n')
        f.seek(0)

        #for line in f:
        #    print line.strip()
        #f.seek(0)

        return f
示例#3
0
文件: OUU.py 项目: elbashandy/FOQUS
    def compress(fname):

        N = 0  # number of samples in x3sample['file']
        with open(fname) as f:  ### TO DO for Jeremy: check sample size in GUI
            header = f.readline()
            header = header.split()
            N = int(header[0])
            nInputs = int(header[1])

        Nmin = 100  # psuade minimum for genhistogram
        if N < Nmin:
            warn = 'OUU: In function compress(), "x3sample file" requires '
            warn = warn + 'at least %d samples.' % Nmin
            Common.showError(warn)
            return {N: fname}  # return original sample file

        outfiles = {}
        nbins_max = 20
        nscenarios_max = 1501
        for nbins in xrange(2, nbins_max):

            # write script to invoke scenario compression
            f = tempfile.SpooledTemporaryFile()
            if platform.system() == 'Windows':
                import win32api
                fname = win32api.GetShortPathName(fname)
            f.write('read_std %s\n' % fname)
            f.write('genhistogram\n')
            for x in xrange(nInputs):
                f.write('%d\n' % nbins)
            f.write('quit\n')
            f.seek(0)

            # invoke psuade
            out, error = Common.invokePsuade(f)
            f.close()
            if error:
                return None

            # check output file
            sfile = 'psuade_pdfhist_sample'
            if os.path.exists(sfile):
                Ns = 0  # number of samples in psuade_pdfhist_sample
                with open(sfile) as f:
                    header = f.readline()
                    header = header.split()
                    Ns = int(header[0])
                sfile_ = Common.getLocalFileName(OUU.dname, fname,
                                                 '.compressed' + str(Ns))
                if os.path.exists(sfile_):
                    os.remove(sfile_)
                os.rename(sfile, sfile_)
                sfile = sfile_
            else:
                error = 'OUU: %s does not exist.' % sfile
                Common.showError(error, out)
                return None

            # append scenario file to data structure
            if len(outfiles) > 1 and Ns > min(N, nscenarios_max):
                return outfiles
            else:
                outfiles[Ns] = (sfile, nbins)

        return outfiles