Beispiel #1
0
def main():
    # Specify number of poitns to be used in the training set
    # Validation data can be provided optionally
    ndata= 50
    nval = 500
    lb = [-15,-15]
    ub = [15,15]
    x = np.random.uniform(lb,ub,(ndata,2))
    xval = np.random.uniform(lb,ub,(nval,2))
    z = [0]*ndata
    zval = [0]*nval
    # specify simulator as examples.sixcamel
    sim = examples.ackley
    for i in range(ndata):
        z[i]=sim(x[i][0],x[i][1])
    for i in range(nval):
        zval[i] = sim(xval[i][0],xval[i][1])

    # Use alamopy's python function wrapper to avoid using ALAMO's I/O format
    almsim = alamopy.wrapwriter(sim)
    # Call alamo through the alamopy wrapper
    res = alamopy.doalamo(x,z,xval=xval,zval=zval,almname='ackley',monomialpower=(1,2,3,4,5,6),expfcns=1,multi2power=(1,2),expandoutput=True)
    # Calculate confidence intervals
    conf_inv = alamopy.almconfidence(res)

    print('Model: {}'.format(res['model']))
    print('Confidence Intervals : {}'.format(conf_inv['conf_inv']))
def _main():
    # Specify number of poitns to be used in the training set
    # Validation data can be provided optionally
    ndata = 10
    x = np.random.uniform([-2, -1], [2, 1], (ndata, 2))
    z = [0] * ndata
    # specify simulator as examples.sixcamel
    sim = examples.sixcamel
    for i in range(ndata):
        z[i] = sim(x[i][0], x[i][1])

    # Use alamopy's python function wrapper to avoid using ALAMO's I/O format
    almsim = alamopy.wrapwriter(sim)
    # Call alamo through the alamopy wrapper
    res = alamopy.doalamo(
        x,
        z,
        almname="cam6",
        monomialpower=(1, 2, 3, 4, 5, 6),
        multi2power=(1, 2),
        simulator=almsim,
        expandoutput=True,
        maxiter=20,
        cvfun=True,
    )
    #    print res
    print("Model: {}".format(res["model"]))
Beispiel #3
0
def main():
    # Specify number of poitns to be used in the training set
    # Validation data can be provided optionally
    ndata = 20
    nval = 100
    x = np.random.uniform([-5, 0], [10, 15], (ndata, 2))
    xval = np.random.uniform([-5, 0], [10, 15], (nval, 2))
    z = [0] * ndata
    zval = [0] * nval
    # specify simulator as examples.sixcamel
    sim = examples.branin
    for i in range(ndata):
        z[i] = sim(x[i][0], x[i][1])
    for i in range(nval):
        zval[i] = sim(xval[i][0], xval[i][1])

    # Use alamopy's python function wrapper to avoid using ALAMO's I/O format
    # Call alamo through the alamopy wrapper
    res = alamopy.doalamo(x,
                          z,
                          xval=xval,
                          zval=zval,
                          almname='branin',
                          xmin=(-5, 0),
                          xmax=(10, 15),
                          monomialpower=(1, 2, 3, 4),
                          expfcns=1,
                          multi2power=(1, 2),
                          expandoutput=True)
    conf_inv = alamopy.almconfidence(res)
    print('Model: {}'.format(res['model']))
    print('Confidence Intervals : {}'.format(conf_inv['conf_inv']))
Beispiel #4
0
def test_single_input_CV():

    if not alamopy.has_alamo():
        return False

    # Specify number of points to be used in the training set
    # Validation data can be provided optionally
    ndata = 10
    x = np.random.uniform([-2, -1], [2, 1], (ndata, 2))
    z = np.zeros((ndata, 1))
    # specify simulator as examples.sixcamel
    sim = sixcamel
    for i in range(ndata):
        z[i, 0] = sim(x[i][0], x[i][1])
        # z[i,1] = sim(x[i][0], x[i][1])

    # # Use alamopy's python function wrapper to avoid using ALAMO's I/O format
    almsim = alamopy.wrapwriter(sim)

    # NON GENERIC
    alamo_settings = {
        'almname': "cam6",
        'monomialpower': (1, 2, 3, 4, 5, 6),
        'multi2power': (1, 2),
        'simulator': almsim,
        'maxiter': 20,
        'cvfun': True
    }

    res = alamopy.doalamo(x, z, lmo=3)
Beispiel #5
0
 def _run_alamo(self):
     kwargs = self._kwargs.copy()
     if self._xv is not None and self._zv is not None:
         kwargs['xval'], kwargs['zval'] = self._xv, self._zv
     # run alamo to generate model
     try:
         results = alamopy.doalamo(self._x, self._z, **kwargs)
     except alamopy.AlamoError as err:
         raise errors.AlamoError(str(err))
     # return the generated model
     return results
Beispiel #6
0
def allcard(xdata, zdata, xval, zval, **kwargs):
    # enumerate all model cardinalities via ccmiqp and
    # use validation/cross-validaiton to determine
    from idaes.surrogate import alamopy
    # PYLINT-TODO-FIX alamopy.writethis.writethis doesn't seem to exist
    # pylint: disable=import-error
    from alamopy.writethis import writethis
    # pylint: enable=import-error
    import numpy as np
    import math
    # import sympy
    import random
    from random import shuffle
    # from scipy.optimize import curve_fit #2.7
    # from scipy.optimize import minimize #2.7
    from scipy.linalg import lstsq  # 2.7
    import os
    # from alamopy import almlsq as almfun
    # from alamopy import almlsqjac as almjac
    import time
    # from sympy.parsing.sympy_parser import parse_expr
    import sys

    random.seed(100)

    # kwargs=opdict
    trans = list([
        'linfcns', 'expfcns', 'logfcns', 'sinfcns', 'cosfcns', 'monomialpower',
        'multi2power', 'multi3power', 'ratiopower'
    ])
    et = list(['multi2power', 'multi3power', 'ratiopower'])
    # datacc = {}
    ins = list(['cvfold'])
    for opt in ins:
        if (opt in kwargs.keys()):
            cvfold = kwargs['cvfold']
    ntrans = 0
    ndata = np.shape(xdata)[0]
    ninputs = np.shape(xdata)[1]

    for t in trans:
        if t in kwargs.keys():
            if (type(kwargs[t]) == list()):
                nt = len(kwargs[t])
            else:
                nt = 1
            if t not in et:
                ntrans = ntrans + ninputs * nt
            else:
                ntrans = ntrans + math.factorial(ninputs) * nt
        else:
            kwargs[t] = 0
    # mseold = 0.0
    rmseold = 0.0
    startt = time.time()
    oldres = {}
    oldp = ()
    ntrans = min(ntrans, 1000)

    # split training and validationd ata before looping through cc
    tlist = list([])
    vlist = list([])
    # ndlist = range(ndata)
    if (cvfold == 'loo'):
        for i in range(ndata):
            vlist.append(np.asarray([i]))
            temp = [x for x in range(ndata) if x != i]
            tlist.append(np.asarray(temp))
    else:
        temp = range(ndata)
        shuffle(temp)
        # tlist = np.asarray(temp)
        if (cvfold == 'valset'):
            tlist = np.asarray(temp)[0:int(0.7 * ndata)]
            vlist = np.asarray(temp)[int(0.7 * ndata):-1]
        else:
            vlist = np.array_split(np.asarray(temp), int(cvfold))
            tlist = [1] * int(cvfold)
            for v in range(len(vlist)):
                tlist[v] = range(ndata)
                for this in vlist[v]:
                    tlist[v].remove(this)

    for ccon in list(range(1, ntrans + 1)):
        # reload(alamopy)
        # try:
        #     del cvalsim, almsim
        # except Exception:
        #     pass

        # res = alamopy.doalamo(xdata,zdata,kwargs.values())
        if (cvfold != 'valset'):
            res = alamopy.doalamo(xdata,
                                  zdata,
                                  xval=xval,
                                  zval=zval,
                                  linfcns=kwargs['linfcns'],
                                  expfcns=kwargs['expfcns'],
                                  logfcns=kwargs['logfcns'],
                                  sinfcns=kwargs['sinfcns'],
                                  cosfcns=kwargs['cosfcns'],
                                  monomialpower=kwargs['monomialpower'],
                                  multi2power=kwargs['multi2power'],
                                  multi3power=kwargs['multi3power'],
                                  ratiopower=kwargs['ratiopower'],
                                  sigma=kwargs['sigma'],
                                  xlabels=kwargs['xlabels'],
                                  zlabels=kwargs['zlabels'],
                                  modeler=6,
                                  convpen=0,
                                  maxterms=ccon,
                                  almname=kwargs['almname'],
                                  expandoutput=kwargs['expandoutput'],
                                  xmax=kwargs['xmax'],
                                  xmin=kwargs['xmin'],
                                  savescratch=kwargs['savescratch'])
        else:
            res = alamopy.doalamo(xdata[tlist, :],
                                  zdata[tlist],
                                  xval=xdata[vlist, :],
                                  zval=zdata[vlist],
                                  linfcns=kwargs['linfcns'],
                                  expfcns=kwargs['expfcns'],
                                  logfcns=kwargs['logfcns'],
                                  sinfcns=kwargs['sinfcns'],
                                  cosfcns=kwargs['cosfcns'],
                                  monomialpower=kwargs['monomialpower'],
                                  multi2power=kwargs['multi2power'],
                                  multi3power=kwargs['multi3power'],
                                  ratiopower=kwargs['ratiopower'],
                                  sigma=kwargs['sigma'],
                                  xlabels=kwargs['xlabels'],
                                  zlabels=kwargs['zlabels'],
                                  modeler=6,
                                  convpen=0,
                                  maxterms=ccon,
                                  almname=kwargs['almname'],
                                  expandoutput=kwargs['expandoutput'],
                                  xmax=kwargs['xmax'],
                                  xmin=kwargs['xmin'],
                                  savescratch=kwargs['savescratch'])
        for fn in ['cvalsim.py', 'cvalsim.pyc', 'almsim.py', 'almsim.pyc']:
            try:
                os.remove(fn)
            except Exception:
                pass
        os.system('cp ' + kwargs['almname'].split('.')[0] + 'cv.py cvalsim.py')
        os.system('cp ' + kwargs['almname'].split('.')[0] + 'alm.py almsim.py')

        if (ccon == 1):
            if (ndata < 10):  # not enough data to do cross validation
                writethis('Not enough data to facilitate cross validation')
                endt = time.time()
                res['totaltime'] = endt - startt
                return res

        # import cvalsim
        import almsim
        xalm = alamopy.almfeatmat(xdata, ccon)
        # Lets do the cross validatione error
        mseval = 0.0
        rmse = {}
        if (cvfold == 'valset'):
            resid = np.sum((zdata[vlist] - almsim.f(xdata[vlist, :]))**2)
            mseval = resid / float(len(vlist))
            params = 'ALM params used for valset'
            rmse = {}
            rmse['val'] = res['rmseval']
            rmse['train'] = res['rmse']
        else:
            mseval = 0.0
            # track = 0
            for tl, vl in zip(tlist, vlist):
                # initb = [ 0.0 for x in range(ccon)]
                xd, xmax, xmin = alamopy.mapminmax(xalm)
                zd, zmax, zmin = alamopy.mapminmax(zdata)
                fitres = lstsq(xd[tl], zd[tl])
                params = fitres[0]
                resid = zd[vl] - np.matmul(xd[vl, :], params[:])
                resid = alamopy.remapminmax(resid, zmax, zmin)
                resid = sum(resid**2)
                if (cvfold == 'loo'):
                    mseval = mseval + resid
                else:
                    mseval = mseval + resid / float(len(vl))
            mseval = mseval / float(len(vlist))
            rmse['val'] = np.sqrt(mseval)
            rmse['train'] = 'not'
        if (ccon > 1):
            if (float(rmse['val']) >= float(rmseold)):
                sys.stdout.write('              Problem name   : ' +
                                 kwargs['almname'] + '\n')
                sys.stdout.write('  rMSEval : ' + str(rmse['val']) +
                                 '    MSEold : ' + str(rmseold) + '\n')
                if (cvfold == 'valset'):
                    sys.stdout.write('   Ntrain : ' + str(len(tlist)) +
                                     '    Nval : ' + str(len(vlist)) + '\n')
                else:
                    sys.stdout.write('   Ntrain : ' + str(len(tlist[0])) +
                                     '    Nval : ' + str(len(vlist[0])) + '\n')
                sys.stdout.write('       optimal model size is : ' +
                                 str(ccon - 1) + '\n')
                sys.stdout.write('    optimal coefficients are : ' +
                                 str(oldp) + '\n')
                os.remove(kwargs['almname'].split('.')[0] + 'alm.py')
                os.system('cp ' + 'oldalmsim.sv ' +
                          kwargs['almname'].split('.')[0] + 'alm.py')
                endt = time.time()
                oldres['totaltime'] = endt - startt
                return oldres
            elif (ccon == ntrans):
                endt = time.time()
                sys.stdout.write('optimal model size is :' + str(ccon) + '\n')
                res['totaltime'] = endt - startt
                return res
            else:
                # mseold = mseval
                oldres = res
        else:
            # mseold = float(mseval)
            rmseold = float(rmse['val'])
            oldres = res
            oldp = params
        # keep track of alm model of old iteratoin
        try:
            os.remove('oldalmsim.sv')
        except Exception:
            pass
        os.system('mv almsim.py oldalmsim.sv')