-
Notifications
You must be signed in to change notification settings - Fork 0
Other Activities
A summary / outline of other, additional or complementary, activities is presented bellow.
The simulation part of the Kriging Metamodel - NOT the calibration of the model - used in the Green Driving tool is implemented in Python. The whole project is contained here, including the various inputs / outputs of the tool and the target values originating from the runs of the original tool in Matlab, while the main function which is included in the krig.py file is presented here.
def KrigingCo2mpas(x, ysc, ssc, theta, beta, gamma, s):
"""
x is an array/dataframe with the following parameters:
'Capacity', 'Mass', 'Driving', 'Transmission', 'Traction', 'SS', 'BERS',
'MechLoad', 'AR', 'RR', 'Slope', 'T', 'P_C', 'AvgV', 'InitT'
# Check inKrig_MGT.csv file
"""
x = (x - ssc.ix[:,0].values) / ssc.ix[:,1].values
sLL = x*beta.ix[1:, 0].values
scalLossL = sLL.sum(axis=1) + beta.ix[0, 0]
xtiled = np.tile(x, len(s)).reshape((-1, len(x.columns)))
stiled = np.tile(s, (len(x), 1))
corrFunFict = (stiled - xtiled)**2
corrFunFict *= - theta.ix[:,0].values
corrFun = np.exp(corrFunFict.sum(axis=1))
corrFun = corrFun.reshape(len(x), len(s))
sLR = corrFun*gamma.ix[:,0].values
scalLossR = sLR.sum(axis=1)
scalLoss= scalLossR + scalLossL
return ysc.ix[0,0] + ysc.ix[1,0]*scalLoss
The conjuction of the previous function with MySQL and PHP, for the scopes of the Green Driving Tool, is implemented in the commit 84f8f4e, as described bellow.
The input array is sent from PHP to Python via:
$k = array(0=>1500,1=>1000,2=>1,3=>1,4=>1,5=>1,6=>1,7=>0,8=>0,9=>1.15,10=>0.56,11=>23,12=>0.1,13=>40,14=>23,15=>0);
$kriging_input_values = array($k, $k, $k)
$kriging_input_values = escapeshellarg(json_encode($kriging_input_values));
$result = shell_exec('C:\Users\mainelo\Downloads\WinPython-64bit-3.5.1.3\python-3.5.1.amd64\python C:\Users\mainelo\Downloads\WinPython-64bit-3.5.1.3\test\krig.py "'.$kriging_input_values.'" 2>&1');
Then Python calls the MySQL database on the server side, where the Kriging parameters are stored, runs the KrigingCo2mpas
function and prints/sents back the result.
import pymysql
conn = pymysql.connect(host='localhost', database='testgr_dri', user='root', password='XXXXXXXXXX')
cur = conn.cursor()
sh = {'ysc': 1, 'ssc': 2, 'theta': 1, 'beta': 1, 'gamma': 1, 's': 15}
X = pd.DataFrame(x) # The x input must be multiple segments ONLY: TO TEST
res = []
for table in tables:
ysc = run_query(table, 'ysc', sh['ysc'], cur)
ssc = run_query(table, 'ssc', sh['ssc'], cur)
theta = run_query(table, 'theta', sh['theta'], cur)
beta = run_query(table, 'beta', sh['beta'], cur)
gamma = run_query(table, 'gamma', sh['gamma'], cur)
s = run_query(table, 's', sh['s'], cur)
r = KrigingCo2mpas(X, ysc, ssc, theta, beta, gamma, s)
res.append(r)
cur.close()
conn.close()
def run_query(table, p, i, cur):
q = "SELECT * FROM %s_%s"%(table, p)
cur.execute(q)
d = {}
for row in cur:
d[row[0]] = row[1:]
return pd.DataFrame.from_dict(d, orient='index').reset_index(drop=True).astype('float')
The raw code can be found here (test php side) and here (python side / krig.py).
Overall, as compared with running the kriging calculation from PHP, an improvement of ~9x in the calculation time has been achieved. It can be further optimized.
A script has been prepared to test various temperature model functions. The target is to test both the "physical" temperature model used in the Parametric CO2MPAS Model on the pool of cars that have been measured, and other models before implementing them in the official version of CO2MPAS.
The draft script is accessible here, while some preliminary results are given bellow.
Initial Implementation
The initial physical model is used, amended with a linear effect of the square of the temperature in the delta Q (Red is the simulation, black is the target points (CO2MPAS output)). Commit: 2490c2e
PID Type of Function
A simplified PID controller is implemented as a function for the temperature's calculation (Red is the simulation, black is the target points (measurement)). Commit: 02933b6