def writeSample(fname, data): xdat = data.getInputData() RSAnalyzer.writeRSsample(fname, xdat, row=True) return fname
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