Removed old/unnecessary notebooks
This commit is contained in:
parent
c915a9e472
commit
12c879f016
13 changed files with 0 additions and 17732 deletions
|
@ -1,867 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matlab.engine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from shutil import copyfile"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/radu/Projects/Master-Project/Simulink\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cd \"../Simulink\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eng = matlab.engine.start_matlab()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<matlab.engine.futureresult.FutureResult at 0x7f7ab416c130>"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"eng.load_system(\"polydome\", background = True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Which experimental set to simulate:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"exp_id = 'Exp1'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Copy the corresponding WDB to the model input location:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"'../Data/input_WDB.mat'"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"copyfile(f\"../Data/Experimental_data_WDB/{exp_id}_WDB.mat\", \"../Data/input_WDB.mat\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>time</th>\n",
|
||||
" <th>timestamp</th>\n",
|
||||
" <th>zenith</th>\n",
|
||||
" <th>azimuth</th>\n",
|
||||
" <th>dni</th>\n",
|
||||
" <th>dhi</th>\n",
|
||||
" <th>OutsideTemp</th>\n",
|
||||
" <th>Tsky_rad</th>\n",
|
||||
" <th>relative_humidity</th>\n",
|
||||
" <th>precipitation</th>\n",
|
||||
" <th>cloud_index</th>\n",
|
||||
" <th>pressure</th>\n",
|
||||
" <th>wind_speed</th>\n",
|
||||
" <th>wind_direction</th>\n",
|
||||
" <th>aoi</th>\n",
|
||||
" <th>incidence_main</th>\n",
|
||||
" <th>incidence_second</th>\n",
|
||||
" <th>poa_direct</th>\n",
|
||||
" <th>poa_diffuse</th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>0</th>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>201706012000</td>\n",
|
||||
" <td>78.691622</td>\n",
|
||||
" <td>290.430819</td>\n",
|
||||
" <td>7.251337</td>\n",
|
||||
" <td>59.908644</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>50</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>0.5</td>\n",
|
||||
" <td>96300</td>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>78.691622</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>1.421911</td>\n",
|
||||
" <td>59.908644</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>1</th>\n",
|
||||
" <td>300</td>\n",
|
||||
" <td>201706012005</td>\n",
|
||||
" <td>79.489651</td>\n",
|
||||
" <td>291.279501</td>\n",
|
||||
" <td>7.672114</td>\n",
|
||||
" <td>56.537088</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>50</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>0.5</td>\n",
|
||||
" <td>96300</td>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>79.489651</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>1.399494</td>\n",
|
||||
" <td>56.537088</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2</th>\n",
|
||||
" <td>600</td>\n",
|
||||
" <td>201706012010</td>\n",
|
||||
" <td>80.282334</td>\n",
|
||||
" <td>292.130503</td>\n",
|
||||
" <td>8.423139</td>\n",
|
||||
" <td>53.492674</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>50</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>0.5</td>\n",
|
||||
" <td>96300</td>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>80.282334</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>1.421769</td>\n",
|
||||
" <td>53.492674</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>3</th>\n",
|
||||
" <td>900</td>\n",
|
||||
" <td>201706012015</td>\n",
|
||||
" <td>81.069332</td>\n",
|
||||
" <td>292.984123</td>\n",
|
||||
" <td>52.657244</td>\n",
|
||||
" <td>65.770239</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>50</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>0.5</td>\n",
|
||||
" <td>96300</td>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>81.069332</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>8.174467</td>\n",
|
||||
" <td>65.770239</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>4</th>\n",
|
||||
" <td>1200</td>\n",
|
||||
" <td>201706012020</td>\n",
|
||||
" <td>81.850261</td>\n",
|
||||
" <td>293.840653</td>\n",
|
||||
" <td>94.364403</td>\n",
|
||||
" <td>62.829177</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>16.0</td>\n",
|
||||
" <td>50</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>0.5</td>\n",
|
||||
" <td>96300</td>\n",
|
||||
" <td>0</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>81.850261</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>-9999</td>\n",
|
||||
" <td>13.377157</td>\n",
|
||||
" <td>62.829177</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" time timestamp zenith azimuth dni dhi \\\n",
|
||||
"0 0 201706012000 78.691622 290.430819 7.251337 59.908644 \n",
|
||||
"1 300 201706012005 79.489651 291.279501 7.672114 56.537088 \n",
|
||||
"2 600 201706012010 80.282334 292.130503 8.423139 53.492674 \n",
|
||||
"3 900 201706012015 81.069332 292.984123 52.657244 65.770239 \n",
|
||||
"4 1200 201706012020 81.850261 293.840653 94.364403 62.829177 \n",
|
||||
"\n",
|
||||
" OutsideTemp Tsky_rad relative_humidity precipitation cloud_index \\\n",
|
||||
"0 22.0 16.0 50 -9999 0.5 \n",
|
||||
"1 22.0 16.0 50 -9999 0.5 \n",
|
||||
"2 22.0 16.0 50 -9999 0.5 \n",
|
||||
"3 22.0 16.0 50 -9999 0.5 \n",
|
||||
"4 22.0 16.0 50 -9999 0.5 \n",
|
||||
"\n",
|
||||
" pressure wind_speed wind_direction aoi incidence_main \\\n",
|
||||
"0 96300 0 -9999 78.691622 -9999 \n",
|
||||
"1 96300 0 -9999 79.489651 -9999 \n",
|
||||
"2 96300 0 -9999 80.282334 -9999 \n",
|
||||
"3 96300 0 -9999 81.069332 -9999 \n",
|
||||
"4 96300 0 -9999 81.850261 -9999 \n",
|
||||
"\n",
|
||||
" incidence_second poa_direct poa_diffuse \n",
|
||||
"0 -9999 1.421911 59.908644 \n",
|
||||
"1 -9999 1.399494 56.537088 \n",
|
||||
"2 -9999 1.421769 53.492674 \n",
|
||||
"3 -9999 8.174467 65.770239 \n",
|
||||
"4 -9999 13.377157 62.829177 "
|
||||
]
|
||||
},
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df_wdb = pd.read_pickle(f\"../Data/Experimental_python/{exp_id}_WDB.pkl\")\n",
|
||||
"df_wdb.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>Power</th>\n",
|
||||
" <th>Setpoint</th>\n",
|
||||
" <th>OutsideTemp</th>\n",
|
||||
" <th>SupplyTemp</th>\n",
|
||||
" <th>InsideTemp</th>\n",
|
||||
" <th>SolRad</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>timestamp</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:00:00+02:00</th>\n",
|
||||
" <td>4325.034483</td>\n",
|
||||
" <td>23.5</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>24.5</td>\n",
|
||||
" <td>24.300000</td>\n",
|
||||
" <td>61.321333</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:05:00+02:00</th>\n",
|
||||
" <td>4287.000000</td>\n",
|
||||
" <td>23.5</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>15.5</td>\n",
|
||||
" <td>24.283333</td>\n",
|
||||
" <td>57.926100</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:10:00+02:00</th>\n",
|
||||
" <td>4319.766667</td>\n",
|
||||
" <td>23.5</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>15.2</td>\n",
|
||||
" <td>24.083333</td>\n",
|
||||
" <td>54.902033</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:15:00+02:00</th>\n",
|
||||
" <td>2893.344828</td>\n",
|
||||
" <td>23.5</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>14.9</td>\n",
|
||||
" <td>23.933333</td>\n",
|
||||
" <td>73.860700</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:20:00+02:00</th>\n",
|
||||
" <td>59.137931</td>\n",
|
||||
" <td>23.5</td>\n",
|
||||
" <td>22.0</td>\n",
|
||||
" <td>18.2</td>\n",
|
||||
" <td>23.666667</td>\n",
|
||||
" <td>76.042533</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" Power Setpoint OutsideTemp SupplyTemp \\\n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:00:00+02:00 4325.034483 23.5 22.0 24.5 \n",
|
||||
"2017-06-01 20:05:00+02:00 4287.000000 23.5 22.0 15.5 \n",
|
||||
"2017-06-01 20:10:00+02:00 4319.766667 23.5 22.0 15.2 \n",
|
||||
"2017-06-01 20:15:00+02:00 2893.344828 23.5 22.0 14.9 \n",
|
||||
"2017-06-01 20:20:00+02:00 59.137931 23.5 22.0 18.2 \n",
|
||||
"\n",
|
||||
" InsideTemp SolRad \n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:00:00+02:00 24.300000 61.321333 \n",
|
||||
"2017-06-01 20:05:00+02:00 24.283333 57.926100 \n",
|
||||
"2017-06-01 20:10:00+02:00 24.083333 54.902033 \n",
|
||||
"2017-06-01 20:15:00+02:00 23.933333 73.860700 \n",
|
||||
"2017-06-01 20:20:00+02:00 23.666667 76.042533 "
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"df = pd.read_pickle(f\"../Data/Experimental_python/{exp_id}_data.pkl\")\n",
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"Tsample = 300\n",
|
||||
"eng.workspace['Tsample'] = Tsample"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Experiment runtime: 1200\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"runtime = df_wdb['time'].iloc[-1] - Tsample\n",
|
||||
"runtime = 1200\n",
|
||||
"print(f\"Experiment runtime: {runtime}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Simulink"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Set the CARNOT simulation initial temperature `t0`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eng.workspace['t0'] = float(df['InsideTemp'][0])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Set the CARNOT simulation air exchange rate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"day_air_exchange_rate = 2.5\n",
|
||||
"night_air_exchange_rate = 2.5"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"air_exchange_rate = np.zeros((df_wdb.shape[0], 2))\n",
|
||||
"air_exchange_rate[:, 0] = df_wdb['time']\n",
|
||||
"air_exchange_rate[:, 1] = np.where(df['Power'] < 100, day_air_exchange_rate, night_air_exchange_rate)\n",
|
||||
"eng.workspace['air_exchange_rate'] = matlab.double(air_exchange_rate.tolist())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Set the CARNOT simulation input heat power"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Get the original electric power consumption"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"power = np.array([df_wdb['time'], df['Power']]).T"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Get the heating power by passing through a heating/cooling COP"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"COP_heating = -5.0\n",
|
||||
"COP_cooling = 5.0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"COP = np.where(df['Setpoint'] > df['InsideTemp'], COP_heating, -1*COP_cooling)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"power[:, 1] = COP * power[:, 1]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eng.workspace['power'] = matlab.double(power.tolist())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 38,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"eng.set_param('polydome', 'StopTime', str(runtime), nargout = 0)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 39,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "MatlabExecutionError",
|
||||
"evalue": "Error due to multiple causes.\n",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-39-49578c2c74ac>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mworkspace\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'result'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msim\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'polydome'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/matlabengine.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mFutureResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stdout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stderr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m return FutureResult(self._engine(), future, nargs, _stdout,\n\u001b[0m\u001b[1;32m 71\u001b[0m _stderr, feval=True).result()\n\u001b[1;32m 72\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/futureresult.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'TimeoutCannotBeNegative'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__future\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcancel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/fevalfuture.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'MatlabFunctionTimeout'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 82\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetFEvalResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_future\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nargout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_err\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 83\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_retrieved\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m: Error due to multiple causes.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"eng.workspace['result'] = eng.sim('polydome')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Interpret the resulting data as a python dataframe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dict_simulation = {}\n",
|
||||
"dict_simulation['values'] = np.asarray(eng.eval('result.SimulatedTemp.Data')).reshape(-1)\n",
|
||||
"dict_simulation['time'] = np.asarray(eng.eval('result.SimulatedTemp.Time')).reshape(-1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_simulation = pd.DataFrame(dict_simulation)\n",
|
||||
"#df_simulation['time'] = df_simulation['time'].astype(int)\n",
|
||||
"df_simulation.set_index('time', inplace = True, drop = True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_simulation['timestamp'] = df.index[0] + df_simulation.index.map(lambda x: pd.Timedelta(seconds = x))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_simulation = df_simulation.reset_index().set_index('timestamp')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"plt.plot(df_simulation.index, df_simulation['values'], label = 'Simulated Temperature')\n",
|
||||
"plt.plot(df.index, df['InsideTemp'], '--',label = 'Inside Temperature')\n",
|
||||
"plt.plot(df.index, df['OutsideTemp'], '-.', label = 'Outside Temperature')\n",
|
||||
"plt.title('Temperatures')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"plt.plot(df.index, df['Setpoint'], label = 'HVAC Controller Setpoint Temperature')\n",
|
||||
"plt.title('Temperatures')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Resample to 5/10/15 min by taking the mean when there are multiple points, and padding with zero order when data is missing"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_resampled_5 = df_simulation['values'].resample('5min').mean().pad()\n",
|
||||
"df_resampled_10 = df_simulation['values'].resample('10min').mean().pad()\n",
|
||||
"df_resampled_15 = df_simulation['values'].resample('15min').mean().pad()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize = (15, 5))\n",
|
||||
"plt.plot(df_simulation.index, df_simulation['values'], label = 'original')\n",
|
||||
"plt.plot(df_resampled_5.index, df_resampled_5, label = 'resampled 5min')\n",
|
||||
"plt.plot(df_resampled_10.index, df_resampled_10, label = 'resampled 10min')\n",
|
||||
"plt.plot(df_resampled_15.index, df_resampled_15, label = 'resampled 15min')\n",
|
||||
"plt.title('Resampling simulation data to different intervals')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Export the resampled data-set for further use"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_resampled_5.to_pickle(f\"../Data/CARNOT_output/{exp_id}_simulation_df.pkl\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df['Power'].plot(figsize = (20, 5))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Add the outputs to the experimental df and export the result: "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df['Heat'] = power[:, 1]\n",
|
||||
"df['SimulatedTemp'] = df_resampled_5"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Quick sanity check \n",
|
||||
"\n",
|
||||
"NOTE: This assumption is only correct for for experiments 1 and 2, but is currently applied everywhere.\n",
|
||||
"Keeping track of this only ensures that the GP can train on the same data that is fed to CARNOT"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(np.where(np.sign(df['Setpoint'] - df['InsideTemp']) == 1, 1, -3) * df['Power']).equals(df['Heat'])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df.to_pickle(f\"../Data/CARNOT_output/{exp_id}_full.pkl\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df['Heat'].plot(figsize = (25, 5))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.6"
|
||||
},
|
||||
"toc-autonumbering": false,
|
||||
"toc-showmarkdowntxt": false
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,986 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "16537827-7386-4163-b95f-2997fc020a2c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pathlib import Path\n",
|
||||
"from shutil import copyfile\n",
|
||||
"import pickle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "a517af1c-4204-45c9-aae4-865a2cb259e9",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Data manipulation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "62628e60-28c6-4a9a-8a81-22e5bfd74722",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "0fa93674-d4e7-4b36-ab3a-ebb11df12ed3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from sklearn.preprocessing import MinMaxScaler, RobustScaler\n",
|
||||
"from sklearn.exceptions import NotFittedError"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "acb33a41-06b9-4a1d-9ea7-6a2d87b1f4fb",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Plotting / Visualisation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "a42ae056-7511-4e17-b4ba-981fbcfaf922",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "0f8b23d5-e253-408b-907f-6f9990a98a96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%matplotlib inline\n",
|
||||
"plt.rcParams[\"figure.figsize\"] = (15, 6)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "90fdac33-eed4-4ab4-b2b1-de0f1f27727b",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Gaussian Process Regression"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "e629471f-350e-4af3-83df-377794a20a02",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import gpflow\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "b33dbca7-4419-4201-8d49-9fe3a5e16a33",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from gpflow.utilities import print_summary"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "fe8d81a8-f8ec-41d0-8a6c-77ee73619def",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"gpflow.config.set_default_summary_fmt(\"notebook\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0aba0df5-b0e3-4738-bb61-1dad869d1ea3",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Load previously exported data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "a8bf0b3f-1236-41c5-ba72-7e274a75d22f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"train_exps = ['Exp1', 'Exp3', 'Exp5', 'Exp6']\n",
|
||||
"test_exps = ['Exp2', 'Exp4', 'Exp7']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "3c8ad21b-8566-4d14-a13c-99e2dc3efc74",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"t_cols = ['time_h', 'time_m']\n",
|
||||
"w_cols = ['SolRad', 'OutsideTemp']\n",
|
||||
"u_cols = ['SimulatedHeat']\n",
|
||||
"y_cols = ['SimulatedTemp']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "ad00a16f-7cb6-44a6-90e7-ecc07003ffad",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"t_lags = 4\n",
|
||||
"w_lags = 1\n",
|
||||
"u_lags = 3\n",
|
||||
"y_lags = 3"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "e3225bc5-70b1-4b27-9893-8d0aae750bc9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#dict_cols = pickle.load(open(Path(\"dict_cols.pkl\"), 'rb'))\n",
|
||||
"#dict_cols"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "815913ad-73b3-407c-8e05-e3acfa65d73c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dict_cols = {\n",
|
||||
" 't': (t_lags, t_cols),\n",
|
||||
" 'w': (w_lags, w_cols),\n",
|
||||
" 'u': (u_lags, u_cols),\n",
|
||||
" 'y': (y_lags, y_cols)\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "b1574455-ece7-4a45-92a9-70410f73266e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dfs_train = pickle.load(open(Path(\"dfs_train.pkl\"), 'rb'))\n",
|
||||
"dfs_test = pickle.load(open(Path(\"dfs_test.pkl\"), 'rb'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"id": "0ebe637c-ad84-4393-9199-6ba088462132",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"scaler = pickle.load(open(Path(\"scaler.pkl\"), 'rb'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "136ba207-b4a7-427d-bdb2-0321110be9b6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_scaled_df(df, dict_cols, scaler):\n",
|
||||
" \n",
|
||||
" t_list = dict_cols['t'][1]\n",
|
||||
" w_list = dict_cols['w'][1]\n",
|
||||
" u_list = dict_cols['u'][1]\n",
|
||||
" y_list = dict_cols['y'][1]\n",
|
||||
" \n",
|
||||
" df_local = df[t_list + w_list + u_list + y_list]\n",
|
||||
" df_scaled = df_local.to_numpy()\n",
|
||||
" \n",
|
||||
" try:\n",
|
||||
" df_scaled = scaler.transform(df_scaled)\n",
|
||||
" except NotFittedError:\n",
|
||||
" df_scaled = scaler.fit_transform(df_scaled)\n",
|
||||
" \n",
|
||||
" df_scaled = pd.DataFrame(df_scaled, index = df_local.index, columns = df_local.columns)\n",
|
||||
" \n",
|
||||
" return df_scaled"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "d5130037-26f7-4f47-8382-81b12a131e2d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_train = pd.concat(dfs_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "a1d9c9f8-6689-40b4-8ae7-75395b343de2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"35185.23586737608"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"np.linalg.cond(df_train.to_numpy())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "b862da6c-9bca-4647-aeec-ee861c11293c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df_train_sc = get_scaled_df(df_train, dict_cols, scaler)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"id": "c0c18759-db54-4c46-8875-535b28d9ede6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"7.478002157732377"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"np.linalg.cond(df_train_sc.to_numpy())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "5b39e5c3-94ca-4765-aa6b-1426b226ca27",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dfs_train_sc = []\n",
|
||||
"dfs_test_sc = []\n",
|
||||
"for df in dfs_train:\n",
|
||||
" df_sc = get_scaled_df(df, dict_cols, scaler)\n",
|
||||
" dfs_train_sc.append(df_sc)\n",
|
||||
" \n",
|
||||
"for df in dfs_test:\n",
|
||||
" df_sc = get_scaled_df(df, dict_cols, scaler)\n",
|
||||
" dfs_test_sc.append(df_sc)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"id": "e8c97d70-2c97-4db8-9e2e-636086bd6271",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def data_to_gpr(df, dict_cols):\n",
|
||||
" \n",
|
||||
" t_list = dict_cols['t'][1]\n",
|
||||
" w_list = dict_cols['w'][1]\n",
|
||||
" u_list = dict_cols['u'][1]\n",
|
||||
" y_list = dict_cols['y'][1]\n",
|
||||
" \n",
|
||||
" df_gpr = df[t_list + w_list + u_list + y_list].copy()\n",
|
||||
" \n",
|
||||
" for lags, names in dict_cols.values():\n",
|
||||
" for name in names:\n",
|
||||
" col_idx = df_gpr.columns.get_loc(name)\n",
|
||||
" for lag in range(1, lags + 1):\n",
|
||||
" df_gpr.insert(col_idx + lag, f\"{name}_{lag}\", df_gpr.loc[:, name].shift(lag))\n",
|
||||
"\n",
|
||||
" df_gpr.dropna(inplace = True)\n",
|
||||
" \n",
|
||||
" return df_gpr"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "b351b32a-7018-4c90-8aa0-7c0be5461c2e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#dfs_gpr_train = pickle.load(open(Path(\"dfs_gpr_train.pkl\"), 'rb'))\n",
|
||||
"#dfs_gpr_test = pickle.load(open(Path(\"dfs_gpr_test.pkl\"), 'rb'))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "b0453fdf-58f3-49e6-bdea-ebe5940422e2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<div>\n",
|
||||
"<style scoped>\n",
|
||||
" .dataframe tbody tr th:only-of-type {\n",
|
||||
" vertical-align: middle;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe tbody tr th {\n",
|
||||
" vertical-align: top;\n",
|
||||
" }\n",
|
||||
"\n",
|
||||
" .dataframe thead th {\n",
|
||||
" text-align: right;\n",
|
||||
" }\n",
|
||||
"</style>\n",
|
||||
"<table border=\"1\" class=\"dataframe\">\n",
|
||||
" <thead>\n",
|
||||
" <tr style=\"text-align: right;\">\n",
|
||||
" <th></th>\n",
|
||||
" <th>time_h</th>\n",
|
||||
" <th>time_h_1</th>\n",
|
||||
" <th>time_h_2</th>\n",
|
||||
" <th>time_h_3</th>\n",
|
||||
" <th>time_h_4</th>\n",
|
||||
" <th>time_m</th>\n",
|
||||
" <th>time_m_1</th>\n",
|
||||
" <th>time_m_2</th>\n",
|
||||
" <th>time_m_3</th>\n",
|
||||
" <th>time_m_4</th>\n",
|
||||
" <th>...</th>\n",
|
||||
" <th>OutsideTemp</th>\n",
|
||||
" <th>OutsideTemp_1</th>\n",
|
||||
" <th>SimulatedHeat</th>\n",
|
||||
" <th>SimulatedHeat_1</th>\n",
|
||||
" <th>SimulatedHeat_2</th>\n",
|
||||
" <th>SimulatedHeat_3</th>\n",
|
||||
" <th>SimulatedTemp</th>\n",
|
||||
" <th>SimulatedTemp_1</th>\n",
|
||||
" <th>SimulatedTemp_2</th>\n",
|
||||
" <th>SimulatedTemp_3</th>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>timestamp</th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" <th></th>\n",
|
||||
" </tr>\n",
|
||||
" </thead>\n",
|
||||
" <tbody>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:20:00+02:00</th>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>-0.272727</td>\n",
|
||||
" <td>-0.454545</td>\n",
|
||||
" <td>-0.636364</td>\n",
|
||||
" <td>-0.818182</td>\n",
|
||||
" <td>-1.000000</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.179560</td>\n",
|
||||
" <td>-0.132679</td>\n",
|
||||
" <td>-0.094006</td>\n",
|
||||
" <td>-0.076890</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:25:00+02:00</th>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>-0.090909</td>\n",
|
||||
" <td>-0.272727</td>\n",
|
||||
" <td>-0.454545</td>\n",
|
||||
" <td>-0.636364</td>\n",
|
||||
" <td>-0.818182</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.208254</td>\n",
|
||||
" <td>-0.179560</td>\n",
|
||||
" <td>-0.132679</td>\n",
|
||||
" <td>-0.094006</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:30:00+02:00</th>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.090909</td>\n",
|
||||
" <td>-0.090909</td>\n",
|
||||
" <td>-0.272727</td>\n",
|
||||
" <td>-0.454545</td>\n",
|
||||
" <td>-0.636364</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.580115</td>\n",
|
||||
" <td>-0.222268</td>\n",
|
||||
" <td>-0.208254</td>\n",
|
||||
" <td>-0.179560</td>\n",
|
||||
" <td>-0.132679</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:35:00+02:00</th>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.272727</td>\n",
|
||||
" <td>0.090909</td>\n",
|
||||
" <td>-0.090909</td>\n",
|
||||
" <td>-0.272727</td>\n",
|
||||
" <td>-0.454545</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.234855</td>\n",
|
||||
" <td>-0.222268</td>\n",
|
||||
" <td>-0.208254</td>\n",
|
||||
" <td>-0.179560</td>\n",
|
||||
" </tr>\n",
|
||||
" <tr>\n",
|
||||
" <th>2017-06-01 20:40:00+02:00</th>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.73913</td>\n",
|
||||
" <td>0.454545</td>\n",
|
||||
" <td>0.272727</td>\n",
|
||||
" <td>0.090909</td>\n",
|
||||
" <td>-0.090909</td>\n",
|
||||
" <td>-0.272727</td>\n",
|
||||
" <td>...</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>0.058824</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.904139</td>\n",
|
||||
" <td>-0.247166</td>\n",
|
||||
" <td>-0.234855</td>\n",
|
||||
" <td>-0.222268</td>\n",
|
||||
" <td>-0.208254</td>\n",
|
||||
" </tr>\n",
|
||||
" </tbody>\n",
|
||||
"</table>\n",
|
||||
"<p>5 rows × 22 columns</p>\n",
|
||||
"</div>"
|
||||
],
|
||||
"text/plain": [
|
||||
" time_h time_h_1 time_h_2 time_h_3 time_h_4 \\\n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:20:00+02:00 0.73913 0.73913 0.73913 0.73913 0.73913 \n",
|
||||
"2017-06-01 20:25:00+02:00 0.73913 0.73913 0.73913 0.73913 0.73913 \n",
|
||||
"2017-06-01 20:30:00+02:00 0.73913 0.73913 0.73913 0.73913 0.73913 \n",
|
||||
"2017-06-01 20:35:00+02:00 0.73913 0.73913 0.73913 0.73913 0.73913 \n",
|
||||
"2017-06-01 20:40:00+02:00 0.73913 0.73913 0.73913 0.73913 0.73913 \n",
|
||||
"\n",
|
||||
" time_m time_m_1 time_m_2 time_m_3 time_m_4 \\\n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:20:00+02:00 -0.272727 -0.454545 -0.636364 -0.818182 -1.000000 \n",
|
||||
"2017-06-01 20:25:00+02:00 -0.090909 -0.272727 -0.454545 -0.636364 -0.818182 \n",
|
||||
"2017-06-01 20:30:00+02:00 0.090909 -0.090909 -0.272727 -0.454545 -0.636364 \n",
|
||||
"2017-06-01 20:35:00+02:00 0.272727 0.090909 -0.090909 -0.272727 -0.454545 \n",
|
||||
"2017-06-01 20:40:00+02:00 0.454545 0.272727 0.090909 -0.090909 -0.272727 \n",
|
||||
"\n",
|
||||
" ... OutsideTemp OutsideTemp_1 SimulatedHeat \\\n",
|
||||
"timestamp ... \n",
|
||||
"2017-06-01 20:20:00+02:00 ... 0.058824 0.058824 -0.904139 \n",
|
||||
"2017-06-01 20:25:00+02:00 ... 0.058824 0.058824 -0.904139 \n",
|
||||
"2017-06-01 20:30:00+02:00 ... 0.058824 0.058824 -0.904139 \n",
|
||||
"2017-06-01 20:35:00+02:00 ... 0.058824 0.058824 -0.904139 \n",
|
||||
"2017-06-01 20:40:00+02:00 ... 0.058824 0.058824 -0.904139 \n",
|
||||
"\n",
|
||||
" SimulatedHeat_1 SimulatedHeat_2 SimulatedHeat_3 \\\n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:20:00+02:00 -0.580115 -0.580115 -0.580115 \n",
|
||||
"2017-06-01 20:25:00+02:00 -0.904139 -0.580115 -0.580115 \n",
|
||||
"2017-06-01 20:30:00+02:00 -0.904139 -0.904139 -0.580115 \n",
|
||||
"2017-06-01 20:35:00+02:00 -0.904139 -0.904139 -0.904139 \n",
|
||||
"2017-06-01 20:40:00+02:00 -0.904139 -0.904139 -0.904139 \n",
|
||||
"\n",
|
||||
" SimulatedTemp SimulatedTemp_1 SimulatedTemp_2 \\\n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:20:00+02:00 -0.179560 -0.132679 -0.094006 \n",
|
||||
"2017-06-01 20:25:00+02:00 -0.208254 -0.179560 -0.132679 \n",
|
||||
"2017-06-01 20:30:00+02:00 -0.222268 -0.208254 -0.179560 \n",
|
||||
"2017-06-01 20:35:00+02:00 -0.234855 -0.222268 -0.208254 \n",
|
||||
"2017-06-01 20:40:00+02:00 -0.247166 -0.234855 -0.222268 \n",
|
||||
"\n",
|
||||
" SimulatedTemp_3 \n",
|
||||
"timestamp \n",
|
||||
"2017-06-01 20:20:00+02:00 -0.076890 \n",
|
||||
"2017-06-01 20:25:00+02:00 -0.094006 \n",
|
||||
"2017-06-01 20:30:00+02:00 -0.132679 \n",
|
||||
"2017-06-01 20:35:00+02:00 -0.179560 \n",
|
||||
"2017-06-01 20:40:00+02:00 -0.208254 \n",
|
||||
"\n",
|
||||
"[5 rows x 22 columns]"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"dfs_gpr_train = []\n",
|
||||
"for df_sc in dfs_train_sc:\n",
|
||||
" dfs_gpr_train.append(data_to_gpr(df_sc, dict_cols))\n",
|
||||
"df_gpr_train = pd.concat(dfs_gpr_train)\n",
|
||||
"df_gpr_train.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "8b866f02-084a-4ec1-b269-818949461530",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#df_gpr_train = pd.concat(dfs_gpr_train)\n",
|
||||
"\n",
|
||||
"df_input_train = df_gpr_train.drop(columns = dict_cols['u'][1] + dict_cols['y'][1])\n",
|
||||
"df_output_train = df_gpr_train[dict_cols['y'][1]]\n",
|
||||
"\n",
|
||||
"np_input_train = df_input_train.to_numpy()\n",
|
||||
"np_output_train = df_output_train.to_numpy().reshape(-1, 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"id": "288c99da-56c2-4216-819e-b3722f2a502c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"## Define Kernel"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"id": "dadb3f43-af78-4cb6-98c6-408559f62479",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"nb_dims = np_input_train.shape[1]\n",
|
||||
"rational_dims = np.arange(0, (dict_cols['t'][0] + 1) * len(dict_cols['t'][1]), 1)\n",
|
||||
"nb_rational_dims = len(rational_dims)\n",
|
||||
"squared_dims = np.arange(nb_rational_dims, nb_dims, 1)\n",
|
||||
"nb_squared_dims = len(squared_dims)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"id": "9bca461d-b4f9-4f9f-a6d6-65f7601fa020",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"rational dims: 10\n",
|
||||
"squared dims: 10\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(f\"rational dims: {nb_rational_dims}\")\n",
|
||||
"print(f\"squared dims: {nb_squared_dims}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"id": "dc8ef81f-271c-4e7a-8eb7-8981557cf7bb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"squared_l = [1e-4] * nb_squared_dims\n",
|
||||
"rational_l = [1e-7] * nb_rational_dims"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"id": "da302d5c-cb00-47e5-9cad-6e4de21eceb2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"squared_l = np.linspace(0.01, 1, nb_squared_dims)\n",
|
||||
"rational_l = np.linspace(0.01, 1, nb_rational_dims)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"id": "61caaaba-7527-4437-a7bf-62b5594c147c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"k0 = gpflow.kernels.SquaredExponential(lengthscales = squared_l, active_dims = squared_dims, variance = 2)\n",
|
||||
"k1 = gpflow.kernels.Constant()\n",
|
||||
"k2 = gpflow.kernels.RationalQuadratic(lengthscales = rational_l, active_dims = rational_dims, variance = 2)\n",
|
||||
"k3 = gpflow.kernels.Periodic(k2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"id": "87e3c3a2-4e67-4181-bd2d-22d684b7ce03",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<table>\n",
|
||||
"<thead>\n",
|
||||
"<tr><th>name </th><th>class </th><th>transform </th><th>prior </th><th>trainable </th><th>shape </th><th>dtype </th><th>value </th></tr>\n",
|
||||
"</thead>\n",
|
||||
"<tbody>\n",
|
||||
"<tr><td>Product.kernels[0].kernels[0].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>2.0 </td></tr>\n",
|
||||
"<tr><td>Product.kernels[0].kernels[0].lengthscales</td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>(10,) </td><td>float64</td><td>[0.01, 0.12, 0.23...</td></tr>\n",
|
||||
"<tr><td>Product.kernels[0].kernels[1].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>1.0 </td></tr>\n",
|
||||
"<tr><td>Product.kernels[1].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>2.0 </td></tr>\n",
|
||||
"<tr><td>Product.kernels[1].lengthscales </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>(10,) </td><td>float64</td><td>[0.01, 0.12, 0.23...</td></tr>\n",
|
||||
"<tr><td>Product.kernels[1].alpha </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>1.0 </td></tr>\n",
|
||||
"</tbody>\n",
|
||||
"</table>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"k = (k0 + k1) * k2\n",
|
||||
"print_summary(k)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "4af25a43-15c9-4543-af73-3c313b5fc7af",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Compile Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"id": "99da48d2-f04e-4ef8-a248-bc9b82fdbabd",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/html": [
|
||||
"<table>\n",
|
||||
"<thead>\n",
|
||||
"<tr><th>name </th><th>class </th><th>transform </th><th>prior </th><th>trainable </th><th>shape </th><th>dtype </th><th>value </th></tr>\n",
|
||||
"</thead>\n",
|
||||
"<tbody>\n",
|
||||
"<tr><td>GPR.kernel.kernels[0].kernels[0].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>2.0 </td></tr>\n",
|
||||
"<tr><td>GPR.kernel.kernels[0].kernels[0].lengthscales</td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>(10,) </td><td>float64</td><td>[0.01, 0.12, 0.23...</td></tr>\n",
|
||||
"<tr><td>GPR.kernel.kernels[0].kernels[1].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>1.0 </td></tr>\n",
|
||||
"<tr><td>GPR.kernel.kernels[1].variance </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>2.0 </td></tr>\n",
|
||||
"<tr><td>GPR.kernel.kernels[1].lengthscales </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>(10,) </td><td>float64</td><td>[0.01, 0.12, 0.23...</td></tr>\n",
|
||||
"<tr><td>GPR.kernel.kernels[1].alpha </td><td>Parameter</td><td>Softplus </td><td> </td><td>True </td><td>() </td><td>float64</td><td>1.0 </td></tr>\n",
|
||||
"<tr><td>GPR.likelihood.variance </td><td>Parameter</td><td>Softplus + Shift</td><td> </td><td>True </td><td>() </td><td>float64</td><td>1.0 </td></tr>\n",
|
||||
"</tbody>\n",
|
||||
"</table>"
|
||||
],
|
||||
"text/plain": [
|
||||
"<IPython.core.display.HTML object>"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"m = gpflow.models.GPR(\n",
|
||||
" data = (np_input_train, np_output_train), \n",
|
||||
" kernel = k, \n",
|
||||
" mean_function = None\n",
|
||||
" )\n",
|
||||
"print_summary(m)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "08f41235-12df-4e9c-bf63-e7a4390cf21a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Train Model"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"id": "9e5e2138-b342-4d44-8987-b7758b0daa6b",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"opt = gpflow.optimizers.Scipy()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"id": "fcfb78e0-2a3b-4a16-a621-6698abdaf3ab",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from datetime import datetime"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"id": "c449b100-acff-414d-a648-c4233879c253",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "InvalidArgumentError",
|
||||
"evalue": " Input matrix is not invertible.\n\t [[node gradient_tape/triangular_solve/MatrixTriangularSolve (defined at /usr/lib/python3.9/site-packages/gpflow/optimizers/scipy.py:173) ]] [Op:__inference__tf_eval_1118]\n\nErrors may have originated from an input operation.\nInput Source operations connected to node gradient_tape/triangular_solve/MatrixTriangularSolve:\n Cholesky (defined at /usr/lib/python3.9/site-packages/gpflow/models/gpr.py:87)\n\nFunction call stack:\n_tf_eval\n",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mInvalidArgumentError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-36-5f570574e163>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mstart_time\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdatetime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mopt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminimize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtraining_loss\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrainable_variables\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Finished fitting in {datetime.now() - start_time}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint_summary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/gpflow/optimizers/scipy.py\u001b[0m in \u001b[0;36mminimize\u001b[0;34m(self, closure, variables, method, step_callback, compile, **scipy_kwargs)\u001b[0m\n\u001b[1;32m 88\u001b[0m \u001b[0mscipy_kwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcallback\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 89\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 90\u001b[0;31m return scipy.optimize.minimize(\n\u001b[0m\u001b[1;32m 91\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial_params\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mjac\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mscipy_kwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 92\u001b[0m )\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/_minimize.py\u001b[0m in \u001b[0;36mminimize\u001b[0;34m(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)\u001b[0m\n\u001b[1;32m 617\u001b[0m **options)\n\u001b[1;32m 618\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mmeth\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'l-bfgs-b'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 619\u001b[0;31m return _minimize_lbfgsb(fun, x0, args, jac, bounds,\n\u001b[0m\u001b[1;32m 620\u001b[0m callback=callback, **options)\n\u001b[1;32m 621\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mmeth\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'tnc'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/lbfgsb.py\u001b[0m in \u001b[0;36m_minimize_lbfgsb\u001b[0;34m(fun, x0, args, jac, bounds, disp, maxcor, ftol, gtol, eps, maxfun, maxiter, iprint, callback, maxls, finite_diff_rel_step, **unknown_options)\u001b[0m\n\u001b[1;32m 358\u001b[0m \u001b[0;31m# until the completion of the current minimization iteration.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 359\u001b[0m \u001b[0;31m# Overwrite f and g:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 360\u001b[0;31m \u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc_and_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 361\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mtask_str\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartswith\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mb'NEW_X'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[0;31m# new iteration\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/_differentiable_functions.py\u001b[0m in \u001b[0;36mfun_and_grad\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 258\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 259\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_update_x_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 260\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_update_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 261\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_update_grad\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 262\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mg\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/_differentiable_functions.py\u001b[0m in \u001b[0;36m_update_fun\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 224\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_update_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 225\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_updated\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 226\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_update_fun_impl\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 227\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf_updated\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 228\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/_differentiable_functions.py\u001b[0m in \u001b[0;36mupdate_fun\u001b[0;34m()\u001b[0m\n\u001b[1;32m 131\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mupdate_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 133\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfun_wrapped\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 134\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_update_fun_impl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mupdate_fun\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/_differentiable_functions.py\u001b[0m in \u001b[0;36mfun_wrapped\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfun_wrapped\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 129\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnfev\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 130\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 131\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 132\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mupdate_fun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/optimize.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, x, *args)\u001b[0m\n\u001b[1;32m 72\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 73\u001b[0m \u001b[0;34m\"\"\" returns the the function value \"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 74\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_compute_if_needed\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 75\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 76\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/scipy/optimize/optimize.py\u001b[0m in \u001b[0;36m_compute_if_needed\u001b[0;34m(self, x, *args)\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_value\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjac\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 67\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0masarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 68\u001b[0;31m \u001b[0mfg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 69\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjac\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfg\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 70\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_value\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfg\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/gpflow/optimizers/scipy.py\u001b[0m in \u001b[0;36m_eval\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 111\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 112\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_eval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0mTuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mndarray\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 113\u001b[0;31m \u001b[0mloss\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgrad\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_tf_eval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconvert_to_tensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 114\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgrad\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumpy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat64\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 115\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 826\u001b[0m \u001b[0mtracing_count\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexperimental_get_tracing_count\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 827\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mtrace\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mTrace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mtm\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 828\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 829\u001b[0m \u001b[0mcompiler\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"xla\"\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_experimental_compile\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0;34m\"nonXla\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 830\u001b[0m \u001b[0mnew_tracing_count\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexperimental_get_tracing_count\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py\u001b[0m in \u001b[0;36m_call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m 860\u001b[0m \u001b[0;31m# In this case we have not created variables on the first call. So we can\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 861\u001b[0m \u001b[0;31m# run the first trace but we should fail if variables are created.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 862\u001b[0;31m \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_stateful_fn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 863\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_created_variables\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 864\u001b[0m raise ValueError(\"Creating variables on a non-first call to a function\"\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 2940\u001b[0m (graph_function,\n\u001b[1;32m 2941\u001b[0m filtered_flat_args) = self._maybe_define_function(args, kwargs)\n\u001b[0;32m-> 2942\u001b[0;31m return graph_function._call_flat(\n\u001b[0m\u001b[1;32m 2943\u001b[0m filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access\n\u001b[1;32m 2944\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36m_call_flat\u001b[0;34m(self, args, captured_inputs, cancellation_manager)\u001b[0m\n\u001b[1;32m 1916\u001b[0m and executing_eagerly):\n\u001b[1;32m 1917\u001b[0m \u001b[0;31m# No tape is watching; skip to running the function.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1918\u001b[0;31m return self._build_call_outputs(self._inference_function.call(\n\u001b[0m\u001b[1;32m 1919\u001b[0m ctx, args, cancellation_manager=cancellation_manager))\n\u001b[1;32m 1920\u001b[0m forward_backward = self._select_forward_and_backward_functions(\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/function.py\u001b[0m in \u001b[0;36mcall\u001b[0;34m(self, ctx, args, cancellation_manager)\u001b[0m\n\u001b[1;32m 553\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0m_InterpolateFunctionError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 554\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcancellation_manager\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 555\u001b[0;31m outputs = execute.execute(\n\u001b[0m\u001b[1;32m 556\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msignature\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 557\u001b[0m \u001b[0mnum_outputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_num_outputs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/tensorflow/python/eager/execute.py\u001b[0m in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[0mctx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mensure_initialized\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 59\u001b[0;31m tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,\n\u001b[0m\u001b[1;32m 60\u001b[0m inputs, attrs, num_outputs)\n\u001b[1;32m 61\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mcore\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_NotOkStatusException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mInvalidArgumentError\u001b[0m: Input matrix is not invertible.\n\t [[node gradient_tape/triangular_solve/MatrixTriangularSolve (defined at /usr/lib/python3.9/site-packages/gpflow/optimizers/scipy.py:173) ]] [Op:__inference__tf_eval_1118]\n\nErrors may have originated from an input operation.\nInput Source operations connected to node gradient_tape/triangular_solve/MatrixTriangularSolve:\n Cholesky (defined at /usr/lib/python3.9/site-packages/gpflow/models/gpr.py:87)\n\nFunction call stack:\n_tf_eval\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"start_time = datetime.now()\n",
|
||||
"opt.minimize(m.training_loss, m.trainable_variables)\n",
|
||||
"print(f\"Finished fitting in {datetime.now() - start_time}\")\n",
|
||||
"print_summary(m)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "7dd49280-bb3f-4903-a339-b7225a56ae16",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Evaluate performance on training data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1bf833f2-83d7-4af4-8d4c-d3a663c18b53",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"nb_plts = len(dfs_gpr_train)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "063ef841-2708-421c-9793-f878ac8a6e1e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize = (20, 20))\n",
|
||||
"\n",
|
||||
"for idx, df_iter in enumerate(dfs_gpr_train):\n",
|
||||
" plt.subplot(nb_plts, 1, idx + 1)\n",
|
||||
" df_input_iter = df_iter.drop(columns = dict_cols['y'][1] + dict_cols['u'][1])\n",
|
||||
" df_output_iter = df_iter[dict_cols['y'][1]]\n",
|
||||
" np_input_iter = df_input_iter.to_numpy()\n",
|
||||
" np_output_iter = df_output_iter.to_numpy().reshape(-1, 1)\n",
|
||||
" \n",
|
||||
" mean, var = m.predict_f(np_input_iter)\n",
|
||||
" \n",
|
||||
" plt.plot(df_iter.index, np_output_iter[:, :], label = 'Measured data')\n",
|
||||
" plt.plot(df_iter.index, mean[:, :], label = 'Gaussian Process Prediction')\n",
|
||||
" plt.fill_between(\n",
|
||||
" df_iter.index, \n",
|
||||
" mean[:, 0] - 1.96 * np.sqrt(var[:, 0]),\n",
|
||||
" mean[:, 0] + 1.96 * np.sqrt(var[:, 0]),\n",
|
||||
" alpha = 0.2\n",
|
||||
" )\n",
|
||||
" plt.title(f\"Model Performance on training data: {train_exps[idx]}\")\n",
|
||||
" plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1d7d8ca2-1c2d-42dc-a1d2-1f4af11c9d19",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Evaluate performance on test data"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "fc73ac1e-024b-4c62-9f2a-4a61b776ecb5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dfs_gpr_test = []\n",
|
||||
"for df_sc in dfs_test_sc:\n",
|
||||
" dfs_gpr_test.append(data_to_gpr(df_sc, dict_cols))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "095c0b27-3faa-4225-a91c-3d18f2a033f0",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize = (20, 20))\n",
|
||||
"\n",
|
||||
"for idx, df_iter in enumerate(dfs_gpr_test):\n",
|
||||
" plt.subplot(nb_plts, 1, idx + 1)\n",
|
||||
" df_input_iter = df_iter.drop(columns = dict_cols['y'][1] + dict_cols['u'][1])\n",
|
||||
" df_output_iter = df_iter[dict_cols['y'][1]]\n",
|
||||
" np_input_iter = df_input_iter.to_numpy()\n",
|
||||
" np_output_iter = df_output_iter.to_numpy().reshape(-1, 1)\n",
|
||||
" \n",
|
||||
" mean, var = m.predict_f(np_input_iter)\n",
|
||||
" \n",
|
||||
" plt.plot(df_iter.index, np_output_iter[:, :], label = 'Measured data')\n",
|
||||
" plt.plot(df_iter.index, mean[:, :], label = 'Gaussian Process Prediction')\n",
|
||||
" plt.fill_between(\n",
|
||||
" df_iter.index, \n",
|
||||
" mean[:, 0] - 1.96 * np.sqrt(var[:, 0]),\n",
|
||||
" mean[:, 0] + 1.96 * np.sqrt(var[:, 0]),\n",
|
||||
" alpha = 0.2\n",
|
||||
" )\n",
|
||||
" plt.title(f\"Model Performance on test data: {test_exps[idx]}\")\n",
|
||||
" plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -1,304 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "73e650e9-ebe9-4ea4-9eb6-8ea978cf25fa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/radu/Projects/Master-Project/Simulink\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"cd ../Simulink"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "4fc6f955-8ece-4796-9479-275515ecfef3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matlab.engine"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "c13d599d-9b97-4178-876c-f40418cae602",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 23,
|
||||
"id": "df5fd736-702f-420e-94e1-885e4c1d3f0c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class SimulinkPlant:\n",
|
||||
" def __init__(self,modelName = 'plant'):\n",
|
||||
" \n",
|
||||
" self.modelName = modelName #The name of the Simulink Model (To be placed in the same directory as the Python Code) \n",
|
||||
" #Logging the variables\n",
|
||||
" self.yHist = 0 \n",
|
||||
" self.tHist = 0 \n",
|
||||
" \n",
|
||||
" def setControlAction(self,u):\n",
|
||||
" #Helper Function to set value of control action\n",
|
||||
" self.eng.set_param('{}/u'.format(self.modelName),'value',str(u),nargout=0)\n",
|
||||
" \n",
|
||||
" def getHistory(self):\n",
|
||||
" #Helper Function to get Plant Output and Time History\n",
|
||||
" return self.eng.eval('out.y'),self.eng.workspace['tout']\n",
|
||||
" \n",
|
||||
" def connectToMatlab(self):\n",
|
||||
" \n",
|
||||
" print(\"Starting matlab\")\n",
|
||||
" self.eng = matlab.engine.start_matlab()\n",
|
||||
" \n",
|
||||
" print(\"Connected to Matlab\")\n",
|
||||
" \n",
|
||||
" #Load the model\n",
|
||||
" self.eng.eval(\"model = '{}'\".format(self.modelName),nargout=0)\n",
|
||||
" self.eng.eval(\"load_system(model)\",nargout=0)\n",
|
||||
" \n",
|
||||
" #Initialize Control Action to 0\n",
|
||||
" self.setControlAction(0)\n",
|
||||
" print(\"Initialized Model\")\n",
|
||||
" \n",
|
||||
" #Start Simulation and then Instantly pause\n",
|
||||
" self.eng.set_param(self.modelName,'SimulationCommand','start','SimulationCommand','pause',nargout=0)\n",
|
||||
" self.yHist,self.tHist = self.getHistory()\n",
|
||||
" \n",
|
||||
" def connectController(self,controller):\n",
|
||||
" self.controller = controller\n",
|
||||
" self.controller.initialize()\n",
|
||||
" \n",
|
||||
" def simulate(self):\n",
|
||||
" # Control Loop\n",
|
||||
" while(self.eng.get_param(self.modelName,'SimulationStatus') != ('stopped' or 'terminating')):\n",
|
||||
" \n",
|
||||
" #Generate the Control action based on the past outputs\n",
|
||||
" u = self.controller.getControlEffort(self.yHist,self.tHist)\n",
|
||||
" \n",
|
||||
" #Set that Control Action\n",
|
||||
" self.setControlAction(u)\n",
|
||||
" \n",
|
||||
" #Pause the Simulation for each timestep\n",
|
||||
" self.eng.set_param(self.modelName,'SimulationCommand','continue','SimulationCommand','pause',nargout=0)\n",
|
||||
" \n",
|
||||
" self.yHist,self.tHist = self.getHistory()\n",
|
||||
" \n",
|
||||
" def disconnect(self):\n",
|
||||
" self.eng.set_param(self.modelName,'SimulationCommand','stop',nargout=0)\n",
|
||||
" self.eng.quit()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"id": "5822db6e-2a7a-41b9-b684-b05804c74406",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class PIController:\n",
|
||||
" def __init__(self):\n",
|
||||
" \n",
|
||||
" #Maintain a History of Variables\n",
|
||||
" self.yHist = []\n",
|
||||
" self.tHist = []\n",
|
||||
" self.uHist = []\n",
|
||||
" self.eSum = 0\n",
|
||||
" \n",
|
||||
" def initialize(self):\n",
|
||||
" \n",
|
||||
" #Initialize the graph\n",
|
||||
" self.fig, = plt.plot(self.tHist,self.yHist)\n",
|
||||
" plt.xlim(0,10)\n",
|
||||
" plt.ylim(0,20)\n",
|
||||
" plt.ylabel(\"Plant Output\")\n",
|
||||
" plt.xlabel(\"Time(s)\")\n",
|
||||
" plt.title(\"Plant Response\")\n",
|
||||
" \n",
|
||||
" def updateGraph(self):\n",
|
||||
" # Update the Graph\n",
|
||||
" self.fig.set_xdata(self.tHist)\n",
|
||||
" self.fig.set_ydata(self.yHist)\n",
|
||||
" plt.pause(0.1)\n",
|
||||
" plt.show()\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" def getControlEffort(self,yHist,tHist):\n",
|
||||
" \n",
|
||||
" # Returns control action based on past outputs\n",
|
||||
" \n",
|
||||
" self.yHist = yHist\n",
|
||||
" self.tHist = tHist\n",
|
||||
" \n",
|
||||
" self.updateGraph()\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" if(type(self.yHist) == float):\n",
|
||||
" y = self.yHist\n",
|
||||
" else:\n",
|
||||
" y = self.yHist[-1][0]\n",
|
||||
" \n",
|
||||
" # Set Point is 10\n",
|
||||
" e = 10-y\n",
|
||||
" \n",
|
||||
" self.eSum += e\n",
|
||||
" u = 1*e + 0.001*self.eSum\n",
|
||||
" \n",
|
||||
" print(y)\n",
|
||||
" self.uHist.append(u)\n",
|
||||
" return u"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"id": "bac4afc7-4cfd-4d58-9563-d60a022c02a6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Starting matlab\n",
|
||||
"Connected to Matlab\n",
|
||||
"Initialized Model\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "MatlabExecutionError",
|
||||
"evalue": "\n File /opt/tmw/MATLAB-r2020b/toolbox/matlab/external/engines/engine_api/+matlab/+internal/+engine/getVariable.m, line 27, in getVariable\nUndefined variable 'tout'.\n",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-25-f6fdbeb35af3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mplant\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mSimulinkPlant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodelName\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"polydome_python\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m#Establishes a Connection\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mplant\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconnectToMatlab\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#Instantiates the controller\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m<ipython-input-23-3a9dacf5f0ab>\u001b[0m in \u001b[0;36mconnectToMatlab\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[0;31m#Start Simulation and then Instantly pause\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_param\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmodelName\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'SimulationCommand'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'start'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'SimulationCommand'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'pause'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mnargout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0myHist\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtHist\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetHistory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 35\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mconnectController\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcontroller\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m<ipython-input-23-3a9dacf5f0ab>\u001b[0m in \u001b[0;36mgetHistory\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 13\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mgetHistory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;31m#Helper Function to get Plant Output and Time History\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 15\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'out.y'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mworkspace\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'tout'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 16\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 17\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mconnectToMatlab\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/matlabengine.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, attr)\u001b[0m\n\u001b[1;32m 118\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__validate_identity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mattr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 119\u001b[0m \u001b[0m_method\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mMatlabFunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"matlab.internal.engine.getVariable\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 120\u001b[0;31m \u001b[0mfuture\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_method\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mattr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 121\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 122\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/matlabengine.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mFutureResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stdout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stderr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m return FutureResult(self._engine(), future, nargs, _stdout,\n\u001b[0m\u001b[1;32m 71\u001b[0m _stderr, feval=True).result()\n\u001b[1;32m 72\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/futureresult.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'TimeoutCannotBeNegative'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__future\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcancel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/fevalfuture.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'MatlabFunctionTimeout'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 82\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetFEvalResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_future\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nargout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_err\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 83\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_retrieved\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m: \n File /opt/tmw/MATLAB-r2020b/toolbox/matlab/external/engines/engine_api/+matlab/+internal/+engine/getVariable.m, line 27, in getVariable\nUndefined variable 'tout'.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plant = SimulinkPlant(modelName=\"polydome_python\")\n",
|
||||
"#Establishes a Connection\n",
|
||||
"plant.connectToMatlab()\n",
|
||||
"\n",
|
||||
"#Instantiates the controller\n",
|
||||
"controller = PIController()\n",
|
||||
"plant.connectController(controller)\n",
|
||||
"\n",
|
||||
"#Control Loop\n",
|
||||
"plant.simulate()\n",
|
||||
"\n",
|
||||
"#Closes Connection to MATLAB\n",
|
||||
"plant.disconnect()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"id": "478001a0-5e15-405e-9b91-1761ef137f4a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "MatlabExecutionError",
|
||||
"evalue": "Attempt to execute SCRIPT ans as a function:\n/opt/tmw/MATLAB-r2020b/toolbox/matlab/lang/ans.m\n",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-28-03a3d9f7ade5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mplant\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meng\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ans'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/matlabengine.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mFutureResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfuture\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnargs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stdout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_stderr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfeval\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 70\u001b[0;31m return FutureResult(self._engine(), future, nargs, _stdout,\n\u001b[0m\u001b[1;32m 71\u001b[0m _stderr, feval=True).result()\n\u001b[1;32m 72\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/futureresult.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'TimeoutCannotBeNegative'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 66\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 67\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__future\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mresult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 68\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 69\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcancel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;32m/usr/lib/python3.9/site-packages/matlab/engine/fevalfuture.py\u001b[0m in \u001b[0;36mresult\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m 80\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTimeoutError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetMessage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'MatlabFunctionTimeout'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 81\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 82\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpythonengine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetFEvalResult\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_future\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_nargout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_out\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_err\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 83\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_retrieved\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 84\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_result\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mMatlabExecutionError\u001b[0m: Attempt to execute SCRIPT ans as a function:\n/opt/tmw/MATLAB-r2020b/toolbox/matlab/lang/ans.m\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plant.eng.eval('ans')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"id": "dfaa3294-60bf-4086-9ec4-e9794f8adf0d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plant.eng.workspace['y'] = 14"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"id": "d9f4ca79-f401-43ef-bdcb-deb8152aeaad",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"14"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plant.eng.workspace['y']"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "d6034609-2a8c-4b90-a8bd-8502f5cf6e76",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,105 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "69938cfc-3188-4458-bdc1-87d87d9e9380",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from pathlib import Path\n",
|
||||
"import pickle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "e2214d3e-f7cf-4e6d-840e-70f35f7ce311",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "0a0c4438-e0cc-44db-b04a-7f0f9c01f14a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import gpflow\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "77493d54-0a7c-474d-b20a-00b94755a984",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import casadi as cs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "ff16257e-e721-4798-beb9-d7927cb658f8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import matplotlib.pyplot as plt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "0688ff0a-d6e8-45a8-b03a-f8e875d987b2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "SyntaxError",
|
||||
"evalue": "invalid syntax (<ipython-input-7-004fb9cd5ffa>, line 1)",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-7-004fb9cd5ffa>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m from ./../server_implementation/controllers import GP_MPCcontroller\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from ./../server_implementation/controllers import GP_MPCcontroller"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e44ecaf3-b9ac-473c-a088-551648d69322",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 336 KiB |
|
@ -1,236 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "25d01f84-863b-4238-964b-e24425bb8107",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import numpy as np\n",
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "242c5eb2-2531-481d-8c21-0b0178ac2db5",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"np.random.seed(1793)\n",
|
||||
"tf.random.set_seed(1793)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "49fa101c-15a9-47be-b254-6cbde6dd04e9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import trieste\n",
|
||||
"from trieste.utils.objectives import branin\n",
|
||||
"\n",
|
||||
"search_space = trieste.space.Box([0, 0], [1, 1])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "050686af-8c93-4996-b852-c925d7cc7e98",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def new_branin(data):\n",
|
||||
" print(data.shape)\n",
|
||||
" return branin(data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "cd44cddb-5090-4e4f-b651-f7d57f145b70",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(5, 2)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from trieste.acquisition.rule import OBJECTIVE\n",
|
||||
"\n",
|
||||
"observer = trieste.utils.objectives.mk_observer(new_branin, OBJECTIVE)\n",
|
||||
"\n",
|
||||
"num_initial_points = 5\n",
|
||||
"initial_query_points = search_space.sample(num_initial_points)\n",
|
||||
"initial_data = observer(initial_query_points)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "696dfd67-0e37-42f0-af5b-64937bffc2aa",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Building a model\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import gpflow\n",
|
||||
"\n",
|
||||
"def build_model(data):\n",
|
||||
" print(\"Building a model\")\n",
|
||||
" variance = tf.math.reduce_variance(data.observations)\n",
|
||||
" kernel = gpflow.kernels.Matern52(variance=variance, lengthscales=[0.2, 0.2])\n",
|
||||
" gpr = gpflow.models.GPR(data.astuple(), kernel, noise_variance=1e-5)\n",
|
||||
" gpflow.set_trainable(gpr.likelihood, False)\n",
|
||||
"\n",
|
||||
" return {OBJECTIVE: {\n",
|
||||
" \"model\": gpr,\n",
|
||||
" \"optimizer\": gpflow.optimizers.Scipy(),\n",
|
||||
" \"optimizer_args\": {\n",
|
||||
" \"minimize_args\": {\"options\": dict(maxiter=100)},\n",
|
||||
" },\n",
|
||||
" }}\n",
|
||||
"\n",
|
||||
"model = build_model(initial_data[OBJECTIVE])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "5fa0dabe-9930-45bc-86cf-843e1dbe10a1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"(1, 2)\n",
|
||||
"Optimization completed without errors\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"bo = trieste.bayesian_optimizer.BayesianOptimizer(observer, search_space)\n",
|
||||
"\n",
|
||||
"result = bo.optimize(25, initial_data, model)\n",
|
||||
"dataset = result.try_get_final_datasets()[OBJECTIVE]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "7d1bcebb-8ac5-4b60-9589-922c2a5d5429",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"query point: [0.54162649 0.14935401]\n",
|
||||
"observation: [0.40162417]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"query_points = dataset.query_points.numpy()\n",
|
||||
"observations = dataset.observations.numpy()\n",
|
||||
"\n",
|
||||
"arg_min_idx = tf.squeeze(tf.argmin(observations, axis=0))\n",
|
||||
"\n",
|
||||
"print(f\"query point: {query_points[arg_min_idx, :]}\")\n",
|
||||
"print(f\"observation: {observations[arg_min_idx, :]}\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"id": "297fc50a-f755-40f1-8c9f-aaed111951cf",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<tf.Tensor: shape=(5, 1), dtype=float64, numpy=\n",
|
||||
"array([[ 52.74206955],\n",
|
||||
" [ 20.91833473],\n",
|
||||
" [110.23393455],\n",
|
||||
" [225.05283879],\n",
|
||||
" [ 52.49971937]])>"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"initial_data[OBJECTIVE].observations"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "624720e0-5600-4a74-a2ce-6d09e248c069",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.9.6"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Binary file not shown.
Before Width: | Height: | Size: 70 KiB |
Loading…
Add table
Add a link
Reference in a new issue