Skip to content

Commit

Permalink
update_simulation_setup
Browse files Browse the repository at this point in the history
  • Loading branch information
YaolinGe committed Sep 5, 2023
1 parent 115acdb commit a9e121c
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 192 deletions.
37 changes: 24 additions & 13 deletions Publication/src/Agents/AgentMyopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from usr_func.checkfolder import checkfolder
from scipy.stats import norm
from sklearn.metrics import mean_squared_error
from scipy.stats import wasserstein_distance
import numpy as np
import os
from time import time
Expand Down Expand Up @@ -53,10 +54,6 @@ def __init__(self, neighbour_distance: float = 120, weight_eibv: float = 1., wei
self.debug = debug
self.counter = 0

# s5: set up monitoring metrics
self.ibv = []
self.vr = []
self.rmse = []
self.threshold = self.grf.get_threshold()

def run(self, num_steps: int = 5) -> None:
Expand All @@ -65,18 +62,31 @@ def run(self, num_steps: int = 5) -> None:
"""
# start logging the data.
self.trajectory = np.empty([0, 2])
N = self.grf.grid.shape[0]

# self.ibv = np.zeros([num_steps, ])
# self.vr = np.zeros([num_steps, ])
# self.rmse = np.zeros([num_steps, ])
# self.wd = np.zeros([num_steps, ])
self.mu_data = np.zeros([num_steps, N])
self.sigma_data = np.zeros([num_steps, N])
self.mu_truth_data = np.zeros([num_steps, N])

t0 = time()
for i in range(num_steps):
print(" STEP: {} / {}".format(i, num_steps),
" Percentage: ", i / num_steps * 100, "%",
" Time remaining: ", (time() - t0) * (num_steps - i) / 60, " min")
t0 = time()
# s0: update simulation data
ibv, vr, rmse = self.update_metrics()
self.ibv.append(ibv)
self.vr.append(vr)
self.rmse.append(rmse)
# s0: update simulation data and save the updated data.
mu, sigma_diag, mu_truth = self.update_metrics()
# self.ibv[i] = ibv
# self.vr[i] = vr
# self.rmse[i] = rmse
# self.wd[i] = wd
self.mu_data[i, :] = mu.flatten()
self.sigma_data[i, :] = sigma_diag.flatten()
self.mu_truth_data[i, :] = mu_truth.flatten()

if self.debug:
self.ap.plot_agent()
Expand All @@ -97,11 +107,12 @@ def run(self, num_steps: int = 5) -> None:
def update_metrics(self) -> tuple:
mu = self.grf.get_mu()
sigma_diag = np.diag(self.grf.get_covariance_matrix())
ibv = self.get_ibv(self.threshold, mu, sigma_diag)
# ibv = self.get_ibv(self.threshold, mu, sigma_diag)
mu_truth = self.auv.ctd.get_salinity_at_dt_loc(dt=0, loc=self.grf.grid)
rmse = mean_squared_error(mu_truth, mu, squared=False)
vr = np.sum(sigma_diag)
return ibv, vr, rmse
# rmse = mean_squared_error(mu_truth, mu, squared=False)
# vr = np.sum(sigma_diag)
# wd = wasserstein_distance(mu_truth, mu.flatten())
return mu, sigma_diag, mu_truth

def get_ibv(self, threshold, mu, sigma_diag) -> np.ndarray:
""" !!! Be careful with dimensions, it can lead to serious problems.
Expand Down
24 changes: 12 additions & 12 deletions Publication/src/Agents/AgentRRTStar.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ def __init__(self, neighbour_distance: float = 120, weight_eibv: float = 1., wei
self.ap = AgentPlotRRTStar(self, figpath)
self.counter = 0

# s5: set up monitoring metrics
self.ibv = []
self.vr = []
self.rmse = []
self.threshold = self.grf.get_threshold()

def run(self, num_steps: int = 5) -> None:
Expand All @@ -69,6 +65,10 @@ def run(self, num_steps: int = 5) -> None:
"""
# start logging the data.
self.trajectory = np.empty([0, 2])
N = self.grf.grid.shape[0]
self.mu_data = np.zeros([num_steps, N])
self.sigma_data = np.zeros([num_steps, N])
self.mu_truth_data = np.zeros([num_steps, N])

t0 = time()
for i in range(num_steps):
Expand All @@ -77,10 +77,10 @@ def run(self, num_steps: int = 5) -> None:
" Time remaining: ", (time() - t0) * (num_steps - i) / 60, " min")
t0 = time()
# s0: update simulation data
ibv, vr, rmse = self.update_metrics()
self.ibv.append(ibv)
self.vr.append(vr)
self.rmse.append(rmse)
mu, sigma_diag, mu_truth = self.update_metrics()
self.mu_data[i, :] = mu.flatten()
self.sigma_data[i, :] = sigma_diag.flatten()
self.mu_truth_data[i, :] = mu_truth.flatten()

if self.debug:
self.ap.plot_agent()
Expand All @@ -105,11 +105,11 @@ def run(self, num_steps: int = 5) -> None:
def update_metrics(self) -> tuple:
mu = self.grf.get_mu()
sigma_diag = np.diag(self.grf.get_covariance_matrix())
ibv = self.get_ibv(self.threshold, mu, sigma_diag)
# ibv = self.get_ibv(self.threshold, mu, sigma_diag)
mu_truth = self.auv.ctd.get_salinity_at_dt_loc(dt=0, loc=self.grf.grid) # dt=0 is cuz it is updated before
rmse = mean_squared_error(mu_truth, mu, squared=False)
vr = np.sum(sigma_diag)
return ibv, vr, rmse
# rmse = mean_squared_error(mu_truth, mu, squared=False)
# vr = np.sum(sigma_diag)
return mu, sigma_diag, mu_truth

def get_ibv(self, threshold, mu, sigma_diag) -> np.ndarray:
""" !!! Be careful with dimensions, it can lead to serious problems.
Expand Down
2 changes: 1 addition & 1 deletion Publication/src/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def __init__(self) -> None:
self.__budget_mode = False

""" Default simulation parameter seteup. """
self.__num_steps = 240 # number of steps.
self.__num_steps = 10 # number of steps.
self.__num_replicates = 3 # number of replicates
self.__num_cores = 3 # number of cores to use

Expand Down
11 changes: 7 additions & 4 deletions Publication/src/Simulators/Simulator_Myopic2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ def run_all(self, num_steps: int = 10) -> None:

def extract_data_for_agent(self, agent: 'Agent' = None) -> tuple:
traj = agent.trajectory
ibv = np.array(agent.ibv).reshape(-1, 1)
vr = np.array(agent.vr).reshape(-1, 1)
rmse = np.array(agent.rmse).reshape(-1, 1)
return traj, ibv, vr, rmse
mu_data = agent.mu_data
sigma_data = agent.sigma_data
mu_truth_data = agent.mu_truth_data
# ibv = np.array(agent.ibv).reshape(-1, 1)
# vr = np.array(agent.vr).reshape(-1, 1)
# rmse = np.array(agent.rmse).reshape(-1, 1)
return traj, mu_data, sigma_data, mu_truth_data

11 changes: 7 additions & 4 deletions Publication/src/Simulators/Simulator_RRTStar.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ def run_all(self, num_steps: int = 10) -> None:

def extract_data_for_agent(self, agent: 'Agent' = None) -> tuple:
traj = agent.trajectory
ibv = np.array(agent.ibv).reshape(-1, 1)
vr = np.array(agent.vr).reshape(-1, 1)
rmse = np.array(agent.rmse).reshape(-1, 1)
return traj, ibv, vr, rmse
# ibv = np.array(agent.ibv).reshape(-1, 1)
# vr = np.array(agent.vr).reshape(-1, 1)
# rmse = np.array(agent.rmse).reshape(-1, 1)
mu_data = agent.mu_data
sigma_data = agent.sigma_data
mu_truth_data = agent.mu_truth_data
return traj, mu_data, sigma_data, mu_truth_data

158 changes: 69 additions & 89 deletions Publication/src/cv_sim_result.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "f094b695",
"metadata": {},
"source": [
"# Simulation result analysis \n",
"# Paper Simulation result analysis \n",
"\n",
"- All replicate results are saved separately, check if they share some commonality\n"
]
Expand All @@ -28,91 +28,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"['sigma_10_nuggget_04', 'R_000', 'R_038', 'R_007', 'R_031', '.DS_Store', 'R_009', 'R_036', 'R_062', 'R_065', 'R_053', 'R_054', 'R_008', 'R_037', 'R_030', 'R_039', 'R_006', 'R_001', 'R_055', 'R_052', 'R_064', 'R_063', 'R_041', 'R_079', 'R_046', 'R_070', 'R_048', 'R_077', 'R_023', 'R_024', 'R_012', 'R_015', 'R_049', 'R_076', 'R_071', 'R_078', 'R_047', 'R_040', 'R_014', 'R_013', 'R_025', 'R_022', 'R_066', 'R_059', 'R_061', 'R_057', 'R_068', 'R_050', 'R_004', 'R_003', 'R_035', 'R_032', 'R_051', 'R_056', 'R_069', 'R_060', 'R_067', 'R_058', 'R_033', 'R_034', 'R_002', 'R_005', 'R_027', 'R_018', 'R_020', 'R_016', 'R_029', 'R_011', 'R_045', 'R_042', 'R_074', 'R_073', 'R_010', 'R_017', 'R_028', 'R_021', 'R_026', 'R_019', 'R_072', 'R_075', 'R_043', 'R_044', 'Test30']\n",
"['R30test']\n",
"['R_000', 'R_001', 'R_002']\n",
"['.DS_Store', 'sigma_01', 'GRFrange700', 'up_startloc', 'GRFRange200', 'R30test']\n",
"total 0\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_000\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_001\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_002\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_003\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_004\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:00 R_005\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_006\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_007\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_008\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_009\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_010\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_011\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_012\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_013\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_014\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_015\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_016\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_017\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_018\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_019\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_020\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_021\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_022\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_023\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_024\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_025\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_026\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_027\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_028\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_029\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_030\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_031\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_032\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_033\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_034\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_035\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_036\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_037\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_038\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_039\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_040\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_041\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_042\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_043\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_044\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_045\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_046\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_047\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_048\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_049\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_050\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_051\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_052\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_053\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_054\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_055\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_056\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_057\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_058\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_059\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_060\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_061\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_062\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_063\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_064\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_065\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_066\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_067\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_068\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_069\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_070\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_071\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_072\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_073\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_074\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_075\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_076\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_077\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_078\n",
"drwx------ 4 yaolin staff 128B Mar 10 14:01 R_079\n",
"drwxr-xr-x 32 yaolin staff 1.0K Mar 9 09:00 Test30\n",
"drwxr-xr-x 82 yaolin staff 2.6K Mar 10 13:58 sigma_10_nuggget_04\n"
"drwxr-xr-x 3 yaolin staff 96B Sep 5 11:41 R_000\n",
"drwxr-xr-x 3 yaolin staff 96B Sep 5 11:41 R_001\n",
"drwxr-xr-x 3 yaolin staff 96B Sep 5 11:41 R_002\n"
]
},
{
Expand All @@ -139,7 +60,7 @@
"plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
"plt.rcParams[\"font.size\"] = 20\n",
"\n",
"filepath = \"./npy/analytical/\"\n",
"filepath = \"./npy/temporal/\"\n",
"print(os.listdir(filepath))\n",
"\n",
"figpath = os.getcwd() + \"/../../../../OneDrive - NTNU/MASCOT_PhD/Projects/GOOGLE/Docs/fig/Sim_2DNidelva/Simulator/AnalyticalEIBV/\"\n",
Expand All @@ -154,6 +75,63 @@
"os.system(\"ls -lh \" + filepath)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d684d799",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of replicates: 3\n"
]
}
],
"source": [
"Simulators = [\"SimulatorRRTStar\", \"SimulatorMyopic2D\"]\n",
"sigmas_nuggets = np.array([[1., .25]])\n",
"replicates = os.listdir(filepath)\n",
"Nrep = 0\n",
"for rep in replicates: \n",
" if rep.startswith(\"R_\"): \n",
" Nrep += 1\n",
"print(\"Number of replicates: \", Nrep)\n",
"\n",
"sigma = 1. \n",
"nugget = .25\n",
"simulator = \n",
"for i in range(Nrep):\n",
" rep = \"R_{:03d}\".format(i)\n",
" string = \"/sigma_{:02d}/\".format(int(10*sigma)) + \"nugget_{:03d}/\".format(int(100*nugget)) + simulator + \"/\"\n",
" datapath = filepath + rep + string\n",
"\n",
"\n",
"\n",
" traj = np.empty([0, 3, num_steps, 2])\n",
" ibv = np.empty([0, 3, num_steps])\n",
" rmse = np.empty([0, 3, num_steps])\n",
" vr = np.empty([0, 3, num_steps])\n",
"\n",
"\n",
" r_traj = np.load(datapath + \"traj.npy\").reshape(1, 3, num_steps, 2)\n",
" r_ibv = np.load(datapath + \"ibv.npy\").reshape(1, 3, num_steps)\n",
" r_vr = np.load(datapath + \"vr.npy\").reshape(1, 3, num_steps)\n",
" r_rmse = np.load(datapath + \"rmse.npy\").reshape(1, 3, num_steps)\n",
"\n",
" traj = np.append(traj, r_traj, axis=0)\n",
" ibv = np.append(ibv, r_ibv, axis=0)\n",
" vr = np.append(vr, r_vr, axis=0)\n",
" rmse = np.append(rmse, r_rmse, axis=0)\n",
"\n",
" savefig = figpath[:-1] + string\n",
" checkfolder(savefig)\n",
"\n",
" for i in tqdm(range(traj.shape[2])):\n",
" plotf(i, \"sigma: {:.1f}, nugget: {:.2f}, simulator: {:s}\".format(sigma, nugget, simulator), filename=savefig + \"P_{:03d}.png\".format(i))\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
Expand Down Expand Up @@ -194,8 +172,8 @@
"# sigmas = [1.]\n",
"# nuggets = [.4]\n",
"\n",
"sigmas = [.1]\n",
"nuggets = [.01]\n",
"sigmas = [1.]\n",
"nuggets = [.25]\n",
"\n",
"replicates = os.listdir(filepath)\n",
"Nrep = 0\n",
Expand Down Expand Up @@ -374,7 +352,9 @@
" encoder : Lavc59.37.100 libx264\n",
" Side data:\n",
" cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A\n",
"frame= 94 fps= 10 q=22.0 size= 1280kB time=00:00:03.20 bitrate=3276.8kbits/s speed=0.35x \r\r frame= 76 fps= 10 q=22.0 size= 768kB time=00:00:01.40 bitrate=4493.9kbits/s speed=0.188x frame= 88 fps= 10 q=22.0 size= 1024kB time=00:00:02.60 bitrate=3226.4kbits/s speed=0.302x frame= 100 fps=9.7 q=-1.0 Lsize= 3189kB time=00:00:09.70 bitrate=2693.4kbits/s speed=0.943x \n",
"frame= 94 fps= 10 q=22.0 size= 1280kB time=00:00:03.20 bitrate=3276.8kbits/s speed=0.35x \r",
"\r",
" frame= 76 fps= 10 q=22.0 size= 768kB time=00:00:01.40 bitrate=4493.9kbits/s speed=0.188x frame= 88 fps= 10 q=22.0 size= 1024kB time=00:00:02.60 bitrate=3226.4kbits/s speed=0.302x frame= 100 fps=9.7 q=-1.0 Lsize= 3189kB time=00:00:09.70 bitrate=2693.4kbits/s speed=0.943x \n",
"video:3187kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.059531%\n",
"[libx264 @ 0x131e06440] frame I:1 Avg QP: 7.07 size:591994\n",
"[libx264 @ 0x131e06440] frame P:57 Avg QP:16.21 size: 41408\n",
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a9e121c

Please sign in to comment.