-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9840073
commit c038058
Showing
6 changed files
with
174 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import numpy as np | ||
from stable_baselines3.common.env_util import make_vec_env | ||
from aviaries.PositionControllerAviaryTest import PositionControllerAviaryTest | ||
from gym_pybullet_drones.utils.enums import ObservationType, ActionType | ||
from trajectories import DiscretizedTrajectory | ||
from stable_baselines3.common.vec_env import VecEnv | ||
from agents.utils.configuration import Configuration | ||
from .base_factory import BaseFactory | ||
|
||
|
||
class PositionControllerFactoryTest(BaseFactory): | ||
action_type: ActionType | ||
observation_type: ObservationType | ||
t_traj: DiscretizedTrajectory | ||
n_env_training: int | ||
initial_xyzs: np.ndarray | ||
seed: int | ||
use_gui_for_test_env: bool | ||
output_path_location: str | ||
|
||
def __init__(self, | ||
config: Configuration, | ||
observation_type: ObservationType, | ||
output_folder: str, | ||
use_gui_for_test_env: bool = True, | ||
n_env_training: int=20, | ||
seed: int = 0, | ||
) -> None: | ||
super().__init__() | ||
initial_xyzs = config.initial_xyzs | ||
action_type = config.action_type | ||
t_traj = config.t_traj | ||
|
||
self.initial_xyzs = initial_xyzs | ||
self.observation_type = observation_type | ||
self.action_type = action_type | ||
self.t_traj = t_traj | ||
self.n_env_training = n_env_training | ||
self.seed = seed | ||
self.use_gui_for_test_env = use_gui_for_test_env | ||
|
||
def get_train_env(self) -> VecEnv: | ||
train_env = make_vec_env( | ||
PositionControllerAviaryTest, | ||
env_kwargs=dict( | ||
target_trajectory=self.t_traj, | ||
initial_xyzs=self.initial_xyzs, | ||
obs=self.observation_type, | ||
act=self.action_type | ||
), | ||
n_envs=self.n_env_training, | ||
seed=self.seed | ||
) | ||
return train_env | ||
|
||
def get_eval_env(self): | ||
eval_env = PositionControllerAviaryTest( | ||
target_trajectory=self.t_traj, | ||
initial_xyzs=self.initial_xyzs, | ||
obs=self.observation_type, | ||
act=self.action_type | ||
) | ||
return eval_env | ||
|
||
def get_test_env_gui(self): | ||
test_env = PositionControllerAviaryTest( | ||
target_trajectory=self.t_traj, | ||
initial_xyzs=self.initial_xyzs, | ||
gui=self.use_gui_for_test_env, | ||
obs=self.observation_type, | ||
act=self.action_type, | ||
record=False | ||
) | ||
return test_env | ||
|
||
def get_test_env_no_gui(self): | ||
test_env_nogui = PositionControllerAviaryTest( | ||
target_trajectory=self.t_traj, | ||
initial_xyzs=self.initial_xyzs, | ||
obs=self.observation_type, | ||
act=self.action_type | ||
) | ||
return test_env_nogui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import numpy as np | ||
import os | ||
import shutil | ||
class PosLoggerConfig: | ||
|
||
def __init__(self, max_len=1000, log_folder: str="./positions") -> None: | ||
self.max_len=max_len | ||
self.log_folder = log_folder | ||
|
||
class PositionLogger: | ||
def __init__(self, config: PosLoggerConfig) -> None: | ||
self.max_len = config.max_len | ||
self.n_logged_files = 0 | ||
self.log_folder = config.log_folder | ||
self.positions = [] | ||
|
||
if os.path.isfile(self.log_folder): | ||
os.remove(self.log_folder) | ||
if os.path.isdir(self.log_folder): | ||
shutil.rmtree(self.log_folder) | ||
os.makedirs(self.log_folder, exist_ok=True) | ||
|
||
def log_position(self, pos): | ||
pos = np.array(pos) | ||
self.positions.append(pos.reshape((1, 3))) | ||
if len(self.positions) >= self.max_len: | ||
self._log() | ||
|
||
def _log(self): | ||
pos_np = np.vstack(self.positions) | ||
fname = os.path.join( | ||
self.log_folder, | ||
f'{self.n_logged_files}.npy' | ||
) | ||
np.save( | ||
fname, pos_np | ||
) | ||
self.positions = [] | ||
self.n_logged_files += 1 | ||
|
||
def flush(self): | ||
self._log() | ||
|
||
def load_positions(log_folder: str) -> np.ndarray: | ||
all_positions = [] | ||
filenames = os.listdir(log_folder) | ||
filenames = sorted(filenames, key=lambda x: int(x.split('.')[0])) | ||
for fname in filenames: | ||
fname_full = os.path.join(log_folder, fname) | ||
positions = np.load(fname_full) | ||
all_positions.append(positions) | ||
all_positions = np.concatenate(all_positions) | ||
return all_positions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters