示例#1
0
    def createPsuadeInFile(data, filename, includePDF=True):
        outf = open(filename, 'w')
        outf.write('PSUADE\n')

        #Write inputs
        outf.write('INPUT\n')
        outf.write('   dimension = %d\n' % data.getNumInputs())
        names = data.getInputNames()
        mins = data.getInputMins()
        maxs = data.getInputMaxs()
        indices = list(range(data.getNumInputs()))
        for i, name, minimum, maximum in zip(indices, names, mins, maxs):
            outf.write('   variable %d %s = %e %e\n' %
                       (i + 1, name, minimum, maximum))
        distributions = data.getInputDistributions()
        for i, dist in zip(indices, distributions):
            distType = dist.getDistributionType()
            distParams = dist.getParameterValues()
            if distType == Distribution.SAMPLE:
                outf.write('   PDF %d %c' %
                           (i + 1, Distribution.getPsuadeName(distType)))
                if distParams[0] is not None:
                    filename = distParams[0]
                    if platform.system() == 'Windows':
                        import win32api
                        filename = win32api.GetShortPathName(filename)
                    outf.write(' %s' % filename)
                if distParams[1] is not None:
                    outf.write(' %d' % distParams[1])
                outf.write('\n')
            elif includePDF:  # write out PDF info for all non-uniform PDF types
                if distType != Distribution.UNIFORM:
                    outf.write('   PDF %d %c' %
                               (i + 1, Distribution.getPsuadeName(distType)))
                    if distParams[0] is not None:
                        outf.write(' %e' % distParams[0])
                    if distParams[1] is not None:
                        outf.write(' %e' % distParams[1])
                    outf.write('\n')
        outf.write('END\n')

        #Write outputs
        outf.write('OUTPUT\n')
        if data.getNumOutputs() == 0:
            outf.write('   dimension = 1\n')
            names = ['ghostOuput']
            indices = list(range(1))
            for i, name in zip(indices, names):
                outf.write('   variable %d %s\n' % (i + 1, name))
        else:
            outf.write('   dimension = %d\n' % data.getNumOutputs())
            names = data.getOutputNames()
            indices = list(range(data.getNumOutputs()))
            for i, name in zip(indices, names):
                outf.write('   variable %d %s\n' % (i + 1, name))

        outf.write('END\n')

        #Write Method
        outf.write('METHOD\n')
        outf.write('   sampling = %s\n' %
                   SamplingMethods.getPsuadeName(data.getSampleMethod()))
        outf.write('   num_samples = %d\n' % data.getNumSamples())
        if data.getSampleMethod() != SamplingMethods.GMOAT:
            outf.write('   randomize\n')
        outf.write('END\n')

        #Write Application
        outf.write('APPLICATION\n')
        driverString = data.getDriverName()
        if driverString is None:
            driverString = 'NONE'
        elif platform.system() == 'Windows':
            if os.path.exists(driverString):
                import win32api
                driverString = win32api.GetShortPathName(driverString)
            else:
                driverString = 'NONE'
        driverString = 'NONE'

        outf.write('   driver = %s\n' % driverString)
        outf.write('   save_frequency = 1\n')
        outf.write('END\n')

        #Write Analysis
        outf.write('ANALYSIS\n')
        outf.write('   diagnostics 2\n')
        outf.write('END\n')

        outf.write('END\n')
        outf.close()
示例#2
0
文件: OUU.py 项目: elbashandy/FOQUS
    def writeOUUdata(outfile, outputs, constraints, derivatives, data, xtable,
                     **kwargs):

        # Charles TODO: Handle y is now a list of inputs
        # Charles TODO: Handle derivatives

        # defaults
        rseed = None
        driver = data.getDriverName()
        if driver is None:
            driver = 'NONE'
        optdriver = data.getOptDriverName()
        if optdriver is None:
            optdriver = 'NONE'
        ensoptdriver = data.getEnsembleOptDriverName()
        if ensoptdriver is None:
            ensoptdriver = 'NONE'
        auxdriver = data.getAuxDriverName()
        if auxdriver is None:
            auxdriver = 'NONE'
        inputLB = None
        inputUB = None
        inputDefaults = None
        distributions = None
        init_input = None

        # process keyworded arguments
        for key in kwargs:
            k = key.lower()
            if k == 'randseed':
                rseed = kwargs[key]
            elif k == 'driver':
                driver = kwargs[key]
            elif k == 'optdriver':
                optdriver = kwargs[key]
            elif k == 'ensoptdriver':
                ensoptdriver = kwargs[key]
            elif k == 'auxdriver':
                auxdriver = kwargs[key]
            elif k == 'inputlowerbounds':
                inputLB = kwargs[key]
            elif k == 'inputupperbounds':
                inputUB = kwargs[key]
            elif k == 'inputpdf':
                distributions = kwargs[key]
            elif k == 'init_input':
                init_input = kwargs[key]

        inputTypes = data.getInputTypes()
        nInputs = data.getNumInputs()
        inputNames = data.getInputNames()
        variableInputIndices = []
        for e in xtable:
            if e['type'] != u'Fixed':
                variableInputIndices.append(inputNames.index(e['name']))
        nVariableInputs = len(variableInputIndices)
        nOutputs = len(outputs)
        nSamples = num_fmin = 1  # number of random restarts
        nConstraints = constraints.count(True)
        nDerivatives = derivatives.count(True)
        totalOutputs = nOutputs + nConstraints + nDerivatives

        f = open(outfile, 'w')
        if init_input:
            f.write('PSUADE_IO\n')
            f.write('%d %d %d\n' % (nVariableInputs, totalOutputs, nSamples))
            f.write("1 0\n")  # assume initial point has not been run
            for x in init_input:
                f.write(' % .16e\n' % x)
            for i in xrange(totalOutputs):
                f.write(' 9.9999999999999997e+34\n')
            f.write("PSUADE_IO\n")

        # TO DO: merge with RSAnalyzer.writeRSdata()
        f.write('PSUADE\n')

        # ... input ...
        numFixed = nInputs - nVariableInputs
        f.write('INPUT\n')
        if numFixed > 0:
            f.write('   num_fixed %d\n' % numFixed)
        f.write('   dimension = %d\n' % nVariableInputs)
        if inputLB is None:
            inputLB = data.getInputMins()
        if inputUB is None:
            inputUB = data.getInputMaxs()
        if inputDefaults is None:
            inputDefaults = data.getInputDefaults()
        indices = range(nInputs)
        variableIndex = 1
        fixedIndex = 1
        for i, name, inType, lb, ub, default in zip(indices, inputNames, \
                             inputTypes, inputLB, inputUB, inputDefaults):
            if i in variableInputIndices:  #inType == Model.VARIABLE:
                f.write('   variable %d %s  =  % .16e  % .16e\n' % \
                        (variableIndex, name, lb, ub))
                variableIndex = variableIndex + 1
            else:
                f.write('   fixed %d %s = % .16e\n' %
                        (fixedIndex, name, default))
                fixedIndex = fixedIndex + 1

        # inject discrete variables in psuade
        opttypes = []
        cnt = 0
        for e in xtable:
            cnt = cnt + 1
            t = e['type']
            if t == u'Opt: Primary Discrete (Z1d)':
                opttypes.append(cnt)

        nn = len(opttypes)
        for ii in range(nn):
            jj = opttypes[ii]
            f.write('   discrete %d\n' % (jj))

        if distributions is None:
            distributions = SampleData.getInputDistributions(data)
        for i, inType, dist in zip(indices, inputTypes, distributions):
            if i in variableInputIndices:  #inType == Model.VARIABLE:
                distType = dist.getDistributionType()
                distParams = dist.getParameterValues()
                if distType != Distribution.UNIFORM:
                    f.write('   PDF %d %c' % (i+1, \
                            Distribution.getPsuadeName(distType)))
                    if distType == Distribution.SAMPLE:
                        error = 'OUU: In function writeOUUdata(), '
                        error = error + 'SAMPLE distribution is not supported.'
                        Common.showError(error)
                        return None
                    else:
                        if distParams[0] is not None:
                            f.write(' % .16e' % distParams[0])
                        if distParams[1] is not None:
                            f.write(' % .16e' % distParams[1])
                    f.write('\n')
        f.write('END\n')

        # ... output ...
        outActive = nOutputs
        nConstrs = 0
        nDerivs = 0
        for ii in range(len(constraints)):
            if constraints[ii]:
                outActive = outActive + 1
                nConstrs = nConstrs + 1
        for ii in range(len(derivatives)):
            if derivatives[ii]:
                outActive = outActive + 1
                nDerivs = nDerivs + 1
        if (nOutputs != 1):
            error = 'OUU: In function writeOUUdata(), '
            error = error + 'multi-objective optimization not supported.'
            Common.showError(error)
            return None
        else:
            if ((nConstrs > 0) and (nDerivs > 0)):
                error = 'OUU: In function writeOUUdata(), '
                error = error + 'LBFGS does not support inequality constraints.'
                Common.showError(error)
                return None
            elif ((nDerivs > 0) and (nDerivs != nVariableInputs)):
                error = 'OUU: In function writeOUUdata(), '
                error = error + 'Number of derivatives not correct'
                Common.showError(error)
                return None

        f.write('OUTPUT\n')
        f.write('   dimension = %d\n' % (outActive))
        outputNames = SampleData.getOutputNames(data)
        for ii in range(nOutputs):
            ind = outputs[ii]
            f.write('   variable %d %s\n' % (ii + 1, outputNames[ind - 1]))
            print('   variable %d %s\n' % (ii + 1, outputNames[ind - 1]))
        outActive = nOutputs + 1
        for ii in range(len(constraints)):
            if constraints[ii]:
                f.write('   variable %d %s\n' % (outActive, outputNames[ii]))
                print('   variable %d %s\n' % (outActive, outputNames[ii]))
                outActive = outActive + 1
        for ii in range(len(derivatives)):
            if derivatives[ii]:
                f.write('   variable %d %s\n' % (outActive, outputNames[ii]))
                print('   variable %d %s\n' % (outActive, outputNames[ii]))
                outActive = outActive + 1
        f.write('END\n')

        # ... method ...
        f.write('METHOD\n')
        f.write('   sampling = MC\n')  # OUU uses this to create
        f.write('   num_samples = 1\n')  # initial guess
        if rseed is not None:
            f.write('random_seed = %d\n' % rseed)  # random seed
        f.write('END\n')

        # ... application ...
        f.write('APPLICATION\n')
        if platform.system() == 'Windows':
            import win32api
            if driver != 'NONE' and driver != 'PSUADE_LOCAL':
                driver = win32api.GetShortPathName(driver)
            if optdriver != 'NONE' and optdriver != 'PSUADE_LOCAL':
                optdriver = win32api.GetShortPathName(optdriver)
            if ensoptdriver != 'NONE' and ensoptdriver != 'PSUADE_LOCAL':
                ensoptdriver = win32api.GetShortPathName(ensoptdriver)
            if auxdriver != 'NONE' and auxdriver != 'PSUADE_LOCAL':
                auxdriver = win32api.GetShortPathName(auxdriver)
        f.write('   driver = %s\n' % driver)
        f.write('   opt_driver = %s\n' % optdriver)
        f.write('   ensemble_opt_driver = %s\n' % ensoptdriver)
        f.write('   aux_opt_driver = %s\n' % auxdriver)
        f.write('   launch_interval = 0\n')
        f.write('END\n')

        # ... analysis ...
        f.write('ANALYSIS\n')
        if (nDerivs > 0):
            f.write('   optimization method = ouu_lbfgs\n')
        else:
            f.write('   optimization method = ouu\n')
        f.write('   optimization num_local_minima = 1\n')
        f.write('   optimization max_feval = 1000000\n')
        f.write('   optimization fmin = 0.0\n')
        f.write('   optimization tolerance = 1.000000e-06\n')
        f.write('   optimization num_fmin = %d\n' % num_fmin)
        f.write('   optimization print_level = 3\n')
        #f.write('   analyzer output_id = %d\n' % y)
        f.write('   analyzer output_id = 1\n')
        f.write('   opt_expert\n')
        f.write('   printlevel 0\n')
        f.write('END\n')

        f.write('END\n')
        f.close()

        return outfile