Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2fd2fe2
Add initial k out of n problem
Jorritboer Nov 9, 2024
97649b4
Set default nr components n=4
Jorritboer Nov 9, 2024
d0e1eef
Add FireEsacpe pomdp
Jorritboer Nov 13, 2024
cfe8b4f
Clean up unused or duplicate code
Jorritboer Nov 14, 2024
9c5d8e8
Clean up unused or duplicate code
Jorritboer Nov 14, 2024
ee5d58e
Merge branch 'Jorrit' of https://github.com/MKrale/ATM into Jorrit
Jorritboer Nov 14, 2024
ade1edb
Add .DS_Store to gitignore
Jorritboer Nov 21, 2024
64e2edb
Start visualizing by starting to update to gymnasium
Jorritboer Nov 21, 2024
190dd2c
Remove unnecessary old frozen_lake v1
Jorritboer Nov 21, 2024
54fab02
Add mp4 to gitignore
Jorritboer Nov 21, 2024
bacb599
Update frozen_lake_v2 to gymnasium and add visualization
Jorritboer Nov 21, 2024
8b9efbb
Add visualizing to blackjack and cliffwalking
Jorritboer Nov 27, 2024
893d114
Prevent first two videos being empty by not resetting unnecesarily
Jorritboer Nov 27, 2024
de73c02
Remove #%%
Jorritboer Nov 27, 2024
ddb38e6
Make new AM wrapper that renders measurements nicely
Jorritboer Nov 28, 2024
697227f
Update AMRL for new AM wrapper
Jorritboer Nov 28, 2024
705b20d
Update fire_escape
Jorritboer Nov 28, 2024
71a87cc
Change requirement from gym to gymnasium
Jorritboer Nov 28, 2024
4885203
Cleanup Run.py
Jorritboer Nov 28, 2024
8f7a99d
Add file arguments for video recording
Jorritboer Nov 28, 2024
5ced1cd
Do running inside run
Jorritboer Dec 4, 2024
ebd5a84
Fix boolean arg variables
Jorritboer Dec 4, 2024
4f1bf4e
Fix remaking env
Jorritboer Dec 4, 2024
ac9bf66
Fix videos ending abrubtly (apparently changing the fps can cause thi…
Jorritboer Dec 5, 2024
2d09ab0
Fix blackjack videos
Jorritboer Dec 5, 2024
a7f3e83
Add partial active measurement, including example for lake
Jorritboer Dec 12, 2024
cac609a
Add text episode recorder
Jorritboer Dec 12, 2024
0cd8ac8
Add text if measured in amwrapper
Jorritboer Dec 12, 2024
164428d
Fix text episode recorder for koutofn environment
Jorritboer Dec 19, 2024
264372f
Add text recorder to run.py
Jorritboer Dec 19, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Temporary_Plots
Unused_Code
.vscode
exp
.DS_Store
*.mp4
228 changes: 0 additions & 228 deletions AM_Gyms/AM_Env_wrapper.py

This file was deleted.

84 changes: 84 additions & 0 deletions AM_Gyms/ActiveMeasurementWrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import numpy as np
import gymnasium as gym
from gymnasium.spaces import Space
from typing import Callable


# Slightly desaturate an RGB image by blending it with its grayscale version.
def desaturate_rgb(rgb, alpha=0.5):
gray = np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])
gray_rgb = np.stack((gray, gray, gray), axis=-1)
desaturated = (1 - alpha) * rgb + alpha * gray_rgb
return np.clip(desaturated, 0, 1 if rgb.dtype.kind == "f" else 255)


class ActiveMeasurementWrapper(gym.Wrapper):

def __init__(
self,
env: gym.Env,
observation_function: Callable[
[Space, Space], Space
] = lambda observation, measurement: (observation if measurement else None),
measurement_cost: Callable[[Space], int] | int = 0.05,
initial_state=-1,
):
"""Custom Active Measurement Wrapper

Classic AM:
- Provide no observation_function
- Let measurement_cost be an integer
then it returns the whole observation if measured for cost of of measurement cost

For more customization:
- Let observation_function: observation -> measurement_action -> new observation
be a custom observation function dependent on the custom measurement action
- Let measurement_cost be dependent on the measurement function
"""
super().__init__(env)
self.observation_function = observation_function
if type(measurement_cost) is float:
self.measurement_cost = lambda measurement_action: (
measurement_cost if measurement_action else 0
)
else:
self.measurement_cost = measurement_cost
self.initial_state = initial_state
self.last_step_measured = False

def reset(self, seed=None, options=None):
self.env.reset(seed=seed, options=options)
self.last_step_measured = False
# do not return observation here
return None, None

def step(self, action):
control_action, measurement_action = action
self.last_step_measured = measurement_action
observation, reward, terminated, truncated, info = self.env.step(control_action)
return (
self.observation_function(observation, measurement_action),
reward - self.measurement_cost(measurement_action),
terminated,
truncated,
info,
)

def render(self):
if self.env.render_mode == "rgb_array":
img = self.env.render()
if not self.last_step_measured:
return desaturate_rgb(img, 0.65)
else:
return img
elif self.env.render_mode in ["ansi", "text"]:
return f"Measure action {self.last_step_measured}:\n" + self.env.render()
self.env.render()

def get_vars(self):
return (
self.env.observation_space,
self.env.action_space,
self.measurement_cost,
self.initial_state,
)
Loading