Skip to content

Commit

Permalink
0.11-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
stillonearth committed Jul 17, 2023
1 parent f932483 commit e2d50db
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
edition = "2021"
name = "bevy_rl"
version = "0.10.2"
version = "0.11.0-beta"
license = "MIT OR Apache-2.0"
description = "Build environments for reinforcement learning with bevy"
keywords = ["ai", "bevy", "gym", "rl"]
repository = "https://github.com/stillonearth/bevy_rl"

[dependencies]
bevy = { version = "0.10.0" }
bevy = { version = "0.11.0" }
bytemuck = "1.12.3"
crossbeam = "0.8.2"
crossbeam-channel = "0.5.6"
Expand All @@ -20,7 +20,7 @@ mime = "0.3.16"
serde = "1.0.89"
serde_derive = "1.0.143"
serde_json = "1.0.83"
wgpu = "0.15.1"
wgpu = "0.16.2"

[dev-dependencies]
bitflags = "2.0.2"
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Build Reinforcement Learning [Gym](https://gym.openai.com/) environments with [B
| 0.7 | 0.0.5 |
| 0.8 | 0.8.4 |
| 0.9 | 0.9.8-beta |
| 0.10 | 0.10.0 |
| 0.10 | 0.10 |
| 0.11 | 0.11.0-beta |

## 📝Features

Expand Down Expand Up @@ -62,7 +63,7 @@ let ai_gym_state = AIGymState::<Actions, State>::new(AIGymSettings {
..default()
});
app.insert_resource(ai_gym_state)
.add_plugin(AIGymPlugin::<Actions, Observations>::default());
.add_plugins(AIGymPlugin::<Actions, Observations>::default());
```

### 2.1 (Optional) Enable Rendering to Buffer
Expand Down Expand Up @@ -170,11 +171,13 @@ pub(crate) fn bevy_rl_reset_request(
Register systems to handle bevy_rl events.

```rust
app.add_system_set(
SystemSet::on_update(SimulationState::PausedForControl)
app.add_systems(
Update,
(
.with_system(bevy_rl_control_request)
.with_system(bevy_rl_reset_request)
.with_system(bevy_rl_pause_request),
).in_set(SimulationState::PausedForControl)
);
```

Expand Down
4 changes: 2 additions & 2 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ fn step<
let query_param = StepQueryString::take_from(&mut state);

let err = serde_json::from_str::<Vec<AgentAction>>(&query_param.payload).err();
if err.is_some() {
return (state, err.unwrap().to_string());
if let Some(message) = err {
return (state, message.to_string());
}
let agent_actions: Vec<AgentAction> = serde_json::from_str(&query_param.payload).unwrap();

Expand Down
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ pub struct AIGymSettings {
}

/// This event is fired when user calls `reset` method of the REST API
#[derive(Event)]
pub struct EventReset;

/// This event is fired when user calls `step` method of the REST API
#[derive(Event)]
pub struct EventControl(pub Vec<Option<String>>);

/// This event is fired when an internal timer would need to pause the simulation
#[derive(Event)]
pub struct EventPause;

/// States of the simulation
Expand Down Expand Up @@ -63,7 +66,7 @@ impl<
> Plugin for AIGymPlugin<T, P>
{
fn build(&self, app: &mut App) {
app.add_startup_system(setup::<T, P>);
app.add_systems(Startup, setup::<T, P>);

let ai_gym_state = app
.world
Expand All @@ -86,17 +89,24 @@ impl<

// Add system scheduling
app.add_state::<SimulationState>()
.add_system(control_switch::<T, P>.in_set(OnUpdate(SimulationState::Running)))
.add_systems(
Update,
control_switch::<T, P>.in_set(SimulationState::Running),
)
.add_systems(
Update,
(
process_control_request::<T, P>,
process_reset_request::<T, P>,
)
.in_set(OnUpdate(SimulationState::PausedForControl)),
.in_set(SimulationState::PausedForControl),
);

if let Ok(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_system(copy_from_gpu_to_ram::<T, P>.in_set(RenderSet::Render));
render_app.add_systems(
Update,
copy_from_gpu_to_ram::<T, P>.in_set(RenderSet::Render),
);
render_app.insert_resource(ai_gym_state);
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use bevy::{
renderer::{RenderDevice, RenderQueue},
},
};
use std::num::NonZeroU32;

use bytemuck;
use image;
Expand All @@ -20,14 +19,13 @@ use crate::state;
fn texture_image_layout(desc: &TextureDescriptor<'_>) -> ImageDataLayout {
let size = desc.size;

let width = size.width * desc.format.block_dimensions().0;
let height = size.width * desc.format.block_dimensions().1;

ImageDataLayout {
bytes_per_row: if size.height > 1 {
NonZeroU32::new(size.width * (desc.format.describe().block_size as u32))
} else {
None
},
bytes_per_row: if size.height > 1 { Some(width) } else { None },
rows_per_image: if size.depth_or_array_layers > 1 {
NonZeroU32::new(size.height)
Some(height)
} else {
None
},
Expand Down
12 changes: 6 additions & 6 deletions tests/test_rest_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ fn start_bevy_app() {

// Basic bevy setup
app.add_plugins(MinimalPlugins);
app.add_plugin(WindowPlugin::default());
app.add_plugin(AssetPlugin::default());
app.add_plugin(ImagePlugin::default());
app.add_plugins(WindowPlugin::default());
app.add_plugins(AssetPlugin::default());
app.add_plugins(ImagePlugin::default());

// Setup bevy_rl
let ai_gym_state = AIGymState::<Actions, EnvironmentState>::new(AIGymSettings {
Expand All @@ -75,14 +75,14 @@ fn start_bevy_app() {
..default()
});
app.insert_resource(ai_gym_state)
.add_plugin(AIGymPlugin::<Actions, EnvironmentState>::default());
.add_plugins(AIGymPlugin::<Actions, EnvironmentState>::default());

// initialize app state
app.insert_resource(initial_state);

// bevy_rl events
app.add_system(bevy_rl_pause_request);
app.add_system(bevy_rl_control_request);
app.add_systems(Update, bevy_rl_pause_request);
app.add_systems(Update, bevy_rl_control_request);

// Run for 1M frames
for _ in 0..1000000 {
Expand Down

0 comments on commit e2d50db

Please sign in to comment.