Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated on 24 Sept #27

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions algorithm/.ipynb_checkpoints/Untitled-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
353 changes: 353 additions & 0 deletions algorithm/.ipynb_checkpoints/Untitled1-checkpoint.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added algorithm/2Million_Training_EXPT_1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithm/2Million_Training_EXPT_2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithm/2Million_Training_EXPT_3-1.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithm/2Million_Training_EXPT_3-2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithm/2Million_Training_EXPT_3.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 158 additions & 0 deletions algorithm/FLP_Testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 19 12:25:32 2022

@author: Shimraz
"""

import gym
import gym.envs
import pandas as pd
from stable_baselines3 import DDPG
import numpy as np
import matplotlib.pyplot as plt
import itertools
import os
import scipy.interpolate
import imageio
from PIL import Image
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

class Test_FLP_Env:
def __init__(self,env_name,training_timesteps,ckpt_name_prefeix,
log_path):
"""
Description :
Training class for the agent.
Here the agent is trained and saved with inbetween checkpoints.
Log is also saved in a csv file.

Input :
env_name : str
Name of the Env to be tested on

training_timesteps : int
Number of steps agent has trained

save_freq : int
After this many timesteps, model will be saved in between total training

ckpt_name_prefeix : str
Name prefix during checkpoint for the saved model

log_path : str
Folder where the log details are saved


"""
training_timesteps = training_timesteps
ckpt_name_prefeix = ckpt_name_prefeix
log_path = log_path
env = gym.make(env_name,mode='rgb_array',instance='P6')

log_dir = log_path + "_ttsteps_"+ str(training_timesteps)


load_file = log_dir+"/FLP_Training_50000_steps.zip"

# model = DDPG.load(load_file, env = env)
model = DDPG.load(load_file, env = env)
print(load_file)
self.Test(env, model, training_timesteps,load_file)



def Test(self,env, model, train_steps, load_file):
"""
Description :
This function run the test file given the model and env

Input :
env : Env
Env to be tested on
model : DDPG Agent
DDPG agent used to predict the pressure
"""
experiment_results={}
# global model
model = model
print("here")
# print("model", model)
for ts in range(train_steps):
ts = int(ts)
print(ts)
fig, (ax1,ax2) = plt.subplots(2,1)

obs = env.reset()
start_cost = getattr(env,"last_cost") #wrap_env.get_attr("last_cost")[0]
print(start_cost)
rewards = []
mhc = []
images = []
gain = 0
gains = []
actions = []
done = False
while done != True:
action, _states = model.predict(obs, deterministic = True)
actions.append(action)
obs, reward, done, info = env.step(action)

gain += reward
img = env.render(mode='rgb_array')
rewards.append(reward)
# print(info)
mhc.append(info['mhc'])
gains.append(gain)
images.append(img)
print(mhc)
print(rewards)
final_cost = mhc[-1]
print("finalcost",final_cost)
cost_saved = final_cost-start_cost
cost_saved_rel = 1-(start_cost/final_cost)
print(cost_saved, cost_saved_rel, '%')
experiment_results[ts]=[cost_saved, cost_saved_rel]
ax1.plot(rewards)
ax2.plot(mhc)
print(actions)
imageio.mimsave(f'gifs/test_env.gif', [np.array(img.resize((200,200),Image.NEAREST)) for i, img in enumerate(images) if i%2 == 0], fps=29)
plt.show()
env.close()
del model
y = np.array([i for i in experiment_results.values()])
print(experiment_results, y)
print(train_steps, abs(y[:,0]))
fig1, axs = plt.subplots(1,1)
axs.plot(train_steps,abs(y[:,0]),)
plt.title("Plot flp")
print("end")



if __name__ == '__main__':
"""
Description :
Main file to test and plot the agent's prediction.

Parameters:

training_timesteps : int
Number of steps agent has trained

ckpt_name_prefeix : str
Name prefix during checkpoint for the saved model

log_path : str
Folder where the log details are saved

env_name : str
Name of the Env to be tested on
"""
training_timesteps = 10000 # Set accordingly
ckpt_name_prefeix = 'FLP_Training'
log_path = r'./FLP_Training/FLP_Training_v0'
#Name of the environment
env_name = 'ofp-v1'

testing = Test_FLP_Env(env_name,training_timesteps,ckpt_name_prefeix,log_path)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
103 changes: 103 additions & 0 deletions algorithm/FLP_Training/FLP_Training_v0_ttsteps_50000/monitor.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#{"t_start": 1661381832.9951506, "env_id": "ofp-v1"}
r,l,t
-1,1,0.135298
-1,1,0.143295
-1,1,0.1453
-1,1,0.150295
-1,1,0.154294
-1,1,0.161295
-1,1,0.166294
-1,1,0.169295
-1,1,0.176294
-1,1,0.180295
-1,1,0.183294
-1,1,0.192294
-1,1,0.196297
-1,1,0.201295
-1,1,0.207295
-1,1,0.211294
-1,1,0.218295
-1,1,0.224299
-1,1,0.229295
-1,1,0.232294
-1,1,0.235302
-1,1,0.239295
-1,1,0.241294
-1,1,0.246294
-1,1,0.252294
-1,1,0.260294
-1,1,0.274294
-1,1,0.277294
-1,1,0.289295
-1,1,0.291295
-1,1,0.294743
-1,1,0.310371
-1,1,0.310371
-1,1,0.310371
-1,1,0.310371
-1,1,0.310371
-1,1,0.325993
-1,1,0.325993
-1,1,0.325993
-1,1,0.325993
-1,1,0.325993
-1,1,0.325993
-1,1,0.341618
-1,1,0.341618
-1,1,0.357242
-1,1,0.357242
-1,1,0.357242
-1,1,0.357242
-1,1,0.372868
-1,1,0.372868
-1,1,0.372868
-1,1,0.372868
-1,1,0.388492
-1,1,0.388492
-1,1,0.404118
-1,1,0.404118
-1,1,0.404118
-1,1,0.404118
-1,1,0.404118
-1,1,0.419743
-1,1,0.419743
-1,1,0.419743
-1,1,0.419743
-1,1,0.435366
-1,1,0.435366
-1,1,0.435366
-1,1,0.435366
-1,1,0.450992
-1,1,0.450992
-1,1,0.450992
-1,1,0.466617
-1,1,0.466617
-1,1,0.466617
-1,1,0.466617
-1,1,0.482242
-1,1,0.482242
-1,1,0.497869
-1,1,0.497869
-1,1,0.497869
-1,1,0.513493
-1,1,0.513493
-1,1,0.513493
-1,1,0.529118
-1,1,0.529118
-1,1,0.529118
-1,1,0.529118
-1,1,0.544741
-1,1,0.544741
-1,1,0.544741
-1,1,0.544741
-1,1,0.544741
-1,1,0.560367
-1,1,0.560367
-1,1,0.560367
-1,1,0.575993
-1,1,0.575993
-1,1,0.575993
-1,1,0.575993
-1,1,0.591619
-1,1,0.591619
-1,1,0.60744
Expand Down
Loading