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

feat(classicalcontrol): add additional info to step/reset return #274

Merged
merged 1 commit into from
Jul 26, 2023
Merged
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: 5 additions & 1 deletion stable_gym/envs/classic_control/cartpole_cost/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ In addition to the observations, the cost and a termination and truncation boole
[observation, cost, termination, truncation, info_dict]
```

The info dictionary currently is empty.
The info dictionary contains the following keys:

* **reference**: The set cart position and angle reference (i.e. the zero position and angle).
* **state\_of\_interest**: The state that should track the reference (SOI).
* **reference\_error**: The error between SOI and the reference.

## How to use

Expand Down
17 changes: 14 additions & 3 deletions stable_gym/envs/classic_control/cartpole_cost/cartpole_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,19 @@ def step(self, action):

# Create observation and info dict.
obs = np.array(self.state)
info_dict = dict(
reference=np.array([0.0, 0.0]),
state_of_interest=np.array([x, theta]),
reference_error=np.array([-x, -theta]),
)

# NOTE: The original returns an empty info dict.
return (
obs,
cost,
terminated,
False,
{},
info_dict,
)

def reset(self, seed=None, options=None, random=True):
Expand Down Expand Up @@ -467,15 +472,21 @@ def reset(self, seed=None, options=None, random=True):
self.steps_beyond_terminated = None
self.t = 0.0

# Create info dict and observation.
# Retrieve observation and info_dict.
obs = np.array(self.state)
x, _, theta, _ = self.state
info_dict = dict(
reference=np.array([0.0, 0.0]),
state_of_interest=np.array([x, theta]),
reference_error=np.array([-x, -theta]),
)

# Render environment reset if requested.
if self.render_mode == "human":
self.render()

# NOTE: The original returns an empty info dict.
return obs, {}
return obs, info_dict

def render(self):
"""Render one frame of the environment."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,10 @@ def cost(self, x, theta):

- cost (float): The current cost.
- r_1 (float): The current position reference.
- r_2 (float): The cart_pole angle reference.
"""
# TODO: Fine-tune cost function. The current one is a initial test.
ref = [self.reference(self.t), 0.0]
ref_cost = np.square(x - ref[0])
ref = self.reference(self.t)
ref_cost = np.square(x - ref)
stab_cost = np.square(theta / self.theta_threshold_radians)

cost = stab_cost + ref_cost
Expand Down Expand Up @@ -465,15 +464,15 @@ def step(self, action):
obs = np.append(
np.array(self.state),
np.array(
[ref[0]]
[ref]
if self._exclude_reference_error_from_observation
else [ref[0], x - ref[0]]
else [ref, x - ref]
),
)
info_dict = dict(
reference=ref[0],
reference=ref,
state_of_interest=x,
reference_error=x - ref[0],
reference_error=x - ref,
)

# NOTE: The original returns an empty info dict.
Expand Down Expand Up @@ -546,20 +545,20 @@ def reset(self, seed=None, options=None, random=True):
self.steps_beyond_terminated = None
self.t = 0.0

# Create info dict and observation.
# Retrieve observation and info_dict.
x, _, theta, _ = self.state
_, ref = self.cost(x, theta)
info_dict = dict(
reference=ref[0],
reference=ref,
state_of_interest=x,
reference_error=x - ref[0],
reference_error=x - ref,
)
obs = np.append(
np.array(self.state),
np.array(
[ref[0]]
[ref]
if self._exclude_reference_error_from_observation
else [ref[0], x - ref[0]]
else [ref, x - ref]
),
)

Expand Down