Skip to content

Commit 215bf5d

Browse files
author
Shunichi09
committed
Update: example script
1 parent 1e0c26c commit 215bf5d

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

PythonLinearNonlinearControl/plotters/plot_func.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def plot(axis, history, history_g=None):
4747
axis1.legend(ncol=1, bbox_to_anchor=(0., 1.02, 1., 0.102), loc=3)
4848
figure.savefig(path, bbox_inches="tight", pad_inches=0.05)
4949

50-
def plot_results(args, history_x, history_u, history_g=None):
50+
def plot_results(history_x, history_u, history_g=None, args=None):
5151
"""
5252
5353
Args:
@@ -56,14 +56,21 @@ def plot_results(args, history_x, history_u, history_g=None):
5656
Returns:
5757
None
5858
"""
59+
env = "Env"
60+
controller_type = "controller"
61+
62+
if args is not None:
63+
env = args.env
64+
controller_type = args.controller_type
65+
5966
plot_result(history_x, history_g=history_g, ylabel="x",
60-
name= args.env + "-state_history",
61-
save_dir="./result/" + args.controller_type)
67+
name= env + "-state_history",
68+
save_dir="./result/" + controller_type)
6269
plot_result(history_u, history_g=np.zeros_like(history_u), ylabel="u",
63-
name= args.env + "-input_history",
64-
save_dir="./result/" + args.controller_type)
70+
name= env + "-input_history",
71+
save_dir="./result/" + controller_type)
6572

66-
def save_plot_data(args, history_x, history_u, history_g=None):
73+
def save_plot_data(history_x, history_u, history_g=None, args=None):
6774
""" save plot data
6875
6976
Args:
@@ -72,16 +79,23 @@ def save_plot_data(args, history_x, history_u, history_g=None):
7279
Returns:
7380
None
7481
"""
75-
path = os.path.join("./result/" + args.controller_type,
76-
args.env + "-history_x.pkl")
82+
env = "Env"
83+
controller_type = "controller"
84+
85+
if args is not None:
86+
env = args.env
87+
controller_type = args.controller_type
88+
89+
path = os.path.join("./result/" + controller_type,
90+
env + "-history_x.pkl")
7791
save_pickle(path, history_x)
7892

79-
path = os.path.join("./result/" + args.controller_type,
80-
args.env + "-history_u.pkl")
93+
path = os.path.join("./result/" + controller_type,
94+
env + "-history_u.pkl")
8195
save_pickle(path, history_u)
8296

83-
path = os.path.join("./result/" + args.controller_type,
84-
args.env + "-history_g.pkl")
97+
path = os.path.join("./result/" + controller_type,
98+
env + "-history_g.pkl")
8599
save_pickle(path, history_g)
86100

87101
def load_plot_data(env, controller_type, result_dir="./result"):

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,17 +178,17 @@ Use that histories to visualize the Animation or Figures.
178178

179179
```py
180180
# plot results
181-
plot_results(args, history_x, history_u, history_g=history_g)
182-
save_plot_data(args, history_x, history_u, history_g=history_g)
181+
plot_results(history_x, history_u, history_g=history_g)
182+
save_plot_data(history_x, history_u, history_g=history_g)
183183

184184
# create animation
185-
animator = Animator(args, env)
185+
animator = Animator(env)
186186
animator.draw(history_x, history_g)
187187
```
188188

189-
## Run Experiments
189+
## Run Example Script
190190

191-
You can run the experiments as follows:
191+
You can run the example script as follows:
192192

193193
```
194194
python scripts/simple_run.py --env CartPole --controller CEM --save_anim 1

scripts/simple_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def run(args):
3737
history_x, history_u, history_g = runner.run(env, controller, planner)
3838

3939
# plot results
40-
plot_results(args, history_x, history_u, history_g=history_g)
41-
save_plot_data(args, history_x, history_u, history_g=history_g)
40+
plot_results(history_x, history_u, history_g=history_g, args=args)
41+
save_plot_data(history_x, history_u, history_g=history_g, args=args)
4242

4343
if args.save_anim:
4444
animator = Animator(env, args=args)

0 commit comments

Comments
 (0)