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 = 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']))
def test_basic(**kwargs): x = [[ 0.17361977, -0.44326123], [-0.30192964, 0.68955226],[-1.98112458, -0.75686176],[0.68299634, 0.65170551],[-1.45317364, 0.15018666],[ 1.56528782, -0.58159576] ,[-1.25868712, -0.78324622],[-1.12121003, 0.95724757] ,[ 1.2467326, -0.65611797],[ 1.26489899, -0.45185251]] z = [-0.58978634828943055, -0.85834512885363479, 4.0241154669754113, 0.91057814668811488, 1.9147616212616931, 0.29103827202206878, 2.4290896722960778, 0.99199475534877579, 0.59688699266830847, 1.167850366995701] xival = [[5,5], [2,2]] zival = [5,2] doalamo(x, z) #, xval=xival, zval=zival, mock=True) doalamo(x, z, mock=True, xlabels=["T", "V"], zlabels= ["P"]) doalamo(x, z, xval=xival, zval=zival, mock=True, lmo=5) 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 = wrapwriter(sim) # Call alamo through the alamopy wrapper res = doalamo(x,z,almname='cam6',monomialpower=(1,2,3,4,5,6), multi2power=(1,2), simulator=almsim, expandoutput=True,maxiter=20, mock=True)#,cvfun=True) conf_inv = almconfidence(res) res = doalamo(x, z, xval = xival, zval = zival) #, expandoutput=True) #, mock=True, loo=True) #, xval=xival, zval=zival, mock=True, loo=True) conf_inv = almconfidence(res) print('Model: {}'.format(res['model'])) print('Confidence Intervals : {}'.format(conf_inv['conf_inv'])) almplot(res) try: deletefile("logscratch") deletfile("../cam6") deletefile("../cam6alm.py") except: pass
def build_alamo_model(self, xdata, zdata, xlabels = None, zlabels='e', **kwargs): # results=alamopy.doalamo(xdata,zdata,xlabels=xlbls, zlabels='expr', monomialpower='1,2,3', multi3power='1,2,3', multi2power='1,2,3', ratiopower='1 2', logfcns=1, expfcns=1, savescratch=1, expandoutput=1); #logfcns=1, expfcns=1, ratiopower='1 2 ', multi3power='1 2', multi2power='1 2 ' # results=alamopy.doalamo(xdata,zdata,xlabels=xlbls, zlabels='e', monomialpower='1 2 3', multi3power='1 2 3', multi2power='1 2 3', savescratch=1, expandoutput=1); #logfcns=1, expfcns=1, ratiopower='1 2 ', multi3power='1 2', multi2power='1 2 ' # results=alamopy.doalamo(xdata,zdata, almname='SteamEnthalpy.alm', almopt='steamcustom.alm', monomialpower='1 2 3', multi3power='1 2 3', multi2power='1 2 3', savescratch=1, expandoutput=1); #logfcns=1, expfcns=1, ratiopower='1 2 ', multi3power='1 2', multi2power='1 2 ' if 'almopt' in kwargs: results=alamopy.doalamo(xdata,zdata, xlabels=xlabels, zlabels='e', almname=self.almname, almopt=kwargs.get("almopt"), monomialpower='1 2 3', multi3power='1 2 3', multi2power='1 2 3', savescratch=1, expandoutput=1); #logfcns=1, expfcns=1, ratiopower='1 2 ', multi3power='1 2', multi2power='1 2 ' else: results=alamopy.doalamo(xdata,zdata, almname=self.almname, monomialpower='1 2 3', multi3power='1 2 3', multi2power='1 2 3', savescratch=1, expandoutput=1); #logfcns=1, expfcns=1, ratiopower='1 2 ', multi3power='1 2', multi2power='1 2 ' print "Model function: %s" % results['model']; print "Sum of squared error = %s" % results['ssr'] print "R^2 = %s" % results['R2']; res = alamopy.almconfidence(results, xdata, zdata); print "Confidence Interval =%s" % results['conf_inv'] alamopy.almplot(res) print results['model'].replace("^","**") alm_model = results['model'] self.models.append(results);
def writemodel(aterm, ccon): import numpy as np import os, sys import alamopy import ripe from alamopy import deletefile data, rspace = ripe.data, ripe.rspace #First an activity term matrix is defined to make assignment easier later if ('clc' in data['otherdata']): aterm = np.zeros([ data['otherdata']['npc'], data['otherdata']['ntimedata'], data['rxnet']['mechanisms'] ]) for i in range(data['otherdata']['npc']): for j in range(data['otherdata']['ntimedata']): #t is an index on the current mechanism t = 0 if ('clc' in data['otherdata']): for m in range(len(data['otherdata']['clc'])): mech = data['otherdata']['clc'][m] # Check for CLC mechanisms if (mech == 'c1'): aterm[i, j, t] = 1.0 - data['concdata']['X'][i][ j] #random nucleation if (mech == 'c2'): aterm[ i, j, t] = 2.0 / 3.0 * data['concdata']['X'][i][j]**( 2.0 / 3.0 - 3.0 / 2.0 ) #Power law of oer 2/3 if (mech == 'c3'): aterm[i, j, t] = 2.0 * data['concdata']['X'][i][j]**( 2.0 - 1.0 / 2.0 ) # Power law model of order 2 if (mech == 'c4'): aterm[i, j, t] = 4.0 * data['concdata']['X'][i][j]**( 4.0 - 1.0 / 4.0 ) # power law model of order 4 if (mech == 'c5'): aterm[i, j, t] = 3.0 * data['concdata']['X'][i][j]**( 3.0 - 1.0 / 3.0 ) # power law model of order 4 if (mech == 'c6'): aterm[i, j, t] = 1.0 # power law model of order 1 if (mech == 'c7'): aterm[i, j, t] = 2.0 * ( 1.0 - data['concdata']['X'][i][j] ) * np.power( -np.log(1.0 - data['concdata']['X'][i][j]), 1.5 ) #nucleation mode - Avrami-Erofeev eq of order 2 if (mech == 'c8'): aterm[i, j, t] = 3.0 * ( 1.0 - data['concdata']['X'][i][j]) * np.power( -np.log(1.0 - data['concdata']['X'][i][j]), 3.0 - (1.0 / 3.0)) # '' order 3 if (mech == 'c9'): aterm[i, j, t] = 0.5 * ( 1.0 - data['concdata']['X'][i][j] ) * (-np.log(1.0 - data['concdata']['X'][i][j]))**( 0.5 - 1.0 / 0.5) # '' order 0.5 if (mech == 'c10'): aterm[i, j, t] = 1.5 * ( 1.0 - data['concdata']['X'][i][j]) * np.power( -np.log(1.0 - data['concdata']['X'][i][j]), 1.5 - (1.0 / 1.5)) # '' order 1.5 if (mech == 'c11'): aterm[i, j, t] = 4.0 * ( 1.0 - data['concdata']['X'][i][j] ) * (-np.log(1.0 - data['concdata']['X'][i][j]))**( 4.0 - 1.0 / 4.0) # '' order 4 if (mech == 'c12'): aterm[i, j, t] = data['concdata']['X'][i][j] * ( 1.0 - data['concdata']['X'][i][j] ) #Prout tompkins # diffusion models if (mech == 'c13'): aterm[i, j, t] = 3.0 * ( 1.0 - data['concdata']['X'][i][j] )**(1.0 / 3.0) * ( 2.0 * ((1.0 - data['concdata']['X'][i][j])** (-1.0 / 3.0) - 1.0) )**( -1 ) #Jander equation (3d difsusion spherical sym) if (mech == 'c14'): aterm[i, j, t] = 3.0 / 2.0 * ( 1.0 + data['concdata']['X'][i][j])**(2.0 / 3.0) * ( ((1.0 + data['concdata']['X'][i][j])** (-1.0 / 3.0) - 1.0))**(-1) #antijander if (mech == 'c15'): aterm[i, j, t] = 1.0 / ( -np.log(1.0 - data['concdata']['X'][i][j]) ) # valensi if (mech == 'c16'): aterm[i, j, t] = 1.0 / ( 2.0 * data['concdata']['X'][i][j]) # parabolic if (mech == 'c17'): aterm[i, j, t] = (3.0 / 2.0) * (( (1.0 - data['concdata']['X'][i][j])** (-1.0 / 3.0) - 1.0))**( -1 ) #Ginstling-Brounstein eq 3d diff cylindrical if (mech == 'c18'): aterm[i, j, t] = 3.0 / 2.0 * ( 1.0 - data['concdata']['X'][i][j])**(4.0 / 3.0) * ( ((1.0 - data['concdata']['X'][i][j])** (-1.0 / 3.0) - 1.0))**( -1) #zhuralev, lesohkhin, tempelman if (mech == 'c19'): aterm[i, j, t] = (1.0 - data['concdata']['X'][i][j])**(2.0 / 3.0) # if(mech=='c19'): aterm[i,j,t]= (0.2)**(0.535) * 1.5 * (0.112)**(1.0/1.5)*((1.0)-data['concdata']['X'][i][j])*(-np.log(1.0-data['concdata']['X'][i][j]))**(1.0-1.0/1.5) # if(mech=='c20'): aterm[i,j,t]= (0.3)**(0.312) * 2.76 * (11.24)**(1.0/2.76)*((0.18 *1.479)-data['concdata']['X'][i][j])*(-np.log(1.0-data['concdata']['X'][i][j])/(0.18 *1.479))**(1.0-1.0/2.76) # if(mech=='c21'): aterm[i,j,t]= (0.3)**(2.13) * 1.63 * (28.4)**(1.0/1.63)*((0.135*1.479)-data['concdata']['X'][i][j])*(-np.log(1.0-data['concdata']['X'][i][j])/(0.135*1.479))**(1.0-1.0/1.63) t = t + 1 else: for m in range(data['rxnet']['mechanisms']): tempa = 1 for s in data['otherdata']['slabels']: if data['rxnet'][i, s] < 0: tempa = tempa * float(data['concdata'][s][i][j])**float( abs(data['rxnet'][i, s])) aterm[i, j, m] = tempa for q in range(data['rxnet']['mechanisms']): if (np.isinf(aterm[i, j, q])): aterm[i, j, q] = 0 if (np.isnan(aterm[i, j, q])): aterm[i, j, q] = 0 if ('time' not in data['procdata']): for q in range(data['rxnet']['nrx']): temp = 1 for s in data['otherdata']['slabels']: if (data['rxnet'][q, s] < 0): temp = temp * data['concdata'][s][i]**abs( data['rxnet'][q, s]) aterm[i, q] = temp # check for isothermal, single species observations if len(data['otherdata']['slabels']) == 1: if 'T' not in data['otherdata']['pclabels']: sys.stdout = open(os.devnull, 'w') r = alamopy.doalamo(np.asarray(aterm[0, :, :]), np.asarray(data['ratedata'][str( data['otherdata']['slabels'][0])][0, :]), linfcns=1, constant=0, maxterms=1, expandoutput=1) r = alamopy.almconfidence(r) sys.stdout = sys.__stdout__ selected = r['model'].split(' * ')[-1] selected = selected[1:] coeff = r['model'].split(' * ')[0] coeff = coeff.split(' = ')[-1] cfi = r['conf_inv'][0].split(' : ')[-1] slist = [ 'Selected model is : ' + rspace['clcnames'][int(selected) - 1], 'Selected model form : ' + rspace['clcforms'][int(selected) - 1], 'Estimated rate constant : ' + cfi ] sys.stdout.write(slist[0] + '\n') sys.stdout.write(slist[1] + '\n') sys.stdout.write(slist[2] + '\n') with open('output.txt', 'w') as o: o.write(slist[0] + '\n') o.write(slist[1] + '\n') o.write(slist[2] + '\n') deletefile('*logscratch*', 'tempalm.py', 'sv.alm', 'tmpscratch') return [aterm, 0] else: writeminlp(aterm, ccon) return [aterm, 0]