Exemple #1
0
def rseval(rsdata, pdata, cdata, rstypes):
    # rsdata: SampleData containing the RS training data
    # pdata: SampleData containing the sample representing the prior over uncertain variables
    # cdata: SampleData containing the candidate set
    # rstypes: dictionary with output index as key and string denote RS types as value; possible values: ['MARS',
    # 'linear', 'quadratic', 'cubic']

    # convert the data into psuade files
    cfile = os.path.join(dname, 'CandidateSet')
    pfile = os.path.join(dname, 'PriorSample')
    rsfile = os.path.join(dname, 'RSTrainData')
    cfile = writeSample(cfile, cdata)
    pfile = writeSample(pfile, pdata)
    y = 1
    rsfile = RSAnalyzer.writeRSdata(rsfile, y, rsdata)

    cmd = 'odoeu_rseval'

    inputNames = rsdata.getInputNames()
    priorNames = pdata.getInputNames()
    priorIndices = []
    for name in priorNames:
        priorIndices.append(inputNames.index(name) + 1)

    rs_list = ['MARS', 'linear', 'quadratic', 'cubic', 'GP3']

    # extract the indices of RS types for outputs
    rs_idx = [ResponseSurfaces.getEnumValue(s) for s in rs_list]
    rsdict = dict(zip(rs_list, rs_idx))

    # write script
    f = tempfile.SpooledTemporaryFile(mode='wt')
    if platform.system() == 'Windows':
        import win32api
        cfile = win32api.GetShortPathName(cfile)
        pfile = win32api.GetShortPathName(pfile)
        rsfile = win32api.GetShortPathName(rsfile)
    f.write('load %s\n' % rsfile)
    f.write('%s\n' % cmd)
    f.write('y\n')
    for i in priorIndices:
        f.write(
            '%d\n' % i
        )  # specify random variables, should be consistent with vars in prior
    f.write('0\n')  # 0 to proceed
    f.write('%s\n' %
            pfile)  # file containing the prior sample (psuade sample format)
    f.write('%s\n' %
            cfile)  # file containing the candidate set (psuade sample format)
    for rs in rstypes:  # for each output, specify RS index
        f.write('%d\n' % rsdict[rstypes[rs]])
    f.write('quit\n')
    f.seek(0)

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

    outfile = 'odoeu_rseval.out'
    assert (os.path.exists(outfile))
    return outfile
Exemple #2
0
def writeSample(fname, data):
    xdat = data.getInputData()
    RSAnalyzer.writeRSsample(fname, xdat, row=True)
    return fname
Exemple #3
0
    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
Exemple #4
0
def odoeu(cdata,
          cfile,
          pdata,
          rsdata,
          rstypes,
          opt,
          nd,
          max_iters=100,
          edata=None):

    # cdata: SampleData containing the original candidate set
    # cfile: PSUADE sample file containing the original candidates with the mean/std of the selected output
    # cdata: SampleData containing the candidate set
    # pdata: SampleData containing the sample representing the prior over uncertain variables
    # rsdata: SampleData containing the RS training data
    # rstypes: dictionary with output index as key and string denote RS types as value; possible values: ['MARS',
    # 'linear', 'quadratic', 'cubic']
    # nd: int denoting design size
    # max_iters: int denoting maximum number of iterations for the optimization routine [default: 100]
    # edata: SampleData containing the evaluation set

    # parse params
    opts = ['G', 'I', 'D', 'A']
    assert (opt in opts)
    optdict = dict(zip(opts, range(1, len(opts) + 1)))
    opt_index = optdict[opt]
    cmd = 'odoeu_optns'

    # TO DO for Pedro: check in GUI?
    # maximum iterations should be in range [100, 1000]
    assert (99 < max_iters < 1001)

    # initialize constants
    ncand = cdata.getNumSamples()
    nOutputs = rsdata.getNumOutputs()

    # extract the indices of random variables
    inputNames = rsdata.getInputNames()
    priorNames = pdata.getInputNames()
    priorIndices = []
    for name in priorNames:
        priorIndices.append(inputNames.index(name) + 1)

    rs_list = ['MARS', 'linear', 'quadratic', 'cubic', 'GP3']
    # TO DO for Pedro: check in GUI?
    # this checks to see if user is trying to pass an unsupported RS
    for rs in rstypes:
        assert (rstypes[rs] in rs_list)
    assert (len(rstypes) == nOutputs)

    # extract the indices of RS types for outputs
    rs_idx = [ResponseSurfaces.getEnumValue(s) for s in rs_list]
    rsdict = dict(zip(rs_list, rs_idx))

    # convert the data into psuade files
    pfile = os.path.join(dname, 'PriorSample')
    rsfile = os.path.join(dname, 'RSTrainData')
    pfile = writeSample(pfile, pdata)
    efile = os.path.join(dname, 'EvaluationSet')
    if edata is None:
        efile = writeSample(efile, cdata)
    else:
        efile = writeSample(efile, edata)
    y = 1
    rsfile = RSAnalyzer.writeRSdata(rsfile, y, rsdata)

    # write script
    f = tempfile.SpooledTemporaryFile(mode='wt')
    if platform.system() == 'Windows':
        import win32api
        pfile = win32api.GetShortPathName(pfile)
        rsfile = win32api.GetShortPathName(rsfile)
        efile = win32api.GetShortPathName(efile)

    f.write('%s\n' % cmd)
    f.write('y\n')
    f.write('%d\n' % opt_index)  # choose G, I, D, A
    f.write('%d\n' % ncand)  # size of the candidate set
    f.write('%d\n' % nd)  # design size
    f.write(
        '%d\n' %
        max_iters)  # max number of iterations, must be greater or equal to 100
    f.write('n\n')  # no initial guess
    f.write('%s\n' %
            rsfile)  # file containing RS training data (psuade format)
    for i in priorIndices:
        f.write(
            '%d\n' % i
        )  # specify random variables, should be consistent with vars in prior
    f.write('0\n')  # 0 to proceed
    f.write('%s\n' %
            pfile)  # file containing the prior sample (psuade sample format)
    f.write('%s\n' %
            cfile)  # file containing the candidate set (psuade sample format)
    f.write('%s\n' % efile
            )  # ... evaluate the optimality values on the (same) candidate set
    for rs in rstypes:  # for each output, specify RS index
        f.write('%d\n' % rsdict[rstypes[rs]])
        # TO DO: as we add more RS, may need to port more code from RSAnalyzer.py to handle different RS types
    f.write('quit\n')
    f.seek(0)

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

    # parse output
    best_indices = None
    best_optval = None
    m = re.findall(cmd + r' best selection = (\d+ \d+)', out)
    if m:
        best_indices = [i for i in m[0].split()]
        s = r'\s*'.join([i for i in best_indices])
        best_optvals = re.findall(s + r'\s*===> output = (\S*)', out)

        best_indices = [int(i) for i in best_indices]
        best_optval = float(best_optvals[0])

    return best_indices, best_optval
Exemple #5
0
    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()